Unit- 4 Linux Ppt
Unit- 4 Linux Ppt
The init process is the first user process which is created when
the system boots for the first time that continues running until
the system is shut down.
Initially the table entries are empty . For each process created
in the system, an entry is used to keep the process details.
When a process terminates, its corresponding entry is cleared.
Kernel Support for Process
Secondly, the user structure is maintained for each process
to keep the execution environment that is created for the
process.
8) Current Directory
This is the reference (inode number) to a working directory file.
9) Root Directory
This is the reference (inode number) to a root directory file.
In addition to the above below are the attributes that are different between the
parent and child processes,
1) Process Identification Number (PID)
Integer identification number unique per process in an entire OS.
2) Parent Process Identification Number (PPID)
The parent process PID
3) Pending signals
The set of signals that are pending delivery to the parent process.
4) Alarm Clock Time
The process alarm clock time is reset to zero in the child process
5) File Locks
Set of file locks owned by the parent process is not inherited by the chid process.
PROCESS ATTRIBUTES
Every process has unique process ID and it is a positive integer.
In Linux there are some special processes, Process ID ‘0’ is
usually the scheduler process (or swapper). It is part of the kernel
and is known as a system process.
Process ID ‘1’ is usually the init process an is invoked by the
kernel at the end of the bootstrap procedure. The program file for
this process is /etc/init in older versions of Linux and is /sbin/init
in newer versions.
This process is responsible for bringing up a Linux system.
Init process usually reads the system dependent initialization files
(/etc/rc*) and brings the system to a certain state (such as multi
user, single user, single user command mode,, etc.,).
The init process never dies. It is a normal user process not a
system process with in the kernel.
Some virtual memory implementations of Linux operating system,
process ID 2 is responsible for supporting the paging of the virtual
memory system.
PROCESS ATTRIBUTES
In addition to the process ID, there are other identifiers for every
process.
The following functions return these identifiers. These functions
does not have error return values like other functions.
#include<sys/types.h>
#include<unistd.h>
pid_t getpid (void) :- returns process ID of calling process
pid_t getppid (void) :- returns parent process ID of calling process
uid_t getuid (void) :-returns real user ID of calling process
uid_t geteuid (void) :- returns effective user ID of calling process
gid_t getgid (void) :- returns reak griyo ID if calling process
gid_t getegid (void) :- returns effective group ID of calling process
PROCESS STATES
Every process in the system can be in one of six states,
Suspende
d
Signal
Signal
Event Wait on
occurs event
Sleeping
PROCESS STATES
1. Running
Which means that the process is currently using the CPU.
2. Runnable
Process can make use of the CPU as soon as it becomes available.
3. Sleeping
Process is waiting for an event to occur. For example, if a process
executes a sleep() system call, it sleeps until the I/O request is completed
4. Suspended
The process has been “frozen” by a signal such as SIGSTOP. It will
resume only when sent a SIGNONT signal. For e.g. a Ctrl+Z from
keyboard suspends all of the processes in the foreground
6. Terminated
process has terminated, but has not yet returned its exit code to its
parent. A process remains a zombie until its parent accepts its return
code via the wait() system call
PROCESS CONTROL
The concept of process control deals with three types of independent
mechanisms namely,
1) Process Creation
2) Process Execution
3) Process Termination
1) Process Creation
In Linux environment a process is created by calling a fork function by any of
the existing process created is called a “child process” and the process which
has created the new process is called as “parent process” the new process
created attains a value ‘0’ where as the parent process is process ID of the new
child under the fork function.
Syntax :
#include<sys/types.h>
#include<unistd.h>
pid_t fork(void);
On creation of the above function creates child process where child process ID is
returns to parent and child remains with a value ‘0’ or returns a value ‘-1’ to
indicate error.
PROCESS CONTROL
2) Process Execution
Once the child and parent processes are created, they both starts
executing the statement .
During this execution the child process gains few features of the parents
like data space, root directory, session ID, process group ID etc.
3) Process Termination
A process is an instance of an executing program. A process
performs certain tasks and terminates. The end of the process is
called process termination.
A process can terminate normally or abnormally.
1. Normal Termination of a Process
A process is said to be terminated normally if,
• It executes a return statement in the function. This is similar to calling the exit function.
• It calls the exit functions. This function flushes the buffer and then returns to the kernel.
• It calls the exit function. This function immediately returns to the kernel without flushing buffers. It is
called by the exit function internally.
2. Abnormal Termination of a process
A process is said to be terminated abnormally if,
It calls abort function which generates the SIGABRT signal.
It receives certain signals that force it to termination
PROCESS CREATION
The fork() function is used to create a new
process. Each time a new process is created,
when ever existing process calls a fork() function.
The fork() function is said to be a “called once
and returns twice” function.
Syntax:
#include<sys/types.h>
#include<unistd.h>
pid_t fork(void);
The only difference in the returns is that the
return value in the child is 0, whereas the return
value in the parent is the process ID of the new
child.
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.
$ ./fork1
fork program starting
This is the child
This is the parent
This is the parent
This is the child
This is the parent
This is the child
$ This is the child
This is the child
WAITING FOR A PROCESS
The wait function is used by the parent process to wait for one of
its children to terminate and to fetch the chid’s termination
status.
Syntax:
#include<sys/types.h>
#include<sys/wait.h>
pid_wait(int *status_loc);
When a process calls wait then wait,
1) Suspends the caller until one of its children terminates.
2) Returns immediately with the termination status of a child that
terminated.
3) Returns immediately with an error if the calling process has no
child(either dead or alive).
A successful call to wait returns,
1) The process ID of the terminated child.
2) Exit status code of the terminated child into the argument
status_loc
3) Frees proc structure of the terminated child.
PROCESS TERMINATIONS
A process performs certain tasks and terminates.
The end of the process is called process termination.
A process can terminate normally or abnormally.
1.Normal Termination of a process
A process is said to be terminated normally if,
• It executes a return statement in the
function. This is similar to calling the exit
function.
• It calls the exit functions. This function
flushes the buffer and then returns to the
kernel.
• It calls the exit function. This function
immediately returns to the kernel without
flushing buffers. It is called by the exit
function internally.
2. Abnormal Termination of a process
A process is said to be terminated abnormally if,
It calls abort function which generates the
SIGABRT signal.
It receives certain signals that force it to
termination
• When a process terminates it informs about its
termination to its parent process.
• A parent process obtain the child’s termination
status by calling either the wait or waitpid
functions which accepts the status of terminating
process as one of the arguments.
• The parent can also examine the termination
status of a child by using the macros defined in
<sys/wait.h> header.
PROCESS TERMINATIONS
Macros to Examine the Termination Status
1. WIFEXITED
This macro returns true if the child terminated normally and exit
status for a child was returned. To get the low-order 8 bits of the
exit status set by the child to exit or_exit we execute
WEXITSTATUS.
2. WIFSIGNALED
This macro return true, if the child terminated abnormally and
the exit status for a child was returned. To get the signal number
that caused the process to terminate we execute WTERMSIG
(status).
we can also use the macro WCOREDUMP (status) to know
whether a core file of the terminated process was generated or
not. This macro returns true if file was generated.
3. WIFSTOPPED
This macro returns true if the child process stopped and the exit
status for currently stopped process was returned. We execute the
macro WSTOPSIG (status) to get the signal number that caused
the child process to stop.
ZOMBIE PROCESS
What is a zombie process?
init
Parent Adopt
Dies child
first
Child
Process
PROCESS
ADOPTION
PROCESS APIs
vfork :
#include <unistd.h>
int execl (const char *pathname, const char *arg0, ... /* (char *)0 */ );
int execv (const char *pathname, char *const argv []);
int execle (const char *pathname, const char *arg0, ... /* (char *)0,
char *const envp[] */ );
int execve (const char *pathname, char *const argv[], char *const
envp []);
int execlp (const char *filename, const char *arg0, ... /* (char *)0 */ );
int execvp (const char *filename, char *const argv []);
PROCESS APIs
There are six different exec functions.
#include <unistd.h>
int execl (const char *pathname, const char *arg0, ... /* (char *)0 */ );
int execv (const char *pathname, char *const argv []);
int execle (const char *pathname, const char *arg0, ... /* (char *)0, char
*const envp[] */ );
int execve (const char *pathname, char *const argv[], char *const envp
[]);
int execlp (const char *filename, const char *arg0, ... /* (char *)0 */ );
int execvp (const char *filename, char *const argv []);
All six return: 1 on error, no return on success.
The first difference in these functions is that the first four take a
pathname argument, whereas the last two take a filename argument.
When a filename argument is specified
If filename contains a slash, it is taken as a pathname.
Otherwise, the executable file is searched for in the directories
specified by the PATH environment variable.
PROCESS APIs
The functions execl, execlp, and execle require each of the
command-line arguments to the new program to be specified as
separate arguments. We mark the end of the arguments with a
null pointer.
For the other three functions (execv, execvp, and execve), we have
to build an array of pointers to the arguments, and the address of
this array is the argument to these three functions.
SIGNALS: Introduction
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.
When a signal is generated for a process, the kernel will set the
corresponding signal flag in the process table slot of the recipient
process.
If the recipient process is asleep, the kernel will awaken the
process by scheduling it.
When the recipient process runs, the kernel will check the
process U-area that contains an array of signal handling
specifications.
If array entry contains a zero value, the process will accept the
default action of the signal.
If array entry contains a 1 value, the process will ignore the signal
and kernel will discard it.
If array entry contains any other value, it is used as the function
pointer for a user -defined signal handler routine
Signal Function
Signal Function:
#include <signal.h>
void (*signal(int signo, void (*func)(int)))(int);
#include <signal.h>
int kill( pid_t pid, int signo);
int raise(int signo);
Both return: 0 if OK,-1 on error.
There are four different conditions for the pid argument to kill.
pid > 0 The signal is sent to the process whose process ID is pid.
pid == 0 The signal is sent to all processes whose process group ID
equals the process group ID of the sender and for which the
sender has permission to send the signal.
pid < 0 The signal is sent to all processes whose process group ID
equals the absolute value of pid and for which the sender has
permission to send the signal.
alarm And pause Functions
pid == 1 The signal is sent to all processes on the system for which
the sender has permission to send the signal.
The super user can send a signal to any process.
Alarm function:
The alarm function allows us to set a timer that will expire at a
specified time in the future.
When the timer expires, the SIGALRM signal is generated.
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
Returns: 0 or number of seconds until previously set alarm.
The seconds value is no.of clock seconds in the future when the
signal should be generated.
alarm And pause Functions
If, when we call alarm, a previously registered alarm clock for the
process has not yet expired, the number of seconds left for that
alarm clock is returned as the value of this function.
If a previously registered alarm clock for the process has not yet
expired and if the seconds value is 0, the previous alarm clock is
canceled. The number of seconds left for that previous alarm
clock is still returned as the value of the function.
pause function:
The pause function suspends the calling process until a signal is
caught.
#include <unistd.h>
int pause(void);
Returns: 1 with errno set to EINTR
The only time pause returns is if a signal handler is executed and
that handler returns.
abort And sleep Functions
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.
abort And sleep Functions
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.