Sie sind auf Seite 1von 49

BY : MANISH ROHILA 2015

Unit 1
General overview of the system
Benefits of Unix

 The system is written in high level language “C” which makes it


easier to read understand and update.
 Complex programs can be built on simple programs on unix
systems.
 It has hierarchal file system which allows easy maintenance and
efficient implementation.
 It has consistent format for files.
 It provides simple and consistent interface to the peripheral
devices.
 It completely hides the machine architecture from the user .

System Structure

The unix system can be viewed as the set of layers. The lowermost
layer is the hardware layer which is not the part of the unix operating
system. The operating system is called the system kernel or the
kernel.

Kernel is the layer where the actual operating system code and
functionality resides. It is in complete isolation from the user
programs.
If a user program want to perform any task it can do so by talking to
the kernel. The programs interact with the kernel by using the system
calls. The system calls instruct the kernel to do various operations.

Other user programs can be built on top of the lower level programs
using these lower level programs and system calls .
BY : MANISH ROHILA 2015

User perspective – The file system

The characteristics of unix file system are

 A hierarchal structure.
 Consistent treatment of data
 Ability to create and delete files
 Dynamic growth of files
 Peripheral devices are also treated as files

The file system is organized as a tree. The root node is called “root”
and is denoted by “/”. Every non leaf node in this structure is a
directory and every leaf node is a file/special device file.

The name of the file is given by the path name.


BY : MANISH ROHILA 2015

Unix treats devices as if they are files. Every device is treated as


special files and occupy position in the file system. Devices are
protected in the same way as files i.e. using access pemissions.

Operating system Services

The kernel layer provides various oerations on behalf of user


processes. Some of the main services provided by the operating
systems kernel are:

 Process control: controlling the creating, termination and


suspension of processes.
 Scheduling processes: Since many programs can execute
simultaneously in unix the process scheduling is also done by the
kernel.
 Main memory management: allocating main memory to the user
programs and protecting the memory region where kernel is
running. Also, protecting the memory region of one process from
another process.
 Virtual memory: managing the swap device and handling the
swapping system. Controlling the pages in the paging
system(memory allocation)
 Secondary memory management: Managing the secondary
storage for the efficient and timely retrieval and storage of data.
BY : MANISH ROHILA 2015

 Peripheral devices: kernel controls the peripheral devices such as


terminals, disk drives and network devices.

Assumptions about the hardware

When a process executes on unix it executes on two levels .

 User level
 Kernel level

Processes in user mode can access their own instructions but not the
kernel instructions or the instruction of other processes. On the other
hand processes in kernel mode can access kernel data and instructions
as well as user data and instructions.

Introduction to kernel

Introduction to system concepts – overview of file subsystem


BY : MANISH ROHILA 2015

The internal representation of the file is in the form of iNode. This


inode contains the information about the file such as its layout on the
disk, its owner, its access permissions and last accessed time.

This inode is short form for index node. Every file has one inode. The
inodes of all the files on the system are stored in inode table. When we
create a new file a new entry in the inode table is created.

The kernel contain two data structures file table and user file
descriptor table. The file table is global table at the kernel level but the
user file descriptor table s for every process. When a process creates a
file or opens a file the entry for that is made in both the tables.

The information about the current state of the file is maintained in the
file table. For example if the file is being written the information about
the current cursor position is kept in the file table. This file table also
checks whether the accessing process has access to that file or not.

The user file descriptor table keeps a track of all the files opened by
the processes and what are the relationships between these files.

The regular files and folders are kept on block devices like disks and
tape drives. The drive has logical block numbers and physical block
numbers and the mapping from logical to physical block numbers is
done by disk driver.
BY : MANISH ROHILA 2015

File system layout

 The boot block occupies the beginning of the file system. This
contains the bootstrap code that is required for the machine to
boot.
 Super block describes the state of the file system i.e. its size,
maximum number of files that can be stored and the free space
information.
 The inode list contains the inode table and the kernel references
the inode list area to get the information about the files stored
on the machine.
 The data block is the end of the inode list and starting of the
blocks that can be used to store the user files. The starting area
of the data block will contain some administrative files and
information and the later block contains the actual files.

Introduction to system concepts – Process subsystem

A process on unix can be created by executing the fork system call.


Only the process 0 is created without using this system call and all
other processes are created using the fork system call. (Process 0 was
created manually by booting the system.)
The process that executes fork is called the parent process and the
process that was created is called the child processes. A process can
have many child but only one parent.
Kernel identifies every process by a unique identifier called the
process ID or PID.

Kernel data structures

The kernel data structures occupy fix size tables rather than
dynamically allocated space. This approach has one advantage, the
kernel code is simpler but there is one disadvantage of this approach
BY : MANISH ROHILA 2015

too. It limits the number of entries in these data structures.


if there are free entries in the kernel data structures we are wasting
the potential resources of kernel .

System Administration

Processes that do various functions performed for the general welfare


of the user community. Conceptually there is no difference between
administrative process and user process.
They only differ from user processes in rights and privileges they have.
So to summarize, kernel cannot distinguish between kernel process
and user process it is just the permission of processes and files that
some behave as administrative processes and some behave as the
user processes.

The Buffer Cache

we know that the files are stored on the hard drive and the processes
can access these files and create new files on the disk. When a process
requests for a file the kernel brings the file into the main memory
where user process can change, read or access the file.

The kernel could read and write the file directly from the hard disk and
put it in memory and vice versa but the response time and throughput
will be very low in this case because of disks slow data transfer speed.

To minimize the frequency of disk usage/access the kernel keeps a


buffer to store the recently accessed files and/or frequently accessed
files. This buffer is called the buffer cache.

When the process want to read a file the kernel attempts to read this
file in the buffer cache, if the data is found in the buffer cache the
data/file is sent to the process. If the file is not found in the buffer
cache then the file is read from the disk and then kept in the buffer
cache so that it can be made available to the process.

To minimize the disk access frequency the kernel may also implement
the pre-caching or write delay functionalities.

Buffer Headers

When the system initializes the kernel allocates the space for the
buffer cache. The buffer cache contains two regions/arts. One for the
data/files that will be read from the disk, second the buffer header.
BY : MANISH ROHILA 2015

There will never be a case when the buffer has two entries for the
same file on disk as this could lead to inconsistencies. There is only
only one copy of a file in the buffer.

Structure of the buffer pool

The kernel caches the least recently used data into the buffer pool.
Once a block from buffer pool is allocated for a file of the system this
block cannot be used for any other file’s data. The kernel also
maintains a free list of buffers. The free list is a doubly circular list of
buffers.

When kernel wants to allocate any buffer it removes a node from the
free list, usually from the beginning of list but is could take it from
middle of the list too. When kernel frees a node from the buffer list it
adds this free node at the end of the free list.
BY : MANISH ROHILA 2015

Scenarios of retrieval of buffer

The five typical scenarios that kernel may follow in getblk to allocate a
buffer in the disk block are

 Block in the hash queue, and its buffer is free.


 Cannot find block on the hash queue => allocate a buffer from
free list.
 Cannot find block on the hash queue => allocate a buffer from
free list but buffer on the free list marked “delayed write” =>
flush “delayed write” buffer and allocate another buffer.
 Cannot find block on the hash queue and free list of buffer also
empty.
 Block in the hash queue, but buffer is busy.
BY : MANISH ROHILA 2015

The first scenario - Block in the hash queue, and its buffer is free.
BY : MANISH ROHILA 2015

The second scenario - Cannot find block on the hash queue =>
allocate a buffer from free list.
BY : MANISH ROHILA 2015

The third scenario - Cannot find block on the hash queue =>
allocate a buffer from free list but buffer on the free list marked
“delayed write” => flush “delayed write” buffer and allocate another
buffer.

The fourth scenario - Cannot find block on the hash queue and free
BY : MANISH ROHILA 2015

list of buffer also empty.

 The fifth scenario - Block in the hash queue, but buffer is


busy.
BY : MANISH ROHILA 2015

the algorithms of buffer pool to manage the buffer cache. The


algorithm for reading and writing disk blocks uses the algorithm
getblk to allocate buffer from the pool.
BY : MANISH ROHILA 2015

Algorithms for Reading and writing disk blocks


BY : MANISH ROHILA 2015

The kernel may read the data, manipulate it and/or change it in the
buffer. While the kernel marks the buffer as busy so that no other
process can access this block. When the kernel is done using this block
it releases the buffer using brelse algorithm.

Advantages of the buffer cache

 Uniform disk access => system design simpler


 Copying data from user buffers to system buffers => eliminates
the need for special alignment of user buffers.
 Use of the buffer cache can reduce the amount of disk traffic.
 Single image of of disk blocks contained in the cache => helps
insure file system integrity

Disadvantages of the buffer cache

 Delayed write => vulnerable to crashes that leave disk data in


incorrect state
 An extra data copy when reading and writing to and from user
processes => slow down when transmitting large data
BY : MANISH ROHILA 2015

Unit -2

INTERNAL REPRESENTATION OF FILES


• Every file on a UNIX system has a unique inode (contains information
necessary for a process to access a file)
• Inodes exist in a static form on disk and the kernel reads them into an in-core
inode to manipulate them

Inodes
Consist of:
• File owner identifier (individual owner & group owner)
• File type (regular, directory, character or block special, or FIFO – pipes)
• File access permissions (owner, group, other: read, write, execute)
• File access time
• Number of links to the file
• Table of contents for the disk addresses of data in a file
• File size
Algorithm for allocation of in-core inodes
algorithm iget
input: file system inode number
output: locked inode
{
while (not done)
{
if (inode in inode cache)
{
if (inode locked)
{
sleep (event inode becomes unlocked);
continue; /*loop back to while */
}
/*special processing for mount points */
if (inode on inode free list)
remove from free list;
increment inode reference count;
return (inode);
}/
*inode not in inode cache */
if (no inodes on free list)
BY : MANISH ROHILA 2015

return (error);
remove new inode from free list;
reset inode number and file system;
remove inode from old hash queue, place on new one;
read inode from disk (algorithm bread);
initialise inode (e.g. reference count to 1);
return (inode);}

Directories
• Give the file system its hierarchical structure
• Important role in conversion of a file name to an inode number
• Initial access: open, chdir, link system calls
• Algorithm namei uses intermediate indoes as it parses a path name

Algorithm for conversion of path name to an inode


algorithm namei /* convert path name to inode */
input: path name
output: locked inode
{
if (path name starts from root)
working inode = root inode (algorithm iget);
else
working inode = current directory inode (algorithm iget);
while (there is more path name)
{
read next path name component from input;
verify that working inode is of directory, access permissions OK;
if (working inode is of root and component is “..”)
continue; /*loop back to while */
read directory (working inode) by repeated use of algorithms
bmap, bread and brelse;
if (component matches an entry in directory (working inode))
{
get inode number for matched component;
release working inode (algorithm iput);
working inode = inode of matched component (algorithm iget);
}
else /*component not in directory */
return (no inode);
}
return (working node);
}

Super block
Consists of:
• The size of the file system
BY : MANISH ROHILA 2015

• The number of free blocks in the file system


• A list of free blocks available on the file system
• The index of the next free block in the free block list
• The size of the inode list
• The number of free inodes in the file system
• A list of free inodes in the file system
• The index of the next free inode in the free inode list
• Lock fields for the free block and free inode lists
• A flag indicating that the super block has been modified

Algorithm for assigning new inodes


algorithm ialloc /* allocate inode */
input: file system
output: locked inode
{ while (not done)
{ if (super blocked locked)
{ sleep (event super block becomes free);
continue; /*loop back to while */
}
if (inode list in super block is empty)
{ lock super block;
get remembered inode for free inode search;
search disk for free inodes until super block full, or no more
free inodes (algorithms bread and brelse);
unlock super block;
wake up (event super block becomes free);
if (no free inodes found on disk)
return (no inode);
set remembered inode for next free inode search;
}
/*there are inodes in super block inode list */
get inode number form super block inode list;
get inode (algorithm iget);
if (inode not free after all)
{ write inode to disk;
release inode (algorithm iput);
continue;
}
/*inode is free */
initialise inode;
write inode to disk;
decrement file system free inode count;
return (inode);}
}
BY : MANISH ROHILA 2015

File System
Calls
Retur Use of Assig File File File Tree
nfile namei n Attribute I/O Sys Manipulati
desc inode s Structu on
open open stat creat
decs s re
creat creat link mkno chown read
File mount chdir
dup chdir unlink d chmod write
Desc umount chown
pipe chroot mkno link stat lseek
close chow d unlin
n mount k
chmo umou Lower Level File System Algorithms
d nt
namei Ialloc
alloc free bmap
iget iput ifree

Buffer allocation algorithms


getblk brelse bread breada bwirte

File System Calls and Relation to Other Algorithms

open
Syntax
 fd = open(pathname, flags, modes);
 pathname: file name
 flags : type of open
 modes : file permission if file is being created
 fd : File Descriptor

{
convert file name to inode(algorithm namei);
if(file does not exist or not permitted access)
return (error);
allocate file table entry for inode, initialize count, offset;
allocate user file descriptor entry, set pointer to file table entry;
if(type of open specifies truncate file)
free all file blocks (algorithm free);
unlock(inode) /* locked above in namei */
BY : MANISH ROHILA 2015

return(user file descriptor);}


}

Read
 number = read(fd, buffer, count)
 fd : file descriptor returned by open
 buffer : address of structure in the user process that will contain the
read data
 count : number of bytes the user wants to read
 number : number of bytes actually read
{
get file table entry from user file descriptor;
check file accessibility;
set parameters in u area for user address, byte count, I/O to user;
get inode from file table;
lock inode;
set byte offset in u area from file table offset
while(count not satisfied) {
convert file offset to disk block (algorithm bmap);
calculate offset into block, number of bytes to read;
if(number of bytes to read is 0)
/* trying to read end of file */
read block(algorithm breada if with read-ahead, algorithm bread
otherwise);
copy data from system buffer to user address;
update u area fields for byte offset,read count,address to write into user
space;
release buffer; /* locked in bread */
}
unlock inode;
update file table offset for next read;
return(total number of bytes read);

file creation

Goal
 Create a New File
Syntax
 fd = creat(pathname, modes);
Function
 If File does not exist
 kernel create new file with specified name and permission mode
 If File exists
BY : MANISH ROHILA 2015

 truncates the file( release all existing data blocks and sets the file
size to 0 ) subject to suitable file access permissions.

get inode for file name (algorithm namei);


if(file already exists)
{
if(not permitted access)
{
release inode (algorithm iput);
return(error);
}
}
else /* file does not exist yet */
{
assign free inode from file system(algorithm ialloc);
create new directory entry in parent directory; include new file name
and newly assigned inode number;
}
allocate file table entry for inode, initialize count;
if(file did exist at time of create)
free all file blocks (algorithm free);
unlock(inode);
return(user file descriptor)

operation of special file

Goal
 creates special files
 named pipes
 device files
 directories
Syntax
 mknod(pathname, type and permissions, dev)
 pathname: name of the node to be created
 type and permissions : node type and access permission
 dev: specifies the major and minor device numbers for block and
character special files
Function
 allocates inode for the file
Algorithm

if(new node not named pipe and user not super user)
return(error);
get inode of parent of new node ( algorithm namei );
if(new node already exists)
{
BY : MANISH ROHILA 2015

release parent inode ( algorithm iput );


return(error);
}

assign free inode from file system for new node (algorithm ialloc);
create new directory entry in parent directory
: include new node name and newly asigned inode nubmer;
release parent directory inode (algorithm iput);

if(new node is block or character special file)


write major, minor numbers into inode structure;

release new node inode (algorithm iput);

change directory

Goal
 Change the current directory of a process
Syntax
 chdir(pathname);
 pathname : directory that becomes the new current direcotry of the
process
Algorithm

get inode for new directory name (algorithm namei);


if(inode not that of directory or process not permitted access to file)
{
release inode (algorithm iput);
return(error);
}
unlock inode;
release "old" current directory inode (algorithm iput);
replace new inode into current directory slot in u area;

change root

Goal
 Change the notion of the file system root
Syntax
 chroot(pathname);
 pathname: Directory that the kernel subsequently treats as the
process’s root directory.
Algorithm
get inode for new directory name (algorithm namei);
if(inode not that of directory or process not permitted access to file)
{
BY : MANISH ROHILA 2015

release inode (algorithm iput);


return(error);
}
unlock inode;
release "old" current directory inode (algorithm iput);
replace new inode into current directory slot in u area;}

Pipes system call:


A pipe allows transfer of data between processes in a first in first out manner and they
also allow synchronization of process execution. This allows process to communicate
even though they do know what process are on the other end of the pipe.
There are two types of pipes-
1 Named pipes
2 Unamed pipes

Named pipes are used when the process are not closely related wants to communicate .
A named pipe is accessed by its pathname . the processes can use a named pipe either
for reading or writing .a process open a named pipe like a file . An unnamed pipe can
only be used by closely related process .
The systax for creation of pipe is –
Pipe(fdptr);
Where fdptr is pointer to integer array that will contain the two file descriptor for reading
and writing the pipe.
Algorithm

Algo : pipe
Input : none
Output: read file descriptor ,write file descriptor
{
Assign new inode from pipe device(algo ialloc);
Allocate the table entry for reading,another for writing ;
Initialize file table entries to point to new inode;
Allocate user dile descriptor for reading,another for writing,initialize to point to
respective file table entries;
set node reference count to 2;
initialize count of inode readers,writers to 1;
}

Mounting and unmounting file system:

Mounting file system:


Mount system call allows user to access data in a disk section as
a file system instead of a sequence of disk blocks. The systax for the mount system call
is-
Mount(special pathname,directory pathname,option);
BY : MANISH ROHILA 2015

Where special pathname is the name of device special file of the disk section containing
the file system to be mounted,directory pathname is the directory in the existing
hierarchy where the file system will be mounted and option indicate whether the file
system should be mounted “read only”.
Algorithem

Algo: mount
Inputs:file name of block special file,directory name of mount point,options(read only)
Output:none
{
If(not super user)
Return(error);
Get inode for block spcial file(algo namei);
Make legality checks:
Get inode for ”mounted on” directory name(algo namei);
If(not directory,or reference count>1)
{
Release inodes (algo iput);
Return(error);
}
Find empty slot in mount table;
Invoke block device open routine;
Get free buffer from buffer cache;
Read super block into free buffer;
Initialize super block fields;
Get root inode of mounted device(algo iget)
Save in ount table;
Mark inode of “mounted on” directory as mount point;
Realese special file inode(algo iput);
Unlock inode of mount point directory;
}
BY : MANISH ROHILA 2015

UNMOUNT SYSTEM CALL

LINK AND UNLINK SYSTEM CALL

Link system call:


Algo link
BY : MANISH ROHILA 2015

Input:existing file name,new file name


Output:none
{
Get inode for existing file name (algo namei);
If(too many links on file or linking directory without super user permission)
{
Release inode(algo iput);
Return(error);
}
Increment link count on inode;
Update disk copy of inode;
Unlock inode;
Get parent inode for directory to contain new file name (algo namei);
If(new file name already exists or existing file,new file on different file system)
{
Undo update done above;
Return(error);
}
Create new directory entry in parent directory of new file name:
Include new file name,inode number of exixting file name;
Release parent directory inode(algo iput );
Release inode of existing file(algo iput);
}
Unlink system call:
Algo unlink
Input:file name
Output :none
{
Get parent inode of file to be unlinked(algo namei);
If(last component of file name is “.”)
Increment inode reference count;
Else
Get inode of file to be unlinked (algo iget);
If(file is directory but user is not super user)
{
Release inode(algo iput);
Return(error);
}
If(shred text file and link count cuuently 1)
Remove from region table;
Write parent directory :zero inode number of unlinked file;
Release inode parent directory (algo iput);
Decrement file link count;
Release file inode(algo iput);
}
BY : MANISH ROHILA 2015

Unit 3

Process states and transitions


The life time of a process can be conceptually divided into a set of states that describes the
process.
The following list contains the complete set of process states :
1. The process is executing in user mode.
2. The process is executing in kernel mode.
3. The process is not executing but is ready to run as soon as the kernel schedules it.
4. The process is sleeping and resides in main memory.
5. The process is ready to run, but the swapper (process 0) must swap the process into main
memory before the kernel schedule it to execute.

6. The process is sleeping , and the swapper has swapped the process to secondary storage to
make room for other processes in main memory .
7. The process is returning from the kernel to user mode, but the kernel preempts it and does
a context switch to schedule another process.
8. The process is newly created and is in a transition state; the process exists, but it is not
ready to run, nor is it sleeping .This state is the start state for all processes except process 0.
9. The process executed the exit system call and is in the zombie state, the process no longer
exists, but it leaves a record containing an exit code and some timing statistics for its parent
process.
BY : MANISH ROHILA 2015

Layout of System Memory


BY : MANISH ROHILA 2015

Assume that the physical memory of machine is addressable, starting at byte offset 0 and
going up to a byte offset equal to amount of memory on the machine.
A process on the unix system consists of three logical sections: text, data and stack.
The text section contains the set of instructions the machine executes for the process;
o address in the text section includes.
o text addresses (for branch instructions or subroutines calls)
o data addresses (for access to global data variables) or
o stack addresses (for access to data structures local to subroutines).
If the machine were to treat the generated addresses as address location in physical memory,
o it would be impossible for two processes to execute concurrently if their set of generated
addresses overlapped .
The compiler could generate addresses that did not overlap between programs that is
impractical for general purpose computer because the amount of memory on a machine is
finite and the set of all programs that could be compiled is infinite.
If compiler used heuristics to try to avoid unnecessary overlap of the generated addresses, the
implementation would be inflexible and therefore undesirable.
The compiler therefore generates addresses for a virtual addresses space with a given address
range, and the machines memory management unit translates the virtual addresses generated
by the compiler in to address location in the physical memory.
o the compiler does not have to know where in memory the kernel will later load the
program for execution.
o several copies of a program can coexists in memory:
o all executes using the virtual addresses but reference different physical addresses
The Context of a Process:

The context of a process consists of the contents of its (user) address space and the contents
of hardware registers and kernel data structure that relate to the process.
Formally, the context of a process is the union of its user-level context, register context, and
system-level context.
The user-level context consists of the process text, data, user stack and shared memory that
occupy the virtual address space of the process.
o Parts of the virtual address space of a process that periodically do not reside in main
memory because of swapping or paging still constitute a part of the user-level context.
The register context consists of the following components:
o The program counter specifies the address of the next instruction the CPU will execute;
the address is a virtual address in kernel or in user memory space.
o The processor status register (PS) specifies the hardware status of the machine as it relates
to the process.
For e.g. the PS usually contains subfields to indicate that the result of a recent
computation resulted in a zero, positive, or negative result or that a register overflowed, and a
carry bit is set.
Other subfields indicate, the current processor execution level (for interrupts) and the current
and most recent modes of execution (such as kernel, user).
Most recent modes determine whether a process can execute privileged instructions and
whether it can access kernel address space.
BY : MANISH ROHILA 2015

o The stack pointer (SP) contains the current address of the next entry in the kernel or user
stack, determined by the mode of execution.
Machine architectures dictate whether the stack pointer points to the next free entry on the
stack or to the last used entry.
o The general-purpose registers contain data generated by the process during its execution.
The system-level context of a process has a “static part” and a “dynamic part”.
o A process has one static part of the system-level context throughout its life time but it can
have a variable number of dynamic parts.
o The dynamic part of the system-level context should be viewed as a stack of context layers
that the kernel pushes and pops on occurrence of various events.
The system-level context consists of the following components:
o The process table entry of a process defines the state of a process and contains control
information i.e. always accessible to the kernel.
o The u area of a process contains process control information that need be accessed only in
the context of a process.
General parameters such as process priority are stored in the process table because they
must be accessed outside the process context.
o Pregion entries, region tables and page tables, define the mapping from virtual to physical
addresses and therefore define text, data, stack and other regions of a process.
If several processes share common regions, the regions are considered part of the context
of each process, because each process manipulates region independently.
o The kernel stack contains the stack frames of kernel procedures as a process executes in
kernel mode.
All processes execute the identical kernel code, but each process has its own private copy
of the kernel stack – these specify their particular invocation of kernel function.
For e.g. one process may invoke the creat system call, and go to sleep waiting for the
kernel to assign a new inode, and another process may invoke read system call and go to
sleep awaiting the transfer of data from disk to memory.
Both processes execute the kernel functions, but they have separate stacks that contain their
private function call sequence.
The kernel must be able to recover the contents of the kernel stack and position of the
stack pointer to resume execution of a process in kernel mode.
System implementation frequently places the kernel stack in the process u area, but it is
logically independent and can exist in an independently allocated area of memory.
The kernel stack is empty when the process executes in user mode.
o The dynamic part of the system-level context of a process consists of a set of layers,
visualized as a last-in-first-out stack.
Each system-level context layer contains the necessary information to recover the
previous layer, including the register context of the previous level.
The kernel pushes a context layer when an interrupt occurs, when a process makes a system
call, or when a process does a context switch.
It pops a context layer when the kernel returns from handling an interrupt, when a process
returns to user mode after the kernel completes the execution of a system call, or when a
process does a context switch.
The context switch thus entails a push or a pop of a system-level context layer:
o The kernel pushes the context layer of the old process and pops the context layer of the
new process.
BY : MANISH ROHILA 2015

o The process table entry stores the necessary information to recover the current context
layer.

Process Creation
The only way for a user to create a new process in the Unix OS 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( );
BY : MANISH ROHILA 2015

Process Termination
Processes on a UNIX system terminate by executing the exit system call.
An existing 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.

THE USER ID OF A PROCESSES:


The kernel associate two user id’s with a process.
 The real user id and

 The effective user id or setuid

The real user id identifies the user who is responsible for the running process.

The effective user id is used to assogn ownership of newely created files,to chek
file access permission and to chek permission to send signals to processes via
the kill system call.thekernel allows a process to change its effective user id
when it execs a setuid program or when it invokes the setuid system call
explicitly.

Setuid(uid)
BY : MANISH ROHILA 2015

In which uid is the new user id and its result depends on the current value of the
effiective user id.

CHANGING SIZE OF PROCESS:

A process may increase or decrease the size of its data region by using the brk
system call. Systax is

Brk(endds);

In which ends becomes the value of the hightest virtual adderess of the data region of
the process.if the process is calling brk to free previously allocated space,the kernel
release the memory.if the process virtual address in pages which it had releated.it
incurs a memory fault.

Algo :brk

Input: new break value

Output:old break value

Lock process data region;

If(region size increasing)

If(new region size is illegal)

{ unlock data region;

Return(error);

Change region size(algo growreg);

Zero out address in new data space;

Unlock process data region;

THE SHELL:

The shell read a command line from its standard input and interprets it according to a
fixed set of rules.the standard input and standard output. File descriptors for the login
shell are usually the terminal on which the user logged in.if the shell recognize the input
BY : MANISH ROHILA 2015

string as a built in command it executes the command internally without creating new
process;otherwise it assumes the command is the name of an excuting file.

INTERPROCESS COMMUNICATION MULTIPROCESSOR SYSTEM:

SYSTEM V IPC:
The unix system V IPC package consists of three mechanisms.
Message allow process to send formatted data stream to arbitary processes,shared
memory allows prosesses to share parts of their virtual address space and
semaphores allows process to syschronize execution.implemented as a unit,they
share common properties.
each mechanism contain a table whose entries describe all instance of the
mechanism.
Each entry contain a numeric key,which is its user chosen name.
Each meachanism contain a “get” system call to create a new entry or to
retrieve an existing one,and the parameters to the calls include a key and
flages.the kernel search the proper table for an entry amed by the key.
For each IPC meachinsm the kernel uses the follwing formula to find the
index into the table of data structures from the descriptor:
index =descriptor modulo(number of entries in table)
Each IPC entry has a permission structure that includes the user ID and
group ID of the process.
Each entry contain other statuss information,such as the process ID of the
last process to update the entry.
Each mechanism contain a “control” system call to query status of an entry
to set status information or to remove the entry from the system.

NETWORK COMMUNICATION
Program such as mail,remote file transfer,and remote login that wish
to communicate with other machines have historically used ad hoc methods to
establish connection and to exchange data.when a person send mail to another user
on the same machine, the mail program appends the mail to addresses file,using
lock files and temporary files to preserve consistency.when a person read mail,the
mail program open the persons mail file and read the message.to send mail to a user
on another machine the mail program must ultimately find appropriate mail file on
the other machine.since it cannot manipulate files there directly a process on the
other machine must act as a agent for the local mail process hence the localprocess
needs way to communicate with it remote agent across machine boundaries.
BY : MANISH ROHILA 2015

The unix system creates new processes via the fork system call the server
process must exist before the client process attempt to establish a connection.it
would be inconsistent with design of the system if the remote kernel were to create a
new process when a connection request come across the network.client and server
program typically choose the network media and protocols according to information
in application data bases or the data may be hard coded into the programs.

SOCKETS
The methods by which they establish communication are likely to difer
depending on protocols and media.the methods may not allow processes to
communicate with otherprocesses on the same machine because they assume the
existence of a server process that sleeps in a driver open or read system call.to
provide common method for interprocess communication and to allow use to
sophisticated network protocol the BSD system provides a mechanism known as
sokets.

The Kernel structure consists of three parts:the soket layer,the protocol layer,and
the device layer. The socket layer provides the interface between system calls and
lower layers,the protocol layer contain the protocol modules used for
communication and thedevice layer contain the device drivers that control the
network device. The socket system call establishes the end point of a communication
link.
Sd=socket(format,type,protocol);
Format specifies the communication domain ,type indicate the type of
communication over the socket and protocol indicates a particular protocol to
control the communication.processes use the socket descriptor sd in other system
calls.
BY : MANISH ROHILA 2015

MULTIPROCESSOR SYSTEM:
A multiprocessor architecture contain two or more cpu
that share common memory and peripherals,potentially providing greater system
throughput,since processes can run concurrently on different processors.each cpu
excutes independently but all of them excute one copy of the kernel.processes
behave exactly as they would on a uniprocess or system-the semantics of each
system call remain the same but they can migrate between processsors
transparently.some multiprocessor system are called attached processor system.

PROBLEM OF MULTIPROCESSOR SYSTEM:


The design of unix system protect the integrity of krnel
data structure by two policies –the kernel cannot preempt a process and switch
cotext to another process while executing in kernel mode and it masks out interrupts
when executing a critical refion of code if an interrupt handler could cooupt kernel
data structure .on a multiprocessor if two or more processes execute simultaneously
in the kernel onseprate processor the kernel could become coorect in spite of the
protective measures that suffice for uniprocessor system.
For example- suppose two processes execute the code simultaneously on different
processors such that processor A want to place structure bpA after bp and
processor B wants to place structure bpB after bp.No assumption can be made
about the relative processor eexecution speed:the worst case is possible where
processor B could execute the four C statement before processor A can execute
another statement.
The kernel must make must sure that such corruption can never occure.if it were
to leave a window open in which a corrupt situation could arise no matter how rare
the kernel would be unsafe and its behaviour unpredictable. There are 3 methods
for preventing such corruption-
BY : MANISH ROHILA 2015

1 excute all critical activity on one processor,relaying on standard uniprocessor


methods for preventing corruption;
2 Serialize access to critical regions of code with locking primitives;
3 redesign algorithem to avoid contention for data strcture.

SOLUTION WITH MASTER AND SLAVE PROCESSORS:


The master processor is responsible for handling all system calls and
interrupts.slave processors execute processes in user mode and inform the master
processor when a process makes a system call.
The scheduler decides which processor should execute a process . a
new field in the process table designates the processor ID that a process must run
on; assume it indicate either master or slave.when a process on a slave processor
executes a system call the slave kernel sets the processor ID field in the process
table,indicating that the process should run only on the master processor and does a
context switch to schedule other processes .
The master kernel schedules the process of highest priority that must run on
the master processor and executes it.when it finishes the system call,it sets the
processor ID field of the process to slave,allowing the process to run on slave
processors again.

SOLUTION WITH SEMAPHORES:

Unit 4
INTRODUCTION

A shell is a program that is an interface between a user and


the raw operating system.

It makes basic facilities such as multitasking and piping easy


to use, and it adds useful file-specific features such as wildcards
and I/O redirection.

There are four common shells in use:

· the Bourne shell


· the Korn shell
· the C shell
· the Bash shell (Bourne Again Shell)

The bourne shell:


Bourne shell was created by steve bourne.it is most popular and
that’s why it is bundled with every unix system .because it was bundled with every
system it became popular.whatever the cause and the effect the fact remains that is
the shell used by many unix users.
BY : MANISH ROHILA 2015

The korn shell:


If there was any doubt about the cause effect relationship of the
popularity of biurne shell and its inclusion in every package,this adds fuel to it.the
not so widely used korn shell is very powerful and is a superset of bourne shell. It
offers a lot more capabilities and is decidedly more efficient than the other.it was
designed to be david korn of AT & T’s Bell Labs.
The C shell:
This shell is used extensively by those who are seriously into unix
programming.it was created by Bill joy.it has two advantages over the bourne shell.
1 it allow alising of commands.
2 it save typing work.

Index of Commands
cat - display or concatenate files
cd - change directory
chmod - change the permissions on a file or
directory
compress - compress a file
cp - copy a file
date - display the current date and time
diff - display differences between text files
echo - echo arguments to the standard output
file - determine the type of a file
find - find files of a specified name or type
finger - display information about a user
ftp - file transfer program
grep - searches files for a specified string or
expression
kill - kill a process
lpr - print out a file
ls - list names of files in a directory
man - display an on-line manual page
mkdir - make a directory
more - scan through a text file page by page
mv - move or rename files or directories
nice - change the priority at which a job is being
run
passwd - change your password
ps - list processes
pwd - display the name of your current directory
quota - disk quota and usage
rm - remove files or directories
rmdir - remove a directory
sort - sort and collate lines
talk - talk to another user
BY : MANISH ROHILA 2015

wc - display a count of lines, words and characters

unix grep command


grep options pattern files
(searches the files for lines that match a given
pattern). e.g.,
grep Drexel *.txt
grep Drexel `find . -name “*.txt” -print`
grep -i drexel *.txt

• VARIABLES

- A shell supports two kinds of variables:


local and environment variables.

Both kinds of variables hold data in a string format.

list of the predefined environment variables that are


common to all shells:

Name Meaning

$HOME the full pathname of your home directory

$PATH a list of directories to search for commands

$MAIL the full pathname of your mailbox

$USER your username

$SHELL the full pathname of your login shell

$TERM the type of your terminal


The syntax for assigning variables differs between shells,
but the way that you access the variables is the same:

If you precede the name of a variable with a $,


this token sequence is replaced by the shell with the value of
the named variable.

To create a variable, simply assign it a value;


variable does not have to be declared.

- the syntax for assigning a variable in the Bourne, Bash and Korn
shells is as follows:
BY : MANISH ROHILA 2015

variableName=value ---> place no spaces around the value


or
variableName=“ value ” ---> here, spacing doesn’t matter.

METACHARACTERS

Some characters are processed specially by a shell and


are known as metacharacters.

All four shells share a core set of common metacharacters,


whose meanings are as follow

Symbol Meaning

> Output redirection; writes standard output to a file.


>> Output redirection; appends standard output to a file.
< Input redirection; reads standard input from a file.
* File-substitution wildcard;
matches zero or more characters.
? File-substitution wildcard;
matches any single character.
[…] File-substitution wildcard;
matches any character between the brackets.

`command` Command substitution; replaced by the output from


command.
| Pipe symbol; sends the output of one process to the
input of another.
; Used to sequence commands.
|| Conditional execution;
executes a command if the previous one fails.
&& Conditional execution;
executes a command if the previous one succeeds.
(…) Groups commands.
& Runs a command in the background.
# All characters that follow up to a new line are ignored
by the shell and program(i.e., used for a comment)
$ Expands the value of a variable.
\ Prevents special interpretation of the next character.
<<tok Input redirection; reads standard input from script up to tok.

Flow Control Commands


! FOR Statement

for variable in string1 string2 ...stringN


BY : MANISH ROHILA 2015

do
commands ...
done
Example:
for file in ex1.c ex2.c yesno.c
do
echo "==== $file ===="
cat $file
done
Unix Shells

! Conditional Expressions
! IF Statement
if test-command
then
commands ... # when status is zero
else
commands ... # when status is non-zero
fi
Example:
if cp yourfile myfile
then
vi myfile
fi
This executes the command "cp yourfile myfile". If it is successful (i.e. the copy
worked), then it will edit myfile
! WHILE Statement
while test-command
do
commands...
done
! BREAK & CONTINUE Statements
break transfers control to statement after the done where as continue transfers
control to the done, and the loop
continues execution.
Unix Shells

! CASE Statement
case test-string in
pattern-1)
commands
;;
pattern-2)
commands
;;
:
BY : MANISH ROHILA 2015

Esac

Unit 5
Awk pattern scanning and processing language:

Awk is a programming language that is developed as programmable editor that is


stream oriented and interprets a scripts editing command.awk is named after its
authors-AHO WEINBERGER AND KERNIGHAN.

The features of awk are-

1 ability to view a text files as made up of records and fields on a textual database.

2 use of arithmetic and string operators.

3 use of variable to manipulate the database.

4 use of common programming constructs such as loop and condition.

5 ablity to gentrate formatted reports.

6 Define function.

7 handling command line arguments.

8 handling multiple stream.

BEGIN AND END PATTERNS:

Awk statements are usually applied to all lines selected by the address and if there are
no address they are applied to every line of input.if we like to print something before
processing the first line,then the BEGIN section can be used.similarly the END section
is useful in printing some calculation after the processing is completed.

The BEGIN and END section are optional.

BEGIN

{action}

END
BY : MANISH ROHILA 2015

{action}

AWK BUILT IN VARIABLE AND OPERATORS

Version Variable Description


awk FILENAME Current filename
FS Field separator (a space)
NF Number of fields in current record
NR Number of the current record
OFMT Output format for numbers ("%.6g") and for conversion to string
OFS Output field separator (a space)
ORS Output record separator (a newline)
RS Record separator (a newline)
$0 Entire input record
$n nth field in current record; fields are separated by FS

OPERATORS

Symbol Meaning
= += -= *= /= %= ^= **= Assignment
?: C conditional expression (nawk only)
|| Logical OR (short-circuit)
&& Logical AND (short-circuit)
in Array membership (nawk only)
~ !~ Match regular expression and negation
< <= > >= != == Relational operators
(blank) Concatenation
+ - Addition, subtraction
* / % Multiplication, division, and modulus (remainder)
+ - ! Unary plus and minus, and logical negation
^ ** Exponentiation
BY : MANISH ROHILA 2015

Symbol Meaning
++ -- Increment and decrement, either prefix or postfix
$ Field reference

BUILT IN FUNCTION

index
index(str, substr)

Return the position (starting at 1) of substr in str, or zero if substr is not present in str.

int
int(x)

Return integer value of x by truncating any fractional part.

length
length([arg])

Return length of arg, or the length of $0 if no argument.

log
log(x)

Return the natural logarithm (base e) of x.

rand
rand()

Generate a random number between 0 and 1. This function returns the same series of
numbers each time the script is executed, unless the random number generator is seeded
using srand(). {N}

return
BY : MANISH ROHILA 2015

return [expr]

Used within a user-defined function to exit the function, returning value of expr. The
return value of a function is undefined if expr is not provided. {N}

sin
sin(x)

Return the sine of x, an angle in radians. {N}

split
split(string, array [, sep])

Split string into elements of array array[1],...,array[n]. The string is split at each
occurrence of separator sep. If sep is not specified, FS is used. The number of array
elements created is returned.

sprintf
sprintf(format [, expressions])

Return the formatted value of one or more expressions, using the specified format (see
printf). Data is formatted but not printed. {N}

sqrt
sqrt(arg)

Return square root of arg.

srand
srand([expr])

Use optional expr to set a new seed for the random number generator. Default is the time
of day. Return value is the old seed. {N

sub
sub(r, s [, t])

Substitute s for first match of the regular expression r in the string t. If t is not supplied,
defaults to $0. Return 1 if successful; 0 otherwise. {N}
BY : MANISH ROHILA 2015

substr
substr(string, beg [, len])

Return substring of string at beginning position beg and the characters that follow to
maximum specified length len. If no length is given, use the rest of the string.

system
system(command)

Function that executes the specified command and returns its status. The status of the
executed command typically indicates success or failure. A value of 0 means that the
command executed successfully. A nonzero value indicates a failure of some sort. The
documentation for the command you're running will give you the details.

The output of the command is not available for processing within the awk script. Use
command | getline to read the output of a command into the script. {N}

systime
systime()

Return a time-of-day value in seconds since midnight, January 1, 1970, UTC. {G}

tolower
tolower(str)

Translate all uppercase characters in str to lowercase and return the new string.[6] {N}

toupper

toupper(str)

Translate all lowercase characters in str to uppercase and return the new string. {N}

if
if (condition)
statement
[else
statement]
BY : MANISH ROHILA 2015

If condition is true, do statement(s); otherwise do statement in the optional else clause

for
for (init-expr; test-expr; incr-expr)
statement

C-style looping construct. init-expr assigns the initial value of a counter variable. test-
expr is a relational expression that is evaluated each time before executing the statement.
When test-expr is false, the loop is exited. incr-expr increments the counter variable after
each pass. All the expressions are optional. A missing test-expr is considered to be true.
A series of statements must be put within braces.

do
do
statement
while (expr)

Looping statement. Execute statement, then evaluate expr and, if true, execute statement
again. A series of statements must be put within braces. {N}

Perl:

Operators
lists all the Perl operators from highest to lowest precedence and indicates
their associativity.
You can clarify your expressions by using parentheses to group any part of an
expression. Anything in parentheses will be evaluated as a single unit within a
larger expression.
With very few exceptions, Perl operators act upon scalar values only, not upon list
values.
Terms that take highest precedence in Perl include variables, quote and quotelike
operators, any expression in parentheses, and any function with arguments in
parentheses.
A list operator is a function that can take a list of values as its argument. List operators
take highest precedence when considering what’s to the left of them. They
. Perl associativity and operators, listed by precedence
Associativity Operators
Left Terms and list operators (leftward)
Left -> (method call, dereference)
Nonassociative ++ -- (autoincrement, autodecrement)
Right ** (exponentiation)
Right ! ~ \ and unary + and - (logical not, bit-not,
reference, unary plus, unary minus)
Left =~ !~ (matches, doesn’t match)
Left * / % x (multiply, divide, modulus, string
BY : MANISH ROHILA 2015

replicate)

Left + - . (addition, subtraction, string


concatenation)

Left << >> (left bit-shift, right bit-shift)


Nonassociative Named unary operators and file-test operators
Nonassociative < > <= >= lt gt le ge (less than, greater
than, less than or equal to, greater than or equal to,
and their string equivalents)
Nonassociative == != <=> eq ne cmp (equal to, not equal
to, signed comparison, and their string equivalents)
Left & (bit-and)
Left | ^ (bit-or, bit-xor)
Left && (logical AND)
Left || (logical OR)
Nonassociative .. ... (range)
Right ?: (ternary conditional)
Right = += -= *= and so on (assignment operators)
Left , => (comma, arrow comma)
Nonassociative List operators (rightward)
Right not (logical not)
Left and (logical and)
Left or xor (logical or, xor)

Das könnte Ihnen auch gefallen