-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
44 lines (37 loc) · 996 Bytes
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <linux/kernel.h>
#define SYS_reader_writer 335
void *writer(void *arg) {
int *id=(int*)arg;
int i;
for (i = 0; i < 5; i++) {
syscall(SYS_reader_writer, 0); // writer mode
printf("Person %d is writing...\n",*id);
sleep(1);
}
pthread_exit(NULL);
}
void *reader(void *arg) {
int *id=(int*)arg;
int i;
for (i = 0; i < 5; i++) {
syscall(SYS_reader_writer, 1); // reader mode
printf("Person %d is reading...\n",*id);
sleep(1);
}
pthread_exit(NULL);
}
int main() {
pthread_t thread1, thread2;
int id1=1,id2=2;
syscall(SYS_reader_writer, -1);
pthread_create(&thread1, NULL, writer, (void*)&id1);
pthread_create(&thread2, NULL, reader, (void*)&id2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}