Created
May 4, 2019 21:23
-
-
Save yasincidem/e8484ae3eb76c8338a5908ec8333644a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <semaphore.h> | |
pthread_t Job0, Job1, Job2, Job3, Job4, Job5, Job6, Job7, Job8, Job9; | |
sem_t sem0, sem1, sem2, sem3, sem4, sem5, sem6, sem7, sem71, sem8, sem9, sem61, sem62; | |
void *job0() { | |
sem_wait(&sem0); | |
sleep(2); | |
printf("%s \n", "Job0 is done"); | |
sem_post(&sem1); | |
return NULL; | |
} | |
void *job1() { | |
printf("%s \n", "Job1 is waiting for Job0"); | |
sem_wait(&sem1); | |
sleep(2); | |
printf("%s \n", "Job1 is done"); | |
sem_post(&sem3); | |
sem_post(&sem5); | |
sem_post(&sem6); | |
return NULL; | |
} | |
void *job2() { | |
sem_wait(&sem2); | |
sleep(2); | |
printf("%s \n", "Job2 is done"); | |
sem_post(&sem61); | |
sem_post(&sem4); | |
return NULL; | |
} | |
void *job3() { | |
printf("%s \n", "Job3 is waiting for Job1"); | |
sem_wait(&sem3); | |
sleep(2); | |
printf("%s \n", "Job3 is done"); | |
return NULL; | |
} | |
void *job4() { | |
printf("%s \n", "Job4 is waiting for Job2"); | |
sem_wait(&sem4); | |
sleep(2); | |
printf("%s \n", "Job4 is done"); | |
return NULL; | |
} | |
void *job5() { | |
printf("%s \n", "Job5 is waiting for Job1"); | |
sem_wait(&sem5); | |
sleep(2); | |
printf("%s \n", "Job5 is done"); | |
sem_post(&sem62); | |
sem_post(&sem7); | |
return NULL; | |
} | |
void *job6() { | |
printf("%s \n", "Job6 is waiting for Job1, Job2 and Job5"); | |
sem_wait(&sem6); | |
sem_wait(&sem61); | |
sem_wait(&sem62); | |
sleep(2); | |
printf("%s \n", "Job6 is done"); | |
sem_post(&sem71); | |
return NULL; | |
} | |
void *job7() { | |
printf("%s \n", "Job7 is waiting for Job5 and Job6"); | |
sem_wait(&sem7); | |
sem_wait(&sem71); | |
sleep(2); | |
printf("%s \n", "Job7 is done"); | |
sem_post(&sem8); | |
return NULL; | |
} | |
void *job8() { | |
printf("%s \n", "Job8 is waiting for Job7"); | |
sem_wait(&sem8); | |
sleep(2); | |
printf("%s \n", "Job8 is done"); | |
sem_post(&sem9); | |
return NULL; | |
} | |
void *job9() { | |
printf("%s \n", "Job9 is waiting for Job8"); | |
sem_wait(&sem9); | |
sleep(2); | |
printf("%s \n", "Job9 is done"); | |
return NULL; | |
} | |
int main(int argc, char *argv[]) { | |
sem_init(&sem0, 0, 1); | |
sem_init(&sem1, 0, 0); | |
sem_init(&sem2, 0, 1); | |
sem_init(&sem3, 0, 0); | |
sem_init(&sem4, 0, 0); | |
sem_init(&sem5, 0, 0); | |
sem_init(&sem6, 0, 0); | |
sem_init(&sem7, 0, 0); | |
sem_init(&sem71, 0, 0); | |
sem_init(&sem8, 0, 0); | |
sem_init(&sem9, 0, 0); | |
sem_init(&sem61, 0, 0); | |
sem_init(&sem62, 0, 0); | |
pthread_create(&Job0, NULL, job0, NULL); | |
pthread_create(&Job1, NULL, job1, NULL); | |
pthread_create(&Job2, NULL, job2, NULL); | |
pthread_create(&Job3, NULL, job3, NULL); | |
pthread_create(&Job4, NULL, job4, NULL); | |
pthread_create(&Job5, NULL, job5, NULL); | |
pthread_create(&Job6, NULL, job6, NULL); | |
pthread_create(&Job7, NULL, job7, NULL); | |
pthread_create(&Job8, NULL, job8, NULL); | |
pthread_create(&Job9, NULL, job9, NULL); | |
pthread_join(Job0, NULL); | |
pthread_join(Job1, NULL); | |
pthread_join(Job2, NULL); | |
pthread_join(Job3, NULL); | |
pthread_join(Job4, NULL); | |
pthread_join(Job5, NULL); | |
pthread_join(Job6, NULL); | |
pthread_join(Job7, NULL); | |
pthread_join(Job8, NULL); | |
pthread_join(Job9, NULL); | |
sem_destroy(&sem0); | |
sem_destroy(&sem1); | |
sem_destroy(&sem2); | |
sem_destroy(&sem3); | |
sem_destroy(&sem4); | |
sem_destroy(&sem5); | |
sem_destroy(&sem6); | |
sem_destroy(&sem7); | |
sem_destroy(&sem71); | |
sem_destroy(&sem8); | |
sem_destroy(&sem9); | |
sem_destroy(&sem61); | |
sem_destroy(&sem62); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment