到目前为止,我们已经讨论了流程,创建,父流程和子流程等.如果不讨论其他相关流程,例如孤儿流程,僵尸流程和守护进程,讨论将是不完整的.
Orphan Process
如名称所示,孤儿意味着无父进程.当我们运行程序或应用程序时,应用程序的父进程是shell.当我们使用fork()创建进程时,新创建的进程是子进程,创建子进程的进程是父进程.反过来,这个的父进程是shell.当然,所有进程的父进程都是init进程(进程ID → 1).
上面是一个常见的情况,但是如果父进程在子进程之前退出会发生什么处理.结果是,子进程现在变成了孤儿进程.然后它的父节点,它的新父节点是所有进程的父节点,它只是init进程(进程ID - 1).
让我们尝试通过以下方式理解这一点例如.
/*文件名:orphan_process.c */
#include#include int main() { int pid; system("ps -f"); pid = fork(); if (pid == 0) { printf("Child: pid is %d and ppid is %d\n",getpid(),getppid()); sleep(5); printf("Child: pid is %d and ppid is %d\n",getpid(),getppid()); system("ps -f"); } else { printf("Parent: pid is %d and ppid is %d\n",getpid(),getppid()); sleep(2); exit(0); } return 0;}
编制和执行步骤
UID PID PPID C STIME TTY TIME CMD4581875 180558 0 0 09:19 ? 00:00:00 sh -c cd /home/cg/root/4581875; timeout 10s main4581875 180564 180558 0 09:19 ? 00:00:00 timeout 10s main4581875 180565 180564 0 09:19 ? 00:00:00 main4581875 180566 180565 0 09:19 ? 00:00:00 ps -fParent: pid is 180565 and ppid is 180564UID PID PPID C STIME TTY TIME CMD4581875 180567 0 0 09:19 ? 00:00:00 main4581875 180820 180567 0 09:19 ? 00:00:00 ps -fChild: pid is 180567 and ppid is 180565Child: pid is 180567 and ppid is 0
Zombie Process
简单来说,假设您有两个进程,即父进程和子进程.父进程负责等待子进程,然后从进程表中清除子进程条目.如果父进程没有准备等待子进程,并且同时子进程完成其工作并退出,该怎么办?现在,子进程将成为僵尸进程.当然,在父进程准备就绪后清理僵尸进程.
让我们借助一个例子来理解这一点.
/*文件名:zombie_process.c */
#include#include int main() { int pid; pid = fork(); if (pid == 0) { system("ps -f"); printf("Child: pid is %d and ppid is %d\n",getpid(),getppid()); exit(0); } else { printf("Parent: pid is %d and ppid is %d\n",getpid(),getppid()); sleep(10); system("ps aux|grep Z"); } return 0;}
编制和执行步骤
UID PID PPID C STIME TTY TIME CMD4581875 184946 0 0 09:20 ? 00:00:00 sh -c cd /home/cg/root/4581875; timeout 10s main4581875 184952 184946 0 09:20 ? 00:00:00 timeout 10s main4581875 184953 184952 0 09:20 ? 00:00:00 main4581875 184954 184953 0 09:20 ? 00:00:00 main4581875 184955 184954 0 09:20 ? 00:00:00 ps -fChild: pid is 184954 and ppid is 184953
守护进程
简单来说,没有任何关联的shell或终端的进程称为守护进程.为什么需要这个?这些是在后台运行以按预定义的间隔执行操作并且还响应某些事件的过程.守护进程不应该有任何用户交互,因为它作为后台进程运行.
内部Linux守护程序进程通常以字母"d"结尾,例如内核守护程序(ksoftirqd,kblockd) ,kswapd等),Printing Daemons(cupsd,lpd等),文件服务守护进程(smbd,nmbd等),管理数据库守护进程(ypbind,ypserv等),电子邮件守护进程(sendmail,popd, smtpd等),远程登录和命令执行守护进程(sshd,in.telnetd等),引导和配置守护进程(dhcpd,udevd等),init进程(init),cron守护进程,atd守护进程等.
现在让我们看看如何创建一个守护进程.以下是步骤 : 号;
步骤1 : 创建子进程.现在我们有两个进程 - 父进程和子进程
通常进程层次结构是SHELL → 父母过程 → 儿童过程
第2步 : 退出终止父进程.子进程现在变成了孤立进程,并由init进程接管.
现在,层次结构是INIT PROCESS → 儿童过程
第3步 : 如果调用进程不是进程组负责人,则调用setsid()系统调用会创建新会话.现在,调用进程成为新会话的组长.此流程将是此新流程组和此新会话中的唯一流程.
步骤4 : 将进程组ID和会话ID设置为调用进程的PID.
步骤5 : 关闭进程的默认文件描述符(标准输入,标准输出和标准错误),因为终端和shell现在已与应用程序断开连接.
/*文件名:daemon_test.c */
#include#include #include #include #include #include #include int main(int argc, char *argv[]) { pid_t pid; int counter; int fd; int max_iterations; char buffer[100]; if (argc < 2) max_iterations = 5; else { max_iterations = atoi(argv[1]); if ( (max_iterations <= 0) || (max_iterations > 20) ) max_iterations = 10; } pid = fork(); // Unable to create child process if (pid < 0) { perror("fork error\n"); exit(1); } // Child process if (pid == 0) { fd = open("/tmp/DAEMON.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644); if (fd == -1) { perror("daemon txt file open error\n"); return 1; } printf("Child: pid is %d and ppid is %d\n", getpid(), getppid()); printf("\nChild process before becoming session leader\n"); sprintf(buffer, "ps -ef|grep %s", argv[0]); system(buffer); setsid(); printf("\nChild process after becoming session leader\n"); sprintf(buffer, "ps -ef|grep %s", argv[0]); system(buffer); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } else { printf("Parent: pid is %d and ppid is %d\n", getpid(), getppid()); printf("Parent: Exiting\n"); exit(0); } // Executing max_iteration times for (counter = 0; counter < max_iterations; counter++) { sprintf(buffer, "Daemon process: pid is %d and ppid is %d\n", getpid(), getppid()); write(fd, buffer, strlen(buffer)); sleep(2); } strcpy(buffer, "Done\n"); write(fd, buffer, strlen(buffer)); // Can't print this as file descriptors are already closed printf("DoneDone\n"); close(fd); return 0;}
Parent: pid is 193524 and ppid is 193523Parent: Exiting4581875 193525 0 0 09:23 ? 00:00:00 main4581875 193526 193525 0 09:23 ? 00:00:00 sh -c ps -ef|grep main4581875 193528 193526 0 09:23 ? 00:00:00 grep main4581875 193525 0 0 09:23 ? 00:00:00 main4581875 193529 193525 0 09:23 ? 00:00:00 sh -c ps -ef|grep main4581875 193531 193529 0 09:23 ? 00:00:00 grep main