Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include <sys/msg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/ipc.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <string.h>
  10. #include <time.h>
  11.  
  12. #define MSG_TEXT_SIZE 1024
  13. int msg_id = -1;
  14.  
  15. struct MSGBuf {
  16.     long msgType;
  17.     char msgText[MSG_TEXT_SIZE];
  18. };
  19.  
  20. int printErrorAndReturn(const char* errorString){
  21.     perror(errorString);
  22.  
  23.     return EXIT_FAILURE;
  24. }
  25.  
  26. int forkMain(){
  27.     int pid = getpid();
  28.     for(;;){
  29.         struct MSGBuf buf;
  30.         long msgTypeFilter = pid;
  31.        
  32.         if(msgrcv(msg_id, &buf, MSG_TEXT_SIZE, msgTypeFilter, 0) == -1){
  33.             puts("ERROR");
  34.             break;
  35.         }
  36.  
  37.         if(buf.msgText[0] == '#')
  38.             break;
  39.  
  40.         printf("%d: %s", pid, buf.msgText);
  41.         fflush(stdout);
  42.     }
  43.  
  44.     return EXIT_SUCCESS;
  45. }
  46.  
  47. int main(const int argc, const char** argv){
  48.     if(argc != 2){
  49.         printf("Usage %s: <number of proc>\n", argv[0]);
  50.         return 1;
  51.     }
  52.     int nmbrOfProc = atoi(argv[1]);
  53.  
  54.     if((msg_id = msgget(IPC_PRIVATE, IPC_CREAT | 0600)) == -1) return printErrorAndReturn("msgget");
  55.  
  56.     int pids[1024*1024] = {-1};
  57.     size_t pidsCounter = 0;
  58.     for(int i=0; i<nmbrOfProc; i++){
  59.         if((pids[pidsCounter] = fork()) == 0)
  60.             return forkMain();
  61.         else if(pids[pidsCounter] == -1) return printErrorAndReturn("FORK");
  62.  
  63.         pidsCounter++;
  64.     }
  65.  
  66.     for (size_t i = 0; i < pidsCounter; i++)
  67.         printf("PID: %d\n", pids[i]);
  68.  
  69.     while(1){
  70.         puts("\nPodaj tekst:");
  71.         char inputText[MSG_TEXT_SIZE];
  72.         if(fgets(inputText, MSG_TEXT_SIZE, stdin) == NULL){
  73.             puts("fgets error!!!");
  74.             break;
  75.         }
  76.        
  77.         int choosenPid = -1;
  78.         if(inputText[0] != '#'){
  79.  
  80.             puts("Podaj pid nadawcy:");
  81.             int goodPid = 0;
  82.             do{
  83.                 while(scanf("%d", &choosenPid) != 1)
  84.                 { puts("wpisz poprawny pid!"); }
  85.  
  86.                 size_t i;
  87.                 for (i = 0; i < pidsCounter; i++){
  88.                     if(choosenPid == pids[i]){
  89.                         goodPid = 1;
  90.                         break;
  91.                     }
  92.                 }
  93.  
  94.                
  95.             }
  96.             while(goodPid == 0);
  97.  
  98.  
  99.             struct MSGBuf buf;
  100.             buf.msgType = choosenPid;
  101.             memcpy(buf.msgText, inputText, MSG_TEXT_SIZE);
  102.  
  103.             if(msgsnd(msg_id, &buf, MSG_TEXT_SIZE, 0) == -1)
  104.                 return printErrorAndReturn("msgsnd");
  105.  
  106.         }
  107.         else{
  108.             size_t i=0;
  109.             for(i=0; i<pidsCounter; i++){
  110.                 struct MSGBuf buf;
  111.                 buf.msgType = pids[i];
  112.                 memcpy(buf.msgText, inputText, MSG_TEXT_SIZE);
  113.  
  114.                 if(msgsnd(msg_id, &buf, MSG_TEXT_SIZE, 0) == -1)
  115.                     return printErrorAndReturn("msgsnd");
  116.             }
  117.  
  118.             break;
  119.         }
  120.  
  121.         fflush(stdout);
  122.         getc(stdin);
  123.         usleep(10000);
  124.     }
  125.  
  126.     msgctl(msg_id, IPC_RMID, NULL);
  127. }
  128.  
  129. /*
  130. Na poczatku dolaczamy potrzebne biblioteki. Przy wykonywaniu programu podajemy parametr odpowiedzialny za liczbe procesow. Tworzymy kolejke komunikatow
  131. przy pomocy funkcji msgget. Nastepnie tworzymy tyle procesow ile podalismy w argumencie programu. Kazdy z nowo utworzonych procesow czyta dane z kolejki i
  132. wypisuje je na wyjscie poprzedzajac swoim pidem. Proces macierzysty wypisuje pidy dzieci a nastepnie w pętli prosi o podanie tresci i pidu do ktorego
  133. ma wyslac komunikat. Znak # konczy prace programu.
  134. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement