Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 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.  
  8.  
  9. struct msgbuf1 {
  10.  
  11. long mtype;
  12. char text[10];
  13. };
  14.  
  15.  
  16. int check(int code, const char *msg) {
  17. if (code == -1) {
  18. perror(msg);
  19. exit(0);
  20. }
  21. return code;
  22. }
  23.  
  24. int msgCreate(key_t key) {
  25. return check(msgget(key, IPC_CREAT | IPC_EXCL | 0700), __FUNCTION__);
  26. }
  27. int msgOpen(key_t key) {
  28. return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
  29. }
  30. int msgRemove(int msgId) {
  31. check(msgctl(msgId, IPC_RMID, NULL), __FUNCTION__);
  32. }
  33.  
  34. int msgSend(int msgid, void* msgp, size_t msgsz) {
  35. const int msgflg = 0;
  36. check(msgsnd(msgid, msgp, msgsz, msgflg), __FUNCTION__);
  37. }
  38.  
  39. int msgReceive(int msgid, void* msgp, size_t msgsz,long msgtyp){
  40. const int msgflg = 0;
  41. check(msgrcv(msgid,msgp,msgsz,msgtyp,msgflg),__FUNCTION__);
  42. }
  43.  
  44.  
  45.  
  46.  
  47. int main()
  48. {
  49.  
  50. int key;
  51. key = ftok(".",'g');
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. int msgiiid1 = msgOpen(key);
  62. struct msgbuf1 element2;
  63. msgReceive(msgiiid1,&element2,sizeof(element2),2);
  64. for(int i = 0; i < sizeof(element2.text); i++)
  65. {
  66. printf("Element: %c \n", toupper(element2.text[i]));
  67. sleep(1);
  68.  
  69. }
  70. msgSend(msgiiid1,&element2,sizeof(element2.text));
  71. sleep(5);
  72. msgRemove(msgiiid1);
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement