C Language Development in Linux (process waiting)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

The so-called process wait is actually very simple. As we have mentioned earlier, we can use fork to create sub-processes. here we can use the wait function to let the parent process run only after the sub-process has finished running. Note: to verify that the parent process does not run until the child process finishes running, we use the sleep function. However, in Linux, the sleep function parameter is in seconds, while in windows, the sleep function parameter is in milliseconds.

# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (INT argc, char * argv []) {pid_t PID; pid = fork (); If (0 = PID) {printf ("this is child process, % d \ n ", getpid (); sleep (5) ;}else {Wait (null); printf (" this is parent process, % d \ n ", getpid ();} return 1 ;}

Next, we need to do two steps. First input GCC fork. C-o fork, and then input./fork. The result will be obtained under the console.

[Root @ localhost fork] #./forkthis is child process, 2135 this is parent process, 2134

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.