漂亮的信号捕捉

#include <unistd.h>;
#include <signal.h>;
#include <stdlib.h>;
#include <setjmp.h>; 
#include <sys/signal.h>; 

static jmp_buf jmpbuf; 

void int_proc(int sig) {         
  siglongjmp(jmpbuf, 1); 
} 

int main() { 
  int c, i; 

        int m;
        m=sigsetjmp(jmpbuf,1);
  if (m==1) printf("\nInterrupt\n";
  else if(m==2) printf("\ninvalid input\n"; 

  signal(SIGINT, int_proc); 
  printf("Choose task a or b:"; 
  c = getchar();
  while(getchar()!='\n')
          continue; 
  printf("you select\n";          

  switch (c) { 
     case 'a': 
        for (i = 0; i < 10; i++) { 
           printf("..%3d%%", (i+1)*10); 
           sleep(2); 
        } 
        break; 
     case 'b': 
        for (i = 0; i < 10; i++) { 
           printf("..%3d%%", (i+1)*10); 
           sleep(1); 
        } 
        break; 
     default: 
        siglongjmp(jmpbuf, 2); 
  } 
  printf("End task\n"; 
}
 

你可能感兴趣的:(C++,c,C#)