Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Li /UNIX P iLi /UNIX P iLinux/UNIX ProgrammingLinux/UNIX Programming
APUE (Signals)APUE (Signals)
문양세문양세양세양세강원대학교강원대학교 ITIT대학대학 컴퓨터과학전공컴퓨터과학전공
SignalsSignalsAPUE (Signals)
Signals are software interrupts from unexpected events
an illegal operation (e g divide by 0)• an illegal operation (e.g., divide by 0)
• a power failure
l l k• an alarm clock
• the death of a child process
• a termination request from a user (Ctrl-C)
• a suspend request from a user (Ctrl-Z)
ProcessSignal #8 ProcessSignal #8
UNIX System Programmingby Yang-Sae MoonPage 2
Predefined Signals (1/2)Predefined Signals (1/2)APUE (Signals)
31 signals
• /usr/include/signal h (/usr/include/sys/iso/signal iso h)• /usr/include/signal.h (/usr/include/sys/iso/signal_iso.h)
Every signal has a name
b i i h ‘SIG’• begin with ‘SIG’
• SIGABRT: abort signal from abort()
• SIGALRM: alarm signal from alarm() • SIGALRM: alarm signal from alarm()
Actions of the default signal handler
• terminate the process and generate a core(dump)• terminate the process and generate a core(dump)
• ignores and discards the signal(ignore)
• suspends the process (suspend)suspends the process (suspend)
• resume the process
UNIX System Programmingby Yang-Sae MoonPage 3
Predefined Signals (2/2)Predefined Signals (2/2)APUE (Signals)
/usr/include/sys/iso/signal_iso.h
UNIX System Programmingby Yang-Sae MoonPage 4
Signal GenerationSignal GenerationAPUE (Signals)
Terminal-generated signals
• CTRL-C SIGINTCTRL C SIGINT
• CTRL-Z SIGSTP signal
Hardware excepts generate signalsHardware excepts generate signals
• divide by 0 SIGFPE
• invalid memory reference SIGSEGVy
kill()
• sends any signal to a process or process groupy g p p g p
• need to be owner or super-user
Software conditions
• SIGALRM: alarm clock expires
• SIGPIPE: broken pipe
UNIX System Programmingby Yang-Sae MoonPage 5
• SIGURG: out-of-band network data
Handling of SignalsHandling of SignalsAPUE (Signals)
Disposition or action:Process has to tell the kernel “if and when this signal occurs, do the g ,following.”
Ignore the signal:all signals can be ignored, except SIGKILL and SIGSTOP g g , p
Catch the signal:Catch the signal:Call a function of ours when a signal occurs.
Let the default action apply:most are to terminate process
UNIX System Programmingby Yang-Sae MoonPage 6
most are to terminate process
Representative UNIX Signals (1/2)Representative UNIX Signals (1/2)APUE (Signals)
SIGART : generated by calling the abort function.
SIGALRM : generated when a timer set with the alarm expiresSIGALRM : generated when a timer set with the alarm expires.
SIGCHLD : whenever a process terminates or stops, the signal is sent t th tto the parent.
SIGCONT : this signal sent to a stopped process when it is continued.
SIGFPE : signals an arithmetic exception, such as divide-by-0, floating point overflow, and so on
SIGILL : indicates that the process has executed an illegal hardware instruction.
SIGINT : generated by the terminal driver when we type the interrupt key and sent to all processes in the foreground process group.
UNIX System Programmingby Yang-Sae MoonPage 7
Representative UNIX Signals (2/2)Representative UNIX Signals (2/2)APUE (Signals)
SIGKILL : can’t be caught or ignored. a sure way to kill any process.
SIGPIPE : if we write to a pipeline but the reader has terminated SIGPIPE : if we write to a pipeline but the reader has terminated, SIGPIPE is generated.
SIGSEGV i di t th t th h d i lid SIGSEGV : indicates that the process has made an invalid memory reference. ( core dumped)
SIGTERM h i i i l b h kill(1) d b SIGTERM : the termination signal sent by the kill(1) command by default.
SIGSTP : Cntl-Z from the terminal driver which is sent to all processes in the foreground process group.
SIGUSR1 : user defined signal 1
SIGUSR2 : user defined signal 2
UNIX System Programmingby Yang-Sae MoonPage 8
signal()signal()APUE (Signals)
Signal Handler 등록
signal(int signo, void(*func)()))
specify the action for a signal (signo func)• specify the action for a signal (signo func)
• func
− SIG IGN (ignore)SIG_IGN (ignore)
− SIG_DFL (default)
− user-defined function
• Return: the previous func
UNIX System Programmingby Yang-Sae MoonPage 9
예제예제: alarm1.c (w/o handler): alarm1.c (w/o handler)APUE (Signals)
#include <stdio.h> // alarm1.cmain( ){{
alarm(3);printf(“Looping forever …\n”);while (1);printf(“This line should never be executed\n”);
}
UNIX System Programmingby Yang-Sae MoonPage 10
예제예제: alarm2.c (w/ handler) (1/2): alarm2.c (w/ handler) (1/2)APUE (Signals)
#include <stdio.h> // alarm2.c#include <signal.h>
int alarmFlag=0;void alarmHandler();
main( ) {signal(SIGALRM, alarmHandler);alarm(3);printf("Looping …\n");while(!alarmFlag) {
pause( );}printf("Loop ends due to alarm signal \n");
}
void alarmHandler( ) {printf("An alarm clock signal was received\n");alarmFlag = 1;
}
UNIX System Programmingby Yang-Sae MoonPage 11
예제예제: alarm2.c (w/ handler) (2/2): alarm2.c (w/ handler) (2/2)APUE (Signals)
실행 결과
UNIX System Programmingby Yang-Sae MoonPage 12
SIGCHLDSIGCHLDAPUE (Signals)
Whenever a process terminates or stops, the signal is sent to the parent.p
자식 프로세스는 자신이 죽을 때 부모 프로세스에게 SIGCHLD를 보낸다자식 프로세스는 자신이 죽을 때, 부모 프로세스에게 SIGCHLD를 보낸다.
UNIX System Programmingby Yang-Sae MoonPage 13
예제예제: timelimit.c (1/3): timelimit.c (1/3)APUE (Signals)
#include <stdio h> // timelimit c
$ timelimit N command // command를 N seconds안에 수행하라!
#include <stdio.h> // timelimit.c#include <signal.h>
int delay;void childHandler( );void childHandler( );
main(int argc, char *argv[]){
int pid;p
sscanf(argv[1], "%d", &delay); // delay = strtol(argv[1], 0, 0);signal(SIGCHLD,childHandler);
pid = fork();if (pid == 0) { // child
execvp(argv[2], &argv[2]);perror("Limit");
} l { // t} else { // parentsleep(delay);printf("Child %d exceeded limit and is being killed\n", pid);kill(pid, SIGINT);
}
UNIX System Programmingby Yang-Sae MoonPage 14
}}
예제예제: timelimit.c (2/3): timelimit.c (2/3)APUE (Signals)
childHandler( ) /* Executed if the child dies before the parent */{
int childPid childStat sint childPid, childStatus;childPid = wait(&childStatus);printf(“Child %d terminated within %d seconds\n”, childPid, delay);exit(0);( );
}
UNIX System Programmingby Yang-Sae MoonPage 15
예제예제: timelimit.c (3/3): timelimit.c (3/3)APUE (Signals)
실행 결과
UNIX System Programmingby Yang-Sae MoonPage 16
kill(), raise()kill(), raise()APUE (Signals)
#include <sys/types.h>#include <signal.h>
int kill(pid_t pid, int signo);int raise(int signo);
Both return: 0 if OK, 1 on error
kill - sends a signal to a process or a group of processes
raise function allows a process to send a signal to itselfraise - function allows a process to send a signal to itself
UNIX System Programmingby Yang-Sae MoonPage 17
kill()kill()APUE (Signals)
pid means
• pid > 0 : signal to the process whose process ID is pidpid > 0 : signal to the process whose process ID is pid
• pid == 0 : signal to the processes whose process group ID equals that of sender
• pid < 0 : signal to the processes whose process group ID equals abs. of pid
• pid == -1 : unspecified (used as a broadcast signal in SVR4, 4.3 + BSD)
Permission to send signals
• The super-user can send a signal to any process.p g y p
• The real or effective user ID of the sender has to equal the real or effective user ID of the receiver.
UNIX System Programmingby Yang-Sae MoonPage 18
alarm()alarm()APUE (Signals)
#include <unistd.h>
unsigned int alarm (unsigned int seconds);unsigned int alarm (unsigned int seconds);Returns: 0 or number of seconds until previously set alarm
alarm() sets a timer to expire at a specified time in futurealarm() sets a timer to expire at a specified time in future.
• when timer expires, SIGALRM signal is generated,
• default action of the signal is to terminate the process.default action of the signal is to terminate the process.
Only one alarm clock per processOnly one alarm clock per process
• previously registered alarm clock is replaced by the new value.
if alarm(0), a previous unexpired alarm is cancelled.
UNIX System Programmingby Yang-Sae MoonPage 19
pause()pause()APUE (Signals)
#include <unistd.h>
int pause (void);int pause (void);Returns: -1 with errno set to EINTR
suspends the calling process until a signal is caughtsuspends the calling process until a signal is caught.
returns only if a signal handler is executed and that handler returns.
그냥 종료하던지 ( i l h dl 가 등록되지 않은 경우)• 그냥 종료하던지 (signal handler가 등록되지 않은 경우),
• signal handler가 등록된 경우 해당 handler가 처리를 마친 후 리턴한다.
UNIX System Programmingby Yang-Sae MoonPage 20
예제예제: : mysleep.cmysleep.c (1/2)(1/2)APUE (Signals)
#include <signal.h> // mysleep.c#include <unistd.h>
static void sig_alrm(int signo) {return; /* nothing to do, just return to wake up the pause */
}
unsigned int mysleep(unsigned int nsecs) {if (signal(SIGALRM, sig_alrm) == SIG_ERR) return nsecs;alarm(nsecs); /* start the timer */pause(); /* next caught signal wakes us up */return alarm(0); /* turn off timer, return unslept time */
}
상기 mysleep() 함수는 다음과 같은 몇 가지 문제점을 안고 있다상기 mysleep() 함수는 다음과 같은 몇 가지 문제점을 안고 있다.
• 만일 mysleep()을 사용하기 이전에 alarm을 set한 경우, 앞서의 alarm이 지워진다.
• Race condition이 발생할 수 있다(즉, pause()가 호출되기 전에 alarm이 발생할 수 있다.).Race condition이 발생할 수 있다(즉, pause()가 호출되기 전에 alarm이 발생할 수 있다.).
상기 문제 해결을 위해서는 보다 견고한 mysleep()의 구현이 필요하다.
UNIX System Programmingby Yang-Sae MoonPage 21
예제예제: : mysleep.cmysleep.c (2/2)(2/2)APUE (Signals)
UNIX System Programmingby Yang-Sae MoonPage 22
abort()abort()APUE (Signals)
#include <stdlib.h>
void abort(void);void abort(void); This function never returns
Causes abnormal program terminationCauses abnormal program termination.
This function sends the SIGABRT signal to the process.
SIGABRT signal handler to perform any cleanup that it wants to do, before the process terminated.
UNIX System Programmingby Yang-Sae MoonPage 23
sleep()sleep()APUE (Signals)
#include <signal.h>
unsigned int sleep(unsigned int seconds) ;unsigned int sleep(unsigned int seconds) ;Returns: 0 or number of unslept seconds
This function causes the calling process to be suspended until eitherThis function causes the calling process to be suspended until either
• The amount of wall clock time specified by second has elapsed (returns 0)
• A signal is caught by the process and the signal handler returns A signal is caught by the process and the signal handler returns (returns the number of unslept seconds)
UNIX System Programmingby Yang-Sae MoonPage 24