Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/msg.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <ctype.h>
  8.  
  9. struct msgbuf1{
  10. long mtype;
  11. char text[10];
  12. };
  13.  
  14.  
  15. int check(int code, const char *msg){
  16. if (code == -1) {
  17. perror(msg);
  18. exit(0);
  19. }
  20. return code;
  21. }
  22.  
  23. int msgCreate(key_t key) {
  24. return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
  25. }
  26.  
  27. int msgOpen(key_t key) {
  28. return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
  29. }
  30.  
  31. int msgRemove(int msgId) {
  32. check(msgctl(msgId, IPC_RMID, NULL), __FUNCTION__);
  33. }
  34.  
  35. int msgSend(int msgid, void* msgp, size_t msgsz) {
  36. const int msgflg = 0;
  37. check(msgsnd(msgid, msgp, msgsz, msgflg), __FUNCTION__);
  38. }
  39.  
  40. int msgReceive(int msgid, void* msgp, size_t msgsz,long msgtyp){
  41. const int msgflg = 0;
  42. check(msgrcv(msgid,msgp,msgsz,msgtyp,msgflg),__FUNCTION__);
  43. }
  44.  
  45.  
  46. int main(){
  47. int key;
  48. key = ftok(".",'g');
  49. msgCreate(key);
  50. int msgiiid = msgOpen(key);
  51. struct msgbuf1 element;
  52. element.mtype = 2;
  53. strcpy(element.text, "abcdefghj");
  54. msgSend(msgiiid,&element,sizeof(element.text));
  55. return 0;
  56. }
  57.  
  58. #include <stdlib.h>
  59. #include <stdio.h>
  60. #include <sys/types.h>
  61. #include <sys/msg.h>
  62. #include <string.h>
  63. #include <unistd.h>
  64. #include <ctype.h>
  65.  
  66.  
  67. struct msgbuf1 {
  68. long mtype;
  69. char text[10];
  70. };
  71.  
  72.  
  73. int check(int code, const char *msg) {
  74. if (code == -1) {
  75. perror(msg);
  76. exit(0);
  77. }
  78. return code;
  79. }
  80.  
  81. int msgCreate(key_t key) {
  82. return check(msgget(key, IPC_CREAT | IPC_EXCL | 0700), __FUNCTION__);
  83. }
  84.  
  85. int msgOpen(key_t key) {
  86. return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
  87. }
  88.  
  89. int msgRemove(int msgId) {
  90. check(msgctl(msgId, IPC_RMID, NULL), __FUNCTION__);
  91. }
  92.  
  93. int msgSend(int msgid, void* msgp, size_t msgsz) {
  94. const int msgflg = 0;
  95. check(msgsnd(msgid, msgp, msgsz, msgflg), __FUNCTION__);
  96. }
  97.  
  98. int msgReceive(int msgid, void* msgp, size_t msgsz,long msgtyp){
  99. const int msgflg = 0;
  100. check(msgrcv(msgid,msgp,msgsz,msgtyp,msgflg),__FUNCTION__);
  101. }
  102.  
  103.  
  104.  
  105.  
  106. int main(){
  107. int key;
  108. key = ftok(".",'g');
  109. int msgiiid1 = msgOpen(key);
  110. struct msgbuf1 element2;
  111. msgReceive(msgiiid1,&element2,sizeof(element2),2);
  112. for(int i = 0; i < sizeof(element2.text); i++){
  113. printf("Element: %c \n", toupper(element2.text[i]));
  114. sleep(1);
  115. }
  116. msgRemove(msgiiid1);
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement