Parent process obtains the sub-process exit status (2)

Source: Internet
Author: User

main. cpp:

# Include <unistd. h> <br/> # include <iostream> <br/> # include <sys/Wait. h> <br/> # include <stdlib. h> </P> <p> using namespace STD; </P> <p> int main () <br/>{< br/> int ret; <br/> ret = fork (); <br/> If (Ret> 0) {<br/> cout <"parent start/N "; <br/> int status; pid_t PID; <br/> pid = wait (& status); <br/> If (PID! = RET) {<br/> cout <"parent wait error (wait return PID:" <pid <", fork return PID:" <RET <") /n "; <br/> exit (1); <br/>}else {<br/> cout <" parent wait return (PID: "<pid <")/n "; <br/>}< br/> cout <" parent's child end-pid: "<pid <", status: "<status <Endl; <br/> If (wifexited (Status )) {<br/> cout <"> child exited, exit code =" <wexitstatus (Status) <Endl; <br/> If (wexitstatus (Status) = 1) {<br/> cout <"> child exec error/N"; <br/>}< br/>} else if (wifsignaled (Status )) {<br/> cout <"> child killed (signal" <wtermsig (Status) <")/n "; <br/> # ifdef wcoredump <br/>} else if (wcoredump (Status) {<br/> cout <"> child core file generated/N "; <br/> # endif <br/>} else if (wifstopped (Status )) {<br/> cout <"> child stopped (signal" <wstopsig (Status) <")/n "; <br/> # ifdef wifcontinued <br/>} else if (wifcontinued (Status) {<br/> cout <"> child continued/N "; <br/> # endif <br/>} else {<br/> cout <"> unexpected status (" <status <")/n "; <br/>}< br/>} else if (ret = 0) {<br/> pid_t ppid = getppid (); <br/> cout <"Child start/N"; <br/> cout <"Child ppid:" <ppid <Endl; <br/> cout <"Child PID:" <getpid () <Endl; <br/> cout <"Child exec/N "; <br/> If (execl (". /child "," child ", (char *) 0) <0) {<br/> cout <" Child exec error/N "; <br/> exit (1); <br/>}< br/>} else {<br/> cout <"fork error/N "; <br/>}< br/> return 0; <br/>}

 

 

 

 

Child. cpp :( g ++ child. cpp-o child)

# Include <unistd. h> <br/> # include <iostream> <br/> using namespace STD; <br/> int main () <br/>{ <br/> cout <"Child exec start/N"; <br/> cout <"Child exec ppid:" <getppid () <Endl; <br/> cout <"Child exec PID:" <getpid () <Endl; <br/> sleep (30 ); <br/> return 0; <br/>}

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.