C program which illustrates sending signal from one process to another by using kill API. Also check if the program have permission to send the signal or not

#include
#include
#include
#include
#include
#include
void sig_handler(int num)
{
printf("\nChild Sent a signal to parent:%d\n",num);
signal(SIGALRM,SIG_DFL);
}
int main()
{
int status;
system("clear");
printf("\n--------Signal Handling Across Processes----------\n");
switch(fork())
{
case -1:
perror("\nFork Failed...\n");
exit(1);
break;
case 0:
alarm(3);
kill(getppid(),SIGALRM);
//signal(SIGALRM,sig_handler);
printf("\nIts Child Process\n");
sleep(5);
break;
default:
signal(SIGALRM,sig_handler);
wait(&status);
printf("\nIts Parent Process\n");
}
return 0;
}

Leave a Reply