Вы находитесь на странице: 1из 12

OPEN SOURCE SOFTWARE UNIT - II IV IT

Linux Processes
The concept of a process is fundamental to any multiprogramming operating system.
A process is usually defined as an instance of a program in execution; thus, if 16 users are
running vi at once, there are 16 separate processes created in the operating system. Processes
are often called "tasks" in Linux source code.

Process attributes
A process has a series of characteristics, which can be viewed with the ps command.
They are
1. The process ID or PID: A unique identification number used to refer to the process.
2. The parent process ID or PPID: The number of the process (PID) that started this
process.
3. Nice number: The degree of friendliness of this process toward other processes
4. Terminal or TTY: Terminal to which the process is connected.
5. Real and effective user (RUID and EUID) IDs: The owner of the process. The real
owner is the user issuing the command. The effective user is the one determining
access to system resources.
6. Real and effective group owner (RGID and EGID): The real group owner of a
process is the primary group of the user who started the process. The effective group
owner is usually the same, except when SGID access mode has been applied to a file.

Process Descriptor
In order to manage processes, the kernel must have a clear picture of what each
process is doing. It must know the process's priority, whether it is running on the CPU or
blocked on some event, what address space has been assigned to it, which files it is allowed
to address, and so on. This is the role of the process descriptor. A task_struct type structure is
maintained which contains all the information related to a single process.

1 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT

Process State
It indicates the state field of the process descriptor which describes what is currently
happening to the process. It consists of an array of flags, each of which describes a possible
process state. In the current Linux version these states are mutually exclusive, and hence
exactly one flag of state is set; the remaining flags are cleared. The following are the possible
process states:

TASK_RUNNING
The process is either executing on the CPU or waiting to be executed.
TASK_INTERRUPTIBLE
The process is suspended (sleeping) until some condition becomes true. Raising a
hardware interrupt, releasing a system resource the process is waiting for, or delivering a
signal are examples of conditions that might wake up the process, that is, put its state back to
TASK_RUNNING.
TASK_UNINTERRUPTIBLE
This process state is seldom used. It is valuable under certain specific conditions in
which a process must wait until a given event occurs without being interrupted. For instance,
2 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


this state may be used when a process opens a device file and the corresponding device driver
starts probing for a corresponding hardware device.
TASK_STOPPED
Process execution has been stopped. The process enters this state after receiving a
SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU signal. When a process is being monitored
by another , any signal may put the process in the TASK_STOPPED state.
TASK_ZOMBIE
Process execution is terminated, but the parent process has not yet issued a wait( )like system call to return information about the dead process. Before the wait( )-like call is
issued, the kernel cannot discard the data contained in the dead process descriptor because the
parent could need it.

The Process List


To allow an efficient search through processes of a given type, the kernel creates
several lists of processes. Each list consists of pointers to process descriptors. A list pointer is
embedded right in the process descriptor's data structure.

Creating Processes
UNIX operating systems rely heavily on process creation to satisfy user requests. As
an example, the shell process creates a new process that executes another copy of the shell
whenever the user enters a command.
Traditional UNIX systems treat all processes in the same way: resources owned by
the parent process are duplicated, and a copy is granted to the child process. This approach
makes process creation very slow and inefficient, since it requires copying the entire address
space of the parent process. The child process rarely needs to read or modify all the resources
already owned by the parent.
Modern UNIX kernels solve this problem by introducing three different mechanisms

3 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


The Copy On Write technique allows both the parent and the child to read the same
physical pages. Whenever either one tries to write on a physical page, the kernel
copies its contents into a new physical page that is assigned to the writing process.
Lightweight processes allow both the parent and the child to share many per-process
kernel data structures, like the paging tables and the open file tables.
The vfork( ) system call creates a process that shares the memory address space of its
parent. To prevent the parent from overwriting data needed by the child, the parent's
execution is blocked until the child exits or executes a new program

The clone( ), fork( ), and vfork( ) System Calls


Lightweight processes are created in Linux by using a function named _clone( ),
which makes use of four parameters:
fn: Specifies a function to be executed by the new process; when the function returns,
the child terminates. The function returns an integer, which represents the exit code
for the child process.
arg: Pointer to data passed to the fn( ) function.
flags: Miscellaneous information.
o The low byte specifies the signal number to be sent to the parent process when
the child terminates.
o The remaining 3 bytes encode a group of clone flags, which specify the
resources shared between the parent and the child process. The flags, when
set, have the following meanings:

CLONE_VM : The memory descriptor and all page tables

CLONE_FS: The table that identifies the root directory and the current
working directory.

CLONE_FILES : The table that identifies the open files

CLONE_SIGHAND : The table that identifies the signal handlers

The traditional fork( ) system call is implemented by Linux as a clone( ) whose first
parameter specifies a SIGCHLD signal and all the clone flags cleared and whose second
parameter is 0.
The old vfork( ) system call, is implemented by Linux as a clone( ) whose first
parameter specifies a SIGCHLD signal and the flags CLONE_VM and CLONE_VFORK and
whose second parameter is equal to 0.
4 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


When either a clone( ), fork( ), or vfork( ) system call is issued, the kernel invokes the
do_fork( ) function.

Destroying Processes
Most processes "die" in the sense that they terminate the execution of the code they
were supposed to run. When this occurs, the kernel must be notified so that it can release the
resources owned by the process; this includes memory, open files, semaphores.
The usual way for a process to terminate is to invoke the exit( ) system call. This
system call may be inserted by the programmer explicitly. Additionally, the exit( ) system
call is always executed when the control flow reaches the last statement of the main
procedure

Process Termination
All process terminations are handled by the do_exit( ) function, which removes most
references to the terminating process from kernel data structures. The do_exit( ) function
executes the following actions:
1. Sets the PF_EXITING flag in the flag field of the process descriptor to denote that
the process is being eliminated.
2. Removes the process descriptor from an IPC semaphore queue via the sem_exit( )
function or from a dynamic timer queue via the del_timer( ) function.
3. Examines the process's data structures related to paging, file system, open file
descriptors, and signal handling, respectively, with the _exit_mm( ),_exit_files( ),
_exit_fs( ), and _exit_sighand( ) functions. These functions also remove any of these
data structures if no other process is sharing it.
4. Sets the state field of the process descriptor to TASK_ZOMBIE.
5. Sets the exit_code field of the process descriptor to the process termination code.
6. This value is either the exit( ) system call parameter (normal termination), or an error
code supplied by the kernel (abnormal termination).

5 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT

Process Scheduling
The scheduler is the component of the kernel that selects which process to run next.
The scheduler can be viewed as the code that divides the finite resource of processor time
between the runnable processes on a system. The scheduler is the basis of a multitasking
operating system such as Linux.
Multitasking operating systems come in two flavors: cooperative multitasking and
preemptive multitasking. Linux, like all UNIX variants and most modern operating
systems, provides preemptive multitasking. In preemptive multitasking, the scheduler
decides when a process is to cease running and a new process is to resume running. The act
of involuntarily suspending a running process is called preemption. The time a process runs
before it is preempted is predetermined, and is called the time slice of the process. The time
slice, gives each process a slice of the processor's time. This time slice is dynamically
calculated in the Linux scheduler to make global scheduling decisions for the system

Scheduling Policy
The set of rules used to determine when and how selecting a new process to run is
called scheduling policy. Linux scheduling is based on the time-sharing technique. Several
processes are allowed to run "concurrently," which means that the CPU time is roughly
divided into "slices," one for each runnable process. Time-sharing relies on timer interrupts
and is thus transparent to processes.
The scheduling policy is also based on ranking processes according to their priority.
In Linux, process priority is dynamic based on the classification of processes.
Processes are traditionally classified as "I/O-bound" or "CPU-bound." The former
make heavy use of I/O devices and spend much time waiting for I/O operations to complete;
the latter are number-crunching applications that require a lot of CPU time.
An alternative classification distinguishes three classes of processes:
Interactive processes: These processes interact constantly with their users, and spend a lot of
time waiting for key presses and mouse operations. When input is received, the process must
be woken up quickly. Typical interactive programs are command shells, text editors, and
graphical applications.
Batch processes: These do not need user interaction, and run in the background. Since such
processes do not need to be very responsive, they are often penalized by the scheduler.
6 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


Typical batch programs are programming language compilers, database search engines, and
scientific computations.
Real-time processes: These have very strong scheduling requirements and never be blocked
by lower-priority processes, they should have a short response time and Typical real-time
programs are video and sound applications, robot controllers.
The scheduler keeps track of what processes are doing and adjusts their priorities
periodically. In this way, processes that have been denied the use of the CPU for a long time
interval are boosted by dynamically increasing their priority. Correspondingly, processes
running for a long time are penalized by decreasing their priority.

The Scheduling Algorithm


The Linux scheduling algorithm works by dividing the CPU time into epochs. In a
single epoch, every process has a specified time quantum whose duration is computed when
the epoch begins. In general, different processes have different time quantum durations.
The time quantum value is the maximum CPU time portion assigned to the process in
that epoch. When a process has exhausted its time quantum, it is preempted and replaced by
another runnable process. A process can be selected several times from the scheduler in the
same epoch, as long as its quantum has not been exhausted. If it suspends itself to wait for
I/O, it preserves some of its time quantum and can be selected again during the same epoch.
The epoch ends when all runnable processes have exhausted their quantum; in this case, the
scheduler algorithm re computes the time-quantum durations of all processes and a new
epoch begins. Each process has a base time quantum: it is the time-quantum value assigned
by the scheduler to the process if it has exhausted its quantum in the previous epoch. The
users can change the base time quantum of their processes by using the nice( ) and
setpriority( ) system calls. A new process always inherits the base time quantum of its parent
in order to select a process to run.
The Linux scheduler must consider the priority of each process. Actually, there are
two kinds of priority:
Static priority - This kind is assigned by the users to real-time processes and ranges from 1
to 99. It is never changed by the scheduler.
Dynamic priority - This kind applies only to conventional processes; it is essentially the sum
of the base time quantum and of the number of ticks of CPU time left to the process before its
quantum expires in the current epoch.

7 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


The schedule( ) Function
schedule( ) implements the scheduler. Its objective is to find a process in the run
queue list and then assign the CPU to it. It is invoked, directly or in a lazy way, by several
kernel routines.
Direct invocation
The scheduler is invoked directly when the current process must be blocked right
away because the resource it needs is not available. In this case, the kernel routine that wants
to block it proceeds as follows:
1. Inserts current in the proper wait queue.
2. Changes the state of current

either

to

TASK_INTERRUPTIBLE or to

TASK_UNINTERRUPTIBLE.
3. Invokes schedule( ).
4. Checks if the resource is available; if not, goes to step 2
5. Once the resource is available, removes current from the wait queue

Lazy invocation
The scheduler can also be invoked in a lazy way by setting the need_resched field of
current to 1.
When current has used up its quantum of CPU time; this is done by the
update_process_times( ) function.
When a process is woken up and its priority is higher than that of the current process.
This task is performed by the reschedule_idle( ) function,
When a setscheduler() or sched yield() system call is issued

8 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT

Linux Personalities
Personalities:
personality - set the process execution domain
Synopsis:
int personality(unsigned long persona);
Description:
Linux supports different execution domains, or personalities, for each process. Among
other things, execution domains tell Linux how to map signal numbers into signal actions.
The execution domain system allows Linux to provide limited support for binaries compiled
under other Unix-like operating systems. personality will make the execution domain
referenced

by

persona

the

new

execution

domain

of

the

current

process.

Return value:
On success, Linux is made with the new execution domain and the previous personal is
returned.

On

error,

-1

is

returned,

and

errno

is

set

appropriately.

Cloning the Linux System


clone() is a system call in the Linux kernel that creates a child process that may share
parts of its execution context with the parent. It is often used to implement
multithreading. In practice, clone() is not often called directly, but instead using a
threading library (such as pthreads) that uses clone() when starting a thread.
The syntax for calling clone under a Linux program is:
#include <sched.h>
int clone (int (*fn) (void *), void *child_stack, int flags, void *arg);
Clone creates a new thread that starts with the function pointed to by the fn argument.
The child_stack argument is a pointer to a memory space to be used as the stack for
the new thread, flags specify what gets inherited from the parent process, and arg is
the argument passed to the function. It returns the process ID of the child process or 1 on failure.
9 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT

Linux Signals
Linux Signals
A signal is a very short message that may be sent to a process or to a group of
processes. The only information given to the process is usually the number identifying the
signal. There is no space maintained for signal arguments, a message, or other accompanying
information. A set of macros whose names start with the prefix SIG is used to identify
signals.
Signals serve two main purposes:
To make a process aware that a specific event has occurred
To force a process to execute a signal handler function included in its code

An important characteristic of signals is that they may be sent at any time to processes
whose state is usually unpredictable. Signals sent to a non running process must be saved by
the kernel until that process resumes execution. Blocking signals require signals to be
queued, which exacerbate the problem of signals being raised before they can be delivered.
Therefore, the kernel distinguishes two different phases related to signal transmission:

Sending the Signal


The kernel updates the descriptor of the destination process to represent that a new
signal has been sent.

Receiving the Signal


The kernel forces the destination process to react to the signal by changing its
execution state or by starting the execution of a specified signal handler or both.

Actions Performed upon Receiving a Signal


There are three ways in which a process can respond to a signal.
Explicitly ignore the signal.
Execute the default action associated with the signal. This action, which is predefined
by the kernel, depends on the signal type and may be any one of the following:
o Abort : The process is destroyed (killed).

10 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


o Dump : The process is destroyed (killed) and a core file containing its
execution context is created, if possible; this file may be used for debug
purpose.
o Ignore : The signal is ignored.
o Stop : The process is stopped, that is, put in a TASK_STOPPED state.
o Continue : If the process is stopped (TASK_STOPPED), it is put into the
TASK_RUNNING state.
o

Catch: The signal by invoking a corresponding signal-handler function.

Some of the Linux Signals are

Signal Name

Number Description

SIGHUP

Hangup (POSIX)

SIGINT

Terminal interrupt (ANSI)

SIGQUIT

Terminal quit (POSIX)

SIGILL

Illegal instruction (ANSI)

SIGTRAP

Trace trap (POSIX)

SIGIOT

IOT Trap (4.2 BSD)

SIGBUS

BUS error (4.2 BSD)

SIGFPE

Floating point exception (ANSI)

SIGKILL

Kill(can't be caught or ignored) (POSIX)

SIGUSR1

10

User defined signal 1 (POSIX)

SIGSEGV

11

Invalid memory segment access (ANSI)

SIGUSR2

12

User defined signal 2 (POSIX)

11 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW

OPEN SOURCE SOFTWARE UNIT - II IV IT


SIGPIPE

13

Write on a pipe with no reader, Broken pipe (POSIX)

SIGALRM

14

Alarm clock (POSIX)

SIGTERM

15

Termination (ANSI)

SIGSTKFLT 16

Stack fault

SIGCHLD

17

Child process has stopped or exited, changed (POSIX)

SIGCONT

18

Continue executing, if stopped (POSIX)

SIGSTOP

19

Stop executing(can't be caught or ignored) (POSIX)

SIGTSTP

20

Terminal stop signal (POSIX)

SIGTTIN

21

Background process trying to read, from TTY (POSIX)

SIGTTOU

22

Background process trying to write, to TTY (POSIX)

SIGURG

23

Urgent condition on socket (4.2 BSD)

SIGXCPU

24

CPU limit exceeded (4.2 BSD)

SIGXFSZ

25

File size limit exceeded (4.2 BSD)

SIGVTALRM 26

Virtual alarm clock (4.2 BSD)

SIGPROF

27

Profiling alarm clock (4.2 BSD)

SIGWINCH

28

Window size change (4.3 BSD, Sun)

SIGIO

29

I/O now possible (4.2 BSD)

SIGPWR

30

Power failure restart (System V)

12 DEPARTMENT OF INFORMATION TECHNOLOGY - SVECW