-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst question.c
69 lines (51 loc) · 1.42 KB
/
first question.c
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
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE];
int count = 0;
int sum = 0;
pthread_mutex_t mutex;
sem_t full, empty;
void *producer(void *arg) {
while (1) {
int num = rand() % 100;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[count] = num;
printf("تولید کننده: %d تولید کرد\n", num);
count++;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
}
void *consumer(void *arg) {
while (1) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
int num = buffer[count - 1];
count--;
sum += num;
printf("مصرف کننده: %d مصرف کرد، مجموع فعلی: %d\n", num, sum);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(2);
}
}
int main() {
pthread_t prod_thread, cons_thread;
pthread_mutex_init(&mutex, NULL);
sem_init(&full, 0, 0);
sem_init(&empty, 0, BUFFER_SIZE);
pthread_create(&prod_thread, NULL, producer, NULL);
pthread_create(&cons_thread, NULL, consumer, NULL);
pthread_join(prod_thread, NULL);
pthread_join(cons_thread, NULL);
pthread_mutex_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
return 0;
}