lab5 Pipe Signals
  • Introduction
  • Signals
    • kill() system call
    • Custom signal handler
  • Knowing about pipe
    • Pipe Overview
    • System call pipe()
  • Learn to use pipe
    • Pipe Creation
    • Pipe with fork
    • Exercise 1
  • Connecting two programs with pipe
    • Implement ls | less in C
  • Shell Examples
    • Get Current Path
    • Change Directory
    • Change Environment Variables
  • Reference
Powered by GitBook
On this page

Was this helpful?

  1. Signals

Custom signal handler

You can override the default signal handler to IGN (ignore) or a custom handler. This is useful if you would like your program to react for certain kind of signals.

The program below sets the signal handler of SIGINT to SIG_IGN. The OS will ignore the signal so it has no response to CTRL-C.

/* Signals/sigign.c */
#include <stdio.h>
#include <signal.h>
int main(int argc,char *argv[])
{
    signal(SIGINT,SIG_IGN);
    printf("Put into while 1 loop..\n");
    while(1) { }
    printf("OK!\n");
    return 0;
}

To define a custom signal handler, you can create a function and set it with signal().

sighandler_t signal(int signum, sighandler_t handler);
/* Signals/custom.c */
#include <stdio.h>
#include <signal.h>

void handler(int signal)
{
    printf("Signal %d Received.Kill me if you can\n",signal);
}

int main(int argc,char *argv[])
{
    signal(SIGINT,handler);
    printf("Put into while 1 loop..\n");
    while(1) { }
    printf("OK!\n");
    return 0;
}
Previouskill() system callNextKnowing about pipe

Last updated 5 months ago

Was this helpful?