僵尸进程 孤儿进程 UNIX异常控制流相关函数

僵尸进程(Zombie Process):子进程结束,父进程没有等待(即调用wait/waitpid)它,那么它成为僵尸进程

孤儿进程(Orphan Process):一个父进程退出,而它一些子进程还在运行中,香港空间,香港空间,那么这些子进程将成为孤儿进程;孤儿进程将被init进程收养,即init进程为其父进程

父进程退出时,香港服务器,若它的某些子进程为僵尸进程,那么这些僵尸进程也退出。因而在系统中,若想杀死僵尸进程,其中的一个办法就是杀死其父进程。

下面这个例子演示的就是僵尸进程,在运行的时候,可以用”ps -ef | grep defunct”进行查看。

#include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(){pid_t pid;if ((pid = fork()) == 0) {printf();exit(0);}else {printf(, pid);// use “ps -ef | grep defunct” to find the zombiesleep(10);wait(NULL);// after wait function, if you use “ps -ef | grep defunct” to find the zombie// you will find nonesleep(10);}}

通过“man 7 signal” 可以查看诸如SIGINT, SIGKILL, SIGTSTP, SIGCHLD, SIGTERM等信号的意义。

#include <sys/types.h>#include <unistd.h>pid_t getpid(void);pid_t getppid(void);Returns: PID of either the caller or the parent

#include <stdlib.h>void exit(int status);This function does not return

#include <sys/types.h>#include <unistd.h>pid_t fork(void);Returns: 0 to child, PID of child to parent, −1 on error

#include <sys/types.h>#include <sys/wait.h>pid_t waitpid(pid_t pid, int *status, int options);Returns: PID of child if OK, 0 (if WNOHANG) or −1 on error

waitpid 函数及其参数详解:

By default (when options = 0), waitpid suspends execution of the calling process until a child process in its wait set terminates. If a process in the wait set has already terminated at the time of the call, then waitpid returns immediately.In either case, waitpid returns the PID of the terminated child that caused waitpid to return, and the terminated child is removed from the system.If pid > 0, then the wait set is the singleton child process whose process ID is equal to pid.If pid = -1, then the wait set consists of all of the parent’s child processes.The default behavior can be modified by setting options to various combinations of the WNOHANG and WUNTRACED constants:. WNOHANG: Return immediately (with a return value of 0) if none of the child processes in the wait set has terminated yet. The default behavior suspends the calling process until a child terminates. This option is useful in those cases where you want to continue doing useful work while waiting for a child to terminate.. WUNTRACED: Suspend execution of the calling process until a process in the wait set becomes either terminated or stopped. Return the PID of the terminated or stopped child that caused the return. The default behavior returns only for terminated children. This option is useful when you want to check for both terminated and stopped children.. WNOHANG | WUNTRACED: Return immediately, with a return value of 0, if none of the children in the wait set has stopped or terminated, or with a return value equal to the PID of one of the stopped or terminated children.

If the status argument is non-NULL, then waitpid encodes status information about the child that caused the return in the status argument. The wait.h include file defines several macros for interpreting the status argument:. WIFEXITED(status): Returns true if the child terminated normally, via a call to exit or a return.. WEXITSTATUS(status): Returns the exit status of a normally terminated child. This status is only defined if WIFEXITED returned true.. WIFSIGNALED(status): Returns true if the child process terminated because of a signal that was not caught.. WTERMSIG(status): Returns the number of the signal that caused the child process to terminate. This status is only defined if WIFSIGNALED(status) returned true.. WIFSTOPPED(status): Returns true if the child that caused the return is currently stopped.. WSTOPSIG(status): Returns the number of the signal that caused the child to stop. This status is only defined if WIFSTOPPED(status) returned true.

#include <sys/types.h>#include <sys/wait.h>pid_t wait(int *status);Returns: PID of child if OK or −1 on error

#include <unistd.h>unsigned int sleep(unsigned int secs);Returns: seconds left to sleep

#include <unistd.h>int pause(void);Always returns −1

#include <unistd.h>**envp[]);Does not return if OK, returns −1 on error

风景如何,其实并不重要。重要的是,你在我的身边。

僵尸进程 孤儿进程 UNIX异常控制流相关函数

相关文章:

你感兴趣的文章:

标签云: