Featured Post

Monday, August 1, 2016

Signals in UNIX

   
                                      http://cjuschools.blogspot.in/


Before going into discussion lets start with the discussion interrupts.
What is an interrupt?
             
                      An interrupt is nothing but a message (or an action to be taken) which is sent to an operating system. For example when you are working with word document then ctrl+p key click will causes the operating system to suspend the present job and switches to printing job and after completion it resumes the original job from the point where it has stopped.


What is a signal?
                       
                       A signal is nothing but a message(or action to be taken) which is sent to a process by an operating system i..e., Signals are used by the operating system to notify processes that some event occurred.  The signals may be user requests or it can be a action to be happen'd  while accessing a illegal memory access etc. Pressing some keys like ctrl+c,  ctrl+d,  ctrl+\ etc from keyboard will send signals to a running process

                      Signals to be short are various notifications sent to a process in order to notify it of various events. Whatever the job the process is doing, forces it to stop execution temporarily and forces the signal code to be executed immediately.  Each signal has a signal number and associated symbolic name that is usually defined in /usr/include/signal.h or one of the files included by it directly or indirectly.
                   
   kill -l is used to see the list of signals supported by a system

  /home/anil/~$ kill -l

  1) SIGHUP   2) SIGINT 3) SIGQUIT 4) SIGKIL  5) SIGTRAP
  6) SIGABRT  7) SIGBUS  8) SIGFPE  9) SIGKILL 10) SIGUSR1
 11)-------------------------------------
-------------------------------------------------------------
     many more signal names are there
-------------------------------
/home/anil/~$


Each signal may have an associated signal handler, which is a function that gets executed when the process receives that signal. The signal handler function code gets executed only when a particular signal is raised, no other code can call this handler code i.e., this function is called as asynchronous mode".

Sending Signals To Processes:


Sending Signals using the keyboard:


 The common way of sending signals to processes is using the keyboard.

Ctrl+c     -----> sends INT signal(SIGINT) to the running process. By default this signal causes the process to immediately terminate.

Ctrl+x   ------> Sends TSTP signal(SIGTSTP) to the running process. By default this signal causes the process to suspend execution.

Ctrl+\   ------>Pressing this key causes the system to send a ABRT signal(SIGABRT) to the running process. By default this signal causes to immediately terminate.

Sending signal from the command line:
      
            Another way of sending signals to processes is done using kill command and raise command.

kill:
    The kill command accepts two parameters a signal name(or number) and a process ID.

Syntax: 
                         kill -<signal>  <PID>

  for example in order to send the INT signal to process with PID 5342, type

                   kill -INT 5342

The above kill example has the same affect as pressing Ctrl +c in the shell the runs that process.

     #include<sys/types.h>
     #include<signal.h>
     int kill(pid_t pid, int sig);

     If the process ID is positive, kill sends the specified signal to the processes with that pid. If pid is negative then kill sends the signal to the process group with group ID . If pid is zero then kill sends the message to all processes of the caller's process group.

raise
       Using raise system call a process can send a signal to itself. This command has only one argument, the signal parameter
   
                 #include<signal.h>

                 int raise(int signo);

/* to demonstrate the use of kill system call */
/*       killdemo.c               */

#include<stdio.h>
#include<sys.stat.h>
#include<signal.h>

void main()
{
 pid_t pid=getpid();   /* get present process id */

printf("\n %d \n",pid);

kill(pid,SIGSTOP); /* here am sending STOP signal to my own process id or if u want you can send
                                    signals to other processes if u know process ID*/

printf("\n %d \n",pid);    /* this statement not get executed as we already sent STOP signal to our                                                  process*/
}

output:

   /home/anil/~$ cc kill.c
   /home/anil/~$ ./a.out


             2352



             [1]+ Stopped      ./a.out

/home/anil/~$

                        UNIX PROGRAMMING


Unix operating system Linux operating system shell programming bourn shell kourne shell c shell difference between unix and linux operating system , history of UNIX, red hat operating system. UNIX OS architecture , kernel utility programs , file handling process handling inter process communication pipes shared memory message queues socket programming TCP/IP , UDP programming , One to One chat Application , Basic shell program to add two numbers , echo function , while loops clear gt lt le ge ne eq if fi while do done case array in unix strings , cut copy paste cat dir pwd getcwd chdir command read write stat lseek fstat flock mechanism.even or add program vi editor command ni vi editor command mode esc mode insert mode aa dd x :q :wq :w System calls. Pipes,mkdir,chdir,open read pipe,shmget,shmat ,msgctl,msgsnd msgrecv, system calls. signals kill command raise command kill system call raise system call. alarm system call, pause system call, stop system call , SIGABRT, AIGALRM, SIGFPE, SIGHUP,SIGILL, SIGINT, SIGKILL, SIGPIPE,SIGSEGV,SIGTERM,SIGUSR1, signal handlers,getpid().getppid(),#include<signal.h>       
                        

No comments:

Post a Comment