linux——消息队列进程间通信
ftok函数key_t ftok( char * fname, int id ) //系统建立IPC通讯如消息队列、共享内存时必须指定一个ID值。通常情况下该id值通过ftok函数得到。 参数 fname就时你指定的文件名(该文件必须是存在而且可以访问的)。 id是子序号 虽然为int但是只有8个比特被使用(0‐255)。 返回值 当成功执行的时候一个key_t值将会被返回否则 ‐1 被返回。1、半双工创建一个msg_write.c#includestdio.h #include sys/types.h #include sys/ipc.h #include sys/msg.h #include stdlib.h #includestring.h struct msgbuf { long mtype; char mtest[128]; char ID[4]; }; int main() { struct msgbuf sendbuf; int msgid; key_t key; key ftok(a.c,1); msgid msgget(key,IPC_CREAT|0755); if(msgid -1) { printf(creat message queue failed\n); return -1; } system(ipcs -q); printf(creat message queue succeed! msgid %d\n,msgid); //int msgbuf sendbuf.mtype 100; while(1) { memset(sendbuf.mtest,0,128); printf(please input to message queue:\n); fgets(sendbuf.mtest,128,stdin); msgsnd(msgid,(void *)sendbuf,strlen(sendbuf.mtest),0); } return 0; }在创建一个msg_read.c#includestdio.h #include sys/types.h #include sys/ipc.h #include sys/msg.h #include stdlib.h #includestring.h struct msgbuf { long mtype; char mtest[128]; char ID[4]; }; int main() { struct msgbuf sendbuf,readbuf; int msgid; key_t key; int readret; key ftok(a.c,1); msgid msgget(key,IPC_CREAT|0755); if(msgid -1) { printf(creat message queue failed\n); return -1; } system(ipcs -q); printf(creat message queue succeed! msgid %d\n,msgid); //int msgbuf sendbuf.mtype 100; while(1) { memset(readbuf.mtest,0,128); readret msgrcv(msgid,(void *)readbuf,128,100,0); printf(message is:%s\n,readbuf.mtest); printf(total is %d byte\n,readret); } return 0; }可以实现进程间通信但这个是半双工的只能write写read读2、全双工创建两个文件msg_service.c#includestdio.h #include sys/types.h #include sys/ipc.h #include sys/msg.h #include stdlib.h #includestring.h #include unistd.h struct msgbuf { long mtype; char mtest[128]; char ID[4]; }; int main() { struct msgbuf sendbuf,readbuf; int msgid; key_t key; pid_t pid; key ftok(a.c,1); msgid msgget(key,IPC_CREAT|0755); if(msgid -1) { printf(creat message queue failed\n); return -1; } system(ipcs -q); printf(creat message queue succeed! msgid %d\n,msgid); //int msgbuf sendbuf.mtype 100; pid fork(); //parent process write 100 if(pid0) { while(1) { memset(sendbuf.mtest,0,128); printf(please input to message queue:\n); fgets(sendbuf.mtest,128,stdin); msgsnd(msgid,(void *)sendbuf,strlen(sendbuf.mtest),0); } } //child process read 200 if(pid0) { while(1) { memset(readbuf.mtest,0,128); msgrcv(msgid,(void *)readbuf,128,200,0); printf(receive byte from message queue is:%s\n,readbuf.mtest); } } return 0; }核心结构用ftok(a.c,1)拿钥匙、msgget建同一个消息队列fork()拆分两个进程父进程负责发 100 类型消息子进程负责收 200 类型消息创建msg_client.c#includestdio.h #include sys/types.h #include sys/ipc.h #include sys/msg.h #include stdlib.h #includestring.h #include unistd.h struct msgbuf { long mtype; char mtest[128]; char ID[4]; }; int main() { struct msgbuf sendbuf,readbuf; int msgid; key_t key; pid_t pid; key ftok(a.c,1); msgid msgget(key,IPC_CREAT|0755); if(msgid -1) { printf(creat message queue failed\n); return -1; } system(ipcs -q); printf(creat message queue succeed! msgid %d\n,msgid); //int msgbuf sendbuf.mtype 200; pid fork(); //child process write 200 if(pid0) { while(1) { memset(sendbuf.mtest,0,128); printf(please input to message queue:\n); fgets(sendbuf.mtest,128,stdin); msgsnd(msgid,(void *)sendbuf,strlen(sendbuf.mtest),0); } } //parent process read 100 if(pid0) { while(1) { memset(readbuf.mtest,0,128); msgrcv(msgid,(void *)readbuf,128,100,0); printf(receive byte from message queue is:%s\n,readbuf.mtest); } } return 0; }子进程负责发 200 类型消息父进程负责收 100 类型消息成功实现值得注意的是一定要提前创建a.c文件不然就会一直读取空消息疯狂刷屏
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2477140.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!