U E D R , A S I H C RSS

Linux Programming/Signal Handling


1. signal ?

μœ„ν‚€ νŽ˜λ””μ•„μ˜ μ •μ˜λΌ λΉŒλ¦¬μžλ©΄ μ‹œκ·Έλ„μ€ ν•œ ν”„λ‘œμ„Έμ„œμ™€ 기타 λ‹€λ₯Έ ν”„λ‘œμ„ΈμŠ€ 사이에 μ „μ†‘λ˜λŠ” 비동기적 이벀트 라고 ν•œλ‹€.

μœ λ‹‰μŠ€ μ‹œμŠ€ν…œμ—μ„œ μ–΄λ–€ 이벀트(event)κ°€ λ°œμƒν•˜λ©΄ 이것을 ν”„λ‘œμ„ΈμŠ€ 사이에 μ•Œλ¦¬λŠ” μˆ˜λ‹¨μœΌλ‘œ μ‹œκ·Έλ„μ΄ μ‚¬μš©λœλ‹€.
μ‰½κ²Œ MFC 와 λΉ„μœ ν•˜μ—¬ μ„λͺ…ν•˜λŠ” μ‹œκ·Έλ„μ€ λ©”μ‹œμ§€μ™€ λΉ„μŠ·ν•˜λ‹€κ³  보면 λœλ‹€. μ‹€μ œλ‘œ signal ν•¨μˆ˜λΌ μ΄μš©ν•΄μ„œ μš°λ¦¬λŠ” MFCμ—μ„œ μ΄λ²€νŠΈμ™€ 이벀트 ν•Έλ“€λŸ¬λΌ μ—°κ²°ν•˜λ“ ν”„λ‘œκ·Έλž˜λ°μ„ ν•œλ‹€λŠ” 사싀을 μ•Œ 수 μžˆλ‹€. (μ΄λ ‡κ²Œ 보면 μ •λ§λ‘œ μœ λ‹‰μŠ€κ°€ C둜 μ§œμ—¬μžˆλ‹€λŠ” 것을 ν™•μ‹€ν•˜κ²Œ μ•Œ 수 μžˆλ‹€.)

2. λŒ€ν‘œμ  signal

SIGABRT - process aborted
SIGALRM - signal raised by alarm
SIGBUS - bus error "access to undefined portion of memory object"(SUS)
SIGCHLD - child process terminated, stopped (*or continued)
SIGCONT - continue if stopped
SIGFPE - floating point exception -- "erroneous arithmetic operation"(SUS)
SIGHUP - hangup
SIGILL - illegal instruction
SIGINT - interrupt
SIGKILL - kill
SIGPIPE - write to pipe with no one reading
SIGQUIT - quit
SIGSEGV - segmentation violation
SIGSTOP - stop executing
SIGTERM - termination
SIGTSTP - terminal stop signal
SIGTTIN - background process attempting to read ("in")
SIGTTOU - background process attempting to write ("out")
SIGUSR1 - user defined 1
SIGUSR2 - user defined 2
SIGURG - urgent data available on socket

SIGPOLL - pollable event
SIGPROF - profiling timer expired
SIGSYS - bad syscall
SIGTRAP - trace/breakpoint trap
SIGVTALRM - signal raised by timer counting virtual time -- "virtual timer expired"(SUS)
SIGXCPU - CPU time limit exceeded
SIGXFSZ - file size limit exceeded

3. sample code

int signal() 예제
~cpp
/**
code written by eternalbleu@gmail.com
desc: ctrl + c handling
**/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void handler(int sig);

int main(int argc, char **argv)
{
	int state;
	int num = 0;

	signal(SIGINT, handler);

	while(1)
	{
		printf("%d : waiting \n", num ++);
		sleep(2);
		if(num > 5)
		break;
	}
	return 0;
}

void handler(int sig)
{
//	signal(SIGINT, handler);
	printf("which signal is passed : %d \n", sig);
}

int sigaction() 예제
~cpp
/**
code written by eternalbleu@gmail.com
desc: ctrl + c handling
**/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void handler(int sig);

int main(int argc, char **argv)
{
	int state;
	int num = 0;

	struct sigaction act;
	act.sa_handler	=	handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags	=	0;

	state	=	sigaction(SIGINT, &act, 0);

	if (state != 0)
	{
		puts("sigaction() error");
		exit(1);
	}
		
	while(1)
	{
		printf("%d : waiting \n", num ++);
		sleep(2);
		if(num > 5)
		break;
	}
	return 0;
}

void handler(int sig)
{
	printf("which signal is passed : %d \n", sig);
}
λ‹Ήμ—°νžˆ μ΄ν•΄λΌ μœ„ν•΄μ„œλŠ” ν•¨μˆ˜ ν¬μΈν„°λΌ μ•Œμ•„μ•Όν•˜λ©°, κ·Έ μ΄μ™Έμ—λŠ” ꡉμž₯히 λ‹¨μˆœν•˜λ‹€λŠ” 것을 μ•Œ 수 μžˆλ‹€.
μ΄λΈ OS μ—μ„œ λͺ¨λ“  μ²˜λ¦¬λΌ λ‹€ν•΄μ£ΌκΈ° λ•Œλ¬Έμ— ν”„λ‘œκ·Έλž˜λ¨ΈλŠ” ν•Έλ“€λŸ¬λ§Œ μ •μ˜ν•˜λ©΄ λ§Œμ‚¬ OKλ‹€

4. signal 을 μ΄μš©ν•œ 타이머

~cpp
/**
code written by eternalbleu@gmail.com
desc: timer program that use SIGALRM signal.
**/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void timer(int sig);

int main(int argc, char **argv)
{
	int state;
	int num = 0;

	struct sigaction act;
	act.sa_handler	=	timer;
	sigemptyset(&act.sa_mask);
	act.sa_flags	=	0;
	
	state	=	sigaction(SIGALRM, &act, 0);
	
	if (state != 0) {
		puts("sigaction() error");
		exit(1);
	}

	alarm(5);

	while(1) {
		puts("waiting");
		sleep(2);
	}
	return 0;
}

void timer(int sig)
{
	puts("this is time for you to reserve");
	exit(0);
}
----
LinuxProgramming
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:38
Processing time 0.0228 sec