#include
#include
#include
#include
#include
#include
struct msg{
char msg_str[128];
};
int main()
{
int qid;
struct msg mymsg;
if(qid = msgget(0x66,0666|IPC_CREAT) < 0)//建立消息队列
perror("msgget");
int pid;
pid = fork();//创建过程
if(pid < 0)
perror("fork");
else if(pid == 0)
{
printf("This is A process!n");
sprintf(mymsg.msg_str,"hello world");//将hello world写入内存
if(msgsnd(qid,&mymsg,128,0) < 0)//将消息送入消息队列
perror("msgsnd");
}
else
{
if(fork() == 0)
{
printf("This is B process!n");
if(msgrcv(qid,&mymsg,128,0,0) < 0)//从消息队列中读取消息
perror("msgrcv");
printf("The msg is: %sn",mymsg.msg_str);
}
else if(fork() == 0)
{
printf("This is C process!n");
sprintf(mymsg.msg_str,"zhangxuyang");//将zhangxuyang写入内存
if(msgsnd(qid,&mymsg,128,0) < 0)//将消息送入消息队列
perror("msgsnd");
}
else
{
printf("This is D process!n");
if(msgrcv(qid,&mymsg,128,0,0) < 0)//从消息队列中读取消息
perror("msgrcv");
printf("The msg is: %sn",mymsg.msg_str);
}
}
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)