Contents

System Programming Ch9 - Ch10

   Dec 9, 2022     10 min read

๐Ÿ’กย Chap_9

Sessions

์‹œ์Šคํ…œํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ์˜ session์€?

โ†’ ํ•˜๋‚˜ ๋˜๋Š” ๊ทธ ์ด์ƒ์˜ ํ”„๋กœ์„ธ์Šค ๊ทธ๋ฃน


Controlling Terminal

controlling terminal

  • session leader : ๋‹ค๋ฅธ๋ง๋กœ login shell ๋˜๋Š” controlling process
    • ์–ด๋–ค ์„œ๋ฒ„์— ์ ‘์†ํ•˜์—ฌ ์—ฐ๊ฒฐ์„ฑ์„ ๊ฐ€์ง€๊ฒŒ ๋จ โ†’ session
    • terminal์„ ํ†ตํ•ด์„œ ๋ช…๋ น์–ด๋ฅผ ์น˜๊ณ , ์ฝ”๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ (login shell์„ ํ†ตํ•ด์„œ)

      โ†’ login shell์ด session์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด๋ฉด์„œ๋„, session leader๊ฐ€ ๋˜๋Š” ๊ฒƒ

    • ์ ‘์†ํ•œ ํ„ฐ๋ฏธ๋„์„ ํ†ตํ•ด process 1๋ถ€ํ„ฐ 5๊นŒ์ง€๋ฅผ ๋‹ค ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ๋จ(ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ์ˆ˜๋„, ์ง€์šธ์ˆ˜๋„, ๋ฉˆ์ถœ์ˆ˜๋„,.. ๊ถŒํ•œ์ด ์žˆ๋‹ค๋ฉด)
  • controlling terminal : pts ๋˜๋Š” tty๋ผ๊ณ  ํ•จ, ์ฆ‰ ์ ‘์†ํ–ˆ์„ ๋•Œ ๋ถ€์—ฌ๋ฐ›๋Š” ์ด shell์„ ์ œ์–ดํ•˜๋Š” ์„œ๋ฒ„์—์„œ ๋ถ€์—ฌํ•œ ์–ด๋–ค๊ฐ’
    • controlling terminal์—์„œ hang-up signal์„ ์ฃผ๋ฉด session leader๋กœ ๊ฐ€๊ณ ,
    • terminal input and terminal-generated signals๋ฅผ ์ฃผ๋ฉด foreground process group์œผ๋กœ ๊ฐ
  • hang-up signal : ์‚ฌ์šฉ์ž๊ฐ€ ์ž ๊น ๋ฉˆ์ถ”๋ผ๊ณ  ์ฃผ๋Š” ๋ช…๋ น์–ด, session leader๊ฐ€ ๋ฐ˜์‘ํ•˜๊ฒŒ ๋จ
  • terminal input and terminal-generated signals : ์ธํ„ฐ๋ŸฝํŠธ

  • foreground ์™€ background์˜ ์ฐจ์ด
    • background : &์„ ๋ถ™์ด๋ฉด ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ๋™์ž‘, ํ„ฐ๋ฏธ๋„์„ ํ‘œํ˜„ํ•ด์ฃผ์ง€ ์•Š๊ณ  ์•Œ์•„์„œ ๋Œ๋ฆผ, shell์˜ ๊ถŒํ•œ์„ ๋‚ด์–ด์ฃผ์ง€ ์•Š์œผ๋ฉด์„œ ๋™์ž‘, ํ”„๋กœ์„ธ์Šค๋ฅผ ๋Œ๋ ค๋†“๊ณ  ๋‹ค๋ฅธ์ผ์„ ํ•  ์ˆ˜ ์žˆ์Œ
    • foreground : &์„ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ๋™์ž‘, ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์คŒ, shell์˜ ๊ถŒํ•œ์„ ๋บ๊น€, ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ๋„๋Š” ํ”„๋กœ์„ธ์Šค๋Š” ๋๋‚ ๋•Œ๊นŒ์ง€ ๋‹ค๋ฅธ ์ผ์„ ํ•  ์ˆ˜ ์—†์Œ


๐Ÿ’กย Chap_10

Interrupted System Calls

Automatic restarting

  • nioctl, read, readv, write, writev, wait, and waitpid โ€ฆ ์ด๋Ÿฐ ํ•จ์ˆ˜๋“ค์€ ์•„์ฃผ ๋Š๋ฆฐ ์Šฌ๋กœ์šฐ์‹œ์Šคํ…œ์ฝœ ํ•จ์ˆ˜๋“ค์ด๋‹ค. (slowsc)
  • ๊ทธ๋Ÿฐ๋ฐ ์–˜๋„ค๊ฐ€ ๋ชจ์ข…์˜ ์ด์œ ๋กœ ๋ฉˆ์ถ”์—ˆ์„ ๋•Œ ์˜์›ํžˆ ๋ธ”๋ก๋  ์ˆ˜ ์žˆ์Œ, ๊ทธ๋Ÿฌ๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ๊ทธ๋ƒฅ ๊บผ์•ผ๋จ, ๊ทผ๋ฐ ๊ทธ๋ƒฅ ๋Œ์ˆ˜๋Š” ์—†๊ณ  ๋ณต๊ตฌ๋ฅผ ํ•ด์•ผ๋จ
  • ์ด ๋ณต๊ตฌ๋ฅผ ์ž๋™์ ์œผ๋กœ ํ•ด์ฃผ๋Š”๊ฒŒ automatic restarting
  • ์Šฌ๋กœ์šฐ์‹œ์Šคํ…œ์ฝœ ํ•จ์ˆ˜์—์„œ ์ธํ„ฐ๋ŸฝํŠธ ๋ฐœ์ƒํ–ˆ์„๋•Œ, ๋ฌดํ•œ๋ธ”๋ก์„ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ์žฌ์‹คํ–‰ ํ•ด์ฃผ๋Š” ๊ฒƒ.


kill and raise Fuction

  • kill : ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ด๋Š”(์—†์• ๋Š”, ์ œ๊ฑฐํ•˜๋Š”, ์ง€์šฐ๋Š”) ํ•จ์ˆ˜
  • raise : ๋‚˜ ์ž์‹  ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ์‹œ๊ทธ๋„์„ ๋„˜๊ฒจ์ฃผ๋Š” ํ•จ์ˆ˜
#include <signal.h>
int kill(pid_t pid, int signo)
int raise(int signo);
  • kill ํ•˜๋ฉด ์–ด๋–ค ํ”„๋กœ์„ธ์Šค๋ฅผ ์ฃฝ์ผ์ง€ pid(process number)๋ฅผ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋˜์–ด์žˆ์Œ, ๊ทผ๋ฐ raise๋Š” ํ”„๋กœ์„ธ์Šค ์•„์ด๋””๋ฅผ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์Œ, ๊ทธ ์ด์œ ๋Š” ๋‚˜ ์ž์‹ ์—๊ฒŒ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ž„


alarm and pause Function (์‹œํ—˜๋ฌธ์ œ ๋‚˜์˜ฌ๊ฑฐ์ž„)

(์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ชจ๋“  ๋ผ์ธ ๋‹ค ์ดํ•ดํ•ด์•ผ๋จ)

#includeย  <signal.h>
#includeย  <unistd.h>

static void
sig_alrm(int signo)
{
ย  /* nothing to do, just return to wake up the pause */
}

unsigned int
sleep1(unsigned int nsecs)
{
ย  if (signal(SIGALRM, sig_alrm) == SIG_ERR) //SIGALARM์ด ๋ฐœ์ƒํ•˜๋ฉด ๋‘๋ฒˆ์งธ ์ธ์ž, sig_alrm์„ ์‹คํ–‰์‹œ์ผœ์ฃผ๋ผ๋Š” ์˜๋ฏธ
ย  return(nsecs);
ย  alarm(nsecs);ย  /* start the timer */
ย  pause();ย  /* next caught signal wakes us up */
ย  return(alarm(0));ย  /* turn off timer, return unslept time */
}

<์ด ์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•  ๊ฒฝ์šฐ>

  • signal(SIGALRM, sig_alrm) ์€ SIGALARM(์‹ค์ œ๋กœ ์‹œ์Šคํ…œ์ด ๋‚ด๋ฑ‰์€ ์‹œ๊ทธ๋„)์ด ๋ฐœ์ƒํ•˜๋ฉด ๋‘๋ฒˆ์งธ ์ธ์ž, sig_alrm์„ ์‹คํ–‰์‹œ์ผœ์ฃผ๋ผ๋Š” ์˜๋ฏธ
  • ๊ทผ๋ฐ ์‹คํ–‰์ด ์•ˆ๋˜๊ณ  ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด ๋ฆฌํ„ดํ•จ
  • alarm : SIGALRM์„ ๋งŒ๋“ค์–ด์คŒ, ์ธ์ž์ธ nsecs๋Š” ์ง„์งœ ์ดˆ, ์ธ์ž๋กœ ๋„˜์–ด์˜จ ์ˆ˜๊ฐ€ 2๋ฉด 2์ดˆ๋’ค์— ์•Œ๋žŒ ํ•จ์ˆ˜ ์ˆ˜ํ–‰, ์‹œ๊ทธ๋„ ๋ฐœ์ƒ, 2์ดˆ๊ฐ€ ๋˜๊ธฐ ์ „์—๋Š” ๊ณ„์† ๋‹ค์Œ๋ผ์ธ ์‹คํ–‰ํ•ด๊ฐ

    alarm์€ ์ธ์ž๋กœ ๋“ค์–ด์˜จ ์ดˆ ์‹œ๊ฐ„ ์ดํ›„์— SIGALARM์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜

  • pause : ์‹œ๊ทธ๋„์ด ๋ญ๋“ ๊ฐ„์— ์‹œ๊ทธ๋„์ด ๋ฐœ์ƒํ•  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋ผ๋Š” ํ•จ์ˆ˜, 2์ดˆ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ pause์—์„œ ๊ธฐ๋‹ค๋ฆฌ๋‹ค๊ฐ€ signalํ•จ์ˆ˜ ๋™์ž‘ํ•ด์„œ sig_alrm ํ˜ธ์ถœํ•ด์„œ ์ˆ˜ํ–‰, ๊ทธ๋Ÿฌ๊ณ  ๋‹ค์‹œ ๋Œ์•„์˜ด
    • ๊ทธ ๋’ค๋กœ๋Š” alarm๊ณผ pause๋Š” ํ•œ๋ฒˆ ์ˆ˜ํ–‰๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ทธ๋ƒฅ ์ง€๋‚˜๊ฐ (alarm 0๋จ, ๋”์ด์ƒ ๋™์ž‘ํ•˜์ง€ ์•Š์Œ)

<race condition>

alarm(nsecs); ์—์„œ race condition ๋ฐœ์ƒ

  • scheduling์ด ๊ผฌ์—ฌ์„œ pause๋ฅผ ๋งŒ๋‚˜๊ธฐ ์ „์— ์•Œ๋žŒ์ด ๋๋‚  ๊ฒฝ์šฐ,
  • ๋ฌดํ•œ ๋ธ”๋ก์— ๊ฑธ๋ ค์„œ pause์— ์žกํ˜€์žˆ๊ฒŒ๋จ
  • ๊ทธ๋ž˜์„œ ์ด๊ฑด ๋งŽ์€ ์–‘์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ์ด๋ ‡๋“ฏ ๋ธ”๋ก์ด ๋ฐœ์ƒํ•ด์„œ ์“ธ ์ˆ˜ ์—†์Œ


์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ

#includeย  <setjmp.h>
#includeย  <signal.h>
#includeย  <unistd.h>

static jmp_bufย  env_alrm;

static void
sig_alrm(int signo)
{
ย  longjmp(env_alrm, 1);
}

unsigned int
sleep2(unsigned int nsecs)
{
ย  if (signal(SIGALRM, sig_alrm) == SIG_ERR)
ย  return(nsecs);
ย  if (setjmp(env_alrm) == 0) { //์•Œ๋žŒ์„ setjmp์•ˆ์—๋‹ค๊ฐ€ ๋„ฃ์–ด์คŒ
ย  alarm(nsecs);ย  /* start the timer */
ย  pause();ย  /* next caught signal wakes us up */
ย  }
ย  return(alarm(0));ย  /* turn off timer, return unslept time */
}
  • setjmp๋ฅผ ๊ฑธ๊ณ  ์•Œ๋žŒ์ด ๋Œ๊ฒŒ๋จ
  • ๊ทธ๋Ÿฌ๋ฉด sig_alrm์ด ์‹คํ–‰๋˜์—ˆ์„ ๋•Œ longjmp๋งŒ๋‚˜๊ณ , 1์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋‹ˆ๊นŒ if๋ฌธ์— ์•ˆ๊ฑธ๋ฆผ, ์ฆ‰ ๋ฌธ์ œ ํ•ด๊ฒฐ
  • ์•Œ๋žŒ์„ ํ†ตํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” racecondition์€ ์ด๋ ‡๊ฒŒ setjmp์™€ longjmp๋กœ ํ•ด์†Œ ๊ฐ€๋Šฅ


(์•„๋ž˜ ์ฝ”๋“œ๋Š” ์œ„์˜ alarm๊ณผ pause์—์„œ ๋ฐœ์ƒํ•˜๋Š” race condition๊ณผ ๋™์ผํ•จ)

static voidย  sig_alrm(int);

int main(void)
{
ย  intย  n;
ย  charย  line[MAXLINE];

ย  if (signal(SIGALRM, sig_alrm) == SIG_ERR)
ย  err_sys("signal(SIGALRM) error");

ย  alarm(10);
ย  if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0)
ย  err_sys("read error");
ย  alarm(0);

ย  write(STDOUT_FILENO, line, n);
ย  exit(0);
}

static void sig_alrm(int signo)
{
ย  /* nothing to do, just return to interrupt the read */
}

<race condition ๋ฐœ์ƒ>

  • alarm(10); ๊ฑธ๋ฆฌ๊ณ  10์ดˆ๋™์•ˆ ๋’ค์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธธ ๊ธฐ๋Œ€ํ•จ, ๊ทธ๋Ÿฐ๋ฐ ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ๋  ์ผ์ด ๋„ˆ๋ฌด ๋งŽ์„ ๊ฒฝ์šฐ(์ปดํ“จํ„ฐ๊ฐ€ ๊ฐœ๊ตฌ๋ฆฌ๋‹ค๋Š” ๊ฐ€์ •), read๋ฅผ ์ˆ˜ํ–‰ ๋ชปํ•˜๊ณ  ์•Œ๋žŒ์ด ๋๋‚˜๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ
  • read๋ฅผ ๋งŒ๋‚˜๊ธฐ๋„ ์ „์— ์ธํ„ฐ๋ŸฝํŠธ๊ฐ€ ๊ฑธ๋ ธ๋‹ค ์™”๋”๋‹ˆ ์ˆ˜ํ–‰์ด ์•ˆ๋˜๊ณ  ๋ฌดํ•œ๋Œ€๊ธฐ๋ฅผ ํ•ด๋ฒ„๋ฆผ, ๋ฌดํ•œ๋ธ”๋ก(read์˜ ํŠน์„ฑ)
  • read๋Š” ์‚ฌ์šฉ์ž์˜ ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ, ๊ทธ๋Ÿฐ๋ฐ ์•Œ๋žŒ์„ ๊ฐ”๋‹ค๊ฐ€ ์ธํ„ฐ๋ŸฝํŠธ๊ฐ€ ๊ฑธ๋ฆฌ๋ฉด readํ•จ์ˆ˜๋Š” ๋”์ด์ƒ ๋™์ž‘ X, ๊ณ„์† ๊ธฐ๋‹ค๋ฆผ(๋ธ”๋ก ์ƒํƒœ)
  • read๊ฐ€ ๋ธ”๋ก์ธ ์ƒํƒœ๋ฅผ ๊บผ๋‚ด์ฃผ๊ฑฐ๋‚˜ read๋ฅผ ํ•œ๋ฒˆ ๋” ์ˆ˜ํ–‰ํ•ด์ค˜์•ผํ•จ


์œ„์˜ ์ƒํ™ฉ์„ longjmp, setjmp๋กœ ํ•ด๊ฒฐํ•œ ์ฝ”๋“œ

static voidย  sig_alrm(int);
static jmp_bufย  env_alrm;

int main(void)
{
ย  intย  n;
ย  charย  line[MAXLINE];

ย  if (signal(SIGALRM, sig_alrm) == SIG_ERR)
ย  err_sys("signal(SIGALRM) error");
ย  if (setjmp(env_alrm) != 0)
ย  err_quit("read timeout");

ย  alarm(10);
ย  if ((n = read(STDIN_FILENO, line, MAXLINE)) < 0)
ย  err_sys("read error");
ย  alarm(0);

ย  write(STDOUT_FILENO, line, n);
ย  exit(0);
}

static void sig_alrm(int signo)
{
ย  longjmp(env_alrm, 1);
}
  • alarm ์„ธํŒ…์€ ๋˜‘๊ฐ™์€๋ฐ setjmp๋ฅผ ๊ฑธ์–ด์คŒ
  • alarm ๋๋‚˜๋ฉด sig_alrm ์‹คํ–‰, setjmp๋กœ ์ ํ”„ํ•˜๋Š”๋ฐ ๋ฐ˜ํ™˜๊ฐ’ 1,
  • ๊ทธ๋Ÿฌ๋ฉด if๋ฌธ์— ๊ฑธ๋ ค์„œ err_quit("read timeout"); ๋ฐœ์ƒ


sigpending Function (์ฝ”๋“œ ์•”๊ธฐ)

static void sig_quit(int signo) //์ดํ•จ์ˆ˜๋ฅผ ์ดํ•ดํ•  ํ•„์š”๋Š” X
{
ย ย  printf("caught SIGQUIT\n");
ย ย  if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
ย  err_sys("can't reset SIGQUIT");
}
int main(void)
{
ย  sigset_tย  newmask, oldmask, pendmask;

ย  if (signal(SIGQUIT, sig_quit) == SIG_ERR) //SIGQUIT๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด sig_quit์‹คํ–‰
	ย  err_sys("can't catch SIGQUIT");

ย  /*
ย  ย * Block SIGQUIT and save current signal mask.
ย  ย */
ย  sigemptyset(&newmask);
ย  sigaddset(&newmask, SIGQUIT);
ย  if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
	ย  err_sys("SIG_BLOCK error");

ย  sleep(5);ย  /* SIGQUIT here will remain pending */

ย  if (sigpending(&pendmask) < 0)
	ย  err_sys("sigpending error");
ย  if (sigismember(&pendmask, SIGQUIT))
	ย  printf("\nSIGQUIT pending\n");

ย  /*
ย  ย * Reset signal mask which unblocks SIGQUIT.
ย  ย */
ย  if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
	ย  err_sys("SIG_SETMASK error");
ย  printf("SIGQUIT unblocked\n");

ย  sleep(5);ย  /* SIGQUIT here will terminate with core file */
ย  exit(0);
}
  • sigset_tย  newmask, oldmask, pendmask; : sigset_t๋Š” ์ž๋ฃŒํ˜•์ธ๋ฐ ์‹œ๊ทธ๋„์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ”๊ตฌ๋‹ˆ์ž„ (set๋‹ˆ๊นŒ),

    ์‹œ๊ทธ๋„์˜ ์ข…๋ฅ˜๋Š” ๊ต‰์žฅํžˆ ๋งŽ์Œ ๊ทธ๊ฒƒ๋“ค์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ง„ ์ž๋ฃŒํ˜•์ž„

  • SIGQUIT : control + \ (๋ฉˆ์ถ”๋Š” ๊ฒƒ)
  • sigemptyset(&newmask); :
    • char arr[100] = {0,} ์ด๋Ÿฐ ์—ญํ• 
    • newmask๋ฅผ ์‹น ์ดˆ๊ธฐํ™” ํ•ด์คŒ, ์•„๋ฌด๊ฒƒ๋„ ๋‹ด์ง€ ์•Š์€ ๋ฐ”๊ตฌ๋‹ˆ๋กœ ๋งŒ๋“ค์–ด์คŒ
    • ์ธ์ž๋กœ ๋“ค์–ด์˜จ ์• ๋ฅผ ์•„๋ฌด๊ฒƒ๋„ ๋‹ด์ง€ ์•Š์€ ๊ฑธ๋กœ ๋งŒ๋“ค์–ด์คŒ
  • sigaddset(&newmask, SIGQUIT); :
    • signal์„ set์— ๋”ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
    • newmask๋ผ๋Š” sigset์— SIGQUIT๋ผ๋Š” ์‹œ๊ทธ๋„์„ ๋”ํ•ด์คŒ
  • if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) :
    • newmask์— ์žˆ๋Š” ์‹œ๊ทธ๋„์„ ๋ฐœ์ƒํ•ด๋„ ๋ธ”๋ก์‹œ์ผœ, ์ฆ‰ ๋ฌด์‹œํ•˜๋ผ๋Š” ๋œป
    • oldmask๋Š” ์ด์ „์—๋„ ๋ธ”๋กํ•  ์‹œ๊ทธ๋„๋“ค์ด ์žˆ์—ˆ์„ ์ˆ˜๋„ ์žˆ์Œ, ์ด์ „์— ์žˆ๋˜ ๋ธ”๋ก ๋ฆฌ์ŠคํŠธ๋“ค์„ oldmask์—๋‹ค๊ฐ€ ์ €์žฅํ•ด๋†“๋Š” ๊ฒƒ, ๋ณต์›์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
  • if (sigpending(&pendmask) < 0) : ์ง€๊ธˆ ๋ธ”๋ก ๋ฆฌ์ŠคํŠธ์— ๋ญ๊ฐ€ ์žˆ๋Š”์ง€,
    • ์ธ์ž๋กœ ๋“ค์–ด์žˆ๋Š” pendmask์—๋‹ค๊ฐ€ ์ง€๊ธˆ ๋ธ”๋ก๋œ ์‹œ๊ทธ๋„์„ ๋„ฃ์–ด์คŒ
  • if (sigismember(&pendmask, SIGQUIT)) : ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๋“ค์–ด๊ฐ„ pendmask์— SIGQUIT๊ฐ€ ์žˆ๋Š๋ƒ๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๊ฒƒ

โ†’ sigpending๊ณผ sigismember๋Š” ๊ทธ๋ƒฅ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜๋“ค, ๊ตณ์ด ์—†์–ด๋„ ๋จ

(์—ฌ๊ธฐ๊นŒ์ง€ SIGQUIT ๋ธ”๋กํ•˜๋Š” ์ฝ”๋“œ)


  • if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) : ์›๋ž˜ block list๋กœ ์›๋ณต


sigaction Function

#include <signal.h>
int sigaction(int signo, const struct sigaction *act, struct sigaction *oact);

struct sigaction {
ย  void ย  (*sa_handler)(int); /* addr of signal handler, */
ย  ย ย ย ย ย ย ย ย ย ย  /* or SIG_IGN, or SIG_DFL */
ย  sigset_t sa_mask;ย  ย ย ย ย ย ย  /* additional signals to block */
ย  int ย  sa_flags;ย  ย ย ย ย ย ย  /* signal options */

ย ย ย ย  /* alternate handler */
ย  voidย  (*sa_sigaction)(int, siginfo_t *, void *);
}
/* Reliable version of signal(), using POSIX sigaction().ย  */
Sigfunc *
signal(int signo, Sigfunc *func)
{
			ย  struct sigactionย  act, oact;

			ย  act.sa_handler = func;
			ย  sigemptyset(&act.sa_mask);
			ย  act.sa_flags = 0;
			ย  if (signo == SIGALRM) {
#ifdefย  SA_INTERRUPT
						ย  act.sa_flags |= SA_INTERRUPT;
#endif
			ย  } else {
#ifdefย  SA_RESTART
						ย  act.sa_flags |= SA_RESTART;
#endif
			ย  }
			ย  if (sigaction(signo, &act, &oact) < 0)
						ย  return(SIG_ERR);
			ย  return(oact.sa_handler);
}
  • ์‹œ๊ทธ์•ก์…˜์ด๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ ์‹œ๊ทธ์•Œ๋žŒ์ด ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ, ์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ๊ฑธ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ์•ผํ•˜๋Š”๊ฐ€.
  • if (signo == SIGALRM) : sig number๊ฐ€ SIGALRM์ด๋ฉด
  • SA_INTERRUPT, sigaction interrupt ๊ฑธ์–ด์ค˜๋ผ