日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IPC通信

發(fā)布時間:2025/3/17 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IPC通信 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

參考 linux 網(wǎng)絡(luò)編程第二版 敲

發(fā)現(xiàn) 出錯了... ...
網(wǎng)上也沒有特別多的參考程序。后來在 man msgrcv 里面找到了參考程序

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h>struct msgbuf {long mtype;char mtext[80]; };static void usage(char *prog_name, char *msg) {if (msg != NULL)fputs(msg, stderr);fprintf(stderr, "Usage: %s [options]\n", prog_name);fprintf(stderr, "Options are:\n");fprintf(stderr, "-s send message using msgsnd()\n");fprintf(stderr, "-r read message using msgrcv()\n");fprintf(stderr, "-t message type (default is 1)\n");fprintf(stderr, "-k message queue key (default is 1234)\n");exit(EXIT_FAILURE); }static void send_msg(int qid, int msgtype) {struct msgbuf msg;time_t t;msg.mtype = msgtype;time(&t);snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",ctime(&t));if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),IPC_NOWAIT) == -1) {perror("msgsnd error");exit(EXIT_FAILURE);}printf("sent: %s\n", msg.mtext); }static void get_msg(int qid, int msgtype) {struct msgbuf msg;if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,MSG_NOERROR | IPC_NOWAIT) == -1) {if (errno != ENOMSG) {perror("msgrcv");exit(EXIT_FAILURE);}printf("No message available for msgrcv()\n");} elseprintf("message received: %s\n", msg.mtext); }int main(int argc, char *argv[]) {int qid, opt;int mode = 0; /* 1 = send, 2 = receive */int msgtype = 1;int msgkey = 1234;while ((opt = getopt(argc, argv, "srt:k:")) != -1) {switch (opt) {case 's':mode = 1;break;case 'r':mode = 2;break;case 't':msgtype = atoi(optarg);if (msgtype <= 0)usage(argv[0], "-t option must be greater than 0\n");break;case 'k':msgkey = atoi(optarg);break;default:usage(argv[0], "Unrecognized option\n");}}if (mode == 0)usage(argv[0], "must use either -s or -r option\n");qid = msgget(msgkey, IPC_CREAT | 0666);if (qid == -1) {perror("msgget");exit(EXIT_FAILURE);}if (mode == 2)get_msg(qid, msgtype);elsesend_msg(qid, msgtype);exit(EXIT_SUCCESS); }

首先一定要會用errno

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/msg.h> #include <unistd.h> #include <time.h> #include <errno.h> #include <sys/ipc.h> void msg_show_attr(int msg_id, struct msqid_ds msg_info) {int ret = -1;sleep(1);ret = msgctl(msg_id, IPC_STAT, &msg_info);if(-1 == ret){printf("獲取消息失敗 ***** %d \n",errno);return ;}printf("\n");printf("現(xiàn)在隊列中的字節(jié)數(shù):%ld\n",msg_info.msg_cbytes);printf("隊列中的消息數(shù):%d\n",(int)msg_info.msg_qnum);printf("隊列中最大的字節(jié)數(shù):%d\n",(int)msg_info.msg_qbytes);printf("最后發(fā)送消息的進(jìn)程pid: %d\n",msg_info.msg_lspid);printf("最后接受消息的進(jìn)程pid: %d\n",msg_info.msg_lrpid);printf("最后發(fā)送消息的時間:%s",ctime(&(msg_info.msg_stime)));printf("最后接受消息的時間:%s",ctime(&(msg_info.msg_rtime)));printf("最后變化的時間:%s",ctime(&(msg_info.msg_ctime)));printf("消息UID是: %d\n",msg_info.msg_perm.uid);printf("消息GID是: %d\n",msg_info.msg_perm.gid);}int main() {int ret = -1;int msg_flags, msg_id;key_t key;int temp;struct msgmbuf{long mtype;char mtext[10];};struct msqid_ds msg_info;struct msgmbuf msg_mbuf;int msg_sflags,msg_rflags;char *msgpath = "/ipc/msg/";key = ftok(msgpath, 'X');if(key != -1){printf("成功建立\n");}else{printf("建立失敗\n");}msg_flags = IPC_CREAT|IPC_EXCL;msg_id = msgget(key, msg_flags|0777);if(-1 == msg_id){printf("消息建立失敗\n");return 0;}msg_show_attr(msg_id, msg_info);msg_sflags = IPC_NOWAIT;msg_mbuf.mtype = 220;memcpy(msg_mbuf.mtext, "abcdef", sizeof("abcdef"));ret = msgsnd(msg_id, (void *)&msg_mbuf, sizeof("abcdef"),msg_sflags);if(-1 == ret){printf("發(fā)送消息失敗\n");}msg_show_attr(msg_id, msg_info);msg_rflags = IPC_NOWAIT|MSG_NOERROR;temp = sizeof(struct msgmbuf) - sizeof(long);ret = msgrcv(msg_id, (void *)&msg_mbuf,temp, 220,msg_rflags);if(ret == -1){printf("接收消息失敗\n");printf("%d***",errno);}else{printf("接受消息成功長度是: %d\n",ret);}msg_show_attr(msg_id, msg_info);msg_info.msg_perm.uid = 1000;msg_info.msg_perm.gid = 1000;//msg_info.msg_qbytes = 120;ret = msgctl(msg_id, IPC_SET,&msg_info);// 設(shè)置消息屬性if(ret == -1){printf("設(shè)置消息屬性失敗\n");return 0;}msg_show_attr(msg_id, msg_info);ret = msgctl(msg_id, IPC_RMID, NULL);if(-1 == ret){printf("刪除消息失敗\n");return 0;}return 0; }

后來修了一個版本 首先 那個消息結(jié)構(gòu)體一定要用long,如果不用long的話。就會一直接收失敗errno的意思大概是,沒有找到對應(yīng)的消息類型。
還有一個問題沒有解決,就是修改屬性是成功的可以,然后就不能成功讀取msg_info消息的值,不清楚到底是什么原因。如果有人知道,請留言。。。。。。
errno錯誤大全

轉(zhuǎn)載于:https://www.cnblogs.com/eat-too-much/p/7881703.html

總結(jié)

以上是生活随笔為你收集整理的IPC通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。