1. ps -t pts/6 -o pid,ppid,tty,stat,args,wchan
1) -t ttylist Select by tty. This selects the processes associated with the terminals given in ttylist.
-t pts/6 : 表示显示在终端 pts/6 中运行的进程。
2)-o format User-defined format. format is a single argument in the form of a blank-separated or comma-separated
list, which offers a way to specify individual output columns. The recognized keywords are described in
the STANDARD FORMAT SPECIFIERS section below. (指定 ps 的输出格式,即要输出哪些选项)
3)nwchan WCHAN address of the kernel function where the process is sleeping (use wchan if you want the kernel
function name). Running tasks will display a dash ('-') in this column.
4)args COMMAND command with all its arguments as a string. Modifications to the arguments may be shown.
The output in this column may contain spaces. A process marked <defunct> is partly dead, waiting to be
fully destroyed by its parent.
2. POSIX Signal Handling
A signal is a notification to a process that an event has occurred. Signals are sometimes called software interrupts. Signals usually occur asynchronously. By this we mean that a process doesn't know ahead of time exactly when a signal will occur.
Signals can be sent :
1)By one process to another process (or to itself)
2)By the kernel to a process
1> The SIGCHLD signal that we described at the end of the previous section is one that is sent by the kernel whenever a process terminates, to the parent of the terminating process.
The two signals SIGKILL and SIGSTOP cannot be caught.
We can ignore a signal by setting its disposition to SIG_IGN. The two signals SIGKILL and SIGSTOP cannot be ignored.
We can set the default disposition for a signal by setting its disposition to SIG_DFL. The default is normally to terminate a process on receipt of a signal, with certain signals also generating a core image of the process in its current working directory. There are a few signals whose default disposition is to be ignored: SIGCHLD and SIGURG are two that we will encounter in this text.
2> POSIX allows us to specify a set of signals that will be blocked when our signal handler is called. Any signal that is blocked cannot be delivered to a process. We set the sa_mask member to the empty set, which means that no additional signals will be blocked while our signal handler is running. POSIX guarantees that the signal being caught is always blocked while its handler is executing.
3. POSIX Signal Semantics
We summarize the following points about signal handling on a POSIX-compliant system:
1> Once a signal handler is installed, it remains installed. (Older systems removed the signal handler each time it was executed.)
2> While a signal handler is executing, the signal being delivered is blocked. Furthermore, any additional signals that were specified in the sa_mask signal set passed to sigaction when the handler was installed are also blocked.
3> If a signal is generated one or more times while it is blocked, it is normally delivered only one time after the signal is unblocked. That is, by default, Unix signals are not queued. The POSIX real-time standard, 1003.1b, defines some reliable signals that are queued, but we do not use them in this text.
4> It is possible to selectively block and unblock a set of signals using the sigprocmask function. This lets us protect a critical region of code by preventing certain signals from being caught while that region of code is executing.
4. The purpose of the zombie state is to maintain information about the child for the parent to fetch at some later time. This information includes the process ID of the child, its termination status, and information on the resource utilization of the child (CPU time, memory, etc.).
If a process terminates, and that process has children in the zombie state, the parent process ID of all the zombie children is set to 1 (the init process), which will inherit the children and clean them up (i.e., init will wait for them, which removes the zombie). Some Unix systems show the COMMAND column for a zombie process as .
(所以一般清理僵尸进程的方法就是:找到他的父进程ID,然后 kill -9 ID 将他的父进程干掉。)
Obviously we do not want to leave zombies around. They take up space in the kernel and eventually we can run out of processes. Whenever we fork children, we must wait for them to prevent them from becoming zombies. To do this, we establish a signal handler to catch SIGCHLD, and within the handler, we call wait.
5. wait and waitpid Functions
#include
pid_t wait (int *statloc);
pid_t waitpid (pid_t pid, int *statloc, int options);
wait and waitpid both return two values: the return value of the function is the process ID of the terminated child, and the termination status of the child (an integer) is returned through the statloc pointer.
There are three macros that we can call that examine the termination status and tell us if the child terminated normally, was killed by a signal, or was just stopped by job control.
If there are no terminated children for the process calling wait, but the process has one or more children that are still executing, then wait blocks until the first of the existing children terminates.
waitpid gives us more control over which process to wait for and whether or not to block. First, the pid argument lets us specify the process ID that we want to wait for. A value of -1 says to wait for the first of our children to terminate. The options argument lets us specify additional options. The most common option is WNOHANG. This option tells the kernel not to block if there are no terminated children.
阅读(1430) | 评论(0) | 转发(0) |