Lec 01
Lec 01
Agenda
• Recap
• What is an operating system
• Why is it important
Operating Systems: What and Why • Summary of OS functionalities
• Basics of UNIX OS
• System calls
• Path of a system call
• User interfaces
• System boot sequence
1
8/17/2022
2
8/17/2022
3
8/17/2022
4
8/17/2022
5
8/17/2022
6
8/17/2022
7
8/17/2022
8
8/17/2022
9
8/17/2022
10
8/17/2022
11
8/17/2022
12
8/17/2022
13
8/17/2022
Basics of the UNIX OS: File system Basics of the UNIX OS: File system
• Block devices • Files are maintained using a data structure called
– Accessed through a buffer cache to exploit spatial index node (inode for short) table
locality – Each file has one inode
– Seen by the kernel as random access storage – Each inode stores the disk layout of the file data and
devices even though physically they may not be other information such as file owner, access
• Character devices permissions, access times, etc.
– Directly talk to the kernel without any caching – On a file system call specifying a file name, the file
name is translated to the corresponding inode
• Even though the OS abstraction for a device is
• Maintains two more tables to do this: user file descriptor
a file, every device needs a device driver to table (UFDT) and kernel file table (FT)
implement the hardware protocols
Basics of the UNIX OS: File system Basics of the UNIX OS: File system
• UFDT and FT
– The UFDT maintains one entry for each open file of a
process
– The UFDT entry holds a pointer to the corresponding
FT entry; two UFDT entries may point to the same FT
entry (same file opened twice)
– The open or creat call returns the index of the UFDT
slot allocated to the file
• Referred to as a file descriptor
– Each FT entry stores the read/write byte offset into a
file, access permissions, a unique pointer to the file
Reproduced from “The Design of the UNIX OS” by M J Bach inode
14
8/17/2022
Basics of the UNIX OS: File system Basics of the UNIX OS: File system
• File systems are kept on the block devices
– Kernel remains oblivious to the physical block device • File system
addresses – Boot block resides in the first sector and contains the
– The translation from the logical file system layout to bootstrap loader
the physical addresses is done by the device driver – Superblock describes the state of the file system: how
– Kernel treats the file system as a sequence of logical large it is, how many files it can store, free list
blocks, each of size multiple of 512 bytes information, etc.
– A file system starts with a boot block followed by a – Inode list size is configured by the admin when
superblock, the inode list, and the data blocks building the kernel
– Inode table contains the indices into the inode list
– Root inode is used as the root of the directory system
– An allocated data block can belong to exactly one file
Reproduced from “The Design of the UNIX OS” by M J Bach
Basics of the UNIX OS: Process control Basics of the UNIX OS: Process control
• Kernel process table entry for each process and u
area to store process information manipulated by
kernel only
– Each process table entry points to a per process
region table and each per process region table entry
points to a global region table
– A region is a contiguous memory segment containing
process text, data, stack
– A global region table entry contains region attributes
such as text/data, private/shared, and the starting
Reproduced from “The Design of the UNIX OS” by M J Bach address of the region
15
8/17/2022
Basics of the UNIX OS: Process control Basics of the UNIX OS: Process control
• The u area contains information necessary to • The context of a process is its complete state
completely describe a process; kernel can including
directly access the u area of the currently – Text (code)
executing process only; the u area contains
– Values of global user variables and data structures
– A pointer to the kernel process table slot of the
currently executing process – Values of processor registers
– Parameters, return values, and error codes of the last – Contents of its kernel process table slot and u area
executed system call of the currently executing – Contents of its user mode stack and kernel mode
process stack
– File descriptors of all open files of the currently
executing process
– Current directory and current root of the file system
– Process and file size limits
Basics of the UNIX OS: Process control Basics of the UNIX OS: Process control
• A process undergoes several state transitions
from creation to termination • Three important system calls for process
– The kernel process table entry maintains the process management
state, user id of the owner, and an event descriptor if – execv, fork, wait
the process is in sleep state – An execv call from a user program executes a new
– A process undergoes a context switch on I/O or program, starting address of which is passed to the
hardware interrupt or timer interrupt call; does not create a new process
• Not all system calls cause a context switch
• Overlays new text, data, stack regions on top of the old
• All system calls cause a mode switch regions, sets up the region table pointers correctly
• Context switch code or system call code executes in the
context of the currently scheduled process – A fork call creates a new process (called the child
• A mode switch changes the stack frame top pointer to user process)
mode or kernel mode stack top depending on the direction • Kernel duplicates the address space for the child from the
of the switch parent by copying or sharing depending on the situation
16
8/17/2022
Basics of the UNIX OS: Process control Basics of the UNIX OS: Process control
• The processes form a tree
• A process is always created by another process
– The root of this tree in UNIX is init
– Created process is the child of the creating process
– The system boot process is killed after init is created
• The system boots up as a process
• The pstree command shows the process tree in
– init process in UNIX UNIX (pstree –p shows the pid also)
– This is the root of all processes
• The pgrep processname command shows the pid
– Every process gets a unique integer ID known as the
of a process
process ID or pid. The root process has pid zero.
• In UNIX, the system boot process has pid zero and init has – pgrep init returns 1
pid one. • To find the parent pid of a process with pid x
– Check the fourth entry in file /proc/x/stat on UNIX
– In a C program, you can use getppid()
Basics of the UNIX OS: Process control Basics of the UNIX OS: Process control
• A process can be created by calling fork()
– Child pid is returned to parent, zero is returned to • The wait() system call makes a process wait for
child. A negative return value indicates error in UNIX. any one of its children to complete
• When the fork() call returns • It is a good practice to wait for all the children
– The child process has been created that a process has created
– Its text and global data are loaded in memory – Can be programmed using a loop which runs number
– The process will start executing when it is scheduled of times equal to the number of children
– Since the child process gets an exact copy of the
– Each loop iteration makes one call to wait()
address space and registers of the parent, it starts
executing at the instruction that returns from fork() – The wait() call takes one argument which is a pointer
• This is the value of the program counter in the parent to the exit code of a completing child process
• The only difference between the parent and the child is in • Can be passed NULL if we do not need the exit code of a
the return value of the fork() call child process
17
8/17/2022
Basics of the UNIX OS: Process control Basics of the UNIX OS: Process control
• The waitpid() system call makes a process wait • The pipe() system call creates a communication
for the child with a specific pid channel with a write end and a read end
– Takes three arguments: pid, exit code pointer, an – The ends of a pipe are implemented using a pair of
option file descriptors corresponding to a “common” file
– First argument can be -1 if any child is waited for – A process can write to one end and read what it has
– The last argument is usually 0 written from the other end
– waitpid(-1, &status, 0) is equivalent to wait(&status) • Not very interesting for a process to do so
• Gets interesting when one process writes to one end of the
– A useful value for the last argument of waitpid is pipe and another process reads from the other end
WNOHANG
– The pipe() call takes an array of two file descriptors as
• Allows the parent to return immediately irrespective of
whether the child waited for has completed or not
argument
• On return, the array is filled up appropriately by OS
• For example, before manipulating the inode list, the • Swap in the new virtual entity to occupy (portion of) the
processor priority level is raised above the disk interrupt physical resource
priority level – Example: physical resource CPU is time-shared
– Typical interrupt priority (low to high): software between virtual entities named “processes”
interrupts (system calls), character devices, network, – Example: physical resource memory is time-shared
disk, timer, machine error between virtual entities named “address spaces”
18
8/17/2022
19
8/17/2022
20
8/17/2022
21
8/17/2022
22