-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex3.c
115 lines (90 loc) · 2.46 KB
/
ex3.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#define _XOPEN_SOURCE 500
#define _REENTRANT
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>
#include <values.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#define BUF_SIZE 10
int b[BUF_SIZE];
int in = 0, out = 0;
#define N_CONSUMERS 6
int arguments[N_CONSUMERS];
pthread_t consumer_id[N_CONSUMERS];
pthread_t producer;
pthread_mutex_t M;
pthread_mutexattr_t mattr;
pthread_cond_t C;
pthread_condattr_t cattr;
void * consumer_body (void *arg) {
int tmp;
int self = *((int *) arg);
fprintf(stdout, "consumer thread starts\n");
for (;;) {
pthread_mutex_lock (&M);
while (out == in)
if (pthread_cond_wait (&C, &M)) {
fprintf (stdout, "pthread_cond_wait: consumer\n");
exit (-1);
}
tmp = b[out];
if (((in + 1) % BUF_SIZE) == out) pthread_cond_signal (&C);
out = (out + 1) % BUF_SIZE;
pthread_mutex_unlock (&M);
fprintf (stdout, "thread %d:", self);
fprintf (stdout, "%d\n", tmp); fflush (stdout);
}
fprintf(stdout, "consumer thread exits\n");
return NULL;
}
void * producer_body (void * arg) {
int i;
fprintf(stdout, "producer thread starts\n");
for (i = 0; i < 100; i++) {
pthread_mutex_lock (&M);
while (((in + 1) % BUF_SIZE) == out)
if (pthread_cond_wait (&C, &M)) {
fprintf (stdout, "pthread_cond_wait: producer\n");
exit (-1);
}
b[in] = i;
in = (in + 1) % BUF_SIZE;
pthread_mutex_unlock (&M);
pthread_cond_signal (&C);
}
return NULL;
}
int main () {
int i, result;
pthread_attr_t attrs;
sigset_t set;
pthread_mutex_init (&M, NULL);
pthread_cond_init (&C, NULL);
pthread_attr_init (&attrs);
pthread_attr_setscope (&attrs, PTHREAD_SCOPE_SYSTEM);
if ((result = pthread_create(&producer,&attrs,producer_body,NULL))) {
fprintf (stdout, "pthread_create: %d\n", result);
exit (-1);
}
fprintf(stdout, "producer thread created\n");
for (i = 0; i < N_CONSUMERS; i++) {
arguments[i] = i;
if ((result = pthread_create (
&consumer_id[i],
&attrs,
consumer_body,
&arguments[i]))) {
fprintf (stdout, "pthread_create: %d\n", result);
exit (-1);
}
}
fprintf(stdout, "consumer threads created\n");
sigfillset (&set);
sigdelset (&set, SIGINT);
sigsuspend (&set);
return 0;
}