#include #include #include #include int sem; void *thread (int p) { struct sembuf op; op.sem_num = 0; op.sem_flg = 0; while (1) { printf("Thread %d ready\n", p); op.sem_op = -1; semop(sem, &op, 1); printf("Thread %d inside C.S.\n", p); usleep(1000000 * p); printf("Thread %d leaving C.S.\n", p); op.sem_op = 1; semop(sem, &op, 1); usleep(1000000); } } int main () { pthread_t thr_id; sem = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT); semctl(sem, 0, SETVAL, 1); pthread_create(&thr_id, NULL, thread, (void *) 1); pthread_create(&thr_id, NULL, thread, (void *) 2); pthread_create(&thr_id, NULL, thread, (void *) 3); pthread_create(&thr_id, NULL, thread, (void *) 4); pthread_create(&thr_id, NULL, thread, (void *) 5); pthread_create(&thr_id, NULL, thread, (void *) 6); usleep(50000000); //simulation time return 0; }