-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1.c
62 lines (54 loc) · 1.08 KB
/
task1.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
#include "mySem.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#define SIGUSR_1 16
extern int createsem();
void sig_load_children();
int main(){
pid_t pid;
int s = 0;
int sid1, sid2;
sid1 = createsem(0);
sid2 = createsem(0);
P(sid1);
//print module
int i = 5;
if ((pid = fork()) > 0) {
while(i>0){
sleep(1);
kill(pid, SIGUSR_1);
P(sid1);
printf("world!\n");
V(sid2);
i --;
}
time_t t;
time(&t);
printf("parent wakes at:%ld\n", t);
wait(&s);
}
else if(pid == 0) {
signal(SIGUSR_1, sig_load_children);
while (i>0) {
P(sid2);
sleep(100);
V(sid1);
i--;
}
sleep(5);
printf("child wakes at:");
fflush(stdout);
execl("/bin/date", "date", "+%s", NULL);
exit(1);
}
printf("it's time to finish!!!");
return 0;
}
void sig_load_children(){
printf("hello, ");
fflush(stdout);
}