Catching SIGINT signal to terminate a custom shell

前端 未结 3 1131
被撕碎了的回忆
被撕碎了的回忆 2021-01-25 02:37

Hope you can help me to resolve this problem.

For school I have to transform Ctrl+C to a command which doesn\'t shut down the shell, but he reminds through

3条回答
  •  一生所求
    2021-01-25 02:49

    Ctrl+C sends an interrupt signal (SIGINT) to the running process.You can use signal() to catch SIGINT like this:

     #include
     #include
    
     void sigint_handler(int sig)
     {
       printf("Type exit to close the shell!\n");
     }
    
    
      int main()
      {
        signal(SIGINT, sigint_handler);
    
        /*Your code should replace the while loop.*/
        while(1)
        {
            printf("Running!\n");
            getchar();
        }
    
        return 0 ;
      }
    

提交回复
热议问题