将消息从 C 传递到 erlang
我尝试将消息从 C 代码发送到 erlang。我用EI库。
我的 C 代码:
int main (int argc, char** argv)
{
erl_init (NULL, 0);
extern const char *erl_thisnodename(void);
extern short erl_thiscreation(void);
#define SELF(fd) erl_mk_pid(erl_thisnodename(), fd, 0, erl_thiscreation())
ETERM *arr[2], *emsg;
int sockfd, creation=1;
arr[0] = SELF(sockfd);
arr[1] = erl_mk_atom("ok");
emsg = erl_mk_tuple(arr, 2);
erl_reg_send(sockfd, "my_server", emsg);
erl_free_term(emsg);
return (EXIT_SUCCESS);
}
和我的 erlang 代码:
start() ->
Pid = spawn(?MODULE, loop, []),
register(my_server, Pid).
loop() ->
receive
{ok} ->
io:format("Ok received \n"),
loop();
stop ->
stop;
_ ->
io:format("Somethig else \n")
end.
stop() ->
my_server ! stop.
但是当我运行我的 C 应用程序时,erlang 应用程序没有收到任何消息。怎么了?
我收到错误:
[启用使用 libthread_db 进行线程调试]
程序收到信号 SIGSEGV,分段错误。 ei_mutex_lock() 中的0x08055fac
谢谢。
I try to send message from C code to erlang. I use EI Library.
My C code:
int main (int argc, char** argv)
{
erl_init (NULL, 0);
extern const char *erl_thisnodename(void);
extern short erl_thiscreation(void);
#define SELF(fd) erl_mk_pid(erl_thisnodename(), fd, 0, erl_thiscreation())
ETERM *arr[2], *emsg;
int sockfd, creation=1;
arr[0] = SELF(sockfd);
arr[1] = erl_mk_atom("ok");
emsg = erl_mk_tuple(arr, 2);
erl_reg_send(sockfd, "my_server", emsg);
erl_free_term(emsg);
return (EXIT_SUCCESS);
}
And my erlang code:
start() ->
Pid = spawn(?MODULE, loop, []),
register(my_server, Pid).
loop() ->
receive
{ok} ->
io:format("Ok received \n"),
loop();
stop ->
stop;
_ ->
io:format("Somethig else \n")
end.
stop() ->
my_server ! stop.
But when i run my C application, erlang application nothing received. What's wrong?
I get error:
[Thread debugging using libthread_db enabled]
Program received signal SIGSEGV, Segmentation fault.
0x08055fac in ei_mutex_lock ()
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至少,您的 C 程序将未初始化的变量 (
sockfd
) 传递给erl_mk_pid()
函数,并在稍后调用erl_reg_send()< /代码>。这可能是一个很好的起点。
At the very least, your C program passes an uninitialized variable (
sockfd
) to theerl_mk_pid()
function, and again later in the call toerl_reg_send()
. That's probably a good place to start.