zoukankan      html  css  js  c++  java
  • Linux下进程控制相关

    以下内容仅作为个人记录的参考,但也欢迎读者前来指正。

    fork()

    #include<sys/types.h>
    #include<unistd.h>
    
    pid_t fork(void)
    
    返回:
    -1:出错。
    0:子进程
    否则为父进程。
    #include<stdio.h>
    
    #include<sys/types.h>
    #include<unistd.h>
    
    int main()
    {
            pid_t result;
            result = fork();
            if(result==-1)
            {
                    printf("fork出错
    ");
                    return 0;
            }
            else if(result==0)
            {
                    printf("this is child process. pid = %d
    .",getpid());
                    printf("my father's pid = %d
    ",getppid());
            }
            else
            {
                    printf("this is parent process.pid = %d
    .",getpid());
            }
            return 0;
    }

     为什么父进程的id不是25905呢,有点奇怪。

    这就是不正常的情况,父进程pid变了,说明原来的父亲没了。没了,在这里看来只能是运行结束了。

    所以延长一下父进程的进程时间。

    #include<stdio.h>
    
    #include<sys/types.h>
    #include<unistd.h>
    
    int main()
    {
            pid_t result;
            result = fork();
            if(result==-1)
            {
                    printf("fork出错
    ");
                    return 0;
            }
            else if(result==0)
            {
                    printf("this is child process. pid = %d
    .",getpid());
                    printf("my father's pid = %d
    ",getppid());
                    printf("add a line to print.
    ");
            }
            else
            {
                    printf("this is parent process.pid = %d
    .",getpid());
                    printf("the result is %d
    ",result);
                    printf("add a line to print.
    ");
                    printf("add a line to print1.
    ");
                    getchar();//阻塞输出
            }
            return 0;
    }

     果然,父进程结束太快,所以儿子被别人收养了。

    那么,刚才pid为1的又是什么进程呢。

     不太清楚,可能就是单纯被某个系统进程收养了吧。

    可以参考此篇博文。

    https://blog.csdn.net/Oerror_/article/details/104361264

    退出进程:

    #include<stdlib.h>
    exit(0)
    
    #include<unistd.h>
    _exit(0)
    
    一般填0代表正常退出。
    
    区别在于exit会对缓冲区做一些操作。
    比如输出缓冲区的内容。
    #include<stdio.h>
    
    #include<sys/types.h>
    #include<unistd.h>
    
    int main()
    {
            pid_t result;
            result = fork();
            if(result==-1)
            {
                    printf("fork出错
    ");
                    return 0;
            }
            else if(result==0)
            {
                    printf("子进程调用exit(0)
    ");
                    printf("this is a test line.");
                    _exit(0);
            }
            else
            {
                    printf("父进程调用exit(0)
    ");
                    printf("this is a test line.");
                    exit(0);
            }
            return 0;
    }

     区别就在于,_exit(0)比exit(0)少输出了一些内容。

    waitpid(),这里就略掉wait()了。

    #include<stdio.h>
    
    #include<sys/types.h>
    #include<unistd.h>
    #include<stdlib.h>
    
    int main()
    {
            pid_t result;
            result = fork();
            if(result==-1)
            {
                    printf("fork出错
    ");
                    return 0;
            }
            else if(result==0)
            {
              printf("i am child. my pid is %d. ",getpid()); sleep(
    5); exit(0); } else { pid_t pr; do{ pr = waitpid(result,NULL,WNOHANG); if(pr==0) { printf("the child process has not exited. "); sleep(1); } } while(pr==0); if(pr==result) { printf("the child pid is %d ",pr); } else { printf("error occured. "); } } return 0; }

    此外,也并不是简单的父进程就输出5遍,还可能因为执行速度执行6遍。

    还有一个创建进程的就是exec函数族。

     

  • 相关阅读:
    每日总结32
    每日总结31
    每日总结30
    Leetcode 115. 不同的子序列(二维DP)
    Leetcode 59. 螺旋矩阵 II
    Leetcode 227. 基本计算器 II
    macOS下将GitHub中单个子文件夹下载到本地
    P3796 【模板】AC自动机(加强版)
    P3808 【模板】AC自动机(简单版)
    【Kubernetes】副本的删除
  • 原文地址:https://www.cnblogs.com/dayq/p/15362222.html
Copyright © 2011-2022 走看看