Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/shm.h>
  4. #include <sys/ipc.h>
  5. #include <sys/stat.h>
  6. #include <sys/sem.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. struct sembuf p = {0,-1,SEM_UNDO};
  13. struct sembuf v = {0,1,SEM_UNDO};
  14. struct sembuf wait = {0,0,SEM_UNDO};
  15.  
  16.  
  17. struct sembuf p1 = {1,-1,SEM_UNDO};
  18. struct sembuf v1 = {1,1,SEM_UNDO};
  19. struct sembuf wait1 = {1,0,SEM_UNDO};
  20.  
  21. struct sembuf p2 = {2,-1,SEM_UNDO};
  22. struct sembuf v2 = {2,1,SEM_UNDO};
  23. struct sembuf wait2 = {2,0,SEM_UNDO};
  24.  
  25. union semun
  26. {
  27.     int val;
  28.     struct semid_ds *buf;
  29.     ushort *array;
  30. } sem;
  31.  
  32. struct sembuf sop;
  33.  
  34. int semdec(int sid, int n)
  35. {
  36.     sop.sem_num = n;
  37.     sop.sem_op = -1;
  38.     semop(sid, &sop, 1);
  39. }
  40.  
  41. int seminc(int sid, int n)
  42. {
  43.     sop.sem_num = n;
  44.     sop.sem_op = 1;
  45.     semop(sid, &sop, 1);
  46. }
  47.  
  48. int waits(int sid, int n)
  49. {
  50.     sop.sem_num = n;
  51.     sop.sem_op = 0;
  52.     semop(sid, &sop, 1);
  53. }
  54.  
  55. void sleepbw(int ms)
  56. {
  57.     usleep(ms*1000);
  58. }
  59.  
  60. int main()
  61. {
  62.    
  63.     key_t key = 11;
  64.  
  65.     int semid = semget (key,1,0777|IPC_CREAT);
  66.  
  67.     printf("Semaphore created (%d)\n",semid);
  68.     if (semid < 0)
  69.     {
  70.         exit(0);
  71.     }
  72.  
  73.     int size = 2 * sizeof(int);
  74.  
  75.     int iterations = 3;
  76.     sem.array = (ushort*)malloc(sizeof(ushort)*3);
  77.     sem.array[0] = 1;
  78.     sem.array[1] = 1;
  79.     sem.array[2] = 1;
  80.     sop.sem_flg = SEM_UNDO;
  81.  
  82.     semctl(semid, 3, SETALL, sem);
  83.     semdec(semid,0);
  84.  
  85.     int pid = fork();
  86.  
  87.     if (pid != 0)
  88.     {
  89.         printf("Process 1 (%d)\n",getpid());
  90.         int shmid = shmget(key,size, 0777| IPC_CREAT);
  91.         char *addr = shmat(shmid,0,0);
  92.         printf("Process 1 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
  93.         int pid1 = fork();
  94.         if (pid1!=0)
  95.         {
  96.             for (int i = 0; i < iterations; ++i)
  97.             {
  98.                 sleepbw(3);
  99.                 printf("Process 1 waiting\n");
  100.                 waits(semid,0);
  101.                 seminc(semid, 0);
  102.                 strcat(addr, "1 ");
  103.                 strcat(addr, "1 ");
  104.                 printf("Process 1 executed\n");
  105.                 semdec(semid, 0);
  106.                 sleepbw(15);
  107.             }
  108.        
  109.             shmdt(addr);
  110.         }
  111.         else
  112.         {
  113.             printf("Process 3 (%d)\n",getpid());
  114.  
  115.             int shmid = shmget(key,size,0);
  116.             char *addr = shmat (shmid, 0,0);
  117.             printf("Process 3 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
  118.             for (int i = 0; i < iterations; ++i)
  119.             {
  120.                 sleepbw(6);
  121.                 printf("Process 3 waiting\n");
  122.                 waits(semid,0);
  123.                 seminc(semid, 0);
  124.                 strcat(addr, "3 ");
  125.                 printf("Process 3 executed\n");
  126.                 semdec(semid, 0);
  127.                 sleepbw(15);
  128.             }
  129.             char buff[512];
  130.             strcpy(buff,addr);
  131.             //int file = open("text.txt",O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
  132.  
  133.             //printf("Process 3 (%d) OPEN FILE\n",getpid());
  134.  
  135.             //int check = write(file, buff, 2 * size);
  136.             //if(check == 2 * size)
  137.             printf("Process 3 (%d) has wrote %s\n",getpid(),buff);
  138.  
  139.             shmdt(addr);
  140.             shmctl(shmid,IPC_RMID, NULL);
  141.             exit(0);
  142.         }
  143.     }
  144.     else
  145.     {
  146.         printf("Process 2 (%d) \n",getpid());
  147.         int shmid = shmget(key,size,0);
  148.         char *addr = shmat (shmid, 0,0);
  149.         printf("Process 2 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
  150.         for (int i = 0; i < iterations; ++i)
  151.         {
  152.             printf("Process 2 waiting\n");
  153.             waits(semid,0);
  154.             seminc(semid, 0);
  155.             strcat(addr, "2 ");
  156.             printf("Process 2 executed\n");
  157.             semdec(semid, 0);
  158.             sleepbw(15);
  159.         }
  160.         shmdt(addr);
  161.         exit(0);
  162.     }
  163.     /*
  164.     if (pid == 0)
  165.     {
  166.         printf("Process 2 (%d) START\n",getpid());
  167.  
  168.         int shmid = shmget(key,size,0);
  169.         char *addr = shmat (shmid, 0,0);
  170.         printf("Process 2 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
  171.  
  172.         for(int i = 0; i<interations; i++)
  173.         {
  174.             semop(semid, &wait,1);
  175.             semop(semid, &v,1);
  176.             printf("Process 2 (%d) blocked semaphore\n",getpid());
  177.  
  178.             strcat(addr, "2 ");
  179.  
  180.             semop(semid, &p,1);
  181.             printf("Process 2 (%d) released semaphore\n", getpid());
  182.             usleep(200000);
  183.         }
  184.         shmdt(addr);
  185.  
  186.     } else {
  187.         int shmid = shmget(key,size, 0777| IPC_CREAT);
  188.         char *addr = shmat(shmid,0,0);
  189.         printf("Process 1 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
  190.  
  191.         for(int i = 0; i < interations; i++)
  192.         {
  193.             usleep(100000);
  194.             semop(semid, &wait,1);
  195.             semop(semid, &v,1);
  196.             printf("Process 1 (%d) blocked semaPHore\n",getpid());
  197.  
  198.             strcat(addr,"1 ");
  199.             strcat(addr,"1 ");
  200.             semop(semid, &p, 1);
  201.             printf("Process 1 (%d) released semaphore\n", getpid());
  202.        
  203.             usleep(100000);
  204.         }
  205.  
  206.         char buff[128];
  207.         strcpy(buff,addr);
  208.  
  209.         shmdt(addr);
  210.         shmctl(shmid,IPC_RMID, NULL);
  211.         printf("Process 2 (%d) deleted shared memory\n",getpid());
  212.  
  213.         if(fork() == 0)
  214.         {
  215.             printf("Process 3 (%d) START\n",getpid());
  216.             int file = open("text.txt",O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
  217.  
  218.             printf("Process 3(%d) OPEN FILE\n",getpid());
  219.  
  220.             int check = write(file, buff, 2 * size);
  221.             if(check == 2 * size) printf("Process 3 (%d) has wrote %s\n",getpid(),buff);
  222.         }
  223.         exit(0);
  224.     }
  225.     */
  226.     return (0);
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement