【Linux】进程控制

文章目录

  • 一、进程创建
  • 二、进程终止
  • 三、进程等待


一、进程创建

1.fork()
在这里插入图片描述
它从已存在的进程中创建一个新进程,新进程为子进程,原进程为父进程。

     1	#include<iostream>
     2	#include<unistd.h>
     3	int main()
     4	{
     5	    std::cout<<"I am a process:"<<getpid()<<std::endl;
     6	    pid_t ret=fork();
     7	    if(ret<0)
     8	    {
     9	        std::cout<<"fork error"<<std::endl;
    10	    }
    11	    else if(ret==0)
    12	    {
    13	        std::cout<<"I am child,my pid is:"<<getpid()<<" my return:"<<ret<<std::endl;
    14	        sleep(2);
    15	    }
    16	    else 
    17	    {
    18	        std::cout<<"I am father,my pid is:"<<getpid()<<" my return is:"<<ret<<std::endl;
    19	        sleep(2);
    20	    }
    21	    return 0;
    22	}  

【Linux】进程控制_第1张图片
上面的代码是根据fork之后根据返回值的不同,不同的进程执行不同的代码

2.fork返回值
给子进程返回 0, 给父进程返回自进程的pid

二、进程终止

1.进程退出场景

  • 代码运用完毕,正常退出,退出码为0
    【Linux】进程控制_第2张图片
  • 代码运行结束, 结果异常,退出码不为0
    【Linux】进程控制_第3张图片
  • 代码异常终止,出现警告
    【Linux】进程控制_第4张图片
    2.进程常见的退出方法
  • 正常终止

从main函数返回
调用exit
调用_exit

  • 异常退出
  • ctrl + c,信号终止

【Linux】进程控制_第5张图片

三、进程等待

1、进程等待的必要性

  • 之前讲过,子进程退出,父进程如果不管不顾,就可能造成‘僵尸进程’的问题,进而造成内存泄漏。
  • 另外,进程一旦变成僵尸状态,那就刀枪不入,“杀人不眨眼”的kill -9 也无能为力,因为谁也没有办法杀死一个已经死去的进程。
  • 最后,父进程派给子进程的任务完成的如何,我们需要知道。如,子进程运行完成,结果对还是不对,或者是否正常退出。
  • 父进程通过进程等待的方式,回收子进程资源,获取子进程退出信息。
    2.进程等待的方法
    【Linux】进程控制_第6张图片
    wait

#include
#include
pid_t wait(int*status);
返回值:
成功返回被等待进程pid,失败返回-1。
参数:
输出型参数,获取子进程退出状态,不关心则可以设置成为NULL

     1	#include<iostream>
     2	#include<sys/wait.h>
     3	#include<stdlib.h>
     4	#include<unistd.h>
     5	int main()
     6	{
     7	    pid_t pid=fork();
     8	    if(pid<0)
     9	    {
    10	        std::cout<<"fork error"<<std::endl;
    11	    }
    12	    else if(pid==0)
    13	    {
    14	        int count=0;
    15	        while(1)
    16	        {
    17	            sleep(1);
    18	            std::cout<<"i am child"<<std::endl;
    19	            if(count==3)
    20	            {
    21	                break;
    22	            }
    23	            count++;
    24	        }
    25	        exit(0);
    26	    }
    27	    else 
    28	    {
    29	        int count=0;
    30	        while(1)
    31	        {
    32	            sleep(1);
    33	            std::cout<<"i am father"<<std::endl;
    34	            while(count==5)
    35	            {
    36	               wait(NULL);
    37	            }
    38	            count++;
    39	        }
    40	        exit(0);
    41	       /* std::cout<<"father before...."<
    44	    }
    45	    return 0;
    46	}


【Linux】进程控制_第7张图片

#include
#include
pid_t waiitpid(pid_t pid,int *status,int options);
//该函数的参数:
//第一个参数pid:
//pid=-1,等待任意一个子进程,此时的作用相当于wait函数。
// pid>0,等待一个进程ID是pid的子进程。
//第二个参数status:
//WIFEXITED(status)(查看子进程是否正常退出),若为正常终止子进程退出的状态,则为真,否则为假
//WEIXTSTATUS(status)(获取进程的退出吗),在该进程正常终止(WIFEXITED(status)不为0)的情况下才有意义,提取子进程退出码。
//第三个参数options:
//WNOHANG:若pid对应的子进程没有结束,则返回0,不予等待,若该子进程结束,则返回子进程的ID.
//返回值:-1 0 大于0
若waitpid等到了正常终止的子进程,则返回该子进程的ID(即为大于0 的情况);
若设置了WNOHANG的值,而等不到正常退出的子进程,即返回0;
若调用waitpid函数出错,则返回-1;
 1	#include<iostream>
     2	#include<sys/wait.h>
     3	#include<unistd.h>
     4	#include<stdlib.h>
     5	int main()
     6	{
     7	    pid_t pid=fork();
     8	    if(pid<0)
     9	    {
    10	        std::cout<<"fork error!\n"<<std::endl;
    11	    }
    12	    else if(pid==0)
    13	    {
    14	        //child
    15	        int count=0;
    16	        while(count<5)
    17	        {
    18	            std::cout<<"child is running,pid="<<getpid()<<std::endl;
    19	            sleep(1);
    20	            count++;
    21	        }
    22	        exit(0);
    23	    }
    24	    //father
    25	    std::cout<<"father wait before!"<<std::endl;
    26	    pid_t ret=waitpid(pid,NULL,0);
    27	    if(ret>0)
    28	    {
    29	        std::cout<<"wait success!"<<std::endl;
    30	    }
    31	    else 
    32	    {
    33	        std::cout<<"wait failed"<<std::endl;
    34	    }
    35	    std::cout<<"father wait after!"<<std::endl;
    36	    return 0;
    37	}

【Linux】进程控制_第8张图片


你可能感兴趣的:(Linux,linux)