#include #include #include #include /* sem_add.c - a program that adds a value to a semaphore **/ /* usage: sem_add key semnum amount * action: program creates a semaphore with key `key', sets its initial * value to zero then blocks waiting to decrement it */ #define oops(m,x) { perror(m); exit(x); } union semun { int val; struct semid_ds *buf; ushort *array; }; main(int ac, char *av[]) { int semid; /* an id */ union semun sem_info; /* some properties */ struct sembuf action; /* action for the sem */ int increment; int keynum ; int semnum; if ( ac != 4 ){ printf("usage: sem_block key semnum amt\n"); exit(1); } increment = atoi(av[3]); semnum = atoi(av[2]); keynum = atoi(av[1]); /* * connect to a semset with 1 semaphore in it */ semid = semget( keynum, semnum+1, 0 ); if( semid == -1 ) oops("semget",2); /* * prepare the increment command */ action.sem_num = semnum; /* action for semaphore */ action.sem_flg = SEM_UNDO; /* auto clean up */ action.sem_op = increment; /* INCREMENT SEMAPHORE */ /* * about to increment semaphore 0 in the set.... */ printf("sem_add about to add %d to semaphore\n", increment); semop( semid, &action, 1); /* only one action */ printf("sem_add DONE!\n"); sleep(3); }