zoukankan      html  css  js  c++  java
  • 003_process_base

    1. Functions associated with creating a process
        system : execute a shell command and it's a blocking function
                system("ls -l");
            Create an independent process,and it has independent code space and memory space.
            The "system" function will return after the process is finished.
        popen : pipe stream to or from a process
            Create a child process and also create a pipe between the parent process and the child process.
        exec : This is a family of functions
            execl/execlp : replace the code data of the current code space and the function itself do not create a new process
        fork : Create a child process and clone the code space of the parent process and it's location that the parent process is executed to.
            The "fork" is not called in the child process, so it return 0.
            The child process and the parent process are run at the same time.

    //system

    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    int main()
    {
        printf("Process:%d ",getpid());
        sleep(3);
        return 99;
    } // test

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<sys/wait.h>
    main()
    {
        printf("system:%d ",getpid());
        int r = system("./test");
        // You can not get the return value directly
        printf("return value: %d ",r>>8&255);
        printf("return value: %d ",WEXITSTATUS(r));
    }

    //popen

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<sys/wait.h>

    main()
    {
        char buf[1025];
        //FILE *f = popen("ls -l","r");
        FILE *f = popen("./test","r");
        int fd = fileno(f);

        int r;
        printf("begin===============================");
        while((r = read(fd,buf,1024))>0)
        {
            buf[r] = 0;
            printf("%s",buf);
        }
        printf("end=============================== ");
        close(fd);
        pclose(f);
    }

    //exec

    #include<stdio.h>
    #include<unistd.h>

    int main()
    {
        printf("main:%d ",getpid());
        //int r = execl("test","param1",NULL);
            //The following code is replaced by the program "test",so the following code will not be executed
            //We do not create a new process,we just replace the code space,so there is only one process all the time and printf the same ID.
        //int r = execl("/bin/ls","ls","-l",NULL); //we must use "/bin/ls"
        int r = execlp("ls","ls","-l",NULL); //we can use "ls", which is different from execl which use "/bin/ls"
            //execl : Only search in the current path
            //execlp : Search in the current path and PATH
        printf("end:%d",r);
    }

    //fork

    #include<stdio.h>
    #include<unistd.h>
    int main()
    {
        printf("Before creating a process! ");
        int pid = fork();
            // We create a new process and clone the code space of the parent process
        if(pid == 0) //child process
        {
            while(1)
            {
                printf("this is child process:%d ",getpid());
                sleep(1);
            }
        }
        else // parent process
        {
            while(1)
            {
                printf("this is parent process:%d ",getpid());
                sleep(1);
            }
        }
        printf("After creating a process: ");
        return 0;
    }

  • 相关阅读:
    文献阅读报告
    Social LSTM 实现代码分析
    文献阅读报告
    对象不止是一个对象——面向对象设计与构造第四章总结暨课程总结
    当代码遇到数理逻辑——面向对象设计与构造第三章总结
    学会拒绝,是一种智慧——OO电梯章节优化框架的思考
    学会与“有生命力”的对象打交道——面向对象设计与构造第二章总结
    从结构和数字看OO——面向对象设计与构造第一章总结
    爬取漫画DB上的JoJo的奇妙冒险 第七部 飙马野郎
    爬取5家公司(如:阿里巴巴、京东、亚马逊、华为、贵州茅台)百度“资讯”新闻的10页内容
  • 原文地址:https://www.cnblogs.com/liujun5319/p/9638455.html
Copyright © 2011-2022 走看看