Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <semaphore.h>
- int x = 1, y = 2, z = 3;
- sem_t semXY;
- sem_t semZ;
- void* a(void* param){
- int i = 0;
- while(1){
- x = x + x;
- y = y + y;
- sem_post(&semXY);
- sem_wait(&semZ);
- printf("%d - %d = %d\n", y, x, z);
- if(++i == 1) break;
- }
- return 0;
- }
- void* b(void* param){
- int i = 0;
- while(1){
- sem_wait(&semXY);
- z = y - x;
- sem_post(&semZ);
- if(++i == 1) break;
- }
- return 0;
- }
- int main(){
- pthread_t nitA, nitB;
- sem_init(&semXY, 0, -1);
- sem_init(&semZ, 0, 0);
- pthread_create(&nitA, 0, a, 0);
- pthread_create(&nitB, 0, b, 0);
- pthread_join(nitA, 0);
- pthread_join(nitA, 0);
- sem_destroy(&semXY);
- sem_destroy(&semZ);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement