Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rdemeter
GitHub Repository: rdemeter/so
Path: blob/master/lab9/rezervarebilete.c
221 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <unistd.h>
4
#include <time.h>
5
#include <pthread.h>
6
#include <semaphore.h>
7
8
#define NUM_SELLERS 50
9
#define NUM_TICKETS 250000
10
11
static int numTickets = NUM_TICKETS;
12
13
void *sellerThread(void* arg)
14
{
15
int total = 0;
16
do {
17
if(numTickets <= 0) break;
18
numTickets--;
19
total++;
20
printf("Seller %ld sold one (%d left)\n", (long) arg, numTickets);
21
} while(numTickets > 0);
22
23
printf("Seller %ld finished! (I sold %d tickets)\n", (long) arg, total);
24
25
pthread_exit((void*) total);
26
}
27
28
int main(void)
29
{
30
long i;
31
int total = 0;
32
33
pthread_t tids[NUM_SELLERS];
34
35
for(i=0; i < NUM_SELLERS; i++)
36
pthread_create(&tids[i], NULL, sellerThread, (void*) i);
37
38
for(i=0; i < NUM_SELLERS; i++) {
39
int value;
40
pthread_join(tids[i], (void**)&value);
41
total += value;
42
}
43
44
printf("Done %d!\n", total);
45
return 0;
46
}
47
48