Unix/linux Process details-code

Source: Internet
Author: User

Unix/linux Process details-code

#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);}

 

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.