Use both Mutex lock and Semaphore to address the racingproblem in the following program (example_thread.c). Please makesure that your output is always equal to 6,000,000.
======================================
example_thread.c
======================================
#include <stdio.h>#include <stdlib.h>#include <pthread.h>int shared= 0;void race(void);int main(){ pthread_t player1, player2, player3; pthread_create(&player1, NULL, (void *)race,NULL); pthread_create(&player2, NULL, (void *)race,NULL); pthread_create(&player3, NULL, (void *)race,NULL); pthread_join(player1, NULL); pthread_join(player2, NULL); pthread_join(player3, NULL); printf("Total Number = %d\n", shared); return 0;}void race(void) { long i,tmp; for(i=1; i<=2000000; i++) { tmp = shared; tmp = tmp + 1; shared = tmp; }}
Use both Mutex lock and Semaphore to address the racing problem in the following program (example_thread.c). Please mak
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am