wait()函数,作用:阻塞等待任意子进程,回收子进程8kb物理内存

#include
#include
#include 
//wait()函数
//作用:阻塞等待任意子进程,回收子进程8kb物理内存
int main(void)
{
    printf("main process pid = %d\n",getpid());

    pid_t pid;
    pid = fork();//创建子进程
    if(pid < 0)
    {
        perror("fork error");
        exit(1);//结束进程
    }

    if(pid == 0)
    {
        printf("子进程pid = %d\n",getpid());
        sleep(1);
        printf("********\n");
        exit(1);
    }

    if(pid > 0)
    {
        printf("父进程pid = %d\n",getpid());
        //阻塞等待任意子进程,回收子进程8kb物理内存
        wait(NULL);
        printf("-------------\n");
        exit(1);
    }

    return 0;
}
/*
$ ./a.out
main process pid = 25446
父进程pid = 25446
子进程pid = 25447
********
-------------
*/

你可能感兴趣的:(C语言程序设计)