CH1-Introduction To Unix Linux Kernel
CH1-Introduction To Unix Linux Kernel
❖ System Structure
❖ The UNIX system has become quite popular since its inception in 1969, running on machine of varying
processing power from microprocessors to mainframes and providing a common execution environment
across all of them.
1. The first part consists of programs and services that have made the UNIX system environment so
popular; it is the part readily apparent to users, including such programs as the shell, mail, text
processing packages, and source code control systems.
2. The second part consists of the operating system that supports these programs and services
❖ System Structure:
▪ High-level architecture of the UNIX system.
▪ The hardware at the center of the diagram provides the
operating system with basic services
▪ The operating system interacts directly with the hardware·,
providing common services to programs.
▪ Viewing the system as a set of layers, the operating system
is commonly called the system kernel.
▪ Kernel emphasizing its separation from user programs,
Because programs are independent of the underlying
hardware, it is easy to move them between UNIX systems
running on different hardware.
❖ Programs such as the shell and editors (ed and vi) shown in the outer layers interact with the kernel by
invoking a well defined set of system calls.
❖ The system calls instruct the kernel to do various operations for the calling program and exchange data
between the kernel and the program.
❖ Several programs shown in the figure are in standard system configurations and are known as Commands,
but private user programs may also exist in this layer as indicated by the program whose name is a.out, it is
standard name for executable files produced by the C compiler.
❖ Other application programs can build on top of lower-level programs, hence the existence of the outermost
layer in the figure. For example, the standard C compiler.
❖ Although the figure depicts a two-level hierarchy of application programs, users can extend the hierarchy to
whatever levels are appropriate.
❖ The file subsystem manages files, allocating file space, administering free space, controlling access to files, and
retrieving data for users.
❖ Processes interact with the file subsystem via a specific set of system calls, such as open (to open a file for
reading or writing), close, read, write, stat (query the attributes of a file), chown (change the record of who
owns the file), and chmod (change the access permissions of a file) .
❖ The file subsystem accesses file data using a buffering mechanism that regulates data flow between the kernel
and secondary storage devices.
❖ The buffering mechanism interacts with block I/O device drivers to initiate data transfer to and from the kernel.
❖ Device drivers are the kernel modules that control the operation of peripheral devices. Block I/O devices are
random access storage devices.
❖ The process control subsystem is responsible for process synchronization, Interprocess communication memory
management, and process scheduling.
❖ The file subsystem and the process control subsystem interact when loading a file into memory for execution.
❖ The process subsystem reads executable files into memory before executing them; Some of the system calls for
controlling processes are fork ,exec (overlay the image of a program onto the running process), exit ,wait, brk
(control the size of memory allocated to a process), and signal (control process response to events).
▪ It schedules them to until the kernel preempts them when their run time exceeds a time quantum.
▪ The scheduler then chooses the highest priority eligible process to run; the original process will run again
when it is the highest priority eligible process available.
❖ Finally, the hardware control is responsible for handling interrupts and for communicating with the machine.
❖ Devices such as disks or terminals may interrupt the CPU while a process is executing.
❖ If so, the kernel may resume execution of the interrupted process after servicing the interrupt.
❖ Interrupts are not serviced by special processes but by special functions in the kernel, called in the context of
the currently running process.
▪ The term inode (index node) is commonly used in literature on the UNIX system.
▪ Every file has one inode, but it may have several names, all of which map into the inode.
▪ When a process refers to a file by name, the kernel parses the file name one component at a time,
checks that the process has permission to search the directories in the path, and eventually retrieves
the inode for the file.
▪ The kernel contains data structures : file table, user file and descriptor table .
▪ The file table is a global kernel structure, but the user file descriptor table is allocated per process.
▪ When a process opens or creates a file, the kernel allocates an entry from each table, corresponding to
the file's inode.
▪ Entries in the three structures - user file descriptor table, file table, and inode table -maintain the state
of the file and the user's access to it.
▪ The file table keeps track of the byte offset in the file where the user's next read or write will start, and
the access rights allowed to the opening process.
▪ The user file descriptor table identifies all open files for a process.
▪ The UNIX system keeps regular files and directories on block devices such as tapes or disks. Because of the
access time of both are different.
▪ Partitioning a disk into several file systems makes it easier to manage the data store.
▪ The conversion between logical device (file system) addresses and physical device (disk) addresses is done
by the disk driver.
▪ A file system consists of a sequence of logical blocks, each containing 512, 1024, 2048, or any convenient
multiple of 512 bytes, depending on the system implementation.
▪ The size of a logical block is homogeneous within a file system but may vary between different file systems in
a system configuration.
▪ Using large logical blocks increases the effective data transfer rate between disk and memory, because the
kernel can transfer more data per disk operation and therefore make fewer time-consuming operations.
2. The super block describes the state of a file system - how large it is, how many files it can store, where
to find free space on the file system, and other information.
4. The data blocks start at the end of the inode list and contain file data and administrative data. An
allocated data block can belong to one and only one file in the file system.
❖ Process:
▪ Process consists of a pattern of bytes that the CPU interprets as machine instructions (called "text"),
data, and stack.
▪ Many processes execute simultaneously as the kernel schedules them for execution, and several
processes may be instances of one program.
▪ A process executes by following a strict sequence of instructions that is self-contained and does not
jump to another process; it reads and writes its data and stack sections, but it cannot read or write
the data and stack of other processes.
▪ In UNIX system , a process is the entity that is created by the fork() system call.
▪ Every process except process 0 is created when another process executes the fork system call.
▪ The process that invoked the fork system call is the parent process, and the newly created process is the
child process.
▪ Every process has one parent process, but a process can have many child processes.
▪ The kernel identifies each process by its process number, called the process ID (PID).
▪ Process 0 is a special process that is created "by hand" when the system boots; after forking a child
process (process 1), process 0 becomes the swapper process
▪ Process 1, known as init, is the ancestor of every other process in the system and enjoys a special
relationship with them,
▪ When the kernel decides that it should execute another process, it does a context switch, so that the
system executes in the context of the other process.
▪ When doing a context switch, the kernel saves enough information so that it can later switch back to the
first process and resume its execution.
▪ The interrupted process may have been executing in user mode or in kernel mode.
▪ Moving between user and kernel mode is a change in mode, not a context switch.
▪ The kernel does a context switch when it changes context from process A to process B; it changes execution
mode from user to kernel or from kernel to user.
▪ The lifetime of a process can be divided into a set of states, each with certain characteristics that
describe the process.
▪ The process states give a static view of a process, but processes move continuously between the states
according to well-defined rules.
▪ A state transition diagram is a directed graph whose nodes represent the states a process can enter and
whose edges represent the events that cause a process to move from one state to another.
▪ State transitions are legal between two states if there exists an edge from the first state to the second.
▪ Several transitions may produce from a state, but a process will follow one and only one transition depending
on the system event that occurs.
▪ Several processes can execute simultaneously in a time-shared manner, and they may all run simultaneously
in kernel mode.
▪ If they were allowed to run in kernel mode without constraint, they could corrupt global kernel data
structures.
▪ By prohibiting arbitrary context switches and controlling the occurrence of interrupts, the kernel protects
its consistency
▪ The kernel allows a context switch only when a process moves from the state "kernel running" to the
state "asleep in memory."
▪ Processes running in kernel mode cannot be preempted by other processes; therefore the kernel is
sometimes said to be non-preemptive, although the system does preempt processes that are in user
mode.
▪ The kernel maintains consistency of its data structures because it is non-preemptive, thereby solving the
mutual exclusion problem - making sure that critical sections of code are executed by at most one process
at a time.
▪ The UNIX system prevents such 'situations by disallowing context switches’ when a process executes
in kernel mode.
▪ If a process goes to sleep, thereby permitting a context switch, kernel algorithms are encoded to make
sure that system data are in a safe, consistent state.
▪ The kernel protects its consistency by allowing a context switch only when a process puts itself to sleep
and by preventing one process from changing the state of another process.
▪ It also raises the processor execution level around critical regions of code to prevent interrupts that
could otherwise cause inconsistencies.
▪ The process scheduler periodically preempts processes executing in user mode so that processes cannot
monopolize use of the CPU.
❖ For instance, when a process must temporarily suspend its execution then "go to sleep".
❖ Consequently, an interrupt handler cannot go to sleep, because if it could, the interrupted process would
be put to sleep by default.
❖ Processes go to sleep because they are awaiting the occurrence of some event, such as waiting for I/0
completion from a peripheral device, waiting for a process to exit, waiting for system resources to
become available, and so on.
❖ Processes are said to sleep on an event, meaning that they are in the sleep state until the event occurs,
at which time they wake up and enter the state "ready to run”.
❖ When a process wakes up, it follows the state transition from the "sleep" state to the "ready-to-run"
state, where it is eligible for later scheduling; it does not execute immediately.
▪ Many processes can simultaneously sleep on an event; when an event occurs, all processes sleeping on the
event wake up because the event condition is no longer true.
▪ Sleeping processes do not consume CPU resources: The kernel does not constantly check to see that a
process is still sleeping but waits for the event to occur and then awakens the process.
▪ It unlocks the lock and awakens all processes asleep on the lock in the following manner: