/* * Illustrate the waiting for a semaphore to be set * New File Mar 23,1989 * Author Jerry LeVan * */ #include #include #include #include #define COUNT 1 /* How many we are going to create */ #define PERM 0666 /* all can read write */ #define KEY 'JHL ' /* our key */ main() { int id; int result; struct sembuf { short sem_num; short sem_op; short sem_flag; } op[1]; id = semget(KEY,1,IPC_CREAT|PERM); printf("status of get key = %d\n",id); if(id == -1) {perror("Bad Semaphore ID fetch"); exit();} /* initialize the op data structure */ op[0].sem_num = 0; /* the first one */ op[0].sem_op = -1; /* wait until we can add 1 */ op[0].sem_flag = 0; /* no options */ /* now try to get past the semaphore */ printf("Ready to test semaphore\n"); result = semop(id,op,1); if(result == -1){ perror("Bad Wait"); exit();} printf("Got past semaphore\n"); }