Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <sys/neutrino.h>
  6. #include <sys/netmgr.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <sys/mman.h>
  11. #include <time.h>
  12.  
  13. struct msg {
  14.     char name[100];
  15.     int size;
  16.     int direction; // 0 - asc, 1 - desc
  17.     int command; // 0 - sort, 1 - max, 2 - min, 3 - avg
  18. };
  19.  
  20.  
  21. int main(void) {
  22.    
  23.     errno = EOK;
  24.     int channelID;  // Channel ID
  25.     pid_t pID; // Process ID
  26.  
  27.  
  28.     if ((channelID = ChannelCreate(0)) == -1) { //tworzenie kanału komunikacji
  29.        
  30.         return EXIT_FAILURE;
  31.     }
  32.  
  33.     if ((pID = fork()) == -1) { //tworzenie procesu
  34.        
  35.         perror("Fork failure!");
  36.         return EXIT_FAILURE;
  37.  
  38.     }
  39.    
  40.     else if (pID == 0) {
  41.         //PROCES POTOMNY
  42.         char sharedMemoryObject[] = "/shm_object";
  43.         int size = 5;
  44.         int connectionID;
  45.         int command = 2;
  46.         char replyMessage[100];
  47.  
  48.         printf("CHILD CHANNEL ID: %d\n", channelID); //obecnie używany kanał komunikacji
  49.         printf("CHILD Process: PID:%d\n", getpid()); //wyprintowanie procesu
  50.  
  51.         connectionID = ConnectAttach(ND_LOCAL_NODE, getpid(), channelID, 0, 0); //ustawienie kanału komunikacji pomiędzy procesem a kanałem
  52.         // ND_LOCAL_NODE - lokalne połączenie, getppid() - pobranie ID procesu, channelID - id kanału, najmniejsze połączenie, flagi
  53.         printf("CHILD Connection id: %d\n", connectionID);
  54.  
  55.         // Opening sharing memory descriptor
  56.         int fileDescription = shm_open(sharedMemoryObject, O_RDWR | O_CREAT, 0777); //O_RDWR readAndWrite access, O_CREAT - tworzony gdy go nie ma (plik dzielonej pamięci) z mode, 0777 - uprawnienia
  57.         ftruncate(fileDescription, size * sizeof(int)); // drugi argument to długoś jaką chcemy by ten plik miał w bajtach
  58.  
  59.         int* addres = (int*)mmap(0, size * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fileDescription, 0); //mmap mapuje rejon pamięci  w przestrzen adresową procesu
  60.  
  61.         srand(time(NULL));
  62.         for (int i = 0; i < size; i++) {
  63.             addres[i] = rand() % 100;
  64.         }
  65.  
  66.         for (int i = 0; i < size; i++) {
  67.             printf("%d\n", addres[i]);
  68.         }
  69.  
  70.         struct msg m;
  71.         strcpy(m.name, sharedMemoryObject);
  72.         m.size = size;
  73.         m.direction = 0;
  74.         m.command = command;
  75.  
  76.         MsgSend(connectionID, &m, sizeof(m), replyMessage, 100); //MsgSend() wysłanie wiadomości do kanału. &m adres bufora z wiadomością do wysłania, replyMessage adres bufora z odpowiedzią
  77.  
  78.         for (int i = 0; i < size; i++) {
  79.             printf("%d\n", addres[i]);
  80.         }
  81.  
  82.         printf("CHILD RMSG = %s\n", replyMessage);
  83.  
  84.         munmap(addres, size * sizeof(int));
  85.         close(fileDescription);
  86.         shm_unlink("/shm_object"); //zamknięcie pliku
  87.         ConnectDetach(connectionID); //zamknięcie połączenia
  88.  
  89.     }
  90.     else {
  91.         /* PARENT PROCESS */
  92.         char rmsg[100] = "Done";
  93.         struct msg m;
  94.  
  95.         printf("PARENT Process: PID:%d\n", getpid());
  96.         int messageID = MsgReceive(channelID, &m, sizeof(m), NULL);
  97.         printf("PARENT MESSAGE FROM CLIENT %d:\n", pID);
  98.  
  99.         int fd = shm_open(m.name, O_RDWR, 0777);
  100.         int* address2 = (int*)mmap(0, m.size * sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  101.  
  102.         switch (m.command) {
  103.         case 0:
  104.             for (int i = 0; i < m.size; i++) { //bubble sort
  105.                 for (int j = 0; j < m.size - i - 1; j++) {
  106.                     if ((m.direction == 0) ? address2[j] > address2[j + 1] : address2[j] < address2[j + 1]) {
  107.                         int temp = address2[j];
  108.                         address2[j] = address2[j + 1];
  109.                         address2[j + 1] = temp;
  110.                     }
  111.                 }
  112.             }
  113.             break;
  114.         case 1:
  115.             int max = address2[0];
  116.             for (int i = 1; i < m.size; i++) {
  117.                 if (address2[i] > max) {
  118.                     max = address2[i];
  119.                 }
  120.             }
  121.             printf("MAX = %d\n", max);
  122.             break;
  123.         case 2:
  124.             int min = address2[0];
  125.             for (int i = 1; i < m.size; i++) {
  126.                 if (address2[i] < min) {
  127.                     min = address2[i];
  128.                 }
  129.             }
  130.             printf("MIN = %d\n", min);
  131.             break;
  132.         case 3:
  133.             int avg = 0;
  134.             int sum = 0;
  135.             for (int i = 0; i < m.size; i++) {
  136.                 sum += address2[i];
  137.             }
  138.             avg = sum / m.size;
  139.             printf("Average = %d\n", avg);
  140.             break;
  141.         default:
  142.             printf("Not implemented.\n");
  143.         }
  144.  
  145.  
  146.         munmap(address2, m.size * sizeof(int));
  147.         close(fd);
  148.  
  149.         MsgReply(messageID, 0, rmsg, sizeof(rmsg) + 1);
  150.  
  151.         sleep(100);
  152.  
  153.         ChannelDestroy(channelID);
  154.     }
  155.  
  156.     return EXIT_SUCCESS;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement