lip unit iv

54
Unit IV Process

Upload: ramaramadevi

Post on 03-Jun-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 1/54

Unit IV

Process

Page 2: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 2/54

Process• A process is the execution of a program

• A process is consists of text (machine code), data andstack

• Many process can run simultaneously as kernel

schedules them for execution• Several processes may be instances of one program

• A process reads and writes its data and stack sections,but it cannot read or write the data and stack of other

processes• A process communicates with other processes and the

rest of the world via system calls

Page 3: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 3/54

• Kernel has a process table that keeps tract of

all active processes

• Each entry in the process table containspointers to the text, data, stack and the U Area

of a process.

•All processes in UNIX system, except the veryfirst process (process 0) which is created by the

system boot code, are created by the fork 

system call

Page 4: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 4/54

Kernel Support for Process

UNIX Process Data Structure

Text

Stack

DataFile Descriptor Table

Per Process Region Table

Kernel ProcessTable

Kernel Region

TableA Process

U Area

Page 5: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 5/54

Data Structure of Parent & Child

Processes after Fork

U Area

U Area

stack

data

text

stack

data

Kernel File

Table

Kernel Region

Table

Parent

Child

Region

table

Region

table

Page 6: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 6/54

• When a process is created by fork, it contains duplicated

copies of text, data & stack segment of its parent.

• Also it has a file descriptor table that contains references

to opened file by its parent.• Along with this, process is assigned following attributes

which are inherited by parent or set by kernel. Real User Identification Number (rUID)

Real Group Identification Number (rGID) Effective User Identification Number (eUID)

Effective Group Identification Number (eGID)

Process Group Identification Number (PGID)

Current Directory

In addition to these attributes are different betweenparent & child processes Process Identification Number (PID)

Parent Process Identification Number (PPID)

Pending Signals 

Root Directory

Signal HandlingSignal Mask

Umask

Nice Value

Controlling Device

Alarm Clock

File Locks

Page 7: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 7/54

Page 8: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 8/54

Process APIs

fork, vfork•

The fork system call is used to create a child process.The function prototype is

#include <sys/types.h>

pid_t fork( void );

• The call succeeds. A child process is created and the

function returns child process ID to the parent. The child

process receives a zero return value from fork.

The call fails. No child process is created and functionsets errno with error code & returns -1.

• The common errors are ENOMEM (Insufficient Memory)

and EAGAIN (No of processes exceed system imposed

limit, try again later).

Page 9: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 9/54

#include <iostream.h>

#include <stdio.h>

#include <unistd.h>

int main(){

pid_t child_pid;

cout<<“PID:”<<getpid()<<“, Parent:”<<getppid(); 

switch(child_pid=fork()) {

case (pid_t)-1 : perror(“Fork”); break; 

case (pid_t)0 : cout<<“Child Created: PID”

<<getpid()<<“Parent:”<<getppid()<<endl;

exit(0);

default : cout<<“Parent after fork. PID :”<<getpid() <<“ChildPID: “ <<child_pid<<endl; 

}

return 0;

}

Page 10: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 10/54

• An alternative API is vfork with same prototype

#include <sys/types.h>

pid_t vfork( void );

• The vfork() function is the same as fork() except that it

does not make a copy of the address space.

• The memory is shared reducing the overhead of

spawning a new process with a unique copy of all thememory.

• This is typically used when using fork() to exec() a

process and terminate.• The vfork() function also executes the child process first

and resumes the parent process when the child process

calls exec or terminates by calling _exit.

i

Page 11: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 11/54

 _exit• The _exit system call terminates a process.

• This API will cause calling process data segment, stack

segment and U area to be deallocated and all open file

descriptors to be closed.

• The process table slot entry for this process is still intact

so that the process exit status and its execution statusare recorded therein.

• The process is now called zombie process, as it can no

longer scheduled to run.

• A zombie process or defunct process is a process that

has completed execution but still has an entry in

the process table. This entry is still needed to allow the

parent process to read its child's exit status.

Page 12: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 12/54

• An orphan process is a computer process

whose parent process has finished or

terminated, though it remains running itself.

• The data stored in process table entry can be

retrieved by the parent process using wait or

waitpid system call.#include<unistd.h>

void _exit (int exit_code);

Page 13: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 13/54

wait, waitpid

• The parent process will often want to wait

until all child processes have beencompleted/terminated.

• This can be implemented with the wait()

waitpid() function call.• These call will deallocate Process Table slot of

the child process. The prototypes are

#include<sys/wait.h>pid_t wait (int *status_p);

pid_t waitpid (pid_t child_pid, int

*status_p, int options);

Page 14: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 14/54

• wait: Blocks calling process until a signal is sent

to the process or the child process terminates or

is stopped. If child process has alreadyterminated, the wait call returns immediately.

• If the calling process has multiple child

processes, the function returns when onereturns.

• waitpid: this is more general than wait.

•Options available to block calling process for aparticular child process not the first one by

specying one of the following values for the

child_pid argument.

Page 15: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 15/54

• If pid  is equal to -1, status is requested for any child

process. In this respect, waitpid() is equivalent to wait().

• If pid  is greater than 0, it specifies the process ID of a

single child process for which status is requested.

• If pid  is 0, status is requested for any child process

whose process group ID is equal to that of the calling

process.• If pid  is less than -1, status is requested for any child

process whose process group ID is equal to the absolute

value of pid .

Page 16: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 16/54

• status_p specifies the location to which the child

process' exit status is stored. If NULL is passed, no exit

status is returned. Otherwise, the following macros

defined in <sys/wait.h> can be used to evaluate the

returned status:

• WIFEXITED(status_p) : Returns a nonzero value if child

was terminated via _exit otherwise zero.• WEXITSTATUS(status_p) Returns a child exit code that

was assigned to an _exit call

• WIFSIGNALED(status_p) Returns a nonzero value if a

child was terminated due to signal interruption.

• WTERMSIG(status_p) If the value of WIFSIGNALED(s) is

non-zero, this macro evaluates to the number of the

signal that caused the termination of the child process.

Page 17: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 17/54

• WIFSTOPPED(status_p) Evaluates to a non-zero value if

status was returned for a child process that is currently

stopped.

• WSTOPSIG(status_p) If the value of WIFSTOPPED(s) is non-

zero, this macro evaluates to the number of the signal that

caused the child process to stop.

• options is the bitwise OR of zero or more of the following

flags, defined in <sys/wait.h>:

• WNOHANG The waitpid() function does not suspend

execution of the calling thread if status is not immediately

available for one of the child processes specified by pid .

• WNOTRACED The status of any child processes specified

by pid  that are stopped, and whose status has not yet been

reported since they stopped, is also reported to the

requesting thread.

Page 18: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 18/54

RETURN VALUES

• If waitpid() was invoked with WNOHANG set in options, and

there are children specified by pid  for which status is not

available, waitpid() returns 0.• If WNOHANG was not set, waitpid() returns the process ID of a

child when the status of that child is available.

• Otherwise, it returns -1 and sets errno to one of the following

values: – ECHILD The process or process group specified by pid  does

not exist or is not a child of the calling process.

 – EFAULT stat_loc is not a writable address.

 – EINTR The function was interrupted by a signal. The value ofthe location pointed to by stat_loc is undefined.

 – EINVAL The options argument is not valid.

Page 19: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 19/54

exec• The exec family of functions will change calling process

context and initiate a program from within a program.

•There are six versions of exec system call. They have samefunction but they differ from each other in their

argument.

• The exec family of functions creates a new process image

from a regular, executable file. This file is either an

executable object file, or an interpreter script.

• There is no return from a successful call to an exec()

function, because the calling process is functionallyreplaced by the new process.

• The exec() functions only return if an error has have

occurred. The return value is -1, and errno is set to

indicate the error.

Page 20: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 20/54

• The prototypes of exec functions are

#include <unistd.h>

int execl(const char * path, const char*arg, ...);int execlp(const char * file, const char*arg,…); 

int execle(const char * path, const char *arg, …,

const char** env );int execv(const char * path, const char** argv   ,…);

int execvp(const char * file, const char** argv , …); 

int execve(const char * path, const char** argv ,

…, const char**env );

Page 21: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 21/54

• The first argument to the function is either path

name or file name of a program to be executed,

this should be an executable file or shellscript(excelp & execvp).

• If call succeeds, the calling process instruction

and data memory are overloaded with newprogram instruction text & data.

• When a new program completes execution, the

process is terminated & exit code will be send toits parent process.

Page 22: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 22/54

PARAMETERS•   path Specifies the path name of the new process image

file.

•   file Is used to construct a path name that identifies the

new process image file. If it contains a slash character, the

argument is used as the path name for this file.

Otherwise, the path prefix for this file is obtained by asearch of the directories in the environment

variable PATH .

•   arg0 , ..., argn Point to null-terminated character strings.

These strings constitute the argument list for the newprocess image. The list is terminated by a NULL pointer.

• The argument arg0 should point to a file name that is

associated with the process being started by

the exec() function.

Page 23: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 23/54

• argv is the argument list for the new process image.

This should contain an array of pointers to character

strings, and the array should be terminated by

a NULL pointer. The value in argv[0] should point to afile name that is associated with the process being

started by the exec() function.

• envp Specifies the environment for the new process

image. This should contain an array of pointers to

character strings, and the array should be terminated

by a NULL pointer.

Page 24: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 24/54

• The const  char  *arg and subsequent ellipses in the

execl(), execlp(), and execle() functions can be thought

of as arg0, arg1, ..., argn.

•  Together they describe a list of one or more pointers to

null-terminated strings that represent the argument list

available to the executed program.

• The first argument, by convention, should point to thefilename associated with the file being executed.

• The list of arguments must  be terminated by a NULL

pointer, and, since these are variadic functions

( variadic function is a function of indefinite arity, i.e.,

one which accepts a variable number of arguments),

this pointer must be cast (char  *) NULL.

Page 25: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 25/54

• The execv(), execvp(), and execvpe() functions provide

an array of pointers to null-terminated strings that

represent the argument list available to the new

program.

• The first argument should point to the filename

associated with the file being executed.

The array of pointers must  be terminated by a NULLpointer.

• The execle() and execvpe() functions allow the caller to

specify the environment of the executed program via

the argument envp.

• The envp argument is an array of pointers to null-

terminated strings and must  be terminated by a NULL

pointer.

Page 26: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 26/54

#include <unistd.h> //common to all programs

main()

{

execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

// (char *) 0 can be replace with NULL

}

main()

{

char *args[] = {"/bin/ls", "-r", "-t", "-l", (char *) 0 };

execv("/bin/ls", args);}

Page 27: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 27/54

main()

{

execlp(“ls", “ls", "-r", "-t", "-l", NULL);

}

main()

{

char *const parmList[] = {"/bin/ls", "-l",

"/u/userid/dirname", NULL};

char *const envParms[2] = {"STEPLIB=SASC.V6.LINKLIB",

NULL};execve("/u/userid/bin/newShell", parmList, envParms);

}

Page 28: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 28/54

system• The system call will execute an OS shell command as

described by a character command string.

• This function is implemented using fork, execand waitpid.

• The command string is executed by calling

/bin/sh -c command-string.

• Example

#include <stdio.h>

#include <stdlib.h>

main(){

system("ls -l");

printf("Command done!");

}

Page 29: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 29/54

Signals

Page 30: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 30/54

• Signals are sometimes called “software interrupts”. 

• Signals are triggered by events and are posted on a

process to notify it that something has happened and

requires some action.

• For ex:- divide-by-zero, <Delete> or <Ctrl-C> key, etc.

• A signal is an asynchronous event which is delivered to

a process.• Asynchronous means that the event can occur at any

time. The process does not know ahead of time exactly

when a signal will occur.

 Signal can be sent by one process to anotherprocess (or to itself) or by the kernel to a process.

 may be unrelated to the execution of the process

 e.g. user types ctrl-C, or the modem hangs.

Page 31: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 31/54

Signal Name UseCore File

generated

at default

SIGALRM Alarm timer time-out. Generated by alarm( ) API. No

SIGABRT Abort process execution. Generated by abort( ) API. Yes

SIGFPE Illegal mathematical operation. Yes

SIGHUP Controlling terminal hang-up. No

SIGILL Execution of an illegal machine instruction. Yes

SIGINTProcess interruption. Can be generated by

<Delete> or <ctrl_C> keys. No

SIGKILLSure kill a process. Can be generated by

“kill  -9 <process_id >” command.Yes

SIGPIPE Illegal write to a pipe. Yes

SIGQUIT Process quit. Generated by <crtl_\> keys. Yes

Signals are defined as integer flags, and the <signal.h>

header depicts the list of signals defined for UNIX.

The table below lists the POSIX defined signals.

Page 32: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 32/54

Signal Name Use

Core File

generated

at default

SIGSEGV

Segmentation fault. generated by de-referencing a

NULL pointer. Yes

SIGTERMprocess termination. Can be generated by

“kill <process_id>” command. Yes

SIGUSR1 Reserved to be defined by user. No

SIGUSR2 Reserved to be defined by user. No

SIGCHLDSent to a parent process when its child process has

terminated.No

SIGCONT Resume execution of a stopped process. No

SIGSTOP Stop a process execution. No

SIGTTINStop a background process when it tries to read from

from its controlling terminal.No

SIGTSTP Stop a process execution by the control_Z keys. No

SIGTTOUT

Stop a background process when it tries to write to its

controlling terminal. No

Page 33: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 33/54

Sources for Generating Signals

* Hardware  - A process attempts to access addresses

outside its own address space.- Divides by zero.

* Kernel - Notifying the process that an I/O device for

which it has been waiting is available.* Other Processes - A child process notifying its parent

process that it has terminated.

* User - Pressing keyboard sequences that generate a

quit, interrupt or stop signal.

Page 34: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 34/54

• When a signal is sent to a process, it is pending on the

process to handle it.

• Process that receives a signal can take one of three

action:

* Perform the system-specified default for the signal

- notify the parent process that it is terminating;- generate a core file

(a file containing the current memory image of the process)

- terminate.

* Ignore the signal :- A process can do ignoring with all signal but

two special signals: SIGSTOP and SIGKILL.

* Catch the Signal :- When a process catches a signal, except

SIGSTOP and SIGKILL, it invokes a special signal handing routine.

Handling Signals

signal API

Page 35: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 35/54

signal API

• All UNIX systems and ANSI-C support the signal API,

which can be used to define the per-signal handling

method.

• The prototype of the signal API is

void (*signal(int signo, void(*handler)(int) ) ) (int);

• Or in simple format

#include <signal.h>

typedef void sighandler_t(int);

sighandler_t *signal( int signo, sighandler_t *handler );signal returns a pointer to a function that returns an int (i.e. it

returns a pointer to sighandler_t)

• RETURN VALUE :- The signal() function returns previous

value of the signal handler, or SIG_ERR on error.

Page 36: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 36/54

Signal Handling

• Use the signal handling library: signal.h

• Then can use the signal call:

#include <signal.h>

void (*signal( int sig, void (*handler)(int))) (int) ;

• signal returns a pointer to the PREVIOUS signalhandler

• #include <signal.h>

typedef void Sigfunc(int);

Sigfunc *signal( int signo, Sigfunc *handler );

Signal is a functionthat takes twoarguments:sig and handler

The signal to becaught or ignoredis given as argumentsig

The function to be calledwhen the specified signalis received is given as apointer to the functionhandler

The handler  functionReceives a single integerArgument and returns void

The signal function itselfreturns a pointer to a function.The return type is the same

as the function that is passed in,i.e., a function that takes anint and returns a void

The returned functiontakes a integerparameter.

Si l M k i k API

Page 37: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 37/54

 Signal Mask - sigprocmask API• Each process has a system mask that defines which signals

are blocked when generated to a process.

• A blocked signal depends on the recipient process to

unblock it and handle it accordingly.

• A process initially inherits the parent’s signal mask when it is

created, but any pending signals for the parent process arenot passed on.

• A process can query or set its signal mask via sigprocmask  

API.

#include<signal.h>

int sigprocmask  ( int cmd, cost sigset_t *new_mask,

sigset_t *old_mask);

• The return value is zero if it succeeds or -1 if it fails.

Page 38: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 38/54

•  new_mask : defines a set of signals to be set or reset in a

calling process signal mask.

new_mask = NULL, current process signal mask unaltered.

•  cmd : specifies how the new_mask value is to be used:

- SIG_SETMASK: Overrides the calling process signal mask

with the value specified in the new_mask argument.

- SIG_BLOCK: Adds the signals specified in the new_maskargument to the calling process signal mask.

- SIG_UNBLOCK: Removes the signals specified in the

new_mask argument from the calling process signal mask

•  old_mask : Address of a sigset_t variable that will beassigned the calling processing’s original signal mask. 

old_mask = NULL, no previous signal mask will be return.

The BSD UNIX & POSIX 1 define a set of API known as sigsetops

Page 39: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 39/54

  The BSD UNIX & POSIX.1 define a set of API known as sigsetops

functions, which set, reset and query the presence of signals in

a sigset_t typed variable

* int sigemptyset (sigset_t* sigmask);

Clears all signal flags in the sigmask argument.

* int sigaddset (sigset_t* sigmask, const int signal_num);

Sets the flag corresponding to the signal_num signal in the

sigmask argument.

* int sigdelset (sigset_t* sigmask, const int signal_num);Clears the flag corresponding to the signal_num signal in the

sigmask argument.

* int sigfillset(sigset_t* sigmask);

Sets all the signal flags in the sigmask argument.These functions returns zero if call succeed, -1 if they fails.

* int sigismember(const sigset_t* sigmask,const int signal_num);

Returns 1 if the flag corresponding to the signal_num signal in

the sigmask argument is set; zero if it is not set; -1 if call fails.

This program checks whether the SIGINT signal is present in

Page 40: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 40/54

This program checks whether the SIGINT signal is present in

a process signal mask and adds it to the mask if it is not

there. It clears the SIGSEGV signal from the process signal

mask.int main( ){

sigset_t sigmask;

sigemptyset(&sigmask); /*initialize set */

if(sigprocmask(0,0,&sigmask)==-1)/*get current signal mask*/

{ perror(“sigprocmask”); exit(1); }

else sigaddset(&sigmask, SIGINT); /* set SIGINT flag*/

sigdelset(&sigmask, SIGSEGV); /* clear SIGSEGV flag */

if (sigprocmask(SIG_SETMASK,&sigmask,0) == -1)

perror(“sigprocmask”); /* set a new signal mask */

}

sigpending API

Page 41: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 41/54

sigpending API

• The sigpending API can find out whether one or more

signals are pending for a process and set up special

signal handing methods for those signals before the

process calls the sigprocmask API to unblock them.

# Include<signal.h>

int sigpending (sigset_t* sigmask);

• sigmask argument, to the sigpending API, is the

address of a sigset_t-type variable and is assigned the

set of signals pending for the calling process by the API.

• The API returns 0 on Success; -1 on Failure and sets

errno.

Page 42: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 42/54

/* This program reports whether the SIGTERM signal is

pending for the process. */

int main ( ) {

sigset_t sigmask;

sigemptyset(&sigmask); /* initialize set */

if (sigpending(&sigmask) == -1)

/* Any signal is pending */

perror(“sigpending”); 

else

cout<<”SIGTERM signal is:” <<

(sigismember (&sigmask,SIGTERM) ? “Set” : “No Set”);

}

sigaction API

Page 43: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 43/54

sigaction API• The sigaction API is a replacement for the signal API in

latest UNIX & POSIX systems.

• The sigaction API setups a signal handling method for

each signal it wants to deal with and passes back the

previous signal handling method for a given signal.

•The sigaction API blocks the signal it is catching allowinga process to specify additional signals to be blocked

when the API is handling a signal.

include<signal.h>

int sigaction ( int signal_num, struct sigaction* action,

struct sigaction* old_action);

• The API returns 0 on Success,

-1 on Failure and sets errno.

Page 44: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 44/54

struct sigaction

{

void (*sa_handler) (int);

sigset_t sa_mask;int sa_flag;

}

sa_handler is the function point of a user-definedsignal handler function, SIG_IGN (ignores a signal), or

SIG_DFL (accepts the default action of a signal).

• sa_mask specifies additional signals that a process

wishes to block when it is handling the signal_numsignal.

• sa_flag specifies special handling for certain signals.

Th l f fl f ll

Page 45: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 45/54

The values for sa_flag are as follows

0: when the signal_num is SIGCHLD, the kernel will send the

SIGCHLD signal to the calling process whenever its child process

is either terminated or stopped.SA_NOCLDSTOP: when the signal_num is SIGCHLD, the kernel will

generate the SIGCHLD signal to the calling process whenever its

child process is either terminated, but not when the child

process has been stopped.

SA_RESETHAND: If signal_num is caught, the sa_handler is set toSIG_DFL before the signal handler function is called, and

signal_num will not be added to the process signal mask

SA_RESTART: If a signal is caught while a process is executing a

system call, the kernel will restart the system call after thesignal handler returns. If this flag is not set in the sa_flag, after

the signal handler returns, the system call will be aborted with a

return value of -1 and will set errno to EINTR (aborted due to a

signal interruption).

Page 46: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 46/54

* signal_num :- The signal_num argument designates which signalhandling action is defined in the action argument.

* old_action :- The previous signal handling method for signal_num

will be returned via the old_action argument if it is not a NULLpointer.

* Action :- action argument sets up a signal handling method forthe signal_num argument.

If the action argument is a NULL pointer, the calling process’s

existing signal handling method for signal_num will beunchanged.

killAPI

Page 47: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 47/54

killAPI• A process may send a signal to another process,

including itself, by calling kill.

• The kill  function shall send a signal to a process or a

group of processes specified by pid .

• The call will fail if the program doesn’t have permission

to send the signal, often because the target process isowned by another user.

• For a process to have permission to send a signal to a

process designated by pid , unless the sending process

has appropriate privileges, the real or effective user ID of

the sending process shall match the real or effective user

ID of the receiving process or sender process has

superuser privileges.

#i l d / h

Page 48: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 48/54

#include <sys/types.h>

#include <signal.h>

int kill(pid_t pid, int sig);

• The possible pid values are

• > 0 send signal to process pid

• 0 send signal to all processes whose process group ID

equals the sender’s pgid. e.g. parent kills all children 

• -1: Sends signal_num to all processes whose real user

ID is the same as the effective user ID of the calling

process.• Return 0 if ok, -1 on error.

raise API

Page 49: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 49/54

raise API• The raise function sends a signal to the calling process

#include <signal.h>

int raise(int sig); 

• It is equivalent to kill(getpid(), sig);

• raise() returns 0 on success, and nonzero for failure.

alarm API

Page 50: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 50/54

alarm API• Signals provide a useful alarm clock facility.

• The alarm function call can be used by a process to

schedule a SIGALRM signal at some time in the future.

#include <signal.h>

unsigned int alarm(unsigned int time_interval);

• The alarm call schedules the delivery of a SIGALRM

signal in seconds.

• In fact, the alarm will be delivered shortly after that, due

to processing delays and scheduling uncertainties.• A value of 0 will cancel any outstanding alarm request.

C lli l b f th i l i i d ill

Page 51: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 51/54

• Calling alarm before the signal is received will cause

the alarm to be rescheduled.

• Each process can have only one outstanding alarm.

• Alarm returns the number of seconds left before any

outstanding alarm call would be sent, or -1 if the call

fails. 

pause API

Page 52: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 52/54

pause API• pause function simply causes the program to suspend

execution until a signal occurs.

• When it receives a signal, any established handler is run

and execution continues as normal. It’s declared as 

#include <unistd.h>

int pause(void);• returns -1 (if the next received signal doesn’t cause the

program to terminate) with errno set to EINTR when

interrupted by a signal.

abort API

Page 53: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 53/54

abort API• The abort first unblocks the SIGABRT signal, and then

raises that signal for the calling process.

• This results in the abnormal termination of the processunless the SIGABRT signal is caught and the signal

handler does not return.

• If the abort function causes process termination, all

open streams are closed and flushed.

• If the SIGABRT signal is ignored, or caught by a handler

that returns, the abort function will still terminate the

process.#include <stdlib.h>

void abort(void);

• The abort() function never returns.

sleep API

Page 54: LIP Unit IV

8/12/2019 LIP Unit IV

http://slidepdf.com/reader/full/lip-unit-iv 54/54

sleep API• sleep function suspends a process for fixed amount of

time, makes the calling process sleep until time

(seconds) have elapsed or a signal arrives which is notignored.

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

• Return Value :- Zero if the requested time has elapsed,

or the number of seconds left to sleep, if the call was

interrupted by a signal handler.