Chapter 1 Handout Operating System
Chapter 1 Handout Operating System
1. History of OS
2. Definition of OS
3. OS services
4. Types of OS
5. OS Structure
Figure 2: Transistors
• These machines now called mainframes, they are locked in A/C rooms
• Separate staffs are allocated to manage them
• Only rich people would offered to buy, Government agencies, Universities, higher
educational institution’s etc..
• To run jobs programmer would write program in the paper then punch it on the cards
• Then card to the input room then hand over to the operator (it will take much time to
finish the job)
• Batch system
• Collect tray full of Jobs from the Input room then read them in the magnetic
tapes using small inexpensive computer IBM 1401
• IBM 7094 used for real computing
• The operator load the program which read the tape & ran it the Output was
written on the second tape
• Then next job, after all the Job (whole batch ) was done the operator remove the
I/p & O/p tapes replace with next job
• The operator take the O/p tape to 1401 for printing
1. Disks,
2. memory,
3. network interfaces,
4. timers, terminals/displays,
5. laser printers, etc (who using what / how it is shared)
• Maintain “multi-user” illusion to several users
– Most systems allow several user programs
– (even you think only one program is running, several internal programs is also
running)
• Managing
– Processor 1,2 or several
– Memories, RAM, cache
– Timers, internal & external
– Disks
– Network interface intranet, LAN, WAN
– Printers
• Processors
– It runs all software and moves data around
– CPU fetches instructions from memory and execute them
– As soon as information is fetched it decode to determine type & operands,
execute it, then subsequently do for all the instructions, then for the complete
program
– CPU contains registers (hold key variables & results)
– Special Registers
• Program counter (memory addr: of next instr: to be executed)
• Stack Pointer (points top of stack)
• Program Status Word (control bits)
– Execution
Figure 6: Pipeline
Superscalar CPU
Figure 7: Superscalar CPU
– System call (to obtain service from the OS, user program makes s/m call which
traps into kernel & appeal to the OS, trap instruction switches from user mode to
kernel mode)
• Multithreaded
– The ability of OS to simultaneously execute different parts of programs called
threads
– The programmer should carefully design the program in such a way that the
threads runs independently without interfering
– Thread is kind of light weight process (running in a program)
– Ex: if 1 process want to read a word from memory (it takes many clock cycles)
– Multithreaded CPU can switch to another thread
– Each thread appears to OS a separate CPU
Multicore chips
– Two or more independent processing units called cores, units that read &
execute program instructions
– Multiple core can run multiple instructions at the same time (increase overall
speed of the program)
– Cores is integrated into single circuit die know as chip multiprocessor or CMP /
multiple dies on to single chip package
Memory
– The second major component in computer is Memory
– Memory should be fast, large & cheap
– No approach satisfies all the three
– A new approach is taken by constructing hierarchy of layers in memory
Register
– Registers are internal to CPU, no delay in accessing registers
– Storage capacity available in them is 32*32 bit on a 32 bit CPU, 64 * 64 bit on 64
bit CPU.
– Programs manage registers themselves in software
Cache
– Controlled by Hardware
– Main memory is divided into cache lines 64 bytes
– 0 – 63 address in cache line 0
– 64 – 127 address in cache line 1 & so on …
– Most heavily used cache lines are kept in speed cache located inside or very
close to CPU.
– cache hit (2 clock cycles)
– Cache miss (goes to main memory)
– Cache memory is limited in size due high cost
– Some machines have 2 or 3 levels of cache
– Many OS will keep heavily used files in main memory instead of disk for repeated
lookups
– Example
– Home\users\projects\subprojects\kernel\check.c
– Into disk address where the file is located can be cached to avoid repeated
lookups
– Cache line 4096 (64 byte) 32 bit address 6b-17 bit used to specify the cache line
– Cache are good idea in modern CPU there are two cache L1 & L2
– Two L1 cache (1st L1 inside CPU, to feed the decoded instructions to execution
engine most CPU have 2nd L1 for very heavily used words)
– L1 cache 16 KB, access data without any delay
– L2 hold several megabytes of recently used words, access data with one or two
clock cycles
Main Memory
– Usually call it as RAM
– All CPU request that cannot be satisfied out of cache go to Main memory
– ROM is a non volatile memory it does not loose it content when power is
switched off
– ROM is programmed at factory and cannot be changed
– ROM is fast & inexpensive
– Some computer use bootstrap loader to start computer is contained in ROM
– FLASH
– CMOS (many computers use to hold current date and time, it can hold
configuration parameters such as which disk to boot)ALZHEIMER
Disks
A disk controller might accept command to read sector 11,206 from disk 2. The controller has
to convert this linear sector number to a cylinder, sector and head.
The controller has to determine which cylinder the disk arm is on and give it a sequence of
pulses to move in and out the requisite number of cylinders.
It has to wait until the proper sector has rotated under the head and then starts reading or
storing the bits as they come of the drive.
Finally it has to assemble the incoming bits into words and store them in memory.
Every controller has a small number of registers that are used to communicate with it.
For example a minimal disk controller might have registers for specifying the disk address,
memory address, sector count and direction
To activate the controller the driver gets a command from the OS. The collection of device
register forms the IO port space.
Input and output in 3 ways
1. In the simplest method a user program issues a system call, which the kernel translates
into procedure call to the driver. The driver starts the IO device, once the IO device has
completed the driver puts the data where they are needed and returns. The operating
system then returns control to the caller, this is called busy waiting
2. The driver start the device and ask it to give an interrupt when it is finished, at that
point driver returns. The OS blocks caller if need & looks for other work to do. When the
controller deduct the end of transfer, it generates and interrupt to signal completion.
3. DMA it is a chip that can control the flow of bits between memory and some controller
without CPU intervention. The CPU sets up the DMA chip telling how many bytes to
transfer, the device and memory address involved and the direction & lets it go.
Figure 9: Three step process for I/O
Step 1: The driver tells the controller by writing into the device register. The controller
then starts the device , once the controller has finished reading / writing
Step 2: It signals the interrupt controller chip using certain bus line
Step 3: If the interrupt controller is prepared to accept the interrupt it declare a pin on
the CPU chip informing IC
Step 4: IC puts the no of device on bus, so the CPU can read it and know which device
has just finished
Interrupt Vector: Is a memory location of Interrupt Handler which prioritizes interrupts and save if more
interrupt is waiting to be handled
Interrupt is a signal from a device attached to a computer / from a program within the computer, the
signal tells the OS to stop & device what to do next.
When an interrupt is generated the OS saves its execution state by means of CONTEXT SWITCH a
procedure that a computer processes follows to change from one task to another, by ensuring not to
conflict
Once the OS saves execution state is starts execution of Interrupt Handler at the Interrupt vector.
Buses
On the mother board a program called as BIOS (basic input output system). The BIOS
contains low level I/O software, including procedures to read the keyboard, written to
the screen and do I/O disk among other thing
When a computer is started the BIOS is started, it first checks how much RAM is
installed and checks all other software are functioning properly
It starts out by scanning ISA and PCI bus to detect all the devices attached to them
BIOS then determines the boot device by trying a list of devices stored in CMOS memory
Typically an attempt is made to boot from the floppy device else an attempt will be
made to boot from the CD ROM, If neither of them is present then it will boot from the
hard disk
The first sector from the boot device is read into memory and executed, the sector
contains a program that normally examines partition table at the end of the boot sector
to determine which partition is active, then boot loader is read in from that partition.
the loader reads in the operating system from the active partition and starts it.
UNIX uses mounted file system on a CD-ROM or DVD to be attached to main tree
Figure 12 (a) Before mounting the files on the CD-ROM are not accessible. (B) After mounting they are part of the file
hierarchy
The file system on the CD-ROM has been mounted on directory b thus allowing access
to files /b/x and /b/y
The UNIX has special files are provided in order to make I/O device look like files. That
way they can be written and read using same system calls are used for reading and
writing files. Two kinds of block special files block and character special files
Block special files consist of collection of randomly addressable blocks, such as disks. If I
want to read information say from block 4, a program can directly access 4 th block of the
device.
Character special files are used to model printer that accept or output a character
stream example: /dev/lp (connection to line printers)
Pipe is to connect to two processes, When process A wants to send data to process B it
writes on the pipe as though it were an output file. B can read data by reading from the
pipe as though it were an input file
Protection
It consist of 9 bit binary protection code, it is divided into 3 three bit binary code, one
for the owner, one for owners of the members group, one for anybody who uses it.
Each field has a bit to read, to write, to execute. These three bits are known as RWX bits
Rwxr_x_ _x means the owners can do all the operations, members of the owners group
can only read and execute. But everyone else can execute the file.
The Shell
when a user logs in, the shell automatically starts, it has terminal as standard I/P and
standard O/P, it starts by typing in the prompt means that is tells waiting for the I/P to
accept command from user
if user types date
the shell starts by running the child process for date program the shell waits for
program to terminate
when the child process finishes, the shell types the prompt and wait to read the next I/P
line
date > file “the output is directed to a file”
sort <file1> file2 “which invoke the sort program from the I/P taken from a file and O/P
is given to file 2”
cat file1 file2 file3/sort> /dev/lp
concatenation file 1,2 & 3 then sort them and given then for line printer
cat file1 file2 file3/sort> /dev/lp &
concatenation will happen in the background, all the user to work another process / job
in the front end
The first and the third parameter are call by value, the second is pass by reference, means the
address of the buffer is passed not the content.
Then comes the actual call to the library procedure in (step 4), the instruction is the normal
procedural call instruction used to call all procedures.
The library procedure puts the system call number in the place where the OS expects it, such as
register (step 5)
Then it executes TRAP instruction to switch from user mode to kernel mode & start execution
at the fixed address within the kernel (step 6)
The kernel code that starts following the TRAP examines the system call number, & then
dispatches to the correct system call handler usually via table of pointers to system call
handlers (step 7)
System call handler runs (step 8)
Once the system call handler completed control may be transferred to user space library
procedure (step 9).
Then library procedure returns control to the user program (step 10)
Finally SP (stack pointer is incremented to read the next instruction from the user program –
step 11)
System call for process management
FORK is the only way to create a new process
It creates an exact duplicate of the original process which includes file descriptor, registers etc…
After fork the original and copy go their separate way
At the time of FORK all the variables will have identical values
After that parent’s data are copied to create child changes in one that does not affect other.
The FORK call returns a value zero in the child and equal to child process identifier PID in the
parent, the two process can see which one parent and child processes
In most of the cases child will need to execute different codes then the parent
Consider a case shell
It reads a command from the terminal
It creates child process to execute the command
When the child process is executed / terminated it reads the next command
To wait for the child to finish the parent execute waitpid system call which wait for the child to
terminate
Waitpid can wait for the child for any other old child to complete by setting the parameter -1
When it completes the address pointed to next parameter statloc will be set to the child’s exit
status
How FORK works
When a command is typed shell forks of a new process (child)
Child process executes the command using EXECVE system call which causes entire core image
is replaced by file named in its first parameter
# define TRUE 1
While (TRUE)
{
Type_prompt(); // display prompt
Read_command (command, parameters); // read input from the terminal
If(fork()!=0)
{
/*---parent code---*/
Waitpid(-1, statloc, 0) // wait for child process to execute
}
Else
{
/*---child code---*/
Execve (command, parameter,0); // execution of child process
}
}
Execve has 3 parameters
1st parameter name of the file to be executed
2nd parameter a pointer to the argument to the array
3rd parameter a pointer to the environment to the array
Command
cp file1 file2
Is used to copy file1 to file2
After the shell is forked, the child process locates and executes the file cp
The main program of the cp contains declaration
Main (argc, argv, envp)
1st parameter argc is count number of items in the command line
2nd parameter argv
argv[0] points the string Cp
argv[1] points the string file1
argv[2] points the string file2
Text segments
Data segments
Stack segments
brk is the system call used to grow data segment by allotting the new address space
where data segment is to end
Call Description
Pid = fork() Create a child process identical to parent
Pid = waitpid (pid, &statloc, 0) Wait for a child to terminate
S = execve (name, argv, environp) Replace a process coreimage
Exit(status) Terminate process execution & return status
Call Description
fd=open(file, how,…) Open a file for reading, writing or both
s=close(fd) Close an open file
n=read(fd,buffer,nbytes) Read data from a file into a buffer
n=write(fd,buffer,nbytes) Write data from a buffer into a file
Position=lseek(fd,offset,whence) Move the file pointer
s=stat(name,&buf) Get file’s status information
lseek – although most programs read and write file sequentially, for some application
need to access any part of file randomly, associated with each file a pointer that indicate
current position in the file. Lseek call changes the value of the position pointer so that
subsequent calls to read write can begin anywhere in the file.
3 parameters of lseek
1. File descriptor
2. File position
3. File position (three)
a. Beginning of the file
b. Current position of the file
c. End of file
Call Description
s=mkdir(name, mode) Create a new directory
s=rmdir(name) Remove an empty directory
s=link(name1, name2) Create a new entry, name2, pointing to name1
s=unlink(name) Remove a directory entry
s=mount(special,name,flag ) Mount a file
s=unmount(special) Unmount a file
Link is to allow same file to appear under two or more names often in different
directories. Typical use is to allow the several users of the same programming team to
share a common file, with each of them having the file appear in his own directory
possibly under different name. When copies are made of a file, changes made in one will
not affect the same files with other names in different directories
The file MEMO in SAM directory has now entered in RAM directory as NOTE
Every file in UNIX has a unique number, its i-number.
i-number is an index into table of i-nodes, 1 per file, telling who owns the file, where the file is
located etc…
A directory simply a file containing (i-numbers, ASCII) pairs
16 bytes (2 bytes for i-number, 14bytes - names)
Mount is merge two files systems in to one
Mount(“/dev/fd0”,”/mnt”,0)
1st parameter name of block special file for drive 0
2nd parameter is the place where it is to be mounted
3rd parameter tells whether the file system is to be mounted read-write or read only
Figure 17 Above figure File system before Below: after mount
1. Monolithic system’s
OS is written as a collection of procedures, linked together in a single large executable
binary program
Each procedure is free to call any other procedure for some computation
Having thousands of procedures that can call any other without restrictions often leads
to difficulty in understanding
To solve this one first compile all individual procedures and then bind all individual file
into single executable file using system linker
In monolithic system it is possible to have some structure, the service provided by the
OS are requested by putting the parameter in a well defined place and then execute the
trap instruction.
The organization suggest a basic structure for the OS
A main program that call upon the requested service procedure
A set of service procedure that carry out system calls
A set of utility procedure that help the service procedures
In this model for each system call there is a service procedure that take care of it &
execute it
The utility procedure do things that are needed by several service procedure such as
fetching data from user program
Layer Function
5 The operator
4 User Program
3 I/O Management
2 Operator process communication
1 Memory and drum management
0 Processor allocation and multiprogramming
3. Microkernels
Example a process needing to read send a message to one file server telling it what to
read.
Reincarnation SERVER its job is to check if other servers & drivers are functioning
correctly.
In case if any fault is detected it automatically replaced without any user intervention.
In this way the system is self healing and achieve high reliability
The system has many restrictions limiting the power of each process.
Processes can also grant permission for other processes to have kernel access their
address space
Example a file system can grant permission to disk driver to let the kernel put a newly
read in disk block at a specific address within the file system’s address space.
The total of all these restrictions is that each driver and server has exactly the power to
do its work and nothing more thus greatly limiting the damage a bug can do
4. Client server model
Since client communication with servers by sending message, the client need to know
whether the service is handled locally or via network to servers on a remote machine
As far as client is concerned the request are sent and replies come back
Client to server model can be used for a single machine or for a network of machines.
Example
A PC can send request for a web page to the server and the web page comes back. This
is typical use of client server model in the network
VM370
The system was originally called as CP/CMS (control program / conversational Monitor
System)
Later renamed as VM/370 based on smart observation TSS provides
Multiprogramming
Extended machine with more convenient interface than the bare H/W
The heart of the system know as virtual machine monitor runs on the bare hardware’s
& does multiprogramming for several virtual machines
The idea of virtualization in the past few years because of new needs, new software’s &
new technologies have combined to make it a hot topic.
Many companies run their mail server, file server & FTP server on separate computers
sometimes with different operating systems
They see virtualization as a way to run them all on the same machine without having
crash of one server brings down the rests
Virtualization is also popular in web hosting world, earlier web hosting customers are
forced to choose between shared hosting & dedicated hosting
SUN MICRO SYSTEM invented java programming , it also invent a virtual machine (a
computer architecture) called JAVA VIRTUAL MACHINE
Java compiler produces code for JVM which is then executed by a S/W JVM interpreter
An abstract computing machine, or virtual machine, JVM is a platform-independent
execution environment that converts Java bytecode into machine language
and executes it. Most programming languages compile source code directly into
machine code that is designed to run on a specific OS, such as Windows or UNIX.
6. Exokernels
It is a program. Its job is to allocated resource to virtual machine and check attempt to
use them to make sure that no machine is trying to use some body else’s resources.
Each user is given a subset of resources. One virtual machine might get blocks 0 to 1023,
the next one might get 1024 to 2047 & so on.
Each user level virtual machine can run its own OS as on VM/370 & the Pentium virtual
8086s except that each one is restricted to using only the resources it has asked for and
been allocated
Advantage: it saves a layer of mapping, the exokernel need to keep track of which
virtual machine is assigned which resource