Linux multi-task programming Five: Exit () and _exit () functions

Source: Internet
Author: User

Source: CSDN Wang Wensong transfer from: Linux commune

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Wait () and Waitpid ()

Function description

The wait () function is used to block the parent process (that is, the process that calls wait () until a child process finishes or the process receives a specified signal. If the parent process has no child processes or its child processes have ended, the wait () function returns immediately.

Waitpid () acts like Wait (), but it does not have to wait for the first terminating child process (which can specify a child process that needs to wait for finalization), and it has several options, such as providing a non-blocking version of the Wait () feature, and also supporting job control. In fact, the wait () function is just a special case of the Waitpid () function, and the Waitpid () function is called directly when implementing the Wait () function inside Linux.

function format

Format of the wait () function

is the format of the Waitpid () function

Basic experiments

Experiment 1

In this experiment, you first create a child process using fork () and then let its child process pause 5s (using the Sleep () function). Next, use the Waitpid () function for the original parent process, and use the parameter Wnohang that the parent process will not block. If a child process exits, waitpid () returns the child process number, or waitpid () returns 0 if no child process exits, and the parent process is judged every 1s cycle. The flowchart of the program is as follows:

Program source code I upload to the website, you can download the waitpid.c file for free, click here to download

After downloading the file, use the command: GCC waitpid.c-o waitpid

Then execute the command:./waitpid results such as;

The execution flow of the program can be seen from the output results. The parent process is executed first, the parent process sleeps 1s, the child process is executed, and the child process sleeps for 5 seconds, then the parent process listens. Oh I go I do not analyze the process, afraid to say again confused.

Experiment 2

This experiment uses the function wait (), as in Experiment 2, is the first to use fork () to create a new child process, and then let the child process pause 5s. Next, use the Wait () function for the original parent process. The difference is that the wait () function causes the parent process to block, as can be seen through the results of this experiment. The code is as follows:

Execution results such as

I suggest you try it yourself, and you can clearly see the difference.

wait.c File Download

Free in http://linux.linuxidc.com/

User name and password are www.linuxidc.com

Specific download directory in/2013 information/June/12th/linux multitasking programming

Linux multitasking programming Five: Exit () and _exit () functions (GO)

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.