-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sync using Signals.c
76 lines (76 loc) · 1.77 KB
/
Sync using 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
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<semaphore.h>
#include<sys/types.h>
#include<fcntl.h>
#include<math.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<signal.h>
void sigfn1(int signo)
{
printf("parent work is over,child-you can proceed\n");
}
void sigfn2(int signo)
{
printf("child works is over,parent-you can proceed\n");
}
int main()
{
signal(SIGUSR1,sigfn1);
signal(SIGUSR2,sigfn2);
int temp;
key_t key=ftok("worstfit.c",165);
int shmid=shmget(key,10*sizeof(int),IPC_CREAT|0666);
if(shmid>0)
{
printf("it is okshm id=%d\n",shmid);
}
int *ptr=(int*)(shmat(shmid,0,0));
int pid=fork();
if(pid==0)
{
printf("child:child is going to wait for data\n");
pause();
printf("child:got signal from parent\n");
for(int i=0; i<10;i++)
printf("child:%d\n",*(ptr+i));
printf("child:i am going to stop\n");
for(int i=0; i<10;i++)
for(int j=i+1; j<10;++j)
if(*(ptr+i)>*(ptr+j))
{
temp=*(ptr+i);
*(ptr+i)=*(ptr+j);
*(ptr+j)=temp;
}
printf("child:stored data\n");
for(int i=0;i<10;i++)
printf("child:%d\n",*(ptr+i));
printf("child:I am signalling parent\n");
kill(getppid(),SIGUSR2);
printf("child i am going to terminate\n");
}
else
{
printf("parent is waiting data\n");
printf("parent:enter 10 data\n");
for(int i=0; i<10;i++)
scanf("%d",ptr+i);
printf("parent:data written in memory\n");
for(int i=0;i<10;i++)
printf("parent:%d\n",*(ptr+i));
printf("p:parent is going to signal child\n");
kill(pid,SIGUSR1);
printf("p:parent is going to WAIT FOR child\n");
pause();
printf("p:parent got signal\n");
printf("p:sorted data\n");
for(int i=0;i<10;i++)
printf("parent:%d\n",*(ptr+i));
shmdt(ptr);
}
}