SlideShare a Scribd company logo
Linux System
Programming
Processes and Signals
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://youtube.com/rfchishti
https://sites.google.com/site/chishti
 Processes and Signals form a fundamental part of the Linux operating
environment. They control almost all activities performed by Linux computer
system.
 An understanding of how Linux manages processes will hold any systems
programmer, applications programmer or system administrator in good stead.
 we’ll look at how processes are handled in the Linux environment and how to
find out exactly what the computer is doing at any given time. How to start
and stop other processes from within our own programs, how to make
processes send and receive messages, and how to avoid zombies (in which
parents died before children).
 A process is defined as “an address space with one or more threads executing
within that address space, and the required system resources for those
threads.” (Single UNIX Specification, Version 2)
Introduction
 A process is a program in execution and can identified by its unique PID
(process identification) number.
 The kernel controls and manages processes. Multiple processes are running
and monitored by the Linux Kernel, allocating each of the processes a little
slice of the CPU in a way that is unnoticeable to the user.
Process Structure
 A process consists of the executable program, its data and stack, variables
(occupying system memory), open files (file descriptor) and an environment.
 Linux allows many users to access the system at the same time. Each user can
run many programs, or even many instances of the same program, at the same
time. The system itself runs other programs to manage system resources and
control user access.
Introduction
 Typically, Linux system will share code and system libraries between processes,
so that there is only one copy of the code in memory at any one time.
 For example: two users, Neil and rick both run the command “grep” on the
shell at the same time to look for different strings in different files. This
produces out two processes, each has it own process identifier (PID). And one
copy of program code of “grep” is loaded into the memory as read only, two
users share this code. And the system library is also shared (Shared Library).
Introduction
Figure: two processes with same shared libraries but different PID No.
Processes
What process is running?
 ps command: ps prints out the information about
 the active processes.
Explanation
 USER: User name;
 PID: Process identifier
 %CPU: cpu time percentage
 %MEM: % of memory occupied VSZ: virtual size of the process;
 RSS: resident pages and amount of shared pages; TTY: terminal identifier;
 STAT: process state, R means running, S means sleeping.
 START: starting time. TIME: cumulative execution time;
 COMMAND :command on shell.
Processes
pstree Command
 pstree is another way to see what process are
 running and what processes are child process.
Explanation:
 pstree command displays all process as a tree
 with its root being the first process that runs,
 called init. If a process creates more than one
 process of the same name
 pstree visually merges the identical branches by putting them in square
brackets and prefixing them with the number of times the process is repeated.
 In Linux each process is started by another process known as its parent
process. A process so started is known as a child process.
Processes
 When Linux starts, it runs a single program, the prime ancestor and process
number 1, init. So it is the grand parent of all processes.
 The Linux operating system determines the priority of a process based on a
“nice” value, which defaults to 0,
 You can set the process nice value using nice and adjust it using renice.
Command.
 Start a process with some priority $nice –n -10 ./a.out
 See the nice value (NI Column) of processes $ps –al
 The range of the value goes from -20 (highest priority) to 19 (lowest priority).
To Change value #renice 10 2073 Here 10 is new value and 2073 is PID of
process
Processes
Using the system function
 You can cause a program to run from inside another program and thereby
create a new process by using the system library function.
#include <stdlib.h>
int system (const char *string);
The system function runs the command passed to it as a string and waits for it
to complete.
 system returns 127 if a shell can’t be started to run the command and -1 if
another error occurs. Otherwise, system returns the exit code of the
command.
 Let’s compile and run the next code system1.c
Starting a New Process
 You The program calls system with the string “ps ax”, which executes the ps
program. The program returns from the call to system when the ps command
has finished.
 This program has to wait until the process started by the call to system
finishes, you can’t get on with other tasks.
 Now change “ps ax &“ to “ps ax &“ to run this command in background and
compile and run the program again.
Starting a New Process
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Running ps with systemn");
system("ps ax");
printf("Done.n");
exit(0);
}
system.c
 The call to system returns as soon as the shell command finishes. Because it’s
a request to run a program in the background.
 The shell returns as soon as the ps program is started, just as would happen if
you had typed $ ps ax & at a shell prompt.
 This program then prints Done and exits before the ps command has had a
chance to finish all of its output.
Starting a New Process
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("Running ps with systemn");
system("ps ax &");
printf("Done.n");
exit(0);
}
system2.c
Using the fork function
 fork function is used to create a new process.
 This system call duplicates the current process and creates a new entry in the
process table.
 The new process is almost identical to the original, executing the same code
but with its own data space, environment, and file descriptors.
 Combined with the exec functions, fork is all you need to create new
processes.
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
Creating a New Process
Creating a New Process
P1
P2
Fork()
Parent Process
fork returns
Child Process ID
Child Process
fork returns 0
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
pid_t pid;
printf("fork program startingn");
pid = fork();
switch(pid){
case -1: perror("fork failed"); break;
case 0: printf("We are childn"); break;
default: printf("We are parentn"); break;
}
exit(0);
}
Using fork function fork.c
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
pid_t pid; char *message; int n;
printf("fork program startingn");
pid = fork();
switch(pid){
case -1: perror("fork failed"); exit(1);
case 0: message = "This is the child "; n = 5; break;
default: message = "This is the parent"; n = 3; break;
}
for( ; n > 0 ; n-- ){
puts(message); printf("my pid is %d",getpid()); sleep(1);
}
exit(0);
}
Using fork function fork1.c
How it Works
 This program runs as two processes. A child is created and prints a message
five times.
 The original process (the parent) prints a message only three times. The
parent process finishes before the child has printed all of its messages, so the
next shell prompt appears mixed in with the output.
 When fork is called, this program divides into two separate processes.
 The parent process is identified by a nonzero return from fork and is used to
set a number of messages to print, each separated by one second.
 Sometimes, you want to wait for a child process to finish.
 For example, in the previous program, the parent finishes ahead of the child
and you get some messy output as the child continues to run.
Using fork function
 You can arrange for the parent process to wait until the child finishes before
continuing by calling wait.
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *stat_loc);
 The wait system call causes a parent process to pause until one of its child
processes is stopped.
 The call returns the PID of the child process
Using fork function
Waiting For a Process
Summary of wait activities.
wait()
Child Process
Present ?
return -1 and
set errno
wait for child---
Process terminated ?
return status value
and child PID
No
No
Yes
Yes
Block
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
pid_t pid; char *message;
int n; int exit_code;
printf("fork program startingn");
pid = fork();
switch(pid){
case -1: perror("fork failed"); exit(1);
case 0: message = "This is the child"; n = 5; exit_code = 37; break;
default: message = "This is the parent"; n = 3; exit_code = 0; break;
}
for( ; n > 0; n--)
{ puts(message); sleep(1); }
(1/2) Waiting For a Process wait.c
// This section of the program waits for the child process to finish.
if (pid != 0){
int stat_val;
pid_t child_pid; // pid_t is int data type
child_pid = wait(&stat_val);
printf("Child has finished: PID = %dn", child_pid);
if(WIFEXITED(stat_val)) // did the process exit normally?
printf("Child exited with code %dn", WEXITSTATUS(stat_val));
// What was the exit code of child process
else
printf("Child terminated abnormallyn");
}
exit(exit_code);
}
(2/2) Waiting For a Process wait.c
How it Works
 The parent process, which got a nonzero return from the fork call, uses the
wait system call to suspend its own execution until status information
becomes available for a child process.
 This happens when the child calls exit; we gave it an exit code of 37. The
parent then continues, determines that the child terminated normally by
testing the return value of the wait call, and extracts the exit code from the
status information.
 Using waitpid: you can use it to waitpid to wait for a specific process to
terminate. #include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *stat_loc, int options);
Waiting For a Process
 TheThe pid argument specifies the PID of a particular process to wait for.
 The status information is written to the location pointed to by stat_loc
 The options argument allows you to modify the behavior of waitpid. The most
useful option is WNOHANG, which prevents the call to waitpid from
suspending execution of the caller.
 You can use it to find out whether any child processes have terminated and, if
not, to continue. Other options are the same as for wait.
 So, if you wanted to have a parent process regularly check whether a specific
child process has terminated, you could use the call
waitpid(child_pid, (int *) 0, WNOHANG);
 This will return zero if the child has not terminated or stopped, or child_pid if it has. waitpid
will return -1 on error and set errno. This can happen if there are no child processes
Using waitpid
 TheThe pid argument specifies the PID of a particular process to wait for.
 The status information is written to the location pointed to by stat_loc
 The options argument allows you to modify the behavior of waitpid. The most
useful option is WNOHANG, which prevents the call to waitpid from
suspending execution of the caller.
 You can use it to find out whether any child processes have terminated and, if
not, to continue. Other options are the same as for wait.
 So, if you wanted to have a parent process regularly check whether a specific
child process has terminated, you could use the call
waitpid(child_pid, (int *) 0, WNOHANG);
 This will return zero if the child has not terminated or stopped, or child_pid if it has. waitpid
will return -1 on error and set errno. This can happen if there are no child processes
Using waitpid
 Using fork to create processes can be very useful, but you must keep track of
child processes. When a child process terminates, an association with its
parent survives until the parent in turn either terminates normally or calls
wait. The child process entry in the process table is therefore not freed up
immediately.
 Although no longer active, the child process is still in the system because its
exit code needs to be stored
 in case the parent subsequently calls wait. It becomes what is known as
defunct, or a zombie process.
 You can see a zombie process being created if you change the number of
messages in the fork example program. If the child prints fewer messages than
the parent, it will finish first and will exist as a zombie until the parent has
finished.
Zombie Process
 The child process entry in the process table is therefore not freed up
immediately. fork2.c is the same as fork1.c, except that the number of
messages printed by the child and parent processes is reversed. Here are the
relevant lines of code:
 How it works
 Run this program with ./fork2 & and then call the ps –aux | grep fork2
program after the child has finished but before the parent has finished, you’ll
see a child process a zombie (Z) or defunct.
Zombie Process
// fork2.c
switch(pid)
{
case -1: perror("fork failed"); exit(1);
case 0: message = "This is the child"; n = 3; break;
default: message = "This is the parent"; n = 5; break;
}
 If the parent then terminates abnormally, the child process automatically gets
the process with PID 1 (init) as parent.
Zombie Process
 If yhere is a whole family of related functions grouped under the exec heading.
They differ in the way that they start processes and present program
arguments.
 An exec function replaces the current process with a new process specified by
the path or file argument. You can use exec functions to “hand off” execution
of your program to another.
 For example, you could check the user’s credentials before starting another
application that has a restricted usage policy.
 The exec functions are more efficient than system because the original
program will no longer be running after the new one is started.
Replacing a Process Image
#include <unistd.h>
char **environ;
int execl(const char *path, const char *arg0, ..., (char *)0);
int execlp(const char *file, const char *arg0, ..., (char *)0);
int execle(const char *path, const char *arg0, ..., (char *)0, char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);
 These functions belong to two types. execl, execlp, and execle take a variable
number of arguments ending with a null pointer.
 execv and execvp have as their second argument an array of strings. In both
cases, the new program starts with the given arguments appearing in the argv
array passed to main.
Replacing a Process Image
 These functions are usually implemented using execve, though there is no
requirement for it to be done this way.
 The functions with names suffixed with a p differ in that they will search the
PATH environment variable to find the new program executable file. If the
executable isn’t on the path, an absolute filename, including directories, will
need to be passed to the function as a parameter.
 The global variable environ is available to pass a value for the new program
environment. Alternatively, an additional argument to the functions execle
and execve is available for passing an array of strings to be used as the new
program environment.
 If you want to use an exec function to start the ps program, you can choose from
among the six exec family functions, as shown in the calls in the code fragment that
follows:
Replacing a Process Image
#include <unistd.h>
/* Example of an argument list Note that we need a program name for argv[0] */
char *const ps_argv[] = {“ps”, “ax”, 0};
/* Example environment, not terribly useful */
char *const ps_envp[] = {“PATH=/bin:/usr/bin”, “TERM=console”, 0};
/* Possible calls to exec functions */
execl(“/bin/ps”, “ps”, “ax”, 0); /* assumes ps is in /bin */
execlp(“ps”, “ps”, “ax”, 0); // assumes /bin is in PATH
execle(“/bin/ps”, “ps”, “ax”, 0, ps_envp); /* passes own environment */
execv(“/bin/ps”, ps_argv);
execvp(“ps”, ps_argv);
execve(“/bin/ps”, ps_argv, ps_envp);
Using exec Family
 The program prints its first message and then calls execlp, which searches the directories
given by the PATH environment variable for a program called ps. It then executes this
program in place of the pexec program.
 When ps finishes, you get a new shell prompt. You don’t return to pexec, so the second
message doesn’t get printed. The PID of the new process is the same as the parent PID.
Using exec Family
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("my pid is %dn",getpid());
printf("Running ps with execlpn");
execlp("ps", "ps", "ax", (char *)0);
// run again for top command and check process id with
// ps –aux | grep username | grep name_of_binary
printf("Done.n");
exit(0);
}
execlp.c
 A signal is an event generated by the Linux/UNIX system in response to some
condition, upon which a process may in turn take some action.
 Signals are generated by some error conditions, such as memory segment
violations, floating point processor errors or illegal instructions. They are
generated by the shell and terminal handlers to cause interrupts.
 Signals can also be explicitly sent from one process to another as a way of
passing information or modifying behavior.
 Generally, Signals can be generated, caught and acted upon, or ignored.
 Signal names are defined by including the header file
< signal.h > as followed:
Signals
SIGNAL Value Description
SIGABRT 6 Process abort
SIGALRM 14 Generated by the timer set by the alarm function.
SIGFPE 8 Generated by floating-point arithmetic exception.
SIGHUP 1 Sent to the controlling process by a disconnecting terminal,
or by the controlling process on termination to each
foreground process.
SIGILL 4 An illegal instruction has been executed by the processor.
Usually caused by a corrupt program or invalid shared memory module.
SIGINT 2 Typically raised from the terminal by typing Ctrl+C or the
configured interrupt character.
SIGKILL 9 Typically used from the shell to forcibly terminate an errant
process, because this signal can’t be caught or ignored.
SIGPIPE 13 Generated by an attempt to write to a pipe with no associated reader.
SIGQUIT 3 Typically raised from the terminal by typing Ctrl+ or the
configured quit character.
Signals
SIGNAL Value Description
SIGSEGV 11 A segmentation violation, usually caused by reading or
writing at an illegal location in memory.
SIGTERM 15 Sent as a request for a process to finish. Used by UNIX when
shutting down to request that system services stop. This is
the default signal sent from the kill command.
SIGUSR1 10 May be used by processes to communicate with each other,
SIGUSR2 12 possibly to cause them to report status information
SIGCHLD 17 Child process is exited by default this signal is ignored
SIGCONT 18 Restarts a stopped process and is ignored if received by a
process that is not stopped.
SIGSTOP 19 Stop executing. (Can’t be caught or ignored.)
SIGTSTP 20 Terminal stop signal, often raised by typing Ctrl+Z.
SIGTTIN 21 Used by the shell to indicate that background jobs have
SIGTTOU 22 stopped because they need to read from the terminal or produce output.
Signals
Programs can handle signals using the signal library function.
#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);
 The signal function that takes two parameters, sig and func.
 The signal to be caught or ignored is given as argument sig.
 The function to be called when the specified signal is received is given as func.
 func function takes a single int argument (the signal received) and is of type
void.
 The signal function itself returns a function of the same type, which is the
previous value of the function set up to handle this signal, or one of these two
special values: SIG_IGN Ignore the signal.
SIG_DFL Restore default behavior.
Signals
 The function ouch reacts to the signal that is passed in the parameter sig.
 This function will be called when a signal occurs. It prints a message and then resets the
signal handling for SIGINT (by default, generated by typing Ctrl+C) back to the default
behavior.
Signals
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch(int sig){
printf("OUCH! - I got signal %dn", sig);
(void) signal(SIGINT, SIG_DFL); // for Ctrl+C
// similarly handle Ctrl+Z signal
}
int main(){
(void) signal(SIGINT, ouch);
while(1){
printf("Hello World!n");
sleep(1);
}
} // send signals to this program using kill command
ctlrc1.c
#include <signal.h> #include <stdio.h> #include <unistd.h>
void ouch(int sig){
printf("OUCH! - I got Ctrl+C signal %dn", sig); (void) signal(SIGINT, SIG_DFL);
}
void ouch2 (int sig){
printf("OUCH! - I got Ctrl+Z signal %dn", sig); (void) signal(SIGTSTP, SIG_DFL);
}
void ouch3 (int sig){
printf("OUCH! - I got Ctrl+ signal %dn", sig); (void) signal(SIGQUIT, SIG_DFL);
}
int main(){
(void) signal(SIGINT, ouch); (void) signal(SIGTSTP, ouch2); (void) signal(SIGQUIT, ouch3);
while(1){
printf("Hello World!n");
sleep(1);
}
} // send signals to this program using kill command or using Ctl+c, Ctrl+Z, Ctrl+
Handling Signals ctl.c
 A process may send a signal to another process, including itself, by calling kill.
 The call will fail if the program doesn’t have permission to send the signal,
often because the target process is owned by another user.
 This is the program equivalent of the shell command of the same name.
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
 The kill function sends the specified signal, sig, to the process whose identifier
is given by pid.
 It returns 0 on success and -1 if it fails.
 Signals provide you with a useful alarm clock facility.
Sending Signals
 The alarm function call can be used by a process to schedule a SIGALRM signal
at some time in the future.
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
 The alarm call schedules the delivery of a SIGALRM signal in seconds 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.
 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.
Sending Signals
 The alarm function call can be used by a process to schedule a SIGALRM signal
at some time in the future.
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
 The alarm call schedules the delivery of a SIGALRM signal in seconds 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.
 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.
Alarm Clock
#include <sys/types.h> #include <signal.h>
#include <stdio.h> #include <unistd.h>
#include <stdlib.h>
static int alarm_fired = 0;
void ding(int sig){
alarm_fired = 1;
}
int main(){
printf("Alarm application is startingn");
(void) signal(SIGALRM, ding);
printf("waiting for alarm to go offn");
alarm(5); //Generate SIGALRM after 5 Seconds
pause(); // suspend execution until a signal occurs
if (alarm_fired)
printf("Ding!an"); // use a for beep sound
printf("donen");
exit(0);
}
Alarm Clock alarm1.c
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
static int alarm_fired = 0;
void ding(int sig){
alarm_fired = 1;
}
// In main, you tell the child process to wait for five
// seconds before sending a SIGALRM signal to its parent.
int main(){
pid_t pid;
printf("alarm application startingn");
(1/2) Alarm Clock alarm.c
pid = fork();
switch(pid) {
case -1: perror("fork failed"); exit(1);
case 0: /* child process */
sleep(5);
kill(getppid(), SIGALRM); exit(0);
}
// The parent process arranges to catch SIGALRM with a
// call to signal and then waits for the inevitable.
/* if we get here we are the parent process */
(void) signal(SIGALRM, ding);
printf("waiting for alarm to go offn");
pause();
if (alarm_fired)
printf("Ding!n");
printf("donen");
exit(0);
}
(2/2) Alarm Clock alarm.c
 When you run this program, it pauses for five seconds while it waits for the
simulated alarm clock.
 This program introduces a new function, pause, which 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);
 The alarm clock simulation program starts a new process via fork. This child
process sleeps for five seconds and then sends a SIGALRM to its parent.
 The parent arranges to catch SIGALRM and then pauses until a signal is
received. You do not call printf in the signal handler directly; rather, you set a
flag and then check the flag afterward.
Alarm Clock
 UNIX specifications recommend a newer programming interface for signals
that is more robust: sigaction.
#include <signal.h>
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
The sigaction structure, used to define the actions to be taken on receipt of
the signal specified by sig.
 It is defined in signal.h and has at least the following members:
void (*) (int) sa_handler //function, SIG_DFL or SIG_IGN
sigset_t sa_mask // signals to block in sa_handler
int sa_flags // signal action modifiers
 sigaction function sets the action associated with the signal sig.
 If oact is not null, sigaction writes the previous signal action to the location it
refers to. If act is null, this is all sigaction does. If act isn’t null, the action for
the specified signal is set.
A Robust Signals Interface
#include <signal.h> // for sigaction
#include <stdio.h> // for printf
#include <unistd.h> // for sleep
void ouch(int sig){
printf("OUCH! - I got signal %dn", sig);
}
int main(){
struct sigaction act;
act.sa_handler = ouch;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);
while(1){
printf("Hello World!n");
sleep(1);
}
}
Using Sigaction ctrl2.c
 When you run this version of the program, you always get a message when
you type Ctrl+C.
 Because SIGINT is handled repeatedly by sigaction.
 To terminate the program, you have to type Ctrl+, which generates the
SIGQUIT signal by default.
How It Works
 The program calls sigaction instead of signal to set the signal handler for Ctrl+C
(SIGINT) to the function ouch.
 It first has to set up a sigaction structure that contains the handler, a signal
mask, and flags.
 In this case, you don’t need any flags, and an empty signal mask is created
with the new function, sigemptyset.
Using Sigaction
Q1. Make a program which creates its child by fork() system call. The task
of parent is to print alphabets from a to z (using for loop), and the task
of child is to print decimal numbers from 0 to 9. You should write this
program using wait() system call in such a way that results of parent
and child could not be jumbled up.
Q2. Using execv system call list all the files of bin, and home directory
Q3. Write a Program to prove that Process ID of parent and it’s child
created through exec system calls is same.
Assignment #2
Q4. A process creates it’s child through the fork() system call. The child
created by fork() creates another process through execl system call.
Prove that the Process ID of process created by fork() and through
execl is same.
Q5. Write a C- program in which child created by fork() system call kills its
parent, and verify that parent has been killed.
Q6. Wright a C program which could handle both Ctrl+z, and Ctrl+c
signals, and prints the caught signal number.
Assignment #2
Q7. Write down a program for Stop Watch using signals. In this program the child process
takes the time interval (in seconds) from user, Now the child process counts down, after
the time is finished the child process sends SIGALARM to the parent process. On receiving
SIGALARM the parent calls the function Times_Up() for Finish Time.
Note: if the user decides to stop the Stop Watch. He presses Ctrl+Z, the count down timer
stops. Here is the sample output.
$./Stop_Watch
$ Enter time (in seconds): 5
$ Time Remaining:5
$ Time Remaining:4
$ Time Remaining:3
$ Time Remaining:2
$ Time Remaining:1
$ Time Remaining:0
$ ( TIME FINISH )
$./Stop_Watch
$ Enter time (in seconds): 5
$ Time Remaining:5
$ Time Remaining:4
$ Time Remaining:3 //Now Press Ctrl+Z
$ OPERATION ABBORTED
Assignment #2
Q8. Write two programs P1.c and P2.c. When the program P1 executes it prints its process ID
on screen, makes handlers for signals SIGINT (Crtl+C) and SIGTSTP (Ctrrl+Z), the signal
handlers on receiving the signals print “SIGINT Received” or “SIGTSTP Received” based on
signal type, P1 then goes to the infinite loop.
 When the process P2 executes, it takes the process ID of P1 from user. After that If the user
inputs C, P2 sends signal SIGINT to the process P1.
 If the user inputs Z, P2 sends signal SIGTSTP to the process P1.
 If the user inputs K, P2 first sends signal SIGKILL to the process P1and then sends the same
signal to it self
 Also write down the sample output to show how you will interact with both the programs.
Assignment #2

More Related Content

Similar to Linux Systems Programming: Process CommunCh11 Processes and Signals

System Administration: Linux Process
System Administration: Linux ProcessSystem Administration: Linux Process
System Administration: Linux Processlucita cabral
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.pptSIDDHARTHANANDCSE202
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptxDiptoRoy21
 
Process management in linux
Process management in linuxProcess management in linux
Process management in linuxMazenetsolution
 
Tarea - 3 Actividad intermedia trabajo colaborativo 2
Tarea - 3 Actividad intermedia trabajo colaborativo 2Tarea - 3 Actividad intermedia trabajo colaborativo 2
Tarea - 3 Actividad intermedia trabajo colaborativo 2HectorFabianPintoOsp
 
Managing Processes in Unix.pptx
Managing Processes in Unix.pptxManaging Processes in Unix.pptx
Managing Processes in Unix.pptxHarsha Patel
 
Managing Processes in Unix.pptx
Managing Processes in Unix.pptxManaging Processes in Unix.pptx
Managing Processes in Unix.pptxHarsha Patel
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptxvnwzympx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docxwkyra78
 
Operating System 3
Operating System 3Operating System 3
Operating System 3tech2click
 
Unix Shell and System Boot Process
Unix Shell and System Boot ProcessUnix Shell and System Boot Process
Unix Shell and System Boot ProcessArvind Krishnaa
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxstilliegeorgiana
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxdenneymargareta
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programmingMohammed Farrag
 

Similar to Linux Systems Programming: Process CommunCh11 Processes and Signals (20)

Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
System Administration: Linux Process
System Administration: Linux ProcessSystem Administration: Linux Process
System Administration: Linux Process
 
11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt11_UNIX_Processes_Including_Select.ppt
11_UNIX_Processes_Including_Select.ppt
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
 
Process management in linux
Process management in linuxProcess management in linux
Process management in linux
 
Tarea - 3 Actividad intermedia trabajo colaborativo 2
Tarea - 3 Actividad intermedia trabajo colaborativo 2Tarea - 3 Actividad intermedia trabajo colaborativo 2
Tarea - 3 Actividad intermedia trabajo colaborativo 2
 
Managing Processes in Unix.pptx
Managing Processes in Unix.pptxManaging Processes in Unix.pptx
Managing Processes in Unix.pptx
 
Managing Processes in Unix.pptx
Managing Processes in Unix.pptxManaging Processes in Unix.pptx
Managing Processes in Unix.pptx
 
UNIX Notes
UNIX NotesUNIX Notes
UNIX Notes
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Process management
Process managementProcess management
Process management
 
Program Assignment Process ManagementObjective This program a.docx
Program Assignment  Process ManagementObjective This program a.docxProgram Assignment  Process ManagementObjective This program a.docx
Program Assignment Process ManagementObjective This program a.docx
 
Operating System 3
Operating System 3Operating System 3
Operating System 3
 
Lect3 process
Lect3 processLect3 process
Lect3 process
 
Unix Shell and System Boot Process
Unix Shell and System Boot ProcessUnix Shell and System Boot Process
Unix Shell and System Boot Process
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
 
Programming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docxProgramming Assignment #2CSci 430 Spring 2019Dates.docx
Programming Assignment #2CSci 430 Spring 2019Dates.docx
 
Unix kernal
Unix kernalUnix kernal
Unix kernal
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxRashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxRashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxRashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxRashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxRashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxRashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2T.D. Shashikala
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Prakhyath Rai
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientistgettygaming1
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectRased Khan
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfAbrahamGadissa
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdfKamal Acharya
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGKOUSTAV SARKAR
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxwendy cai
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdfKamal Acharya
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdfKamal Acharya
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDrGurudutt
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单
一比一原版(UofT毕业证)多伦多大学毕业证成绩单一比一原版(UofT毕业证)多伦多大学毕业证成绩单
一比一原版(UofT毕业证)多伦多大学毕业证成绩单tuuww
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...Amil baba
 
一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单
一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单
一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单tuuww
 
retail automation billing system ppt.pptx
retail automation billing system ppt.pptxretail automation billing system ppt.pptx
retail automation billing system ppt.pptxfaamieahmd
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamDr. Radhey Shyam
 
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfKamal Acharya
 

Recently uploaded (20)

Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2Research Methodolgy & Intellectual Property Rights Series 2
Research Methodolgy & Intellectual Property Rights Series 2
 
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...Software Engineering - Modelling Concepts + Class Modelling + Building the An...
Software Engineering - Modelling Concepts + Class Modelling + Building the An...
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单
一比一原版(UofT毕业证)多伦多大学毕业证成绩单一比一原版(UofT毕业证)多伦多大学毕业证成绩单
一比一原版(UofT毕业证)多伦多大学毕业证成绩单
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单
一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单
一比一原版(UNK毕业证)内布拉斯加州立大学科尼分校毕业证成绩单
 
retail automation billing system ppt.pptx
retail automation billing system ppt.pptxretail automation billing system ppt.pptx
retail automation billing system ppt.pptx
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdfONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
ONLINE CAR SERVICING SYSTEM PROJECT REPORT.pdf
 

Linux Systems Programming: Process CommunCh11 Processes and Signals

  • 1. Linux System Programming Processes and Signals Engr. Rashid Farid Chishti chishti@iiu.edu.pk https://youtube.com/rfchishti https://sites.google.com/site/chishti
  • 2.  Processes and Signals form a fundamental part of the Linux operating environment. They control almost all activities performed by Linux computer system.  An understanding of how Linux manages processes will hold any systems programmer, applications programmer or system administrator in good stead.  we’ll look at how processes are handled in the Linux environment and how to find out exactly what the computer is doing at any given time. How to start and stop other processes from within our own programs, how to make processes send and receive messages, and how to avoid zombies (in which parents died before children).  A process is defined as “an address space with one or more threads executing within that address space, and the required system resources for those threads.” (Single UNIX Specification, Version 2) Introduction
  • 3.  A process is a program in execution and can identified by its unique PID (process identification) number.  The kernel controls and manages processes. Multiple processes are running and monitored by the Linux Kernel, allocating each of the processes a little slice of the CPU in a way that is unnoticeable to the user. Process Structure  A process consists of the executable program, its data and stack, variables (occupying system memory), open files (file descriptor) and an environment.  Linux allows many users to access the system at the same time. Each user can run many programs, or even many instances of the same program, at the same time. The system itself runs other programs to manage system resources and control user access. Introduction
  • 4.  Typically, Linux system will share code and system libraries between processes, so that there is only one copy of the code in memory at any one time.  For example: two users, Neil and rick both run the command “grep” on the shell at the same time to look for different strings in different files. This produces out two processes, each has it own process identifier (PID). And one copy of program code of “grep” is loaded into the memory as read only, two users share this code. And the system library is also shared (Shared Library). Introduction
  • 5. Figure: two processes with same shared libraries but different PID No. Processes
  • 6. What process is running?  ps command: ps prints out the information about  the active processes. Explanation  USER: User name;  PID: Process identifier  %CPU: cpu time percentage  %MEM: % of memory occupied VSZ: virtual size of the process;  RSS: resident pages and amount of shared pages; TTY: terminal identifier;  STAT: process state, R means running, S means sleeping.  START: starting time. TIME: cumulative execution time;  COMMAND :command on shell. Processes
  • 7. pstree Command  pstree is another way to see what process are  running and what processes are child process. Explanation:  pstree command displays all process as a tree  with its root being the first process that runs,  called init. If a process creates more than one  process of the same name  pstree visually merges the identical branches by putting them in square brackets and prefixing them with the number of times the process is repeated.  In Linux each process is started by another process known as its parent process. A process so started is known as a child process. Processes
  • 8.  When Linux starts, it runs a single program, the prime ancestor and process number 1, init. So it is the grand parent of all processes.  The Linux operating system determines the priority of a process based on a “nice” value, which defaults to 0,  You can set the process nice value using nice and adjust it using renice. Command.  Start a process with some priority $nice –n -10 ./a.out  See the nice value (NI Column) of processes $ps –al  The range of the value goes from -20 (highest priority) to 19 (lowest priority). To Change value #renice 10 2073 Here 10 is new value and 2073 is PID of process Processes
  • 9. Using the system function  You can cause a program to run from inside another program and thereby create a new process by using the system library function. #include <stdlib.h> int system (const char *string); The system function runs the command passed to it as a string and waits for it to complete.  system returns 127 if a shell can’t be started to run the command and -1 if another error occurs. Otherwise, system returns the exit code of the command.  Let’s compile and run the next code system1.c Starting a New Process
  • 10.  You The program calls system with the string “ps ax”, which executes the ps program. The program returns from the call to system when the ps command has finished.  This program has to wait until the process started by the call to system finishes, you can’t get on with other tasks.  Now change “ps ax &“ to “ps ax &“ to run this command in background and compile and run the program again. Starting a New Process #include <stdio.h> #include <stdlib.h> int main(){ printf("Running ps with systemn"); system("ps ax"); printf("Done.n"); exit(0); } system.c
  • 11.  The call to system returns as soon as the shell command finishes. Because it’s a request to run a program in the background.  The shell returns as soon as the ps program is started, just as would happen if you had typed $ ps ax & at a shell prompt.  This program then prints Done and exits before the ps command has had a chance to finish all of its output. Starting a New Process #include <stdio.h> #include <stdlib.h> int main(){ printf("Running ps with systemn"); system("ps ax &"); printf("Done.n"); exit(0); } system2.c
  • 12. Using the fork function  fork function is used to create a new process.  This system call duplicates the current process and creates a new entry in the process table.  The new process is almost identical to the original, executing the same code but with its own data space, environment, and file descriptors.  Combined with the exec functions, fork is all you need to create new processes. #include <sys/types.h> #include <unistd.h> pid_t fork(void); Creating a New Process
  • 13. Creating a New Process P1 P2 Fork() Parent Process fork returns Child Process ID Child Process fork returns 0
  • 14. #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(){ pid_t pid; printf("fork program startingn"); pid = fork(); switch(pid){ case -1: perror("fork failed"); break; case 0: printf("We are childn"); break; default: printf("We are parentn"); break; } exit(0); } Using fork function fork.c
  • 15. #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(){ pid_t pid; char *message; int n; printf("fork program startingn"); pid = fork(); switch(pid){ case -1: perror("fork failed"); exit(1); case 0: message = "This is the child "; n = 5; break; default: message = "This is the parent"; n = 3; break; } for( ; n > 0 ; n-- ){ puts(message); printf("my pid is %d",getpid()); sleep(1); } exit(0); } Using fork function fork1.c
  • 16. How it Works  This program runs as two processes. A child is created and prints a message five times.  The original process (the parent) prints a message only three times. The parent process finishes before the child has printed all of its messages, so the next shell prompt appears mixed in with the output.  When fork is called, this program divides into two separate processes.  The parent process is identified by a nonzero return from fork and is used to set a number of messages to print, each separated by one second.  Sometimes, you want to wait for a child process to finish.  For example, in the previous program, the parent finishes ahead of the child and you get some messy output as the child continues to run. Using fork function
  • 17.  You can arrange for the parent process to wait until the child finishes before continuing by calling wait. #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc);  The wait system call causes a parent process to pause until one of its child processes is stopped.  The call returns the PID of the child process Using fork function
  • 18. Waiting For a Process Summary of wait activities. wait() Child Process Present ? return -1 and set errno wait for child--- Process terminated ? return status value and child PID No No Yes Yes Block
  • 19. #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(){ pid_t pid; char *message; int n; int exit_code; printf("fork program startingn"); pid = fork(); switch(pid){ case -1: perror("fork failed"); exit(1); case 0: message = "This is the child"; n = 5; exit_code = 37; break; default: message = "This is the parent"; n = 3; exit_code = 0; break; } for( ; n > 0; n--) { puts(message); sleep(1); } (1/2) Waiting For a Process wait.c
  • 20. // This section of the program waits for the child process to finish. if (pid != 0){ int stat_val; pid_t child_pid; // pid_t is int data type child_pid = wait(&stat_val); printf("Child has finished: PID = %dn", child_pid); if(WIFEXITED(stat_val)) // did the process exit normally? printf("Child exited with code %dn", WEXITSTATUS(stat_val)); // What was the exit code of child process else printf("Child terminated abnormallyn"); } exit(exit_code); } (2/2) Waiting For a Process wait.c
  • 21. How it Works  The parent process, which got a nonzero return from the fork call, uses the wait system call to suspend its own execution until status information becomes available for a child process.  This happens when the child calls exit; we gave it an exit code of 37. The parent then continues, determines that the child terminated normally by testing the return value of the wait call, and extracts the exit code from the status information.  Using waitpid: you can use it to waitpid to wait for a specific process to terminate. #include <sys/types.h> #include <sys/wait.h> pid_t waitpid(pid_t pid, int *stat_loc, int options); Waiting For a Process
  • 22.  TheThe pid argument specifies the PID of a particular process to wait for.  The status information is written to the location pointed to by stat_loc  The options argument allows you to modify the behavior of waitpid. The most useful option is WNOHANG, which prevents the call to waitpid from suspending execution of the caller.  You can use it to find out whether any child processes have terminated and, if not, to continue. Other options are the same as for wait.  So, if you wanted to have a parent process regularly check whether a specific child process has terminated, you could use the call waitpid(child_pid, (int *) 0, WNOHANG);  This will return zero if the child has not terminated or stopped, or child_pid if it has. waitpid will return -1 on error and set errno. This can happen if there are no child processes Using waitpid
  • 23.  TheThe pid argument specifies the PID of a particular process to wait for.  The status information is written to the location pointed to by stat_loc  The options argument allows you to modify the behavior of waitpid. The most useful option is WNOHANG, which prevents the call to waitpid from suspending execution of the caller.  You can use it to find out whether any child processes have terminated and, if not, to continue. Other options are the same as for wait.  So, if you wanted to have a parent process regularly check whether a specific child process has terminated, you could use the call waitpid(child_pid, (int *) 0, WNOHANG);  This will return zero if the child has not terminated or stopped, or child_pid if it has. waitpid will return -1 on error and set errno. This can happen if there are no child processes Using waitpid
  • 24.  Using fork to create processes can be very useful, but you must keep track of child processes. When a child process terminates, an association with its parent survives until the parent in turn either terminates normally or calls wait. The child process entry in the process table is therefore not freed up immediately.  Although no longer active, the child process is still in the system because its exit code needs to be stored  in case the parent subsequently calls wait. It becomes what is known as defunct, or a zombie process.  You can see a zombie process being created if you change the number of messages in the fork example program. If the child prints fewer messages than the parent, it will finish first and will exist as a zombie until the parent has finished. Zombie Process
  • 25.  The child process entry in the process table is therefore not freed up immediately. fork2.c is the same as fork1.c, except that the number of messages printed by the child and parent processes is reversed. Here are the relevant lines of code:  How it works  Run this program with ./fork2 & and then call the ps –aux | grep fork2 program after the child has finished but before the parent has finished, you’ll see a child process a zombie (Z) or defunct. Zombie Process // fork2.c switch(pid) { case -1: perror("fork failed"); exit(1); case 0: message = "This is the child"; n = 3; break; default: message = "This is the parent"; n = 5; break; }
  • 26.  If the parent then terminates abnormally, the child process automatically gets the process with PID 1 (init) as parent. Zombie Process
  • 27.  If yhere is a whole family of related functions grouped under the exec heading. They differ in the way that they start processes and present program arguments.  An exec function replaces the current process with a new process specified by the path or file argument. You can use exec functions to “hand off” execution of your program to another.  For example, you could check the user’s credentials before starting another application that has a restricted usage policy.  The exec functions are more efficient than system because the original program will no longer be running after the new one is started. Replacing a Process Image
  • 28. #include <unistd.h> char **environ; int execl(const char *path, const char *arg0, ..., (char *)0); int execlp(const char *file, const char *arg0, ..., (char *)0); int execle(const char *path, const char *arg0, ..., (char *)0, char *const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execve(const char *path, char *const argv[], char *const envp[]);  These functions belong to two types. execl, execlp, and execle take a variable number of arguments ending with a null pointer.  execv and execvp have as their second argument an array of strings. In both cases, the new program starts with the given arguments appearing in the argv array passed to main. Replacing a Process Image
  • 29.  These functions are usually implemented using execve, though there is no requirement for it to be done this way.  The functions with names suffixed with a p differ in that they will search the PATH environment variable to find the new program executable file. If the executable isn’t on the path, an absolute filename, including directories, will need to be passed to the function as a parameter.  The global variable environ is available to pass a value for the new program environment. Alternatively, an additional argument to the functions execle and execve is available for passing an array of strings to be used as the new program environment.  If you want to use an exec function to start the ps program, you can choose from among the six exec family functions, as shown in the calls in the code fragment that follows: Replacing a Process Image
  • 30. #include <unistd.h> /* Example of an argument list Note that we need a program name for argv[0] */ char *const ps_argv[] = {“ps”, “ax”, 0}; /* Example environment, not terribly useful */ char *const ps_envp[] = {“PATH=/bin:/usr/bin”, “TERM=console”, 0}; /* Possible calls to exec functions */ execl(“/bin/ps”, “ps”, “ax”, 0); /* assumes ps is in /bin */ execlp(“ps”, “ps”, “ax”, 0); // assumes /bin is in PATH execle(“/bin/ps”, “ps”, “ax”, 0, ps_envp); /* passes own environment */ execv(“/bin/ps”, ps_argv); execvp(“ps”, ps_argv); execve(“/bin/ps”, ps_argv, ps_envp); Using exec Family
  • 31.  The program prints its first message and then calls execlp, which searches the directories given by the PATH environment variable for a program called ps. It then executes this program in place of the pexec program.  When ps finishes, you get a new shell prompt. You don’t return to pexec, so the second message doesn’t get printed. The PID of the new process is the same as the parent PID. Using exec Family #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main(){ printf("my pid is %dn",getpid()); printf("Running ps with execlpn"); execlp("ps", "ps", "ax", (char *)0); // run again for top command and check process id with // ps –aux | grep username | grep name_of_binary printf("Done.n"); exit(0); } execlp.c
  • 32.  A signal is an event generated by the Linux/UNIX system in response to some condition, upon which a process may in turn take some action.  Signals are generated by some error conditions, such as memory segment violations, floating point processor errors or illegal instructions. They are generated by the shell and terminal handlers to cause interrupts.  Signals can also be explicitly sent from one process to another as a way of passing information or modifying behavior.  Generally, Signals can be generated, caught and acted upon, or ignored.  Signal names are defined by including the header file < signal.h > as followed: Signals
  • 33. SIGNAL Value Description SIGABRT 6 Process abort SIGALRM 14 Generated by the timer set by the alarm function. SIGFPE 8 Generated by floating-point arithmetic exception. SIGHUP 1 Sent to the controlling process by a disconnecting terminal, or by the controlling process on termination to each foreground process. SIGILL 4 An illegal instruction has been executed by the processor. Usually caused by a corrupt program or invalid shared memory module. SIGINT 2 Typically raised from the terminal by typing Ctrl+C or the configured interrupt character. SIGKILL 9 Typically used from the shell to forcibly terminate an errant process, because this signal can’t be caught or ignored. SIGPIPE 13 Generated by an attempt to write to a pipe with no associated reader. SIGQUIT 3 Typically raised from the terminal by typing Ctrl+ or the configured quit character. Signals
  • 34. SIGNAL Value Description SIGSEGV 11 A segmentation violation, usually caused by reading or writing at an illegal location in memory. SIGTERM 15 Sent as a request for a process to finish. Used by UNIX when shutting down to request that system services stop. This is the default signal sent from the kill command. SIGUSR1 10 May be used by processes to communicate with each other, SIGUSR2 12 possibly to cause them to report status information SIGCHLD 17 Child process is exited by default this signal is ignored SIGCONT 18 Restarts a stopped process and is ignored if received by a process that is not stopped. SIGSTOP 19 Stop executing. (Can’t be caught or ignored.) SIGTSTP 20 Terminal stop signal, often raised by typing Ctrl+Z. SIGTTIN 21 Used by the shell to indicate that background jobs have SIGTTOU 22 stopped because they need to read from the terminal or produce output. Signals
  • 35. Programs can handle signals using the signal library function. #include <signal.h> void (*signal(int sig, void (*func)(int)))(int);  The signal function that takes two parameters, sig and func.  The signal to be caught or ignored is given as argument sig.  The function to be called when the specified signal is received is given as func.  func function takes a single int argument (the signal received) and is of type void.  The signal function itself returns a function of the same type, which is the previous value of the function set up to handle this signal, or one of these two special values: SIG_IGN Ignore the signal. SIG_DFL Restore default behavior. Signals
  • 36.  The function ouch reacts to the signal that is passed in the parameter sig.  This function will be called when a signal occurs. It prints a message and then resets the signal handling for SIGINT (by default, generated by typing Ctrl+C) back to the default behavior. Signals #include <signal.h> #include <stdio.h> #include <unistd.h> void ouch(int sig){ printf("OUCH! - I got signal %dn", sig); (void) signal(SIGINT, SIG_DFL); // for Ctrl+C // similarly handle Ctrl+Z signal } int main(){ (void) signal(SIGINT, ouch); while(1){ printf("Hello World!n"); sleep(1); } } // send signals to this program using kill command ctlrc1.c
  • 37. #include <signal.h> #include <stdio.h> #include <unistd.h> void ouch(int sig){ printf("OUCH! - I got Ctrl+C signal %dn", sig); (void) signal(SIGINT, SIG_DFL); } void ouch2 (int sig){ printf("OUCH! - I got Ctrl+Z signal %dn", sig); (void) signal(SIGTSTP, SIG_DFL); } void ouch3 (int sig){ printf("OUCH! - I got Ctrl+ signal %dn", sig); (void) signal(SIGQUIT, SIG_DFL); } int main(){ (void) signal(SIGINT, ouch); (void) signal(SIGTSTP, ouch2); (void) signal(SIGQUIT, ouch3); while(1){ printf("Hello World!n"); sleep(1); } } // send signals to this program using kill command or using Ctl+c, Ctrl+Z, Ctrl+ Handling Signals ctl.c
  • 38.  A process may send a signal to another process, including itself, by calling kill.  The call will fail if the program doesn’t have permission to send the signal, often because the target process is owned by another user.  This is the program equivalent of the shell command of the same name. #include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig);  The kill function sends the specified signal, sig, to the process whose identifier is given by pid.  It returns 0 on success and -1 if it fails.  Signals provide you with a useful alarm clock facility. Sending Signals
  • 39.  The alarm function call can be used by a process to schedule a SIGALRM signal at some time in the future. #include <unistd.h> unsigned int alarm(unsigned int seconds);  The alarm call schedules the delivery of a SIGALRM signal in seconds 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.  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. Sending Signals
  • 40.  The alarm function call can be used by a process to schedule a SIGALRM signal at some time in the future. #include <unistd.h> unsigned int alarm(unsigned int seconds);  The alarm call schedules the delivery of a SIGALRM signal in seconds 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.  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. Alarm Clock
  • 41. #include <sys/types.h> #include <signal.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> static int alarm_fired = 0; void ding(int sig){ alarm_fired = 1; } int main(){ printf("Alarm application is startingn"); (void) signal(SIGALRM, ding); printf("waiting for alarm to go offn"); alarm(5); //Generate SIGALRM after 5 Seconds pause(); // suspend execution until a signal occurs if (alarm_fired) printf("Ding!an"); // use a for beep sound printf("donen"); exit(0); } Alarm Clock alarm1.c
  • 42. #include <sys/types.h> #include <signal.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> static int alarm_fired = 0; void ding(int sig){ alarm_fired = 1; } // In main, you tell the child process to wait for five // seconds before sending a SIGALRM signal to its parent. int main(){ pid_t pid; printf("alarm application startingn"); (1/2) Alarm Clock alarm.c
  • 43. pid = fork(); switch(pid) { case -1: perror("fork failed"); exit(1); case 0: /* child process */ sleep(5); kill(getppid(), SIGALRM); exit(0); } // The parent process arranges to catch SIGALRM with a // call to signal and then waits for the inevitable. /* if we get here we are the parent process */ (void) signal(SIGALRM, ding); printf("waiting for alarm to go offn"); pause(); if (alarm_fired) printf("Ding!n"); printf("donen"); exit(0); } (2/2) Alarm Clock alarm.c
  • 44.  When you run this program, it pauses for five seconds while it waits for the simulated alarm clock.  This program introduces a new function, pause, which 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);  The alarm clock simulation program starts a new process via fork. This child process sleeps for five seconds and then sends a SIGALRM to its parent.  The parent arranges to catch SIGALRM and then pauses until a signal is received. You do not call printf in the signal handler directly; rather, you set a flag and then check the flag afterward. Alarm Clock
  • 45.  UNIX specifications recommend a newer programming interface for signals that is more robust: sigaction. #include <signal.h> int sigaction(int sig, const struct sigaction *act, struct sigaction *oact); The sigaction structure, used to define the actions to be taken on receipt of the signal specified by sig.  It is defined in signal.h and has at least the following members: void (*) (int) sa_handler //function, SIG_DFL or SIG_IGN sigset_t sa_mask // signals to block in sa_handler int sa_flags // signal action modifiers  sigaction function sets the action associated with the signal sig.  If oact is not null, sigaction writes the previous signal action to the location it refers to. If act is null, this is all sigaction does. If act isn’t null, the action for the specified signal is set. A Robust Signals Interface
  • 46. #include <signal.h> // for sigaction #include <stdio.h> // for printf #include <unistd.h> // for sleep void ouch(int sig){ printf("OUCH! - I got signal %dn", sig); } int main(){ struct sigaction act; act.sa_handler = ouch; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGINT, &act, 0); while(1){ printf("Hello World!n"); sleep(1); } } Using Sigaction ctrl2.c
  • 47.  When you run this version of the program, you always get a message when you type Ctrl+C.  Because SIGINT is handled repeatedly by sigaction.  To terminate the program, you have to type Ctrl+, which generates the SIGQUIT signal by default. How It Works  The program calls sigaction instead of signal to set the signal handler for Ctrl+C (SIGINT) to the function ouch.  It first has to set up a sigaction structure that contains the handler, a signal mask, and flags.  In this case, you don’t need any flags, and an empty signal mask is created with the new function, sigemptyset. Using Sigaction
  • 48. Q1. Make a program which creates its child by fork() system call. The task of parent is to print alphabets from a to z (using for loop), and the task of child is to print decimal numbers from 0 to 9. You should write this program using wait() system call in such a way that results of parent and child could not be jumbled up. Q2. Using execv system call list all the files of bin, and home directory Q3. Write a Program to prove that Process ID of parent and it’s child created through exec system calls is same. Assignment #2
  • 49. Q4. A process creates it’s child through the fork() system call. The child created by fork() creates another process through execl system call. Prove that the Process ID of process created by fork() and through execl is same. Q5. Write a C- program in which child created by fork() system call kills its parent, and verify that parent has been killed. Q6. Wright a C program which could handle both Ctrl+z, and Ctrl+c signals, and prints the caught signal number. Assignment #2
  • 50. Q7. Write down a program for Stop Watch using signals. In this program the child process takes the time interval (in seconds) from user, Now the child process counts down, after the time is finished the child process sends SIGALARM to the parent process. On receiving SIGALARM the parent calls the function Times_Up() for Finish Time. Note: if the user decides to stop the Stop Watch. He presses Ctrl+Z, the count down timer stops. Here is the sample output. $./Stop_Watch $ Enter time (in seconds): 5 $ Time Remaining:5 $ Time Remaining:4 $ Time Remaining:3 $ Time Remaining:2 $ Time Remaining:1 $ Time Remaining:0 $ ( TIME FINISH ) $./Stop_Watch $ Enter time (in seconds): 5 $ Time Remaining:5 $ Time Remaining:4 $ Time Remaining:3 //Now Press Ctrl+Z $ OPERATION ABBORTED Assignment #2
  • 51. Q8. Write two programs P1.c and P2.c. When the program P1 executes it prints its process ID on screen, makes handlers for signals SIGINT (Crtl+C) and SIGTSTP (Ctrrl+Z), the signal handlers on receiving the signals print “SIGINT Received” or “SIGTSTP Received” based on signal type, P1 then goes to the infinite loop.  When the process P2 executes, it takes the process ID of P1 from user. After that If the user inputs C, P2 sends signal SIGINT to the process P1.  If the user inputs Z, P2 sends signal SIGTSTP to the process P1.  If the user inputs K, P2 first sends signal SIGKILL to the process P1and then sends the same signal to it self  Also write down the sample output to show how you will interact with both the programs. Assignment #2