其他流程
到目前为止,我们已经讨论了进程、进程的创建、父进程和子进程等。如果不讨论其他相关进程,例如孤儿进程、僵尸进程和守护进程,那么讨论将是不完整的。
孤儿进程
正如名称所示,孤儿意味着无父进程。当我们运行一个程序或应用程序时,该应用程序的父进程是shell。当我们使用fork()创建进程时,新创建的进程是子进程,创建子进程的进程是父进程。反过来,它的父进程是shell。当然,所有进程的父进程都是init进程(进程ID → 1)。
上面是一个常见的场景,但是,如果父进程先于子进程退出会发生什么情况。结果是,子进程现在成为孤儿进程。那么它的父进程呢,它的新父进程是所有进程的父进程,只不过是init进程(进程ID - 1)。
让我们尝试使用以下示例来理解这一点。
/* 文件名:orphan_process.c */
#include<stdio.h>
#include<stdlib.h>
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 CMD
4581875 180558 0 0 09:19 ? 00:00:00 sh -c cd /home/cg/root/4581875;
timeout 10s main
4581875 180564 180558 0 09:19 ? 00:00:00 timeout 10s main
4581875 180565 180564 0 09:19 ? 00:00:00 main
4581875 180566 180565 0 09:19 ? 00:00:00 ps -f
Parent: pid is 180565 and ppid is 180564
UID PID PPID C STIME TTY TIME CMD
4581875 180567 0 0 09:19 ? 00:00:00 main
4581875 180820 180567 0 09:19 ? 00:00:00 ps -f
Child: pid is 180567 and ppid is 180565
Child: pid is 180567 and ppid is 0
僵尸进程
简单来说,假设您有两个进程,即父进程和子进程。父进程的责任是等待子进程,然后从进程表中清除子进程条目。如果父进程没有准备好等待子进程,而子进程同时完成其工作并退出,该怎么办?现在,子进程将成为僵尸进程。当然,僵尸进程会在父进程就绪后被清理。
让我们通过一个例子来理解这一点。
/* 文件名:zombie_process.c */
#include<stdio.h>
#include<stdlib.h>
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 CMD
4581875 184946 0 0 09:20 ? 00:00:00 sh -c cd /home/cg/root/4581875;
timeout 10s main
4581875 184952 184946 0 09:20 ? 00:00:00 timeout 10s main
4581875 184953 184952 0 09:20 ? 00:00:00 main
4581875 184954 184953 0 09:20 ? 00:00:00 main
4581875 184955 184954 0 09:20 ? 00:00:00 ps -f
Child: pid is 184954 and ppid is 184953
守护进程
简单来说,没有任何关联的 shell 或终端的进程称为守护进程。为什么需要这个?这些进程在后台运行,以预定义的时间间隔执行操作并响应某些事件。守护进程不应该有任何用户交互,因为它作为后台进程运行。
Linux 内部守护进程通常以字母“d”结尾,例如内核守护进程(ksoftirqd、kblockd、kswapd 等)、打印守护进程(cupsd、lpd 等)、文件服务守护进程(smbd、nmbd 等) 、管理数据库守护进程(ypbind、ypserv 等)、电子邮件守护进程(sendmail、popd、smtpd 等)、远程登录和命令执行守护进程(sshd、in.telnetd 等)、引导和配置守护进程(dhcpd) 、udevd等)、初始化进程(init)、cron守护进程、atd守护进程等。
现在让我们看看如何创建守护进程。以下是步骤 -
步骤 1 - 创建一个子进程。现在我们有两个进程——父进程和子进程
通常进程层次是SHELL → PARENT PROCESS → CHILD PROCESS
步骤 2 - 通过退出终止父进程。子进程现在成为孤儿进程并由 init 进程接管。
现在,层次结构是 INIT PROCESS → CHILD PROCESS
步骤3 - 如果调用进程不是进程组领导者,则调用setsid()系统调用会创建一个新会话。现在调用进程成为新会话的组长。该进程将是该新进程组和该新会话中的唯一进程。
步骤 4 - 将进程组 ID 和会话 ID 设置为调用进程的 PID。
步骤 5 - 关闭进程的默认文件描述符(标准输入、标准输出和标准错误),因为终端和 shell 现在已与应用程序断开连接。
/* 文件名: daemon_test.c */
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
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 193523 Parent: Exiting 4581875 193525 0 0 09:23 ? 00:00:00 main 4581875 193526 193525 0 09:23 ? 00:00:00 sh -c ps -ef|grep main 4581875 193528 193526 0 09:23 ? 00:00:00 grep main 4581875 193525 0 0 09:23 ? 00:00:00 main 4581875 193529 193525 0 09:23 ? 00:00:00 sh -c ps -ef|grep main 4581875 193531 193529 0 09:23 ? 00:00:00 grep main