Signal Handling ( 금 ) 한 민 규
2 Contents TIME_WAIT STATE Signal Handling POSIX Signal Semantics Wait, Waitpid Function
3 TIME_WAIT STATE To implement TCP ’ s full-duplex connection termination reliably. Because that end is the one that might have to retransmit the final ACK To allow old duplicate segments to expire in the network. To do TCP will not initiate a new incarnation of a connection that is currently in the TIME_WAIT state. Since the duration of the TIME_WAIT state is twice the MSL(120 sec), this allows MSL seconds for a packet in one direction to be lost, and another MSL seconds for the reply to be lost. By enforcing this rule we are guaranteed that when we successfully establish a TCP connection, all old duplicates from previous incarnations of this connection have expired in the network
4 Signal Handling Definition A signal is a notification to a process that an event has occurred Signals usually occur asynchronously Operation By one process to another process (or to itself) By the kernel to a process Ex) The SIGCHLD signal is one that is sent by the kernel whenever a process terminates, to the parent of the terminating process Every signal has a disposition, which is also called the action associated with the signal. We set the disposition of a signal by calling the sigaction function
5 POSIX Signal Semantics Once a signal handler is installed, it remains installed. Older systems removed the signal handler each time it was executed. While a signal handler is executing, the signal being delivered is blocked. Furthermore any additional signals that were specified in the sa_mask singnal set passed to sigaction when the handler was installed are also blocked. In Figure 5.6 we set sa_mask to the empty set, meaning no additional signals are blocked other than the signal being caught. That is, by default Unix signals are not queued 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
6 Signal Function Unp.h Definition Lib/signal.c Normal function prototype : void (*signal(int signo, void (*func) (int))) (int);
7 Handling SIGCHLD Signals 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 Information on the resource utilization of the child
8 Handling Zombies We establish the signal handler by adding the function call Signal(SIGCHLD, sig_chld);
9 Handling Interrupted System call 4. Since the signal was caught by the parent while the parent was blocked in a slow system call(accept), the kernel causes the accept to return an error of EINTR(interrupted system call). The parent does not handle this error, so it aborts
10 Handling Interrupted System call(Con’t) parent process 가 slow system call(accept) 에서 blocked 됐을 때 signal 이 발생하면 accept() 는 EINTR error 로 return 하게 됨 parent process aborted ( 교재 p.133~135 참고 )
11 Wait & waitpid Functions #include pid_twait(int *statloc); return : process ID if OK, 0 or -1 on error 정의 child process 가 종료될때 까지 또는 현재 프로세스를 종료시키거나 시그널 처리 함수를 호출하는 행동을 하는 신호가 전달될 때까지 현재 프로세스의 실행을 일시 중지한다. 만일 child process 가 wait() 사용시 이미 종료되었다면 (zombie process), 함수는 즉시 리턴한다. child process 가 사용한 resource 들은 모두 반환된다.
12 Wait & waitpid Functions(Con’t) 정의 pid 인자가 가리키는 child process 가 종료될때까지 또는 현재 프로세스를 종료시키거나 시그널 처리함수를 호출하는 행동을 하는 신호가 전달될때까지 현재 프로세스의 실행을 일시 중지한다. 만일 pid 로 지정된 child process 가 waitpid() 사용시 이미 종료되었다면 (zombie process), 함수는 즉시 리턴한다. child process 가 사용한 resource 들은 모두 반환된다. pid < -1 : 프로세세 그룹 ID 가 pid 의 절대값과 같은 child process 를 대기 -1 : child process 를 대기. (wait 와 유사 ) 0 : process group ID 가 호출 process ID 와 같은 child process 를 대기 > 0 : process ID 가 pid 의 값과 같은 child process 를 대기 options WNOHANG : 어떠한 child process 도 종료되지 않았다면 즉시 리턴 #include pid_t waitpid(pid_t pid, int *statloc, int options); return : process ID if OK, 0 or -1 on error
13 Wait & waitpid Functions(Con’t)
14 Wait & waitpid Functions(Con’t)
15 Wait & waitpid Functions(Con’t)
16 Wait & waitpid Functions(Con’t) 여러개의 client process 들이 동시에 terminate 될 경우 하나의 SIGCHLD 만 parent process 에 전달되고 나머지는 처리되지 않음. signal 은 queuing 되지 않는다.
17 Server Program 작성시 주의사항 We must catch the SIGCHLD signal when forking child processes We must handle interrupted system calls when we catch signals A SIGCHLD handler must be coded correctly using waitpid() to prevent any zombies from being left around