andrelievable

klient

Jan 10th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <pthread.h>
  10. #include <signal.h>
  11.  
  12. #define MAX 10
  13. #define SERWER 1
  14.  
  15. int IDqueue;
  16. int amount_msg;
  17. int end_msg = 0;
  18. int count_one = 0;
  19. int count_two = 0;
  20. char buf[BUFSIZ];
  21.  
  22. typedef struct message
  23. {
  24.     long msg_1;
  25.     long msg_2;
  26.     char text[MAX];
  27. } Message;
  28.  
  29. void *msg_s()
  30. {
  31.     Message msg;
  32.     int i, j;
  33.     char mark;
  34.     msg.msg_1 = SERWER;
  35.     msg.msg_2 = getpid();
  36.  
  37.     for (i = 0; i < amount_msg; i++)
  38.     {
  39.         j = 0;
  40.         printf("Set message: ");
  41.  
  42.         mark = getchar();
  43.  
  44.         if (mark != '\n')
  45.         {
  46.             msg.text[j] = mark;
  47.             j++;
  48.         }
  49.  
  50.         while ((j < MAX) && ((mark = getchar()) != '\n'))
  51.         {
  52.             msg.text[j] = mark;
  53.             j++;
  54.         }
  55.  
  56.         msg.text[j] = '\0';
  57.  
  58.         printf("\n CLIENT:Sending - %s ---> %ld \n", msg.text, msg.msg_1);
  59.         if (msgsnd(IDqueue, &msg, strlen(msg.text) + 9, IPC_NOWAIT) == -1)
  60.         {
  61.             perror("Message sending failed\n");
  62.             end_msg = 1;
  63.             pthread_exit(0);
  64.         }
  65.         count_one++;
  66.     }
  67.     end_msg = 1;
  68.     pthread_exit(0);
  69. }
  70.  
  71. void *msg_r()
  72. {
  73.     Message msg;
  74.     int i;
  75.     msg.msg_1 = SERWER;
  76.     msg.msg_2 = getpid();
  77.  
  78.     while (1)
  79.     {
  80.         if (end_msg == 1)
  81.         {
  82.             while (count_one != count_two)
  83.             {
  84.                 msgrcv(IDqueue, &msg, 5000, msg.msg_2, 0);
  85.                 count_two++;
  86.                 printf("\nCLIENT: Recieve - %s addressed to %ld", msg.text, msg.msg_1);
  87.                 sprintf(buf, "%s", msg.text);
  88.             }
  89.             break;
  90.         }
  91.  
  92.         if ((msgrcv(IDqueue, &msg, MAX + 9, msg.msg_2, IPC_NOWAIT)) != -1)
  93.         {
  94.             count_two++;
  95.             printf("\nCLIENT: Recieve - %s addressed to %ld\n", msg.text, msg.msg_1);
  96.             sprintf(buf, "%s", msg.text);
  97.         }
  98.     }
  99.  
  100.     pthread_exit(0);
  101. }
  102.  
  103. int main()
  104. {
  105.     int i;
  106.     char tab[255];
  107.     int create_thread_one, create_thread_two;
  108.     int attach_thread_one, attach_thread_two;
  109.  
  110.     pthread_t thread_send;
  111.     pthread_t thread_receive;
  112.  
  113.     printf("PID of process: %d\n", getpid());
  114.  
  115.     if ((IDqueue = msgget(10, IPC_CREAT | 0600)) == -1)
  116.         {
  117.         perror("get messages queues fail\n");
  118.         exit(EXIT_FAILURE);
  119.     }
  120.     printf("ID of queue: %d\n", IDqueue);
  121.     printf("MAX size of message = %d\n", MAX);
  122.  
  123.     printf("set amount of messages: ");
  124.     scanf("%s", tab);
  125.  
  126.     for (i = 0; i < strlen(tab); i++)
  127.     {
  128.         if (tab[i] < 48 || tab[i] > 57)
  129.         {
  130.             printf("Bad amount of messages\n");
  131.             exit(EXIT_FAILURE);
  132.         }
  133.     }
  134.     amount_msg = atoi(tab);
  135.  
  136.     if ((create_thread_one = pthread_create(&thread_send, NULL, msg_s, NULL)) == -1)
  137.     {
  138.         perror("create thread one failed\n");
  139.         exit(EXIT_FAILURE);
  140.     }
  141.     if ((create_thread_two = pthread_create(&thread_receive, NULL, msg_r, NULL)) == -1)
  142.     {
  143.         perror("create thread two failed\n");
  144.         exit(EXIT_FAILURE);
  145.     }
  146.  
  147.     if ((attach_thread_one = pthread_join(thread_send, NULL)) == -1)
  148.     {
  149.         perror("attach thread one failed\n");
  150.         exit(EXIT_FAILURE);
  151.     }
  152.  
  153.     if ((attach_thread_two = pthread_join(thread_receive, NULL)) == -1)
  154.     {
  155.         perror("attach thread two failed\n");
  156.         exit(EXIT_FAILURE);
  157.     }
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment