I am a computer science student , i am curently taking a operating systems course that requires us to work with C one question that i am trying to solve is the following :
Write a program that forks a child.
The child should sleep for 5 sec.
The child should print “I am ready to murder my parent!”
The child should kill its parent via a signal(use kill(parent_id,SIGINT)).
The child should print “I am an orphan now”.
The parent should wait for the child and print “I am the parent”.
i have tried to solve it using the following code , but i wasnt able to find a way to get the parent's id .any help is appreciated , thanks in advance :)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
int main ()
{
pid_t pid =fork();
if (pid<0)
{
printf("%s\n","Error in forking");
}
else if (pid==0)//child
{
sleep(5);
printf("%s","I am ready to murder my parent!");
kill(//parent id here,SIGINT);
printf("%s","I am an orphan now");
}
else{ // parent
printf("%s\n","I am the parent");
}
return 0;
}