0% found this document useful (0 votes)
11 views45 pages

494 33 Powerpoint-Slides Ch6 Unix

The document provides an overview of UNIX process management, including process creation, states, and control mechanisms. It covers key concepts such as the difference between processes and threads, job handling, scheduling, and signal management. Additionally, it discusses the structure of processes, memory management, and commands for manipulating processes and their execution in UNIX systems.

Uploaded by

All TechFact
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views45 pages

494 33 Powerpoint-Slides Ch6 Unix

The document provides an overview of UNIX process management, including process creation, states, and control mechanisms. It covers key concepts such as the difference between processes and threads, job handling, scheduling, and signal management. Additionally, it discusses the structure of processes, memory management, and commands for manipulating processes and their execution in UNIX systems.

Uploaded by

All TechFact
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

UNIX and SHELL

Programming

B. M. Harwani

© Oxford University Press 2013. All rights


Chapter 6

Manipulating Processes
and Signals

© Oxford University Press 2013. All rights


Learning Objectives
 Process and their address space, structure, data structures describing the
processes, and process states.
 Difference between a process and a thread.
 Commands related to scheduling processes at the desired time, handling
jobs, switching jobs from the foreground to the background and vice
versa, etc.
 Suspending, resuming, and terminating jobs, executing commands in a
batch, ensuring process execution even when user logs out, increasing
and decreasing priority of processes, and killing processes.
 Signals, their types, and the methods of signal generation
 Virtual memory and its role in executing large applications in a limited
physical memory and mapping of a virtual address to the physical
memory.

© Oxford University Press 2013. All rights


Process Basics
 All processes in the Unix system are created when an existing
process executes a process creation system call known as fork.
The first process in the Unix system, also known as process 0, is
related to bootstrapping.
 After performing these initial activities, the kernel creates an
init process with process identification 1 (PID 1).
 The process 0 is a part of the kernel itself and basically functions
as a sched (or swapper). It also does the job of swapping, that
is, moving in and out of the processes.
 The init process always remains in the background while the
system is running. It is the ancestor of all further processes. It is
the init process that forks the getty process, which enables
users to log in to the Unix system.
© Oxford University Press 2013. All rights
Process Basics
A process operates in either user mode or kernel mode:
User mode User mode is the mode in which processes related to user activities get
executed. Commands, programs, utilities, etc., executed by the user are run in this
mode. These processes being trivial in nature, the code in the user mode runs in a
non-privileged protection mode. Switching from user to kernel mode takes place
either when a user’s process requests services from the operating system by making
a system call or when some interrupt occurs during the events such as timers,
keyboard, and hard disk input/output (I/O).

Kernel mode In kernel mode, the system processes, that is, the processes related to
managing a computer system and its resources get executed. The processes used to
allocate memory to access hardware peripherals such as printer and disk drive run in
this mode. These processes are critical in nature, that is, they can make an operating
system inconsistent if they are not handled properly. Hence for security reasons,
these processes are run in a privileged protection mode.

© Oxford University Press 2013. All rights


Process Basics

© Oxford University Press 2013. All rights


Process Basics
 File Subsystem : The file subsystem manages the files of the Unix system.
Communication between the hardware and their respective device
drivers are managed by the file subsystem. Even the buffers that are used
for storing the data that is either fetched from the devices or is to be
written to the devices are managed by the file subsystem.

 Process Control Subsystem : The process control subsystem manages all


the tasks required for successful execution of processes. It allocates
memory to the processes and schedules, synchronizes, and even
implements communication between them. The process control
subsystem comprises the following three modules:
 Interprocess communication
 Memory management
 Scheduler

© Oxford University Press 2013. All rights


Process Address Space
 Process Address Space : Each process runs in its private address space. A
process running in user mode refers to a stack, data, and code areas. When
running in kernel mode, the process addresses the kernel data and code
areas and uses a kernel stack.

A process includes three segments:

• Text: It represents the program code, that is, the executable instructions.
• Data: It represents the program variables and other data processed by the
program code. It is a global content that can be accessed by the program and
its subroutines (if any).
• Stack: It represents a program segment that is used while implementing
procedure calls for storing information pertaining to parameters, return
addresses, etc.

© Oxford University Press 2013. All rights


Process Address Space
 Process Structure : A process structure comprises a complex set of data
structures that provide the operating system with all the information
necessary to manage and dispatch processes. It consists of an address space
and a set of data structures in the kernel to keep track of that process.

 Process Table : The process table is an array of structures that contains an


entry per process. Every process entry contains process control information
required by the kernel to manage the process and is hence maintained in the
main memory. The process entry contains the following information:
• Process State
• Process Identification Information
– Process Identifier (PID)
– User identifier (UID)
– Parent Process Identifier(PPID)

© Oxford University Press 2013. All rights


Information in Process Table
 Program Counter
 CPU resgisters
 CPU Scheduling Information
 Memory-management Information
 Acounting Information
 I/O Status Information

© Oxford University Press 2013. All rights


Process Structure
 User Area : The Unix kernel executes in the context of certain
processes. The user area (U area) refers to private information
in the context of a process.
 File table It is a global kernel structure that contains
information such as storing the byte offset in the file and
indicating the location from where the next write/read
operation will start, mode of opening, and reference count of
all the currently opened files & permissions assigned to the
process.
 User file descriptor table An individual file descriptor is
allocated per process. It keeps track of the files that are
opened by the process.

© Oxford University Press 2013. All rights


Relation b/w User File Descriptor Table, File
Table, and Inode table

© Oxford University Press 2013. All rights


Process Structure
 Per process region table : Processes have Per Process Region
Table. Each entry in Per Process Region Table is pointer to a
region. Each region is a list of pointers to Page Tables.

 Region table : A region is a continuous area of a process’s


address space such as text, data, and stack. Region table
entries indicate whether the region is shared or private. They
also point to the location of the region in the memory (refer
to Fig. 6.3).

© Oxford University Press 2013. All rights


Region Table

© Oxford University Press 2013. All rights


Process States & Transitions

© Oxford University Press 2013. All rights


Unix Process States

© Oxford University Press 2013. All rights


Zombie Process
 A zombie process is a process that has completed execution and is currently
dead, but still has an entry occupied in the process table, waiting for the process
that started it to read its exit status.
 The zombie process does not consume any memory or other resources.
 We can identify a zombie process by executing the ps command. Zombie
processes contain a character Z in their state field (S), as shown in the following
output:

© Oxford University Press 2013. All rights


Zombie Process

© Oxford University Press 2013. All rights


Context Switching
 The tasks conducted during process switching, that is, saving
the state of the current process and loading the saved state of
the new process is known as context switching.

 While context switching, the information of the program


counter and other registers of the blocked process is saved.

 In addition, its PCB is updated to change its state from the


running state to a pre-empted, sleeping, or other state. The PCB
of the process that is scheduled to run is updated to indicate its
running state.

© Oxford University Press 2013. All rights


Threads
 A thread is the smallest unit of processing. A process can have one or more
threads.
 A thread has its own independent flow of control as long as its parent
process exists and dies if the parent process dies.

© Oxford University Press 2013. All rights


Comparison b/w Threads & Processes

© Oxford University Press 2013. All rights


ps: Status of Process

© Oxford University Press 2013. All rights


Handlin Jobs
 A job refers to a command or program executed by the user to
perform some task. The jobs or processes are controlled by the
shell. For example, the following command is a job or process :
$cat letter.txt

 fg :Foreground Jobs
 Jobs that require a high level of user interaction are executed as
foreground jobs. In addition, the most preferred jobs, whose results we
want to see immediately, are executed in the foreground.
Syntax fg [%job]
Here, %job represents the job we wish to run in the foreground.

 To suspend a running foreground job, we press the Ctrl-z keys


and to resume the suspended job, we use the fg command.
© Oxford University Press 2013. All rights
Handlin Jobs
 bg : background Jobs The jobs whose results are not urgent,
that is, jobs that have no time constraint and usually take a
longer time to complete are executed in the background. To
execute any job in the background, simply add the ampersand
symbol (&) after the command.

Syntax bg [%job]
Here, %job represents the job we wish to run in the
background.

 To suspend a background job, we use the stop command. To


restart it, we use the bg command. To terminate it, we use
the kill command.
© Oxford University Press 2013. All rights
jobs : Showing Job Status
 The jobs command displays all the jobs with their job number and the current
status (running or stopped mode).
Syntax :
jobs [ -l][-p ] [ %job_id][%str][%?str] [%%][%+][%−]
 The options of the jobs command are briefly described in Table 6.5.

© Oxford University Press 2013. All rights


Scheduling of Processes
 Scheduling of a process is a mechanism of defining a timetable for
different processes to auto execute at a prescribed date and time.
The tasks that are to be executed at the defined period or time
can be scheduled.

 cron: Chronograph—Time-based Job Scheduler : cron is a


daemon that keeps running and ticks every minute, that is, it gets
activated every minute and opens its special fi le to check if there
are any processes waiting to be executed in that particular minute.
If none of the processes is found waiting, it goes to sleep again. If
there are any processes to be executed in that minute, it executes
them and again goes to sleep. This daemon continues to execute
until the Unix system shuts down.

© Oxford University Press 2013. All rights


crontab : Creating Crontab Files
 The crontab command creates a crontab file (containing the list
of processes and their schedule time) and places it in the
/usr/spool/cron/crontabs directory with our login name.
Syntax : crontab [-l | -r | -e] [filename]

© Oxford University Press 2013. All rights


at :Scheduling Commands at Specific Dates &
Times
 The at command is used for executing Unix commands at a
specific date and time.
Syntax : at [-f filename] [-m] [-l] [-r] time

© Oxford University Press 2013. All rights


batch :Executing Commands Collectively
 Batch command is used for issuing a set of commands that we want to
execute collectively (in a batch). The commands given in the batch will be
executed later when the system load permits, that is, when the CPU is
free, it will execute the commands specified by the batch command.
Syntax : batch [-f filename] [-m] [-l] [-r] time

© Oxford University Press 2013. All rights


nohup :No Hangups
 To ensure that the processes we have executed do not die
even when we log out, we use the nohup command.
nohup stands for ‘no hangups’.

Syntax : nohup command_name &

 Here, command_name represents the command name, shell


script, or command name and & ensures that the command is
run in the background.

© Oxford University Press 2013. All rights


nice :Modifying Priority
 The nice command is used for modifying the priority of the processes.
The priority of a process is decided by a number associated with it called
nice value. Higher the nice value of a process, lower is its priority.
Syntax : nice [-increment | -n increment ] command
[argument…]

© Oxford University Press 2013. All rights


kill :Killing Processes
 Killing a process means canceling the ongoing execution of a
command.
Syntax : kill [-s signal_name][-l] PID

© Oxford University Press 2013. All rights


Signals
 A signal is a technique of informing or sending notifications to a
process about a particular event that requires immediate
action. Each signal has a default action associated with it, which
can be changed, so that different signals can take different
actions.

© Oxford University Press 2013. All rights


Signals

© Oxford University Press 2013. All rights


Signals
 Synchronous signals These signals are also known as traps and
usually occur when an unrecoverable error such as an illegal
instruction (say, division by zero) or an invalid address reference
occurs. The occurrence of a trap is handled by the kernel trap
handler to be delivered to the thread that caused the error.

 Asynchronous signals These occur when an external event


happens, for example, when a process or thread sends a signal
via kill(), and then the user puts the process to sleep. On the
occurrence of such a signal, it is delivered to the desired
process.

© Oxford University Press 2013. All rights


Classes of Signals
Reliable Signals
Old Unix system signals were unreliable. A frequent problem was race
condition (i.e., receiving another signal while the current signal was still being
handled). The recent method available for signal implementation is quite
reliable and more functional. The signals are not lost and hence, are properly
handled. Besides this, the state of the signals can also be controlled—they can
be blocked, kept in pending state, or can be unblocked to deliver them
whenever needed.

Unreliable Signals
Unreliable signals are those that are lost or cannot work properly in the
absence of their signal handlers. For a signal to work properly, it is required
that its signal handler exists or is installed before the signal is generated. In
the absence of the signal handler, the default action is taken, that is, the signal
is lost or killed.
© Oxford University Press 2013. All rights
Sending Signals Using kill() & raise ()
The two functions used to send signals are kill() and raise().

kill()
The syntax for kill() is as follows:
Syntax : int kill(int pid, int signal)
Here, pid is the non-zero (positive) process identifi er to which the signal is
sent.

raise()
The syntax for raise() is as follows:
Syntax : int raise(int signal)
Here, signal represents the integer value or symbolic constant of the signal
that we wish to send to the executing program.

© Oxford University Press 2013. All rights


Signal Handling Using signal()
 The signal() function is used to define the signal and the signal
handler in the following syntax:
Syntax : int (*signal(int sig, void
(*sig_handler)()))()

Here, sig_handler is the function that is called on the


occurrence or receiving of the
signal, sig. The signal() function, when successful, returns a
pointer to the function sig_handler. When it fails, the
signal() function returns −1 and also sets the value to the
errno variable.

© Oxford University Press 2013. All rights


Compiling C Programs; alarm() & pause()
functions
 To compile a C program, we use the gcc command in the following format:
Syntax : $ gcc program.c –o executable_name
Here, program.c represents the C program that we wish to compile and
executable_name is the name by which we wish to create the executable file.
 alarm()
The alarm function is used for generating the SIGALRM signal. The function actually
sets a timer. When the time set through the timer expires, the function generates
the SIGALRM signal.
Syntax : unsigned int alarm(unsigned int seconds);
Here, seconds refers to the time in seconds, which when expired generates the
SIGALRM signal.
 Pause()
The pause function suspends the calling process until a signal is caught.
Syntax int : pause(void);
The pause function returns when a signal is caught and its signal handler is executed
© Oxford University Press 2013. All rights
abort and sleep functions
 abort function
The abort function causes abnormal program termination.
Syntax : void abort(void);

 sleep function
The sleep function suspends the execution of a process for the
specified interval of time.
Syntax : unsigned sleep(unsigned seconds);

© Oxford University Press 2013. All rights


Virtual Memory
 Virtual memory refers to the concept of running applications whose size
is larger than that of the primary memory. This is achieved by logically
splitting the application into small parts and keeping only the parts
currently focused by the user in the memory and the rest on the swap
area of the disk.
 Swap area is a part of the hard disk that is meant for holding swapped
pages. Pages, here, refer to the parts or logical pieces of the application.
 In the virtual memory technique, an application is assigned a virtual
address space, which is a block of memory addresses. When a part of
the application is supposed to run, its virtual memory space is mapped
to the physical address space (swapping in from the swap area to the
physical memory).
 Virtual memory is implemented in one of the following two ways:
1. Paging
2. Segmentation
© Oxford University Press 2013. All rights
Paging
 The entire application cannot be accommodated in the physical memory
at one go, the virtual address space assigned to the application is divided
into small chunks known as pages.
 To accommodate the pages, the physical memory is also divided into sizes
equal to the size of the pages. The corresponding range of consecutive
addresses in the physical memory is called page frame.
 Depending on the size of the primary memory, more than one page can
be kept in the corresponding page frames of the memory.

© Oxford University Press 2013. All rights


Demand Paging
 A page fault represents the specific page (part of the application) demanded by
the application. The process of swapping only the demanded page of the
application into the physical memory is known as demand paging.
p- Page number
d- offset

© Oxford University Press 2013. All rights


vtmstat : Fetching Virtual Memory
Information
 vmstat displays the virtual memory activity since the computer
system was booted.

Syntax : vmstat [-c] [-i] [-s] [-S] [interval]


The explanation of the syntax is as follows:
-c displays the number of cache fl ushed since booting.
-i displays the number of interrupts per device.
-s displays the total number of system events since booting.
-S displays swapping information.
Interval displays the virtual memory information after every interval
second. When used without options, the vmstat command
displays a summary of the virtual memory activity since booting.

© Oxford University Press 2013. All rights


Segmentation & Memory Mapped I/O
 Segmentation : segmentation is implemented by assigning
segments or blocks of memory of variable size to the desired
process with gaps between the segments. A process may have
one or more segments. In fact, the process address space
consists of different segments such as the main code
segment, the functions segment, the library code segment,
the table segment, and the stack segment.

 Memory-mapped Input / Output : Memory-mapped I/O


is a method used to perform an I/O operation between the
CPU and I/O devices (i.e., peripheral devices).

© Oxford University Press 2013. All rights

You might also like