-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.c
More file actions
52 lines (41 loc) · 925 Bytes
/
task3.c
File metadata and controls
52 lines (41 loc) · 925 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void signalHandler(int sig) {
switch (sig) {
SIGUSR1:
printf("Hello from child!\n");
break;
SIGUSR2:
break;
default:
break;
}
}
int main(void) {
pid_t pid = fork();
if (pid == 0) {
// Child process
printf("[Child] pid %d \n", getpid());
signal(SIGUSR1, NULL);
sleep(13);
signal(SIGUSR1, signalHandler);
signal(SIGUSR2, signalHandler);
sleep(3);
_exit(EXIT_SUCCESS);
} else if (pid > 0) {
// Parent process
sleep(5);
kill(pid, SIGUSR2);
alarm(13);
while (1) {
// Break if child terminated
if (wait(NULL)) break;
sleep(5);
kill(pid, SIGUSR1);
}
}
return EXIT_SUCCESS;
}