Os Module 2
Os Module 2
OS Principles
System calls, System/Application Call Interface – Protection: User/Kernel modes -
Interrupts
-Processes - Structures (Process Control Block, Ready List etc.), Process creation,
management in Unix – Threads: User level, kernel level threads and thread
models.
Operating-System Structures
• Operating System Services
• User Operating System Interface
• System Calls
• Types of System Calls
• System Programs
• Operating System Design and Implementation
• Operating System Structure
• Virtual Machines
• Operating System Debugging
• Operating System Generation
• System Boot
Objectives
• To describe the services an operating system
provides to users, processes, and other systems
• Three most common APIs are Win32 API for Windows, POSIX API for POSIX-
based systems (including virtually all versions of UNIX, Linux, and Mac OS
X), and Java API for the Java virtual machine (JVM)
(Note that the system-call names used throughout this text are generic)
Example of System
Calls
• System call sequence to copy the contents of one file to another file
Example of Standard
API
• Consider the ReadFile() function
• Win32 API—a function for reading from a file
• Most users’ view of the operation system is defined by system programs, not the actual system calls
System Programs
• Provide a convenient environment for program development
and execution
• Some of them are simply user interfaces to system calls; others are
considerably more complex
• Status information
• Some ask the system for info - date, time, amount of available
memory, disk space, number of users
• Others provide detailed performance, logging, and debugging
information
• Typically, these programs format and print the output to the
terminal or other output devices
• Some systems implement a registry - used to store and retrieve
configuration information
System Programs (Cont.)
• File modification
• Text editors to create and modify files
• Special commands to search contents of files or perform
transformations of the text
• With modularity, layers are selected such that each uses functions
(operations) and services of only lower-level layers
Traditional UNIX System Structure
UNIX
• UNIX – limited by hardware functionality, the original UNIX operating system had
limited structuring. The UNIX OS consists of two separable parts
• Systems programs
• The kernel
• Consists of everything below the system-call interface and above the physical
hardware
• Provides the file system, CPU scheduling, memory management, and other
operating-system functions; a large number of functions for one level
Layered Operating System
Microkernel System Structure
• Moves as much from the kernel into “user” space
• Benefits:
• Easier to extend a microkernel
• Easier to port the operating system to new architectures
• More reliable (less code is running in kernel mode)
• More secure
• Detriments:
• Performance overhead of user space to kernel space
communication
• In microkernels, the kernel is broken down into separate processes, known as servers.
Some of the servers run in kernel space and some run in user-space.
• All servers are kept separate and run in different address spaces. Servers invoke "services"
from each other by sending messages via IPC (Interprocess Communication).
• This separation has the advantage that if one server fails, other servers can still work
efficiently.
Modules
• Most modern operating systems implement kernel modules
• Uses object-oriented approach
• Each core component is separate
• Each talks to the others over known interfaces
• Each is loadable as needed within the kernel
• The operating system host creates the illusion that a process has its
own processor and (virtual memory).
• Bootstrap program – code stored in ROM that is able to locate the kernel,
load it into memory, and start its execution
System Boot
• Operating system must be made available to hardware so hardware can
start it
• Small piece of code – bootstrap loader, locates the kernel, loads it into memory,
and starts it
• Sometimes two-step process where boot block at fixed location loads bootstrap
loader
• When power initialized on system, execution starts at a fixed memory location
• Firmware used to hold initial boot code
Processes
• Process Concept
• Process Scheduling
• Operations on Processes
• Inter-process Communication
• Examples of IPC Systems
• Communication in Client-Server Systems
Objectives
• To introduce the notion of a process -- a program
in execution, which forms the basis of all
computation
• A process includes:
• program counter
• stack
• data section
The Process
• Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
• Program is passive entity, process is active
• Program becomes process when executable file loaded into memory
• Execution of program started via GUI mouse clicks, command line entry of its name,
etc
• One program can be several processes
• Consider multiple users executing the same program
Process in Memory
Process State
• As a process executes, it changes state
• new: The process is being created
• running: Instructions are being executed
• waiting: The process is waiting for some event to occur
• ready: The process is waiting to be assigned to a processor
• terminated: The process has finished execution
Diagram of Process State
Process Control Block (PCB)
• Resource sharing
• Parent and children share all resources
• Children share subset of parent’s resources
• Execution
• Parent and children execute concurrently
• Parent waits until children terminate
Process Creation (Cont.)
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork system call creates new process
• exec system call used after a fork to replace the process’ memory space with
a new program
Process Creation
C Program Forking Separate Process
else if (pid == 0) {
#include <stdio.h> // This is the child process
#include <unistd.h> printf("Child Process: PID = %d, Parent PID
= %d\n", getpid(), getppid());
int main() { }
pid_t pid; else {
// This is the parent process
pid = fork(); // Create a new process printf("Parent Process: PID = %d, Child PID
= %d\n", getpid(), pid);
if (pid < 0) { }
perror("Fork failed");
return 1; return 0;
} }
A Tree of Processes on Solaris
Process Termination
• Process executes last statement and asks the operating
system to delete it (exit)
• Output data from child to parent (via wait)
• Process’ resources are deallocated by operating system
• Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Bounded Buffer – Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
• Solutions
• Allow a link to be associated with at most two processes
• Allow only one process at a time to execute a receive
operation
• Allow the system to select arbitrarily the receiver. Sender is
notified who the receiver was.
Synchronization
• Message passing may be either blocking or non-blocking
• Sockets
• Pipes
• Issues
• Is communication unidirectional or bidirectional?
• In the case of two-way communication, is it half or full-
duplex?
• Must there exist a relationship (i.e. parent-child) between the
communicating processes?
• Can the pipes be used over a network?
Ordinary Pipes
• Ordinary Pipes allow communication in standard producer-
consumer style
• Consumer reads from the other end (the read-end of the pipe)
• Communication is bidirectional
• Responsiveness
• Resource Sharing
• Economy
• Scalability
Multicore Programming
• Multicore systems putting pressure on programmers,
challenges include:
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
Multithreaded Server Architecture
Concurrent Execution on a Single-core System
Parallel Execution on a Multicore System
User Threads
• Thread management done by user-level threads library
• Examples
• Windows XP/2000
• Solaris
• Linux
• Tru64 UNIX
• Mac OS X
Aspect User Threads Kernel Threads
Threads managed entirely by user- Threads managed directly by the
Difference Between User-Level Thread and Definition
level libraries operating system kernel
Managed by user-space thread Managed by kernel (e.g., Windows NT,
Management
library (e.g., POSIX pthreads) Linux)
Faster, no need to switch to kernel Slower due to switching between user
Context Switching
mode and kernel modes
Kernel knows and schedules all kernel
System Call Visibility Kernel is unaware of user threads
threads
Scheduling Done by user-level thread library Done by kernel thread scheduler
One thread blocking causes all One thread can block without affecting
Blocking
threads in the process to block others
Kernel-Level Thread
• One-to-One
• Many-to-Many
Many-to-One
• Many user-level threads mapped to single kernel thread
• Examples:
• Solaris Green Threads
• GNU Portable Threads
Many-to-One Model
One-to-One
• Each user-level thread maps to kernel thread
• Examples
• Windows NT/XP/2000
• Linux
• Solaris 9 and later
One-to-one Model
Many-to-Many Model
• Allows many user level threads to be mapped to many
kernel threads
• Examples
• IRIX
• HP-UX
• Tru64 UNIX
• Solaris 8 and earlier
Two-level Model
Thread Libraries
• Thread library provides programmer with API for
creating and managing threads
• Signal handling
• Synchronous and asynchronous
Threading Issues (Cont.)
• Thread pools
• Thread-specific data
Create Facility needed for data private to thread
• Scheduler activations
Semantics of fork() and exec()
• Options:
• Deliver the signal to the thread to which the signal applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the process
Thread Pools
• Create a number of threads in a pool where they await
work
• Advantages:
• Usually slightly faster to service a request with an existing
thread than create a new thread
• Allows the number of threads in the application(s) to be
bound to the size of the pool
Thread Specific Data
• Allows each thread to have its own copy of data
• Linux Thread
Windows XP Threads Data Structures
Windows XP Threads
• Implements the one-to-one mapping, kernel-level
• The register set, stacks, and private storage area are known as
the context of the threads