I am trying to create a child process through fork()
system call, then trying to send a signal to parent and print out something on the screen.
Here is my code:-
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
void func1(int signum) {
if(signum == SIGUSR2) {
printf("Received sig from child\n");
}
}
int main() {
signal(SIGUSR2, func1);
int c = fork();
if(c > 0) {
printf("parent\n");
}
else if(c == -1) {
printf("No child");
}
else {
kill(getppid(), SIGUSR2);
printf("child\n");
}
}
When I execute my program all I get is:-
child
Segmentation fault (core dumped)
I am a novice to C language system calls, and don't get why this is happening, and how to get the desired output, which would be printing of all the three printf
statements. Any help for the same would be appreciated.