1

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;



}
Fawzi
  • 359
  • 2
  • 12

2 Answers2

3

As Haris mentioned in his answer, You should use getppid() to get the pid of the parent.


In response to your comment here,

well i tried it , and lol that might seems funny , but my program killed my os , my linux machine shuted down

Let's have a look at your code,

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");
}

What will the parent process do after it finishes executing printf("%s\n","I am the parent"); ? It terminates.

So, who is the parent of a process whose original parent has terminated ? The child process becomes an orphan. Quoting from Wikipedia

An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself. In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process.

Therefore, when you are invoking kill() you are doing so on the init process. This is the reason for lol that might seems funny , but my program killed my os , my linux machine shuted down

Do have a look at this answer.

Community
  • 1
  • 1
Ghazanfar
  • 1,419
  • 13
  • 21
2

You can use the function getppid()

getppid() returns the process ID of the parent of the calling process.

Haris
  • 12,120
  • 6
  • 43
  • 70
  • well i tried it , and lol that might seems funny , but my program killed my os , my linux machine shuted down – Fawzi Oct 21 '15 at 16:20
  • It's a bit hard to believe that you could kill it. But if that happened is cause parent pid was 1 (The init process in linux). Are you sure you did it in the child process? – Mr. E Oct 21 '15 at 18:15
  • @CaptainZi That is happening because your parent process is completed even before you call `kill` on it. I'll post a detailed answer in sometime if you are not able to figure it out. – Ghazanfar Oct 22 '15 at 04:52
  • so i guess i should wait in the parent process – Fawzi Oct 22 '15 at 05:35