-
Notifications
You must be signed in to change notification settings - Fork 0
/
Signals.c
93 lines (88 loc) · 2.15 KB
/
Signals.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
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
92
93
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
void handler(int signum);
int * child;
int main(int argc, char **argv) {
int i=0,j=0,end=0,status,pid;
if (argc<1) {
printf("Too few arguments. Exiting!\n");
exit(1);
}
signal( SIGUSR1, handler);
int target=atoi(argv[1]);
int * array= malloc((argc-2) * sizeof(int));
int n=argc-2;
for(i=0;i<n;i++)
{
array[i]=atoi(argv[i+2]);
}
printf("I am the parent, my pid = %d \n", getpid());
child=malloc( 2*sizeof(int));
for(i=0;i<2;i++)
{
child[i]=fork();
if(child[i] ==0)
{
if(i==0)
{
printf("I am the first child, pid = %d ppid= %d \n", getpid(),getppid());
end=ceil(n/2);
}
else
{
printf("I am the second child, pid = %d ppid= %d \n", getpid(),getppid());
j=ceil(n/2);
end=n;
}
break;
}
}
if(i==2)
{
sleep(5);
for(i=0;i<2;i++)
{
waitpid( child[i] ,&status,0);
if(WIFEXITED(status));
printf("A child with pid %d terminated with exit code %d\n", child[i], WEXITSTATUS(status));
}
printf("value not found \n");
}
else
{
for (j; j <end ;j++)
{
if(array[j]==target)
{
kill(getppid(), SIGUSR1);
exit(j);
}
}
sleep(3);
printf("Child %d terminates\n",i+1);
exit(0);
}
return 0;
}
void handler(int signum)
{
int pid, stat_loc;
pid = wait(&stat_loc);
if(WIFEXITED(stat_loc))
{
if(pid==child[0])
printf("child 1: value found at position %d\n", stat_loc>>8);
else
printf("child 2: value found at position %d\n", stat_loc>>8);
killpg(getpgrp(), SIGKILL);
}
void handler(int signum);
}
/* a) parent
b) becuse the child who found value sent signal and then sent exit and paretn wait exit code so it always handle
*/