Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <semaphore.h>
  4. #include <unistd.h>
  5. #include "lib.h"
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8.  
  9. typedef struct{
  10.         sem_t semA;
  11.         sem_t semB;
  12.  
  13. }DATA;
  14.  
  15. DATA *data;
  16.  
  17.  
  18. int main(){
  19.  
  20.         pid_t pid;
  21.  
  22.         if((data = lib_init( sizeof(DATA)) ) == NULL ){
  23.                 perror("Err lib init\n");
  24.                 exit(-1);
  25.  
  26.         }
  27.  
  28.         if(sem_init( &data->semA, 1, 1 ) == -1 || sem_init( &data->semB, 1, 1 ) == -1){
  29.                 perror("Error sem init \n");
  30.                 exit(-1);
  31.         }
  32.        
  33.         pid = fork();
  34.  
  35.         switch( pid ){
  36.         case 0:
  37.                 sem_wait(&data->semA);
  38.                 printf("a1\n");
  39.                 sem_post(&data->semB);
  40.                
  41.                 sem_wait(&data->semA);
  42.                 printf("a2\n");
  43.                 sem_post(&data->semB);
  44.  
  45.                 lib_fini();
  46.                 return EXIT_SUCCESS;
  47.  
  48.         case -1:
  49.                 perror("Err fork\n");
  50.                 exit(-1);
  51.         default:
  52.                 sem_wait(&data->semB);
  53.                 printf("b1\n");
  54.                 sem_post(&data->semA);
  55.                
  56.                 sem_wait(&data->semB);
  57.                 printf("b2\n");
  58.                 sem_post(&data->semA);
  59.  
  60.                 wait(NULL);
  61.  
  62.                 lib_fini();
  63.         }
  64.  
  65.  
  66.         return EXIT_SUCCESS;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement