Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #define NUM_THREADS 5
- void *s_one(void*);
- void *s_two(void*);
- void *s_three(void*);
- void *s_four(void*);
- void *s_five(void*);
- sem_t s2;
- sem_t s4;
- sem_t mainwait;
- sem_t s5;
- int S;
- int A=1;
- int B=2;
- int C=3;
- int D=4;
- int main()
- {
- //pthread_t thread_id[NUM_THREADS];
- sem_init(&s2, 0, 1);
- sem_init(&s4, 0, 0);
- sem_init(&s5, 0, 1);
- sem_init(&mainwait, 0, 1);
- pthread_t thread1;
- pthread_t thread2;
- pthread_t thread3;
- pthread_t thread4;
- pthread_t thread5;
- pthread_create(thread1,NULL,s_one,NULL);
- pthread_create(thread2,NULL,s_two,NULL);
- pthread_create(thread3,NULL,s_three,NULL);
- pthread_create(thread4,NULL,s_four,NULL);
- pthread_create(thread5,NULL,s_five,NULL);
- sem_wait(&mainwait);
- return 0;
- }
- void *s_one(void* arg)
- {
- printf("s_one");
- A = B+C;
- sem_post(&s2);
- }
- void *s_two(void* arg)
- {
- sem_wait(&s2);
- printf("s_two");
- C = B * D;
- sem_post(&s4);
- }
- void *s_three(void* arg)
- {
- printf("s_three");
- S = 0;
- sem_post(&s4);
- }
- void *s_four(void* arg)
- {
- sem_wait(&s4);
- int j;
- int X=0;
- for(j=A; j<=100; j++)
- {
- X = j + 1;
- S = S + (X+1); //(X+1) can be replaced by j+1.
- }
- sem_post(&s5);
- }
- void *s_five(void* arg)
- {
- sem_wait(&s5);
- if(S > 1000)
- C = C * 2;
- sem_post(&mainwait);
- }
Add Comment
Please, Sign In to add comment