C/C++ launch a subprocess and wait it finish

This is a usage sample of how to fork a sub-process and wait the sub-process ended, and check its return code.


1. main.cpp

2. sub.cpp


main process will fork a process to execute the sub process, and wait until sub process is finished, and print the sub process's return code.


usage steps:

1. g++ main.cpp -o main

2. g++ sub.cpp -o sub


case 1: call sub

$ main sub
main: entry
main: fork subpid=1390
fork: entry, with process=sub
sub: entry
sub: exit
main: waitpid return 512
main: WIFEXITED=2
main: exit

sub-process was normally launched, and exit with a return code 2


case 2: call un-existed sub-process

$ main fake-sub     
main: entry
main: fork subpid=3161
fork: entry, with process=fake-sub
fork: sub execvp failure
main: waitpid return 2304
main: WIFEXITED=6
main: exit

sub-process cannot be launched, the return code is returned from main.cpp


case 3: call sub, and kill the sub-process before it finish

$ main sub
main: entry
main: fork subpid=4170
fork: entry, with process=sub
sub: entry
...


now kill the sub-process in another terminal

$ kill -9 4170


$ main sub
main: entry
main: fork subpid=4170
fork: entry, with process=sub
sub: entry
main: waitpid return 9
main: WIFSIGNALED=9
main: exit

sub-process is terminated by a signal 9.


Appendix:


sub.cpp

$ cat sub.cpp 
#include <string>
#include <iostream>

int main(int argc, char * argv[]) {
    std::cout << "sub: entry" << std::endl;
    system("sleep 10");
    std::cout << "sub: exit" << std::endl;

    if (argc > 1) {
        return atoi(argv[1]);
    }
    return 0;
}


main.cpp

#include <sys/types.h>
#include <sys/wait.h>

#include <string>
#include <iostream>

int main(int argc, char * argv[]) {
    const char * args[3];
    args[0] = argc > 1 ? argv[1] : "sub";
    args[1] = "2";
    args[2] = NULL;

    std::cout << "main: entry" << std::endl;

    pid_t childpid = fork();
    if (childpid == -1) {       // error
        std::cout << "main: cannot fork()" << std::endl;
    }
    else if (childpid == 0) {
        std::cout << "fork: entry, with process=" << args[0] << std::endl;
        if (execvp(args[0], (char **)args) == -1) {   // child process
            std::cout << "fork: sub execvp failure" << std::endl;
            exit (6);
        }
        std::cout << "fork: never come here" << std::endl;
    }
    else {                      // parent process
        std::cout << "main: fork subpid=" << childpid << std::endl;
        
        int status;
        if (childpid == waitpid(childpid, &status, 0)) {
            std::cout << "main: waitpid return " << status << std::endl;
        
            if (WIFEXITED(status)) {
                std::cout << "main: WIFEXITED=" << WEXITSTATUS(status) << std::endl;
            }
            else if (WIFSIGNALED(status)) {
                std::cout << "main: WIFSIGNALED=" << WTERMSIG(status) << std::endl;
            }
            else {
                std::cout << "main: others" << std::endl;
            }
        }
        else {
            std::cout << "main: waitpid return failure" << std::endl;
        }
    }

    std::cout << "main: exit" << std::endl;
    
    return 0;
}


Thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值