当前位置:   article > 正文

IOday7作业

IOday7作业

1> 将信号和消息队列的课堂代码敲一遍

2> 使用消息队列完成两个进程间相互通信

  1. #include<myhead.h>
  2. #define MSGSIZE sizeof(struct msgbuf)-sizeof(long)
  3. struct msgbuf
  4. {
  5. long mtype;
  6. char mtext[1024];
  7. };
  8. int main(int argc, const char *argv[])
  9. {
  10. pid_t pid=fork();
  11. if(pid>0)
  12. {
  13. key_t key = 0;
  14. if((key=ftok("/",'k'))==-1)
  15. {
  16. perror("ftok error");
  17. return -1;
  18. }
  19. printf("ftok success key = %#x\n",key);
  20. int msqid = -1;
  21. if((msqid = msgget(key,IPC_CREAT|0664))==-1)
  22. {
  23. perror("msgget error");
  24. return -1;
  25. }
  26. printf("msgget success msqid = %d\n",msqid);
  27. struct msgbuf sbuf;
  28. while(1)
  29. {
  30. bzero(sbuf.mtext, sizeof(sbuf.mtext));
  31. printf("消息类型:");
  32. scanf("%ld",&sbuf.mtype);
  33. getchar();
  34. printf("请输入正文:");
  35. fgets(sbuf.mtext,sizeof(sbuf.mtext), stdin);
  36. sbuf.mtext[strlen(sbuf.mtext)-1]=0;
  37. msgsnd(msqid,&sbuf, MSGSIZE, 0);
  38. printf("发送成功\n");
  39. if(strcmp(sbuf.mtext, "quit")==0)
  40. {
  41. break;
  42. }
  43. }
  44. }else if(pid==0)
  45. {
  46. key_t key1 = 0;
  47. if((key1=ftok("/",'k'))==-1)
  48. {
  49. perror("ftok error");
  50. return -1;
  51. }
  52. printf("ftok success key1 = %#x\n",key1);
  53. int msqid1 = -1;
  54. if((msqid1 = msgget(key1,IPC_CREAT|0664))==-1)
  55. {
  56. perror("msgget error");
  57. return -1;
  58. }
  59. printf("msgget success msqid1 = %d\n",msqid1);
  60. struct msgbuf rbuf;
  61. while(1)
  62. {
  63. bzero(rbuf.mtext, sizeof(rbuf.mtext));
  64. msgrcv(msqid1,&rbuf,MSGSIZE,2,0);
  65. printf("收到消息为:%s\n",rbuf.mtext);
  66. if(strcmp(rbuf.mtext, "quit")==0)
  67. {
  68. break;
  69. }
  70. }
  71. if(msgctl(msqid1,IPC_RMID,NULL)!=0)
  72. {
  73. perror("msgctl error");
  74. return -1;
  75. }
  76. }
  77. else
  78. {
  79. perror("fork error");
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. #include<myhead.h>
  85. #define MSGSIZE sizeof(struct msgbuf)-sizeof(long)
  86. struct msgbuf
  87. {
  88. long mtype;
  89. char mtext[1024];
  90. };
  91. int main(int argc, const char *argv[])
  92. {
  93. pid_t pid=fork();
  94. if(pid>0)
  95. {
  96. key_t key = 0;
  97. if((key=ftok("/",'k'))==-1)
  98. {
  99. perror("ftok error");
  100. return -1;
  101. }
  102. printf("ftok success key = %#x\n",key);
  103. int msqid = -1;
  104. if((msqid = msgget(key,IPC_CREAT|0664))==-1)
  105. {
  106. perror("msgget error");
  107. return -1;
  108. }
  109. printf("msgget success msqid = %d\n",msqid);
  110. struct msgbuf sbuf;
  111. while(1)
  112. {
  113. bzero(sbuf.mtext, sizeof(sbuf.mtext));
  114. printf("消息类型:");
  115. scanf("%ld",&sbuf.mtype);
  116. getchar();
  117. printf("请输入正文:");
  118. fgets(sbuf.mtext,sizeof(sbuf.mtext), stdin);
  119. sbuf.mtext[strlen(sbuf.mtext)-1]=0;
  120. msgsnd(msqid,&sbuf, MSGSIZE, 0);
  121. printf("发送成功\n");
  122. if(strcmp(sbuf.mtext, "quit")==0)
  123. {
  124. break;
  125. }
  126. }
  127. }else if(pid==0)
  128. {
  129. key_t key1 = 0;
  130. if((key1=ftok("/",'k'))==-1)
  131. {
  132. perror("ftok error");
  133. return -1;
  134. }
  135. printf("ftok success key1 = %#x\n",key1);
  136. int msqid1 = -1;
  137. if((msqid1 = msgget(key1,IPC_CREAT|0664))==-1)
  138. {
  139. perror("msgget error");
  140. return -1;
  141. }
  142. printf("msgget success msqid = %d\n",msqid1);
  143. struct msgbuf rbuf;
  144. while(1)
  145. {
  146. bzero(rbuf.mtext, sizeof(rbuf.mtext));
  147. msgrcv(msqid1,&rbuf,MSGSIZE,1,0);
  148. printf("收到消息为:%s\n",rbuf.mtext);
  149. if(strcmp(rbuf.mtext, "quit")==0)
  150. {
  151. break;
  152. }
  153. }
  154. if(msgctl(msqid1,IPC_RMID,NULL)!=0)
  155. {
  156. perror("msgctl error");
  157. return -1;
  158. }
  159. }
  160. else
  161. {
  162. perror("fork error");
  163. return -1;
  164. }
  165. return 0;
  166. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/164072
推荐阅读
相关标签
  

闽ICP备14008679号