Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h> /* Pour les constantes O_ */
- #include <sys/stat.h> /* Pour les constantes de mode */
- #include <semaphore.h>
- #define SHM_FAIL = -1;
- #define SHM_SUCCESS = 1;
- int main( int argc, char *argv[] )
- {
- unsigned int val;
- int sem_val;
- char * nom;
- int ok;
- int pid;
- if (argc < 3){
- fprintf(stderr, "Veuillez spécifier le nom du sémaphore "
- " à créer ainsi que ça valeur.\n");
- exit(EXIT_FAILURE);
- }
- nom = argv[1];
- val = atoi(argv[2]);
- shm = shm_open( nom, O_CREAT|O_EXCL, S_IRWXU, val );
- if(shm == SHM_FAIL){
- fprintf( stderr, "Erreur, lors de la création du sémaphore %s.\n",nom );
- exit( EXIT_FAILURE );
- } else {
- ftruncate( sem, &sem_val );
- printf( "Sémaphore %s créé avec valeur %d.\n", nom, sem_val );
- }
- pid = fork();
- if(pid != 0){
- printf("Je suis le père\n");
- sleep(1);
- sem_post(sem);
- wait(NULL);
- } else {
- sem_wait(sem);
- printf("Je suis le fils\n");
- sem_close(sem);
- sem_unlink(nom);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement