UNIT-4: P.Ram Santosh Naidu Assistant Professor MVGR (A) - Cse Subject Duos (Design of Unix Operating Systems)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 70

UNIT-4

P.RAM SANTOSH NAIDU


SUBJECT
ASSISTANT PROFESSOR
DUOS
MVGR(A)-CSE
(DESIGN OF UNIX OPERATING SYSTEMS)
UNIT-4

Topics to be covered:
Process Control:
Process Creation, Signals, Process Termination, Awaiting Process termination, invoking other programs,
The User ID of a process, Changing the size of a process, The Shell, System boot and the INIT process.

02/22/2022 Santosh Naidu P 2


UNIT-4

PROCESS CONTROL

02/22/2022 Santosh Naidu P 3


PROCESS Vs PROGRAM
“A process is an instance of a running program”
Process vs. Program
Process > program
 Program is just part of process state
 Example: many users can run the same program
 Each process has its own address space, i.e., even though program has single set of variable names, each
process will have different values
Process < program
 A program can invoke more than one process
 Example: Fork

We will learn about the usage and implementation of the system calls that control the process context.

02/22/2022 Santosh Naidu P 4


02/22/2022 Santosh Naidu P 5
CONTROL OF PROCESS CONTEXT
fork : create a new process
exit : terminate process execution
wait : allow parent process to synchronize its execution with the exit of a child process
exec : invoke a new program
brk : allocate more memory dynamically
signal : inform asynchronous event
RELATIONSHIP BETWEEN THE SYSTEM CALLS AND THE MEMORY MANAGEMENT ALGORITHMS.

System Calls Dealing with Memory Management System Calls Dealing with Synchronization Miscellaneous

fork exec brk exit wait signal kill setpgrp setuid


dupreg detachreg growreg detachreg
allocreg
attachreg
attachreg
growreg
loadreg
mapreg
02/22/2022 Santosh Naidu P 6
DIFFERENT KERNEL CONTEXTS

Different Kernel Contexts


Entire kernel code can be divided into three categories.

 Process Context

 Interrupt Context

 Kernel Context

02/22/2022 Santosh Naidu P 7


DIFFERENT KERNEL CONTEXTS

Process Context:
 User applications cant access the kernel space directly but there is an interface using which
user applications can call the functions defined in  the kernel space. This interface is known as system
call . A user application can  request for kernel services using a system call.
 read() , write() calls are examples of a system call. A user application calls read() / write() , that in
turn invokes sys_read() / sys_write() in the kernel space . In this case kernel code executes on the
request of user space application. So, the kernel code that executes on the request or on behalf of a
user application is called process context code. All system calls fall in this category.

02/22/2022 Santosh Naidu P 8


DIFFERENT KERNEL CONTEXTS

Interrupt Context:
 Whenever a device wants to communicate with the kernel, it sends an interrupt signal to the
kernel. 
 The moment kernel receives an interrupt request from the hardware, it starts executing some
routine in the response to that interrupt request.
 This response routine is called as interrupt service routine or an interrupt handler. Interrupt
handler routine is said to execute in the interrupt context.

02/22/2022 Santosh Naidu P 9


DIFFERENT KERNEL CONTEXTS

Kernel Context:
 There is some code in the Linux kernel that is neither invoked by a user application nor it is
invoked by an interrupt.
 This code is integral to the kernel and keeps on  running always . Memory
management  , process management , I/O schedulers , all that code lies in  this category. This
code is said to execute in the kernel context.

02/22/2022 Santosh Naidu P 10


PROCESS CREATION

Process Creation:
 The only way for a user to create a new process in the UNIX operating system is to
invoke the fork system call.
 The process that invokes fork is called the parent process, and the newly created
process is called the child process.
 The syntax for the fork system call is
pid = fork();

02/22/2022 Santosh Naidu P 11


PROCESS CREATION

Process Creation:
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose
of fork() is to create a new process, which becomes the child process of the caller. After a new child
process is created, both processes will execute the next instruction following the fork() system call.
Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value
of fork():
Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: returns a the process ID of the child process, to the parent.

02/22/2022 Santosh Naidu P 12


PROCESS CREATION

Process Creation:

02/22/2022 Santosh Naidu P 13


PROCESS CREATION

Process Creation:
Sequence of Operations for fork:
1. It allocates a slot in process table for the new process
2. It assigns a unique ID number to the child process
3. It makes a logical copy of the context of the parent process. Since certain portion of process, such
as the text region, may be shared between processes, the kernel can sometimes increment a region
reference count instead of copying the region to a new physical location in memory
4. It increments file and inode table counters for files associated with the process.
5. It returns the ID number of child process to the parent process, and a 0 value to the child process.

02/22/2022 Santosh Naidu P 14


PROCESS CREATION
Algorithm for fork
input : none
output : to parent process, child PID number
to child process, 0
{
check for available kernel resources;
get free process table slot, unique PID number;
check that user not running too many process;
mark child state "being created";
copy data from parent process table to new child slot;
increment counts on current directory inode and changed root(if applicable);
increment open file counts in file table;
make copy of parent context(u area, text, data, stack) in memory;
push dummy system level context layer onto child system level context;
dummy context contains data allowing child process
to recognize itself, and start from here
02/22/2022
when scheduled; Santosh Naidu P 15
PROCESS CREATION

Process Creation:
Algorithm for fork

if ( executing process is parent )


{
change child state to "ready to run";
return(child ID); /* from system to user */
}
else /* executing process is the child process */
{
initialize u area timing fields;
return(0); /* to user */
}
}

02/22/2022 Santosh Naidu P 16


PROCESS CREATION
Fork Creating a New Process Context
Parent Process
File
Per Process U Area Table
Parent Open Files
Region Table
Data Current Directory
Changed Root
Parent .
.
User .
.
Stack .
Kernel Stack

Shared
Text
Inode
Per Process U Area Table
Open Files
Child Region Table
Current Directory
Data
Changed Root
Child .
.
User .
.
Stack .
Kernel Stack
Child Process
02/22/2022 Santosh Naidu P 17
PROCESS CREATION
Process Creation:
Example-1:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main() {
     // make two process which run same
    // program after this instruction
    fork();
     printf("Hello world!\n");
    return 0;
}
02/22/2022 Santosh Naidu P 18
PROCESS CREATION
Process Creation:
Example-2:
#include  <stdio.h>
#include  <sys/types.h>
int main()
{
    fork();
    fork();
    fork();
    printf("hello\n");
    return 0; NOTE: For Examples please refer lab manual of week-3
} 02/22/2022 Santosh Naidu P 19
PROCESS CREATION

02/22/2022 Santosh Naidu P 20


PROCESS CONTROL
Fork/exit/wait example:

02/22/2022 Santosh Naidu P 21


SIGNALS

Signals inform processes of the occurrence of asynchronous events.


A signal is a small message that notifies a process that an event of some types has occurred in the system.
 Kernel Abstraction for exceptions and interrupts
 Sent from Kernel (sometimes at the request of another process) to a process.
 Different signals are identified by small integers.
 The only information in a signal is its ID and the fact that it arrived.

Processes may send each other signal with the kill system call, or the kernel may send signals internally.
There are 19 signals in UNIX system

02/22/2022 Santosh Naidu P 22


SIGNALS

02/22/2022 Santosh Naidu P 23


SIGNALS
Algorithm for recognizing signals

02/22/2022 Santosh Naidu P 24


SIGNALS

 A UNIX signal corresponds to an event


 It is raised by one process (or hardware) to call another process’s attention to an event
 UNIX has a fixed set of signals (Linux has 32 of them)
 signal.h defines the signals in the OS
 Appl. programs can use SIGUSR1 & SIGUSR2 for arbitrary signalling
 Raise a signal with kill(pid, signal)
 3 ways to handle a signal
 Ignore it: signal(SIG#, SIG_IGN)
 Run the default handler: signal(SIG#, SIG_DFL)
 Run the user handler: signal(SIG#, myHandler)
02/22/2022 Santosh Naidu P 25
SIGNALS
SIGNAL HANDLING

/* code for process p */ void sig_hndlr(...){


. . . …
signal(SIG#, sig_hndlr); /* handler code */
. . .
/* ARBITRARY CODE */ }

02/22/2022 Santosh Naidu P 26


SIGNALS

02/22/2022 Santosh Naidu P 27


SIGNALS

Signals:

Some of the important signals as follows:

02/22/2022 Santosh Naidu P 28


SIGNALS

Signals:
HANDLING SIGNALS:

02/22/2022 Santosh Naidu P 29


SIGNALS

HANDLING SIGNALS:
1. The kernel accesses the user saved register context, finding the program counter and stack pointer that it had
saved for return to the user process.
2. It clears the signal handler field in the u area, setting it to the default state.
3. The kernel creates a new stack frame on the user stack, writing in the values of the program counter and stack
pointer it had retrieved from the user saved register context and allocating new space, if necessary.
a. The user stack looks as if the process had called a user-level function (the signal catcher) at the point where it
had made the system call or where the kernel had interrupted it (before recognition of the signal).
4. The kernel changes the user saved register context: It resets the value for the program counter to the address of
the signal catcher function and sets the value for the stack pointer to account for the growth of the user stack.

02/22/2022 Santosh Naidu P 30


SIGNALS
HANDLING SIGNALS:
signal(SIGINT, INThandler);
signal(SIGQUIT, QUIThandler);
void INThandler(int sig)
{
// SIGINT handler code
}
void QUIThandler(int sig)
{
// SIGQUIT handler code
}
02/22/2022 Santosh Naidu P 31
SIGNALS

Signals:
How to catch Interrupt Signals?
If a handler has been established for the signal,
the do_signal( ) function must enforce its
execution.
It does this by invoking handle_signal( ):

handle_signal(signr, &info, &ka, oldset, regs);

02/22/2022 Santosh Naidu P 32


SIGNALS
How to catch Interrupt Signals?
The setup_frame( ) function receives four parameters, which have the following meanings:

sig
Signal number

ka
Address of the k_sigaction table associated with the signal

oldset
Address of a bit mask array of blocked signals

regs
Address in the Kernel Mode stack area where the User Mode register contents are saved

02/22/2022 Santosh Naidu P 33


SIGNALS

How to catch Interrupt Signals?


A frame is a sigframe table that includes the following fields

02/22/2022 Santosh Naidu P 34


SIGNALS
PROCESS GROUPS:
 A process group is a collection of one or more processes (usually associated with the same job) that can
receive signals from the same terminal.
 Each process group has a unique process group ID. Process group IDs are similar to process IDs: they
are positive integers and can be stored in a pid_t data type.
 The function getpgrp returns the process group ID of the calling process. The getpgid function took
a pid argument and returned the process group for that process.
#include <unistd.h>
pid_t getpgrp(void);
/* Returns: process group ID of calling process */
pid_t getpgid(pid_t pid);
/* Returns: process group ID if OK, −1 on error */
02/22/2022 Santosh Naidu P 35
SIGNALS

PROCESS GROUPS:
For getpgid, if pid is 0, the process group ID of the calling process is returned. Thus,
getpgid(0);
is equivalent to:
getpgrp();
Each process group can have a process group leader, whose process group ID equals to its
process ID.

02/22/2022 Santosh Naidu P 36


SIGNALS
PROCESS GROUPS:

Process group lifetime:

 The process group life time is the period of time that begins when the group is created and ends when the

last remaining process leaves the group.

 It is possible for a process group leader to create a process group, create processes in the group, and then

terminate.

 The process group still exists, as long as at least one process is in the group, regardless of whether the

group leader terminates.

 The last remaining process in the process group can either terminate or enter some other process group.
02/22/2022 Santosh Naidu P 37
SIGNALS
PROCESS GROUPS:
setpgid function
A process can join an existing process group or creates a new process group by calling setpgid.
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
The setpgid function sets the process group ID of the process whose process ID equals pid to pgid.
Arguments:
 If pid == pgid, the process specified by pid becomes a process group leader.
 If pid == 0, the process ID of the caller is used.
 If pgid == 0, then the specified pid is used as the process group ID.

02/22/2022 Santosh Naidu P 38


SIGNALS
PROCESS GROUPS:

02/22/2022 Santosh Naidu P 39


SIGNALS

PROCESS GROUPS:

02/22/2022 Santosh Naidu P 40


SIGNALS
To Send Signal to a Process:
Use Unix system call kill() to send a signal to another process:
int kill(pid_t pid, int sig);
The kill command can also be used to send a signal to a process:
kill –l /* list all signals */
kill –XXX pid1 pid …… pid
 In the above XXX is the signal name without the initial letters SIG.
 kill –KILL 1357 2468 kills process 1357 and 2468.
 kill –INT 6421 sends a SIGINT to process 6421.
 A kill without a signal name is equivalent to SIGTERM.
 -9 is equal to –SIGKILL.
02/22/2022 Santosh Naidu P 41
SIGNALS
To Send Signal to a Process:
 If pid is a positive integer, the kernel sends the signal to the process with process ID pid.
 If pid is 0, the kernel sends the signal to all processes in the sender's process group.
 If pid is -1, the kernel sends the signal to all processes whose real user lD equals the effective user ID of
the sender. If the sending process has effective user ID of super user, the kernel sends the signal to all
processes except processes 0 and 1.
 If pid is a negative integer but not - 1 , the kernel sends the signal to all processes in the process group
equal to the absolute value of pid.
 In all cases, if the sending process does not have effective user ID of super user, or its real or effective
user ID do not match the real or effective user ID of the receiving process, kill fails.

02/22/2022 Santosh Naidu P 42


PROCESS TERMINATION

Processes on a UNIX system terminate by executing the exit system call. An exiting process enters

the zombie state, relinquishes its resources, and dismantles its context except for its slot in the

process table. The syntax for the call is

exit (status);

Where the value of status is returned to the parent process for its examination.

02/22/2022 Santosh Naidu P 43


PROCESS TERMINATION

02/22/2022 Santosh Naidu P 44


PROCESS TERMINATION

AWAITING PROCESS TERMINATION:


A process can synchronize its execution
with the termination of a child process by
executing the wait system call. The syntax
for the system call is
pid = wait(stat_addr);
Where pid is the process ID of the zombie
child, and stat_addr is the address in user
space of an integer that will contain the
exit status code of the child.
02/22/2022 Santosh Naidu P 45
INVOKING OTHER PROGRAMS

 The exec system call invokes another program, overlaying the memory space of a process with a copy
of an executable file. The contents of the user-level context that existed before the exec call are no
longer accessible afterward except for exec's parameters, which the kernel copies from the old
address space to the new address space.
 The syntax for the system call is execve(filename, argv, envp)
 There are several library functions that call the exec system call such as execl, execv, execle, and so
on. All call execve eventually; hence it is used here to specify the exec system call.
 When a program uses command line parameters, as in main (argc, argv) The array argv is a copy of
the argv parameter to exec. The character strings in the environment are of the form "name-value"
and may contain useful information for programs, such as the user's home directory and a path of
directories to search for executable programs.
02/22/2022 Santosh Naidu P 46
INVOKING OTHER PROGRAMS

02/22/2022 Santosh Naidu P 47


INCREASING THE SIZE OF THE PROCESS

 A process may increase or decrease the size of its data region by using the brk system call. The
syntax for the brk system call is brk(endds); where endds becomes the value of the highest virtual
address of the data region of the process (called its break value).
 Alternatively, a user can call oldendds = sbrk(increment); where increment changes the current
break value by the specified number of bytes, and oldendds is the break value before the call.

02/22/2022 Santosh Naidu P 48


INCREASING THE SIZE OF THE PROCESS

02/22/2022 Santosh Naidu P 49


THE USER ID OF A PROCESS

 The kernel associates two user IDs with a process, independent of the process lD: the real user ID
and the effective user ID or setuid (set user ID).
 The real user lD identifies the user who is responsible for the running process.
 The effective user ID is used to assign ownership of newly created files, to check file access permissions, and
to check permission to send signals to processes via the kill system call.
 The syntax for the setuid system call is setuid(uid); where uid is the new user ID, and its result depends on
the current value of the effective user ID. If the effective user ID of the calling process is super user, the
kernel resets the real and effective user ID fields in the process table and u area to uid.
 If the effective user ID of the calling process is not super user, the kernel resets the effective user ID in the u
area to uid if uid has the value of the real user ID or if it has the value of the saved user ID.

02/22/2022 Santosh Naidu P 50


THE USER ID OF A PROCESS

02/22/2022 Santosh Naidu P 51


SHELL

 The Linux/Unix shell is a command-line interface which lets its users to interact with the operating
system by accepting commands from the users through the keyboard; the shell executes the
commands and prints the output on the screen.
 Unlike the graphical environment (GUI based) that we would normally observe in most of the
present computers, the interaction is purely text-based and being command-oriented this kind of
interface is called “Command Line interface or CLI”.
 Before GUI environments were introduced in computer systems, CLI was the only way using which
an user could interact and operate the computer system.

02/22/2022 Santosh Naidu P 52


SHELL
MOST COMMONLY USED TERMS:

Process: Any task performed by the user within the system is a process

X-windows or windows: In Linux, the screen can be divided in small portions called windows. It allows users to

perform several tasks at a time. Also it becomes easy while switching from one task to another in a nice graphical

way.

Text terminal: It is the screen through which user can enter commands in text format to instruct the OS to

perform a task.

Session: Time span between system log in and log off.


02/22/2022 Santosh Naidu P 53
TYPES OF SHELLS

Bourne Shell:

It was developed by Stephen Bourne at Bell Laboratories as an alternative for Thompson Shell and

it still remains as a default shell for most of the Unix-like operating systems. Every Unix-like

operating system consists at least one shell which is compatible with Bourne Shell. Unlike C and

Korn Shells, Bourne Shell doesn’t have complex programming constructs and interactive

features. sh is the name of Bourne Shell program and it is situated at /bin/sh. 

02/22/2022 Santosh Naidu P 54


TYPES OF SHELLS

C Shell:

C shell was developed by Bill Joy at University of California as a replacement for the oldest Unix shell-

Bourne Shell. csh is the name of C Shell program and % is the shell prompt. It was introduced for those

programmers who prefer syntax similar to those used in C language.  

02/22/2022 Santosh Naidu P 55


TYPES OF SHELLS

Korn Shell:

Korn Shell was invented by David Korn at Bell Laboratories as a combined flavor of other Unix shells.

It has all the features of C Shell and scripting language very much similar as that of Bourne Shell. It is

considered as a member of Bourne Shell family as it uses the shell prompt of Bourne Shell, the $ symbol.

Korn shell is the easiest to learn and hence preferred by most of the inexperienced users. 

02/22/2022 Santosh Naidu P 56


TYPES OF SHELLS

The BASH Shell:

BASH is the acronym for Bourne-Again Shell, pun on the name of developer of Bourne Shell - Stephen

Bourne and a description of itself as bashing sh, csh and ksh together. It was developed by Brian Fox for

GNU Project and as a free software alternative for Bourne Shell. BASH has been distributed all over as a

shell for GNU operating system and observed as a default shell on Linux and MacOS X.

02/22/2022 Santosh Naidu P 57


BOOT AND INIT PROCESS

BOOT PROCESS: (ALGORITHM)


 Kernel initializes its internal data structures.
 eg., Constructs linked list of free buffers and inodes, constructs hash queues for buffers and inodes,
initializes region structures , page table entries and so on.
 Then it mounts the root file system onto root (“/”) and fashions the environment for process 0
 Creating a new u area
 Initializing slot 0 in the process table
 Making root the current directory of process 0

02/22/2022 Santosh Naidu P 58


BOOT AND INIT PROCESS
BOOT PROCESS: (ALGORITHM)
 When the environment of process 0 is set up, the system is running as process 0.
 Process 0 forks invoking the fork algorithm directly from the kernel since it is executing in the
kernel mode.
 New process, process 1, running in kernel mode, creates its user-level context by allocating a data
region and attaching it to its address space.
 Grows the region to its proper size and copies code from the kernel address space to the new
region.
 Sets up the saved user register context, and “returns” from kernel to user mode , and executes the
code it had just copied.
 Process 1 is a user-level process whereas process 0 is a kernel-level process.
02/22/2022 Santosh Naidu P 59
BOOT AND INIT PROCESS

BOOT PROCESS: (ALGORITHM)


 The text for process 1 is copied from the kernel consists of
 a call to exec system call
 to execute the process /etc/init
 Process 1 calls exec and executes the program in the normal fashion.
 Process 1 is called the “init” process, because it is responsible for initialization of
new processes.

02/22/2022 Santosh Naidu P 60


INIT PROCESS

02/22/2022 Santosh Naidu P 61


INIT PROCESS

02/22/2022 Santosh Naidu P 62


INIT PROCESS

02/22/2022 Santosh Naidu P 63


INIT PROCESS

02/22/2022 Santosh Naidu P 64


INIT PROCESS

02/22/2022 Santosh Naidu P 65


INIT PROCESS

02/22/2022 Santosh Naidu P 66


INIT PROCESS

 Once kernel and drivers are loaded, Linux starts loading the rest of the system. This starts with the First
Process, known as init and it has the process id of 1 (the kernel itself has the process id of 0, which cannot
be displayed by using the "ps" command).
 The init process takes control of the boot operation. The init process in turn runs /etc/rc.d/rc.sysinit,
which performs a number of tasks, including network configuration, SELinux status, keyboard maps,
system clock, partition mounts, and host names.
 The runlevels are controlled by a configuration file which init process reads from the location /etc. The
name of the init configuration file is "inittab".
 The init process then determines the runlevel by looking at the initdefault directive in /etc/inittab
configuration file. The following are the defined runlevels. The init process remains active as long as the
system is running.
02/22/2022 Santosh Naidu P 67
INIT PROCESS

 Runlevel value Description

0 Halt

1 Single-user mode

2   Multiuser, with some network services

3   Multiuser, with networking

4 Unused

5 Full Multiuser mode with X Windows (GUI login screen)

6 Reboot

02/22/2022 Santosh Naidu P 68


OTHER TYPES OF PROCESSESS

 Processes in UNIX are either user processes, daemon processes or kernel processes.
 User Processes: Most processes are user processes associated with users at a terminal Daemon Processes: Not
associated with any users, but do system-wide functions, such as administration and control of networks, executing of
time-dependent activities, line printer spooling and so on.
 Init may spawn daemon processes that exist throughout the lifetime of a system, or on occasion users may spawn
them
 Kernel Processes:
 Executes only in kernel mode
 Process 0 spawns kernel processes
 Similar to daemon processes but have greater control over their execution priorities.
 Extremely powerful since it can access kernel algorithms and data structures directly without the use of system

02/22/2022 Santosh Naidu P 69


THANK YOU!

P.RAM SANTOSH NAIDU


ASSISTANT PROFESSOR
MVGR(A)-CSE

P.RAM SANTOSH NAIDU, MVGR(A)-CSE 70

You might also like