#include #include #include #include #include #include static int start_semvalue(int sem_num) { return( semget((key_t)sem_num, 1, 0666 | IPC_CREAT)); } static int set_semvalue(int sem_id,int semVal) { union semun{ int val; struct semid_ds *buf; ushort *array; }sem_union; sem_union.val = semVal; if (semctl(sem_id, 0, SETVAL, sem_union) == -1) return(0); return(1); } static void del_semvalue(int sem_id) { union semun{ int val; struct semid_ds *buf; ushort *array; }sem_union; if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1) fprintf(stderr, "Failed to delete semaphore\n"); } static int semaphore_p(int sem_id) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = -1; /* P() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_p failed\n"); return(0); } return(1); } static int semaphore_v(int sem_id) { struct sembuf sem_b; sem_b.sem_num = 0; sem_b.sem_op = 1; /* V() */ sem_b.sem_flg = SEM_UNDO; if (semop(sem_id, &sem_b, 1) == -1) { fprintf(stderr, "semaphore_v failed\n"); return(0); } return(1); }