Linux进程理解与实践(二)僵尸&孤儿进程 和文件共享

it2022-06-08  83

孤儿进程与僵尸进程

孤儿进程:

   如果父进程先退出,子进程还没退出那么子进程的父进程将变为init进程。(注:任何一个进程都必须有父进程)

[cpp]  view plain copy #include <stdio.h>   #include <stdlib.h>   #include <errno.h>   #include <unistd.h>      int main()   {       pid_t pid;       //创建一个进程       pid = fork();       //创建失败       if (pid < 0)       {           perror("fork error:");           exit(1);       }       //子进程       if (pid == 0)       {           printf("I am the child process.\n");           //输出进程ID和父进程ID           printf("pid: %d\tppid:%d\n",getpid(),getppid());           printf("I will sleep five seconds.\n");           //睡眠5s,保证父进程先退出           sleep(5);           printf("pid: %d\tppid:%d\n",getpid(),getppid());           printf("child process is exited.\n");       }       //父进程       else       {           printf("I am father process.\n");           //父进程睡眠1s,保证子进程输出进程id           sleep(1);           printf("father process is  exited.\n");       }       return 0;   }  

僵尸进程:

   如果子进程先退出,父进程还没退出,那么子进程必须等到父进程捕获到了子进程的退出状态才真正结束,否则这个时候子进程就成为僵尸进程。

[cpp]  view plain copy #include <stdio.h>   #include <unistd.h>   #include <errno.h>   #include <stdlib.h>      int main()   {       pid_t pid;       pid = fork();       if (pid < 0)       {           perror("fork error:");           exit(1);       }       else if (pid == 0)       {           printf("I am child process.I am exiting.\n");           exit(0);       }       printf("I am father process.I will sleep two seconds\n");       //等待子进程先退出       sleep(2);       //输出进程信息       system("ps -o pid,ppid,state,tty,command");       printf("father process is exiting.\n");       return 0;   }  

<defunct>僵尸进程

孤儿进程由init处理,并不会有什么危害。但是僵尸进程的大量存在会占用PID等资源,可能会导致系统无法产生新的进程。任何一个子进程(init除外)在exit()之后,并非马上就消失掉,而是留下一个称为僵尸进程(Zombie)的数据结构,等待父进程处理。这是每个 子进程在结束时都要经过的阶段。如果子进程在exit()之后,父进程没有来得及处理,这时用ps命令就能看到子进程的状态是“Z”。如果父进程能及时 处理,可能用ps命令就来不及看到子进程的僵尸状态,但这并不等于子进程不经过僵尸状态。  如果父进程在子进程结束之前退出,则子进程将由init接管。init将会以父进程的身份对僵尸状态的子进程进行处理

避免僵尸进程

 通过信号机制

 子进程退出时向父进程发送SIGCHILD信号,父进程处理SIGCHILD信号。在信号处理函数中调用wait进行处理僵尸进程。测试程序如下所示:

[cpp]  view plain copy //示例: 避免僵尸进程   int main(int argc, char *argv[])   {       signal(SIGCHLD, SIG_IGN);       pid_t pid = fork();       if (pid < 0)           err_exit("fork error");       else if (pid == 0)           exit(0);       else       {           sleep(50);       }       exit(0);   }  

文件共享

   父进程的所有文件描述符都被复制到子进程中, 就好像调用了dup函数, 父进程和子进程每个相同的打开文件描述符共享一个文件表项(因此, 父子进程共享同一个文件偏移量);

[cpp]  view plain copy //根据上图: 理解下面这段程序和下图的演示   int main(int argc, char *argv[])   {       signal(SIGCHLD, SIG_IGN);          int fd = open("test.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666);       if (fd == -1)           err_exit("file open error");          cout << "We Don`t flash memory\n";          char buf[BUFSIZ];       bzero(buf, sizeof(buf));          pid_t pid = fork();       if (pid < 0)           err_exit("fork error");       else if (pid > 0)       {           strcpy(buf, "Parent...");           write(fd, buf, strlen(buf));           close(fd);           cout << "fd = " << fd << endl;           exit(0);       }       else if (pid == 0)       {           strcpy(buf, "Child...");           write(fd, buf, strlen(buf));           close(fd);           cout << "fd = " << fd << endl;           exit(0);       }   }   结果是fd(文件描述符)的值相等。这说明父子进程共享同一个文件描述符,当然文件偏移量等信息也是共享的。  

fork与vfork的区别

1. fork子进程拷贝父进程的数据段(但是现在提供了写时复制技术,只有当子进程真正需要写内存时,才复制出该内存的一段副本),因此,在父进程/子进程中对全局变量所做的修改并不会影响子进程/父进程的数据内容.

    vfork子进程与父进程共享数据段,因此父子进程对数据的更新是同步的;

2. fork父、子进程的执行次序是未知的,取决于操作系统的调度算法

    vfork:子进程先运行,父进程后运行

3. 如果创建子进程是为了调用exec执行一个新的程序的时候,就应该使用vfork,但是你在vfork后执行其它语句却是非常危险的,因为很容易和父进程产生冲突。

[cpp]  view plain copy #include <unistd.h>   #include <stdio.h>   int main(void)   {       pid_t pid;       int count = 0;       pid=vfork();       count++;       printf("count=%d\n",count);       return 0;   }   在学习linux进程编程的时候遇到一个问题,就是使用vfork()函数以后本以为下面会打印出1和2,但是结果却出人意料。

打印出的结果是:

count=1 count=1 Segmentation fault

出现了段错误,经过查证得知,vfork()创建子进程成功后是严禁使用return的,只能调用exit()或者exec族的函数,否则后果不可预料,在main函数里return和exit()效果一样是有前提的:没有调用vfork。

(如果return处什么都没有也会出现段错误,结果如下

count=1 count=9 Segmentation fault

)

转载于:https://www.cnblogs.com/Zoran-/p/5819288.html

相关资源:数据结构—成绩单生成器

最新回复(0)