Working on Linux, I want to catch the signal I have sent using kill
in the child process and then print the loop but I don't know how.
I can't seem to get my code that catches the signal.
Here is my code so far:
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int SHMSIZE = 9;
int alarmFlag = 0;
void main()
{
int shmid;
int *shm;
pid_t pid = fork();
if(pid == 0) {
pause();
shmid = shmget(4000, SHMSIZE, 0);
shm = shmat(shmid,0,0);
int i;
for(i=0;i<SHMSIZE;i++)
printf("<%d , ",shm[i]);
}
else
{
int *n;
shmid = shmget(4000,SHMSIZE,0666 | IPC_CREAT);
shm = shmat(shmid,0,0);
n = shm;
int i;
for(i=0;i<SHMSIZE;i++)
n[i] = i;
int result = kill(pid, SIGUSR1);
wait(NULL);
}
}
The pause()
in your program on its own doesn't catch a signal, it just sleeps until a signal is delivered, but if that signal isn't caught (using sigaction()
or signal()
, as Jonathan Leffler wrote), it terminates the process. So, you have to add eg signal(SIGUSR1, catch);
before pause();
and
void catch(int signum) { }
before main()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.