0% found this document useful (0 votes)
14 views6 pages

Signals Test

The document contains C code examples demonstrating the use of signals and signal handling in Linux. It includes programs that set signal handlers for common signals like SIGINT and SIGALRM. It shows how to use functions like signal, alarm, pause to install and generate signals. It also demonstrates changing the signal handler from within the handler using signal and concepts like signal masking.

Uploaded by

Mehul Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Signals Test

The document contains C code examples demonstrating the use of signals and signal handling in Linux. It includes programs that set signal handlers for common signals like SIGINT and SIGALRM. It shows how to use functions like signal, alarm, pause to install and generate signals. It also demonstrates changing the signal handler from within the handler using signal and concepts like signal masking.

Uploaded by

Mehul Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

header.

h
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/sem.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<pthread.h>
pause.h
#include<stdio.h>
main()
{
printf("pid = %d\n",getpid());
printf("hello\n");
pause();
printf("bye\n");
}

Alarm1
#include"header.h"
main()
{
printf("Hello....\n");
alarm(10);
printf("hai\n");
while(1);
}
Alarm2
#include"header.h"
void my_isr(int n)
{
printf("In isr....n=%d\n",n);
alarm(1);
sleep(10);
printf("after alarm\n");
//
signal(SIGALRM,SIG_DFL);
}
main()
{
signal(SIGALRM,my_isr);
printf("Hello....\n");
alarm(2);
sleep(2);
printf("after alarm parent\n");
alarm(1);
printf("hai\n");
while(1);
}
alarm3
#include"header.h"
main()
{
int r;
printf("hello\n");
r=alarm(10);
printf("1) r=%d\n",r);
//
sleep(3);
r=alarm(2);
printf("2) r=%d\n",r);
printf("hai\n");
while(1);
}

signal.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
printf("in isr n=%d\n",n);
}
main()
{
signal(SIGINT,my_isr);
printf("hello\n");
}

signal2.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
printf("in isr n=%d\n",n);
}
main()
{
signal(SIGINT,my_isr);
printf("hello %d\n",getpid());
pause();
printf("hai\n");
}

signal3.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
printf("in isr n=%d\n",n);
}
main()
{
signal(SIGINT,SIG_IGN);
printf("hello\n");
while(1);
printf("hai\n");
}
signal4.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
printf("in isr n=%d\n",n);
signal(SIGINT,SIG_DFL);
}
main()
{
signal(SIGINT,my_isr);
printf("hello\n");
while(1);
printf("hai\n");
}

signal5.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
printf("in isr n=%d\n",n);
signal(SIGINT,my_isr);
}
main()
{
signal(SIGINT,my_isr);
printf("hello\n");
while(1);
}

signal6.c
#include<stdio.h>
#include<signal.h>
void my_isr(int );
main()
{
signal(SIGINT,my_isr);
printf("hello\n");
while(1);
printf("hai\n");
}
void my_dir(int n)
{
printf("in dir n=%d\n",n);
signal(SIGINT,main);
}
void my_isr(int n)
{
printf("in isr n=%d\n",n);
signal(SIGINT,my_dir);
}

signal7.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
static int i;
printf("in isr n=%d\n",n);
i++;
if(i==3)
signal(SIGINT,SIG_DFL);
}
main()
{
signal(SIGINT,my_isr);
printf("hello\n");
while(1);
}
signal8.c
#include<stdio.h>
#include<signal.h>
void my_isr(int n)
{
printf("in isr....\n");
sleep(10);
printf("after sleep in isr....\n");
}
main()
{
signal(2,my_isr);
while(1);
}

You might also like