登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Code@Pig Home

喜欢背着一袋Code傻笑的Pig .. 忧美.欢笑.记忆.忘却 .之. 角落

 
 
 

日志

 
 

[ZeroMQ] Why lua-zmq crash ?  

2010-09-04 08:32:31|  分类: net_ZeroMQ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
There are many language binding for 0MQ, and I choose my favourite lua to have a try.

Problem
Thing doesn't go without a hitch. First, I try the lua-zmq in OpenBSD 4.7 and got a crash. 
But the C version goes well.
$ lua examples/client.lua 
Segmentation fault (core dumped)

And I try it in FreeBSD 8.0 and it hung. Again, C version is ok.
$ lua examples/client.lua      # hung
Using top to see what happens, find it hung in umtxn status.
PID USERNAME    THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
41277 kasicass      1  45    0 15012K  2688K umtxn   0   0:00  0.00% lua

Once more, I try it in my debian box, everything is ok. Strange huh? 
Then I found thing goes wrong when libzmq.so call pthread_create().

Why
Now I create a simple program to show why.
---------------- mydll.c -------------------
#include <pthread.h>
#include <stdio.h>

void *threadfunc(void *_)
{
    return (void *)0;
}

void myfoo()
{
    pthread_t pid;
    puts("111");
    pthread_create(&pid, NULL, threadfunc, NULL);
    puts("222");
}
--------------- main.c --------------------
#include <dlfcn.h>

typedef void (*myfunc)(void);
int main()
{
    void *dll;
    myfunc f;

    dll = dlopen("./mydll.so", RTLD_NOW);
    f = dlsym(dll, "myfoo");
    f();
    dlclose(dll);
    return 0;
}
------------------ Makefile ------------------
all:
        gcc -o mydll.o -Wall -fPIC -c mydll.c
        gcc -o mydll.so -shared mydll.o -pthread
        gcc -Wall main.c         # gcc -Wall main.c -pthread
------------------------------------------------
$ ./a.out
Segmentation fault (core dumped)

$ ldd a.out              # without -pthread
a.out:
        Start            End              Type Open Ref GrpRef Name
        0000000010000000 0000000010044000 exe  1    0   0      a.out
        0000000055078000 00000000551d8000 rlib 0    1   0      /usr/lib/libc.so.53.1
        0000000056d40000 0000000056d40000 rtld 0    1   0      /usr/libexec/ld.so

$ ldd a.out              # with -pthread
a.out:
        Start            End              Type Open Ref GrpRef Name
        0000000010000000 0000000010044000 exe  1    0   0      a.out
        0000000057a80000 0000000057ae8000 rlib 0    1   0      /usr/lib/libpthread.so.12.0
        0000000059798000 00000000598f8000 rlib 0    1   0      /usr/lib/libc.so.53.1
        0000000051710000 0000000051710000 rtld 0    1   0      /usr/libexec/ld.so
------------------------------------------------

Aha, that's it. The program without -pthread crashes in OpenBSD, FreeBSD and debian.
As lua is designed without multi-thread function, it's been linked without -pthread.

Debian's lua is linked without -pthread, but I don't know why lua-zmq runs correctly.
I think it's not safe either.

Solution
Download lua source and rebuild it with -pthread
Modify lua-5.1.4/src/Makefile:   LIBS= -lm $(MYLIBS) -pthread

ps. lua-zmq need be linked with -lstdc++ explicitly in my OpenBSD/loongson. OpenBSD sucks?
  评论这张
 
阅读(3078)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018