-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosa2q1.c
58 lines (48 loc) · 1.15 KB
/
osa2q1.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
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
void handler(int pid_t){
write(STDOUT_FILENO , "exiting", 7);
exit(EXIT_FAILURE);
}
void DoStuff(void) {
printf("Timer went off.\n");
}
int main() {
int s1;
s1 = fork();
int pid_t = getpid();
if(s1==0){
signal(SIGTERM,handler);
printf("%d \n", pid_t);
sleep(2);
}
if(s1>0){
int st = fork();
int sr = fork();
if(st==0){
#define INTERVAL 500
void DoStuff(void);
struct itimerval it_val;
if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) {
perror("Unable to catch SIGALRM");
exit(1);
}it_val.it_value.tv_sec = INTERVAL/1000;
it_val.it_value.tv_usec = (INTERVAL*1000) % 1000000;
it_val.it_interval = it_val.it_value;
if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) {
perror("error calling setitimer()");
exit(1);
}while (1)
pause();
signal(SIGALRM,handler);
// int setitimer(int which, struct itimerval *value, struct itimerval *ovalue);
}if(sr==0){
kill(pid_t,SIGTERM);
signal(SIGALRM,handler);
}
}
return 0;
}