-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsem_count.c
More file actions
91 lines (74 loc) · 1.55 KB
/
sem_count.c
File metadata and controls
91 lines (74 loc) · 1.55 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
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
/*
* Semaphore test
*
* Test the synchronization of two threads sharing two semaphores. They should
* print all the numbers from 0 to 20 - 1 (by default), in order, one number per
* thread at a time.
*/
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <thread.h>
#include <sem.h>
#define MAXCOUNT 20
struct test3 {
sem_t sem1;
sem_t sem2;
size_t x;
size_t maxcount;
};
void* thread2(void* arg)
{
struct test3 *t = (struct test3*)arg;
while (t->x < t->maxcount) {
printf("thread 2, x = %zu\n", t->x++);
sem_up(t->sem1);
sem_down(t->sem2);
}
return NULL;
}
void* thread1(void* arg)
{
struct test3 *t = (struct test3*)arg;
while (t->x < t->maxcount) {
sem_down(t->sem1);
printf("thread 1, x = %zu\n", t->x++);
sem_up(t->sem2);
}
return NULL;
}
unsigned int get_argv(char *argv)
{
long int ret = strtol(argv, NULL, 0);
if (ret == LONG_MIN || ret == LONG_MAX) {
perror("strtol");
exit(1);
}
return ret;
}
int main(int argc, char **argv)
{
struct test3 t;
size_t maxcount = MAXCOUNT;
pthread_t tid[2];
if (argc > 1)
maxcount = get_argv(argv[1]);
t.maxcount = maxcount;
t.x = 0;
t.sem1 = sem_create(0);
t.sem2 = sem_create(0);
pthread_create(&tid[0], NULL, thread1, &t);
pthread_create(&tid[1], NULL, thread2, &t);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}