forked from Lowkee1g/DockerContainerForC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiThread.c
More file actions
66 lines (53 loc) · 2.51 KB
/
multiThread.c
File metadata and controls
66 lines (53 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define NUM_THREADS 10
#define MAX_RANDOM 10
pthread_mutex_t lock;
int winningThread = 0;
int secretNumber;
// Function for threads to guess the number
void *guessNumber(void *threadid) {
long tId = (long)threadid; // Cast threadid to long for thread identification
int guess = 0;
while (!winningThread) { // Continue guessing while no thread has won yet
sleep(1); // Each thread pauses for a second to simulate time taken to "think" about the guess
// Generate a guess within the range 1 to MAX_RANDOM
guess = rand() % MAX_RANDOM + 1; // rand() % MAX_RANDOM generates a number between 0 and 99, so +1 makes it 1 to 100
printf("Thread %ld guesses %d\n", tId, guess);
pthread_mutex_lock(&lock); // Lock the mutex to update the winningThread variable safely
if (guess == secretNumber && !winningThread) { // Check if the guess is correct and no thread has won yet
winningThread = tId; // Set the winning thread ID
printf("Thread %ld guessed the correct number %d!\n", tId, guess);
}
else if (guess == secretNumber){
printf("Thread %ld also guessed the correct number %d!\n", tId, guess);
}
pthread_mutex_unlock(&lock); // Unlock the mutex to allow other threads to guess
if (winningThread) break; // If a winning thread is set, exit the loop
}
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS]; // Array to hold thread IDs
srand(time(NULL)); // Seed the random number generator
secretNumber = rand() % MAX_RANDOM + 1; // Choose the secret number at random between 1 and 100
printf("Secret Number is: %d\n", secretNumber);
pthread_mutex_init(&lock, NULL); // Initialize the mutex
for (long t = 1; t <= NUM_THREADS; t++) {
printf("Creating thread %ld\n", t);
int rc = pthread_create(&threads[t-1], NULL, guessNumber, (void *)t); // Create each thread
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1); // Exit if thread creation fails
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL); // Wait for all threads to complete
}
printf("Game Over: Thread %d wins by guessing the secret number %d!\n", winningThread, secretNumber);
pthread_mutex_destroy(&lock); // Clean up the mutex
pthread_exit(NULL); // Exit the pthread library cleanly
}