HmHimu

lab7.8

Nov 9th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. /* Global variables */
  6. int x = 0;
  7. sem_t sync;
  8. /* Thread function */
  9. void *my_func(void *arg)
  10. {
  11. /* wait for signal from main thread */
  12. sem_wait(&sync);
  13. printf("X = %d\n", x); //x er value 55 hobe jokon lock khule jab
  14. }
  15. void main ()
  16. {
  17. pthread_t thread; //
  18. /* semaphore sync should be initialized by 0 */
  19. if (sem_init(&sync, 0, 0) == -1) { //initialize value, semapore ar value asign kore.2nd
  20. // value ta shared value. 0 thakle onno process semapore k use korte parbe na.last er ta value.
  21. perror("Could not initialize mylock semaphore");
  22. exit(2);
  23. }
  24. if (pthread_create(&thread, NULL, my_func, NULL) < 0) { //thread create
  25. perror("Error: thread cannot be created");
  26. exit(1);
  27. }
  28. /* perform some operation(s) */
  29. x = 55;
  30. /* send signal to the created thread */
  31. sem_post(&sync); //count er value 1 barbe
  32. /* wait for created thread to terminate */
  33. pthread_join(thread, NULL);
  34. /* destroy semaphore sync */
  35. sem_destroy(&sync);
  36. exit(0);
  37. }
Add Comment
Please, Sign In to add comment