0

I have been asked this question for homework, and am having trouble figuring it out. If anyone can help me i would really appreciate it.

What Linux library function is like a fork(), but the parent process is terminated?

1
  • 1
    There is none AFAIK, although perhaps looking up exec would be useful as it is often used in conjunction with fork (e.g. "fork-exec"). Commented Apr 24, 2013 at 2:10

2 Answers 2

2

I'm fairly certain that whoever assigned you this homework is looking for the exec() family of functions, from the POSIX API header <unistd.h>, because there is nothing else that more closely resembles the sort of functionality you describe.

The exec() family of functions executes a new process and replaces the currently running process address space with the newly executed process.

From the man page:

The exec() family of functions replaces the current process image with a new process image.

It's not exactly the same as "terminating" the parent process, but in practice it results in a similar situation where the parent process address space is erased (replaced) with the address space of the child process.

Sign up to request clarification or add additional context in comments.

1 Comment

So you are saying that exec is the library function?
0

What Linux library function is like a fork(), but the parent process is terminated?

The parent process should not terminate because , it must wait for the child processes to finish executing , after which they will be in a state called "zombie state", now it is the responsibility of the parent to clean up the leftovers of the child process. The parent process can terminate without cleaning up the child processes, but then, it is not a proper way to do it, as the exit status of the child processes should be collected and checked by the parent process.

Here is an example, to demonstrate , what i just said...

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>

int main()
{
  pid_t cpid = 1 ;
  int status;

  cpid = fork();

  // Load a application to the child using execl() , and finish the job

  printf("Parent waiting for child to terminate\n");

  int wait_stat = waitpid(-1,&status,0);     // Parent will hang here till all child processes finish executing..
  if (wait_stat < 0)
  {
    perror("waitpid error");
    exit(-1);
  } 

  // WIFEXITED and WEXITSTATUS are macros to get exit status information, of the child process

  if (WIFEXITED (status))          
  {
  printf("Child of id %u cleaned up\n",wait_stat);
  printf("Exit status of application = %u\n",WEXITSTATUS(status));
  }

}

5 Comments

The parent process can terminate, in which case init wait()s on the children.
@JorgeIsraelPeña , true , but its best done by the parent , where the exit code of the child can be interpreted and appropriate measures can be taken
Sure, I'm just saying it's indeed possible for the parent to terminate without waiting on the child; it's not necessarily a bad practice. In some circumstances the parent might not care at all about the status of the child. In fact one strategy consists of forking twice and terminating the first child so that the original process can continue processing without caring about the status of the remaining (second) child process (as it will then be handled by init).
All that said, I think the OP was actually asking about exec(), though I agree the phrasing probably made it sound like he was actually asking about terminating a parent process.
@JorgeIsraelPeña , yes agreed , there may be other ways to cleaning up than what i thought, but OP looking like a beginer may best stick to cleaning up in the traditional way..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.