Advertisement
Guest User

Untitled

a guest
May 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <time.h>
  10.  
  11. #define MSGSIZE 100
  12. #define STRUCTMSGSIZE (MSGSIZE + sizeof(pid_t))
  13. #define ERRCODE -1
  14. #define NO_FLAGS 0
  15. #define MSGJOINQ 1
  16. #define MSGLEAVEQ 2
  17. #define MSG_TYPE_DATA 3
  18. #define MSG_ALL_TYPES 0
  19. #define QUEUE_RIGHTS 0600
  20. #define PROJECT_PREFIX 97
  21. #define COUNT_MESSAGES 10
  22. #define START_CHAR 32
  23. #define END_CHAR 127
  24. #define SLEEP_TIME 1
  25.  
  26. struct message {
  27.   long mtype;
  28.   pid_t pid;
  29.   char mtext[MSGSIZE];
  30. };
  31.  
  32.  
  33. int qregister(int queue) {
  34.   pid_t pid = getpid();
  35.   struct message buf = {MSGJOINQ, pid, ""};
  36.   char greetings[MSGSIZE];
  37.   struct message hi =  {MSG_TYPE_DATA, pid, ""};
  38.   sprintf(hi.mtext, "hi from %d!", pid);
  39.  
  40.   if(msgsnd(queue, &buf, sizeof(pid_t), NO_FLAGS) == ERRCODE) {
  41.     perror("msgsnd");
  42.     return EXIT_FAILURE;
  43.   }
  44.  
  45.   if(msgsnd(queue, &hi, strlen(hi.mtext) + 1 + sizeof(pid_t), NO_FLAGS) ==  ERRCODE) {
  46.     perror("msgsnd");
  47.     return EXIT_FAILURE;
  48.   }
  49.  
  50.   return EXIT_SUCCESS;
  51. }
  52.  
  53. int qleave(int queue) {
  54.   pid_t pid = getpid();
  55.   struct message buf = {MSGLEAVEQ, pid, ""};
  56.   struct message bye = {MSG_TYPE_DATA, pid, "bye!"};
  57.  
  58.   if(msgsnd(queue, &bye, strlen(bye.mtext) + 1 + sizeof(pid_t), NO_FLAGS) ==  ERRCODE) {
  59.     perror("msgsnd");
  60.     return EXIT_FAILURE;
  61.   }
  62.  
  63.   if(msgsnd(queue, &buf, sizeof(pid_t), NO_FLAGS) ==  ERRCODE) {
  64.     perror("msgsnd");
  65.     return EXIT_FAILURE;
  66.   }
  67.  
  68.   return EXIT_SUCCESS;
  69. }
  70.  
  71. int msggen(char *buf) {
  72.   int length = rand() % (MSGSIZE - 2) + 1; //length from 1 to MSGSIZE-1
  73.   int i;
  74.   for(i = 0; i < length; ++i) {
  75.     buf[i] = (char)(rand() % (END_CHAR - START_CHAR) + START_CHAR);
  76.   }
  77.   buf[length] = '\0';
  78.   ++length;
  79.  
  80.   return length;
  81. }
  82.  
  83. int qsndmsg(int queue) {
  84.   pid_t pid = getpid();
  85.   struct message buf = {MSG_TYPE_DATA, pid, ""};
  86.   int i;
  87.  
  88.   printf("I'm %d\n", pid);
  89.   int length;
  90.   for(i = 0; i < COUNT_MESSAGES; ++i) {
  91.     length = msggen(buf.mtext);
  92.     if (length == 0){
  93.       break;
  94.     }
  95.     printf("Send message: %s\n", buf.mtext);
  96.  
  97.     if(msgsnd(queue, &buf, length + sizeof(pid_t), NO_FLAGS) ==  ERRCODE) {
  98.       perror("msgsnd");
  99.       return EXIT_FAILURE;
  100.     }
  101.     sleep(SLEEP_TIME);
  102.   }
  103.  
  104.   return EXIT_SUCCESS;
  105. }
  106.  
  107. int main() {
  108.   srand(time(NULL));
  109.  
  110.   key_t queueKey = ftok("master.c", PROJECT_PREFIX);
  111.   if(queueKey ==  ERRCODE) {
  112.     perror("ftok");
  113.     return EXIT_FAILURE;
  114.   }
  115.  
  116.   int queue;
  117.   queue = msgget(queueKey, NO_FLAGS);
  118.   if(queue ==  ERRCODE)     {
  119.     perror("msgget");
  120.     return EXIT_FAILURE;
  121.   }
  122.  
  123.   printf("Connected to queue\n");
  124.  
  125.   if(qregister(queue) != EXIT_SUCCESS) {
  126.     return EXIT_FAILURE;
  127.   }
  128.  
  129.   if(qsndmsg(queue) != EXIT_SUCCESS) {
  130.     qleave(queue);
  131.     return EXIT_FAILURE;
  132.   }
  133.  
  134.   if(qleave(queue) != EXIT_SUCCESS) {
  135.     return EXIT_FAILURE;
  136.   }
  137.  
  138.   return EXIT_SUCCESS;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement