daily pastebin goal
92%
SHARE
TWEET

Untitled

a guest Jan 13th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5.  
  6. #define NUM_THREADS 5
  7.  
  8. sem_t one;
  9. sem_t two;
  10. sem_t three;
  11.  
  12. void *s_one(void*);
  13. void *s_two(void*);
  14. void *s_three(void*);
  15. void *s_four(void*);
  16. void *s_five(void*);
  17.  
  18.  
  19. int S;
  20. int A=1;
  21. int B=2;
  22. int C=3;
  23. int D=4;
  24.  
  25.  
  26. int main()
  27. {
  28.     //pthread_t thread_id[NUM_THREADS];
  29.  
  30.     pthread_t* thread1;
  31.     pthread_t* thread2;
  32.     pthread_t* thread3;
  33.     pthread_t* thread4;
  34.     pthread_t* thread5;
  35.  
  36.     sem_init(&one,0,1);
  37.  
  38.     pthread_create(thread1,NULL,s_one,NULL);
  39.     pthread_create(thread2,NULL,s_two,NULL);
  40.     pthread_create(thread3,NULL,s_three,NULL);
  41.     pthread_create(thread4,NULL,s_four,NULL);
  42.     pthread_create(thread5,NULL,s_five,NULL);
  43.  
  44.     pthread_join(thread1,NULL);
  45.     pthread_join(thread2,NULL);
  46.     pthread_join(thread3,NULL);
  47.     pthread_join(thread4,NULL);
  48.     pthread_join(thread5,NULL);
  49.  
  50.     printf("before end of main");
  51.     return 0;
  52. }
  53.  
  54.  
  55. void *s_one(void *p)
  56. {
  57.     printf("s_one");
  58.     A = B+C;
  59. }
  60.  
  61. void *s_two(void *p)
  62. {
  63.     printf("s_two");
  64.     C = B*D;
  65. }
  66.  
  67. void *s_three(void *p)
  68. {
  69.     printf("s_three");
  70.     S = 0;
  71. }
  72.  
  73. void *s_four(void *p)
  74. {
  75.     printf("s_four");
  76.  
  77.     int j=0;
  78.     int X=0;
  79.  
  80.     for(j=0; j<=100; j++)
  81.     {
  82.         X = j + 1;
  83.         S = S + (X+1); //(X+1) can be replaced by j+1.
  84.     }
  85. }
  86.  
  87. void *s_five(void *p)
  88. {
  89.     printf("s_five");
  90.     if(S > 1000)
  91.         C = C * 2;
  92. }
RAW Paste Data
Top