为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 97
学习任务

北城半夏4806197 的学生作业:

#include #include #include #include #include #include #include #define PATHNAME "." #define PRO_ID 10 #define MSG_TYPE1 100 #define MSG_TYPE2 200 #define MSG_SZ 64 struct msgbuf{ long mtype; char mtext[MSG_SZ]; }; int main(int argc, const char *argv[]) { key_t key; int msgid,ret; struct msgbuf msg; key = ftok(PATHNAME,PRO_ID); if(key == -1){ perror("fotk(): "); exit(EXIT_FAILURE); } msgid = msgget(key,IPC_CREAT | 0666); if(msgid == -1) { perror("msgget(): "); exit(EXIT_FAILURE); } printf("msg id : %d\n",msgid); pid_t cpid1 = fork(); if(cpid1 == -1) { perror("[ERROR] fork():"); } else if(cpid1 == 0){ struct msgbuf process; while(1) { ret = msgrcv(msgid,(void*)&process,MSG_SZ,MSG_TYPE1,0); if(ret == -1) { perror("[ERROR] msgcrv():"); exit(EXIT_FAILURE); } printf("A Process get \n",process.mtext); } } else if(cpid1 > 0) { pid_t cpid2 = fork(); if(cpid2 == -1) { perror("[ERROR] fork():"); exit(EXIT_FAILURE); } else if(cpid2 == 0) { struct msgbuf process; while(1) { ret = msgrcv(msgid,(void*)&process,MSG_SZ,MSG_TYPE2,0); if(ret == -1) { perror("[ERROR] msgcrv():"); exit(EXIT_FAILURE); } printf("B Process get \n",process.mtext); } } else if(cpid2 > 0) { struct msgbuf msgsand; while(1) { printf("type:\n"); scanf("%ld",&msgsand.mtype); printf("buf:\n"); scanf("%s",msgsand.mtext); ret = msgsnd(msgid,(const void *)&msgsand,strlen(msgsand.mtext) + 1,0); } } } return 0; }

得分 0
学习任务

北城半夏4806197 的学生作业:

写程序`#include #include #include #include #include #include #include #include #define FIFO_NAME "./fifo" int main(int argc, const char *argv[]) { int fd; int ret; ret = access(FIFO_NAME,F_OK);//判断有名管道文件是否存在 if (ret == -1) mkfifo(FIFO_NAME,0644); // 创建管道 fd = open(FIFO_NAME,O_RDWR);// 打开有名管道 if (fd == -1){ perror("[ERROR] open():"); exit(EXIT_FAILURE); } time_t t; char time_buffer[64] = {0}; struct tm *p_datetime; t = time(NULL); p_datetime = localtime(&t); sprintf(time_buffer," %d-%d-%d %d::%d::%d\n" ,p_datetime->tm_year + 1900 ,p_datetime->tm_mon + 1 ,p_datetime->tm_mday ,p_datetime->tm_hour ,p_datetime->tm_min ,p_datetime->tm_sec ); int wdata = write(fd,time_buffer,strlen(time_buffer) + 1); if(wdata == -1) { perror("write():"); } close(fd); return 0; }` 读程序 #include #include #include #include #include #include #include #define FIFO_NAME "./fifo" int main(int argc, const char *argv[]) { int fd; int ret; char buffer[64] = {0}; ret = access(FIFO_NAME,F_OK);//判断有名管道文件是否存在 if (ret == -1) { perror("access():"); exit(EXIT_FAILURE); } fd = open(FIFO_NAME,O_RDONLY);// 打开有名管道 if (fd == -1){ perror("[ERROR] open():"); exit(EXIT_FAILURE); } ret = read(fd,buffer,sizeof(buffer)); if(ret > 0) { printf("%s\n",buffer); } close(fd); return 0; } 老师 能不能帮我看一下为什么实现不了 谢谢啦/(ㄒoㄒ)/~~

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号