linux 舊信號新計時器實例

#include <stdio.h>
#include <curses.h>
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>

#define INPUTLEN 100

int main()
{
        void inthandler(int);
  void quithandler(int);
  char input[INPUTLEN];
  int nchars;

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

  do{
   printf("/n Type a message/n");
   nchars = read(0,input,(INPUTLEN-1));
   if(nchar == -1)
    perror("read returned an error");
   else {
    input[nchars] = '/0';
    printf("You typed: %s",input);
    }
   }
  while(strncmp(input , "quit",4)!=0);
   
        return 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");

}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章