Thursday, January 14, 2016

                                                      Signal Handling
1) Signal handling is one of the way of Inter Process Communication in which one process can notify the other process by sending the signal but in case of signal handling we can not send actual data to the other process.

2) In case of signal handling signal is pass through the operating system and operating system will forward that signal to the receiver process.

3) Every signal is identified by unique number which is called as signal number.

4) To perform signal handling we have to use two function as : i) signal( ) and ii) kill( ).

     i) signal( ) function is use to decide the action which is to send the signal to any of the process whose pid should be pass as parameter.

    ii) kill( ) function is use to send the signal to the any process.

5) By using signal handling we can send signal to any process which can be related or not.

--------------------------------------------------------------------------------------------------------------------------
A program which creates two process as sender and receiver in which sender process send signal to receiver process.
--------------------------------------------------------------------------------------------------------------------------
Program Name : Sender.c
--------------------------------------------------------------------------------------------------------------------------

#include  <stdio.h>
#include  <sys/types.h>
#include  <signal.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#include<stdlib.h>

int main()
{
pid_t   pid;
key_t MyKey;
int   ShmID;
pid_t *ShmPTR;
char  line[100], c;
int   i;

MyKey   = ftok(".", 's');      

ShmID   = shmget(MyKey, sizeof(pid_t), 0666);

ShmPTR  = (pid_t *) shmat(ShmID, NULL, 0);

pid     = *ShmPTR;              

shmdt(ShmPTR);                  

while (1)
{                    
printf("Want to interrupt the other guy or kill it (i or k)? ");
gets(line);
for (i = 0; !(isalpha(line[i])); i++)
;
c = line[i];
if (c == 'i' || c == 'I') {
kill(pid, SIGINT);
printf("Sent a SIGINT signal\n");
}
else if (c == 'k' || c == 'K') {
printf("About to send a SIGQUIT signal\n");
kill(pid, SIGQUIT);  
printf("Done.....\n");
exit(0);
}
else
printf("Wrong keypress (%c).  Try again\n", c);
}
return 0;
}

/*
// OutPut :

./Sender

Want to interrupt the other guy or kill it (i or k)? k
About to send a SIGQUIT signal
Done.....
*/

--------------------------------------------------------------------------------------------------------------------------

Program Name : - Receiver.c
--------------------------------------------------------------------------------------------------------------------------

#include  <stdio.h>
#include  <sys/types.h>
#include  <signal.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#include<unistd.h>
#include<stdlib.h>

void  SIGINT_handler(int);
void  SIGQUIT_handler(int);

int   ShmID;
pid_t *ShmPTR;  

int main()
{
int   i;
pid_t pid = getpid();
key_t MyKey;

signal(SIGINT, SIGINT_handler);
signal(SIGQUIT, SIGQUIT_handler);

MyKey   = ftok(".", 's');  
ShmID   = shmget(MyKey, sizeof(pid_t), IPC_CREAT | 0666);

ShmPTR  = (pid_t *) shmat(ShmID, NULL, 0);

*ShmPTR = pid;            

for (i = 0; ; i++)
{      
printf("From process %d: %d\n", pid, i);
sleep(1);
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////

void  SIGINT_handler(int sig)
{
signal(sig, SIG_IGN);
printf("From SIGINT: just got a %d (SIGINT ^C) signal\n", sig);
signal(sig, SIGINT_handler);
}

void  SIGQUIT_handler(int sig)
{
signal(sig, SIG_IGN);
printf("From SIGQUIT: just got a %d (SIGQUIT ^\\) signal"
" and is about to quit\n", sig);
shmdt(ShmPTR);
shmctl(ShmID, IPC_RMID, NULL);

exit(3);
}

/*
 OutPut :

./Receiver

From process 3720: 0
From process 3720: 1
From process 3720: 2
From process 3720: 3
From process 3720: 4
From process 3720: 5
From process 3720: 6
From process 3720: 7
From process 3720: 8
From process 3720: 9
From process 3720: 10
From SIGQUIT: just got a 3 (SIGQUIT ^\) signal and is about to quit

*/
--------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2

    SAVITIBAI PHULE UNIVERSITY OF PUNE S. Y. B.Sc. (Computer Science) Semester III Practical Examination       SUBJECT: CS-233 Practical cou...