unix/linux進程詳解——代碼

來源:互聯網
上載者:User

unix/linux進程詳解——代碼

#include <iostream>#include <vector>#include <cstdint>#include <cstring>#include <error.h>#include <unistd.h>using namespace std;int main(){    //system("ps aux &");//run on background    char *const ps_argv[]={"ps", "ax", 0};    //eg env    char *const ps_envp[]={"PATH=/bin:/usr/bin","TERM=console",0};    //possible exec    //execl("/bin/ps","ps","ax",0);//assume ps in the bin    //execlp("ps","ps","ax",0);//assume /bin is in the PATH    execle("/bin/ps","ps","ax",0,ps_envp);//pass own env    //execv("/bin/ps",ps_argv);//    execvp("ps",ps_argv);//    execve("/bin/ps",ps_argv,ps_envp);//    execlp("ps","ps","ax",0);   pid_t new_pid;    new_pid = fork();    new_pid = getpid();    cout << new_pid << endl;    switch (new_pid) {    case -1:        break;    case 0:        break;    default:        break;    }    cout << "errx" << endl;    return 0;}

 

      
#include <iostream>#include <vector>#include <cstdint>#include <cstring>#include <error.h>#include <unistd.h>#include <sys/types.h>using namespace std;int main(){    pid_t pid;    string message;    int n;    pid = fork();    //pid = getpid();    //cout << pid << endl;    switch (pid) {    case -1:        perror("fork failed");        exit(1);    case 0:        message = "this is the child";        n=5;        break;    default:        message = "this is the parent";        n=3;        break;    }    for(; n > 0;n--)    {        cout<<message<<endl;        sleep(1);    }    //cout << "errx" << endl;    return 0;}this is the parentthis is the childthis is the parentthis is the childthis is the parentthis is the child$ this is the childthis is the child#include <iostream>#include <vector>#include <cstdint>#include <cstring>#include <error.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>using namespace std;int main(){    pid_t pid;    string message;    int n;    int exit_code;    pid = fork();    //pid = getpid();    //cout << pid << endl;    switch (pid) {    case -1:        perror("fork failed");        exit(1);    case 0:        message = "this is the child";        n=5;        exit_code = 37;//casual set        break;    default:        message = "this is the parent";        exit_code = 0;        n=3;        break;    }    for(; n > 0;n--)    {        cout << message << ",which pid:" << getpid() << endl;        sleep(1);    }    if(pid != 0)    {        int stat_val;        pid_t child_pid;        child_pid = wait(&stat_val);        printf("child has finished: PID = %d\n",child_pid);        if(WIFEXITED(stat_val))            printf("child exited with code %d\n",WEXITSTATUS(stat_val));        else {            printf("child terminated abnormally\n");        }    }    //cout << "errx" << endl;    exit(exit_code);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.