PE-1 (Unix Programming) - Unit - 4 - Process and Signals
PE-1 (Unix Programming) - Unit - 4 - Process and Signals
Process
https://sites.google.com/view/sandeeprawat
Process - Concepts
Process
• Each instance of a running program constitutes a process.
• A Process consists of program code, data, variables (occupying system
memory), open files (file descriptors) and an environment.
Process Structure
• Each process is allocated a unique number, a process identifier, or PID. This is
usually a positive integer between 2 and 32768.
• Processes Share Memory area among themselves which is used to hold Program
code. Same way The system libraries can also be shared.
What are Not Shared
• Variables that it uses are distinct for each process. processes have their own set of file
descriptors used for file access.
• Process has its own stack space.
• Process must also maintain its own program counter.
• Process is started by another, known as its parent process. A process so started is known
as a child process.
• When UNIX starts, it runs a single program, the prime ancestor and process number 1,
init.
• We 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);
Duplicating the Process
a process duplicate is created with fork ( ) function.
Replacing the Process Image.
A process image is replaced with another process by the exec( ) family functions.
Waiting for a Process
a parent process can wait for it’s child processes to complete execution with the help of
variations of wait( ) system calls.
Zombie process, process control
Zombie Processes
• A process which has finished the execution but still has entry in the process table to report
to its parent process is known as a zombie process.
• A child process always first becomes a zombie before being removed from the process
table.
• The parent process reads the exit status of the child process which reaps off the child
process entry from the process table.
// Parent process
if (child_pid > 0)
sleep(50);
// Child process
else
exit(0);
return 0;
}
Orphan Processes
• An orphan process is a computer process whose parent process has finished or
terminated, though it remains running itself.
• In a Unix-like operating system any orphaned process will be immediately adopted by the
special init system process.
• This operation is called re-parenting and occurs automatically. Even though technically the
process has the init process as its parent, it is still called an orphan process since the
process that originally created it no longer exists.
int execlp (const char *filename, const char *arg0, ... /* (char
*)0 */ );
Signals
https://sites.google.com/view/sandeeprawat
Signal functions, unreliable signals
Signals:
• Signals are software interrupts. Signals provide a way of handling asynchronous
events.
• Every signal has a name. These names all begin with the three characters SIG.
These names are all defined by positive integer constants (the signal number) in
the header <signal.h>.
• No signal has a signal number of 0.
The following table lists out common signals you might encounter and want to use in your
programs
pause() causes the calling process (or thread) to sleep until a signal is delivered that either
terminates the process or causes the invocation of a signal-catching function.
abort Function:
• The abort function causes abnormal program termination.
#include <stdlib.h>
void abort(void);
This function never returns.
• This function sends the SIGABRT signal to the caller.
system Function:
• It is convenient to execute a command string from within
a program.
#include <stdlib.h>
int system(const char *cmdstring);
• If cmdstring is a null pointer, system returns nonzero only
if a command processor is available. This feature
determines whether the system function is supported on
a given operating system. Under the UNIX System,
system is always available.
The system is implemented by calling fork, exec, and
waitpid, there are three types of return values.
• If either the fork fails or waitpid returns an error ,system
returns 1 with errno set to indicate the error.
• If the exec fails, implying that the shell can't be executed,
the return value is as if the shell had executed exit(127).
• Otherwise, all three functions fork, exec, and waitpid
succeed, and the return value from system is the
termination status of the shell, in the format specified for
waitpid.
sleep Function:
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
Returns: 0 or number of unslept seconds.
This function causes the calling process to be suspended
until either
• The amount of wall clock time specified by seconds has
elapsed. The return value is 0.
• A signal is caught by the process and the signal handler
returns. Return value is the number of unslept seconds.