Drkcore

08 10 2009 Linux Tweet

Unix/Linuxプログラミング理論と実践 7章 (シグナル)

シグナル

ProductName Unix/Linuxプログラミング理論と実践
Bruce Molay
アスキー・メディアワークス / ¥ 6,090 ()
在庫あり。

#include <stdio.h>
#include <signal.h>

#define INPUTLEN 100

int main(int ac, char *av[])
{
  void inthandler(int);
  void quithandler(int);
  char input[INPUTLEN];
  int nchars;

  signal( SIGINT, inthandler );
  signal( SIGQUIT, quithandler );

  do {
    printf("\nType a message\n");
    nchars = read(0, input, (INPUTLEN-1));
    if ( nchars == -1 )
      perror("read returned an error");
    else {
      input[nchars] = '\0';
      printf("You typed: %s", input);
    }
  }
  while( strncmp( input , "quit" , 4 ) != 0 );
}

void inthandler(int s) 
{
  printf(" Received signal %d .. waiting\n", s );
  sleep(2);
  printf(" Leaving inthandler \n");
}

void quithandler(int s)
{
  printf(" Received signal %d .. waiting\n", s);
  sleep(3);
  printf(" Leaving quithandler \n");
}

実行してみる

Type a message
^C Received signal 2 .. waiting
^C^C^C Leaving inthandler 
 Received signal 2 .. waiting
 Leaving inthandler 

macbookでやってみると二番目のシグナルはブロックされて、三番目以降のシグナルは無視される。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021