x1n53n

Untitled

Nov 19th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5. #include <wait.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8.  
  9. int main(void)
  10. {
  11.     int key = 5678;
  12.     int i = 65;
  13.    
  14.     /*
  15.      * Create the segment.
  16.      */
  17.     int shmid;
  18.     shmid = shmget(key, sizeof(int), IPC_CREAT | 0666);
  19.    
  20.      /*
  21.      * Attach the segment to our data space.
  22.      */
  23.     int *shm;
  24.     shm = shmat(shmid, NULL, 0);
  25.  
  26.     *shm = i;
  27.    
  28.     int pid = fork();
  29.     //Son
  30.     if (pid == 0){
  31.     int reg = *shm;
  32.     sleep(2);
  33.     reg--;
  34.         *shm = reg;
  35.     exit(0);
  36.    
  37.     }
  38.     //Father
  39.     else{
  40.         int reg = *shm;
  41.     sleep(1);
  42.     reg++;
  43.     *shm = reg;
  44.     }
  45.  
  46.     wait(NULL);
  47.     printf("End --> valeur de shm: %d\n", *shm);
  48. }
Add Comment
Please, Sign In to add comment