进程通信 ???

sunboylilei 2009-11-04 05:30:19
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define KEY 12
typedef struct {
long type;
char *ch;
}msgp;
int main()
{ msgp msg;
pid_t pid;
int m;
key_t key=1234;
m=msgget(key,IPC_CREAT|0600);
static int k=0;
if(m==-1)
{ printf("getting message goes wrong!\n");
printf("please enter any keys to withdraw....\n");
getchar();
return 2;
}
while((pid=fork())==-1);
if(pid==0){
msg.ch=(char *)malloc(100);
if(msg.ch==NULL){
printf("memory malloc wrong!\n");
exit(0);
}

char buff[100];
int flag;
msg.type=1;
printf("please input a string !\n");
gets(buff);
strcpy(msg.ch,buff);
flag=msgsnd(m,(void*)&msg,strlen(msg.ch),0); // 发送消息到消息队列
k=1;
exit(0);
}
else{
sleep(5);// 等待子进程结束
while(k==0);
msgrcv(m,(void*)&msg,strlen(msg.ch),1,0);
printf("parent receive :");
printf("%ld,%s\n",msg.type,msg.ch);
msgctl(m,IPC_RMID,NULL);
exit(0);
}
}


该程序能编译过去,但在执行的时候显示 “bad system call” 我 不知道错在那了 ,请各位帮帮忙!
...全文
43 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
winter_sui 2009-11-04
  • 打赏
  • 举报
回复
gdb跟踪一下,或者printf打印一下,看看到哪一步出错的。

感觉是
msgctl(m,IPC_RMID,NULL);

2,161

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 UNIX文化
社区管理员
  • UNIX文化社区
  • 文天大人
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧