Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i asked this question before but the answer was not clear so i upload again. bel

ID: 3802945 • Letter: I

Question

i asked this question before but the answer was not clear so i upload again.

below is my c code.

i created 2 child processes.

but i do not know how to solve below requirements...

can you add some codes and explain for me? thanks

Is it allowed for a parent to terminate before one of its child processes has terminated? If so, does this affect the child processes in any way?

int main()
{
int fd;
pid_t pid;

pid = fork();

if (pid < 0)
{
  fprintf(stderr, "Error");
  exit(EXIT_FAILURE);
}

//child 1
else if (pid == 0)
{
  printf("I am child 1. ");
}
else
{
  pid = fork();

  // child 2
  if (pid == 0)
  {
   printf("I am child 2. ");  
  }
  else
  {

   wait (NULL);
   exit(EXIT_SUCCESS);
  }
}
}

Expert A

Explanation / Answer

Yes, A parent process can exit normally,Abnormally,by kill, ^C, assert failure or anything else.,Before the child processes have terminated

If the parent is killed, children become children of the init process (that has the process id 1 and is launched as the first user process by the kernel).

The init process checks periodically for new children, and kills them if they have exited (thus freeing resources that are allocated by their return value).

Under Linux, you can install a parent death signal in the child, e.g.:

Note that storing the parent process id before the fork and testing it in the child after prctl()eliminates a race condition between prctl() and the exit of the process that called the child.

Also note that the parent death signal of the child is cleared in newly created children of its own. It is not affected by an execve().

That test can be simplified if we are certain that the system process who is in charge of adopting all orphans has PID 1:

Relying on that system process being init and having PID 1 isn't portable, though. POSIX.1-2008 specifies:

The parent process ID of all of the existing child processes and zombie processes of the calling process shall be set to the process ID of an implementation-defined system process. That is, these processes shall be inherited by a special system process.

Traditionally, the system process adopting all orphans is PID 1, i.e. init - which is the ancestor of all processes.

On modern systems like Linux or FreeBSD another process might have that role. For example, on Linux, a process can call prctl(PR_SET_CHILD_SUBREAPER, 1) to establish itself as system process that inherits all orphans of any of its descendants