Fork ();

Source: Internet
Author: User
Tags exit in

Zombie Process: The parent process does not wait for the child process, and the wait () child process becomes a zombie process.

int main (int arg, char *args[])
{
 
pid_t pid = fork ();//call fork to produce a sub-
int status;
if (PID = =-1)
{
printf ("fork failed\n");
return 0;
}
if (PID = = 0)//child process call Execve, execute ls-l command
{
Exit (0);//the child process exits and becomes a zombie process.

}
Else
{

printf ("Father exit");

Wait (&status);//block the call until the child process exits, and wait does not return

Sleep (10);//Not waiting for child process state.

Return 0;//Parent Process exit
}
}

Orphan process: The parent process exits a child process that has not exited and will be taken over by the INIT process into an orphan process.

int main (int arg, char *args[])
{
Close (Stdout_fileno);//Turn off standard output
Open ("/dev/pts/2", o_wronly);//Turn on "/DEV/PTS/2" as standard output
pid_t pid = fork ();//call fork to produce a sub-
int status;
if (PID = =-1)
{
printf ("fork failed\n");
return 0;
}
if (PID = = 0)//child process call Execve, execute ls-l command
{
Char *args[] = {"/bin/ls", "-L", NULL};
Execve ("/bin/ls", args, NULL);

}
Else
{

Return 0;//Parent Process exit
}
}


Code that the parent process waits for the child process to exit
int main (int arg, char *args[])
{
pid_t pid = fork ();//There are two processes after the fork is called
int status;
if (PID = = 0)
{
printf ("Child begin\n");
Sleep (5);
printf ("Child end\n");
return-1;
}

if (PID > 0)
{
printf ("Parent begin\n");
Wait (&status);//block the call until the child process exits, and wait does not return
printf ("Child return =%d\n", Wexitstatus (status));
printf ("Parent end\n");
}

return 0;
}

Zombie Process
When the parent process does not call wait, the child process exits, and the child process becomes a zombie process.

5 ways to exit a process:

Return is the same as exit in main function main, only in the sub-function, there is a difference, the child function return just exit the child function, exit is the exit process.

Exit is the same as return in the main function, except that it is not the same in a child function.

_exit

Abort exception exits. Generate a Core.xxx file

Signal

Fork ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.