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函数族。

     

  • 相关阅读:
    Kubernetes1.91(K8s)安装部署过程(一)--证书安装
    开源仓库Harbor搭建及配置过程
    有关centos7 图形化root用户登录
    linux服务器查看tcp链接shell
    django表格form无法保存评论排查步骤
    Redis 4.x 安装及 发布/订阅实践和数据持久化设置
    django博客项目-设置django为中文语言
    windows 环境下如何使用virtualenv python环境管理工具
    【转载】python中利用smtplib发送邮件的3中方式 普通/ssl/tls
    php安装phpize工具
  • 原文地址:https://www.cnblogs.com/dayq/p/15362222.html
Copyright © 2011-2022 走看看