UNIT-4: P.Ram Santosh Naidu Assistant Professor MVGR (A) - Cse Subject Duos (Design of Unix Operating Systems)
UNIT-4: P.Ram Santosh Naidu Assistant Professor MVGR (A) - Cse Subject Duos (Design of Unix Operating Systems)
UNIT-4: P.Ram Santosh Naidu Assistant Professor MVGR (A) - Cse Subject Duos (Design of Unix Operating Systems)
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.
PROCESS CONTROL
We will learn about the usage and implementation of the system calls that control the process context.
System Calls Dealing with Memory Management System Calls Dealing with Synchronization Miscellaneous
Process Context
Interrupt Context
Kernel Context
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.
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.
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.
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();
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.
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.
Process Creation:
Algorithm for fork
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
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
Signals:
Signals:
HANDLING 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.
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( ):
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
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.
The process group life time is the period of time that begins when the group is created and ends when the
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
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.
PROCESS GROUPS:
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
exit (status);
Where the value of status is returned to the parent process for its examination.
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
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.
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.
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.
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.
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
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
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.
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.
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
0 Halt
1 Single-user mode
4 Unused
6 Reboot
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