OS_Unit_1
OS_Unit_1
OS is a resource allocator
Manages all resources
Decides between conflicting requests for efficient and fair resource
use
OS is a control program
Controls execution of programs to prevent errors and improper use
of the computer
Operating System Definition (Cont.)
After I/O starts, control returns to user program without waiting for
I/O completion
System call – request to the operating system to allow user to
wait for I/O completion
Device-status table contains entry for each I/O device
indicating its type, address, and state
Operating system indexes into I/O device table to determine
device status and to modify table entry to include interrupt
Direct Memory Access Structure
Used for high-speed I/O devices able to transmit information at close
to memory speeds
Only one interrupt is generated per block, rather than the one interrupt
per byte
Storage Structure
Main memory – only large storage media that the CPU can access
directly
Random access
Typically volatile
Secondary storage – extension of main memory that provides large
nonvolatile storage capacity
File-System management
Files usually organized into directories
Access control on most systems to determine who can access
what
OS activities include
Creating and deleting files and directories
Primitives to manipulate files and dirs
Mapping files onto secondary storage
Backup files onto stable (non-volatile) storage media
Mass-Storage Management
Usually disks used to store data that does not fit in main memory or
data that must be kept for a “long” period of time
Proper management is of central importance
Entire speed of computer operation hinges on disk subsystem and its
algorithms
OS activities
Free-space management
Storage allocation
Disk scheduling
Some storage need not be fast
Tertiary storage includes optical storage, magnetic tape
Still must be managed – by OS or applications
Varies between WORM (write-once, read-many-times) and RW
(read-write)
Performance of Various Levels of Storage
Another set of OS functions exists for ensuring the efficient operation of the
system itself via resource sharing
Resource allocation - When multiple users or multiple jobs running
concurrently, resources must be allocated to each of them
Many types of resources - Some (such as CPU cycles, main memory,
and file storage) may have special allocation code, others (such as I/O
devices) may have general request and release code
Accounting - To keep track of which users use how much and what kinds
of computer resources
Protection and security - The owners of information stored in a multiuser
or networked computer system may want to control use of that information,
concurrent processes should not interfere with each other
Protection involves ensuring that all access to system resources is
controlled
Security of the system from outsiders requires user authentication,
extends to defending external I/O devices from invalid access attempts
If a system is to be protected and secure, precautions must be
instituted throughout it. A chain is only as strong as its weakest link.
A View of Operating System Services
User Operating System Interface - CLI
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 in the
Win32 API—a function for reading from a file
The caller need know nothing about how the system call is
implemented
Just needs to obey API and understand what OS will do as a
result call
Most details of OS interface hidden from programmer by API
Managed by run-time support library (set of functions built into
libraries included with compiler)
API – System Call – OS Relationship
Standard C Library Example
C program invoking printf() library call, which calls write() system call
System Call Parameter Passing
Often, more information is required than simply identity of desired
system call
Exact type and amount of information vary according to OS and
call
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
The operating system host creates the illusion that a process has its
own processor and (virtual memory).
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)
Information associated with each process
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
Process Control Block (PCB)
CPU Switch From Process to Process
Process Scheduling
Maximize CPU use, quickly switch processes onto CPU for time
sharing
Process scheduler selects among available processes for next
execution on CPU
Maintains scheduling queues of processes
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory,
ready and waiting to execute
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
Process Representation in Linux
Represented by the C structure task_struct
pid t pid; /* process identifier */
long state; /* state of the process */
unsigned int time slice /* scheduling information */ struct task struct
*parent; /* this process’s parent */ struct list head children; /* this
process’s children */ struct files struct *files; /* list of open files
*/ struct mm struct *mm; /* address space of this pro */
Ready Queue And Various
I/O Device Queues
Representation of Process Scheduling
Schedulers
Context-switch time is overhead; the system does no useful work while switching
The more complex the OS and the PCB -> longer the context switch
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
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
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;
Solution is correct, but can only use BUFFER_SIZE-1 elements
Bounded-Buffer – Producer
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
Messages are directed and received from mailboxes (also referred to as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
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
Pipes
Remote procedure call (RPC) abstracts procedure calls between processes on networked systems
The client-side stub locates the server and marshalls the parameters
The server-side stub receives this message, unpacks the marshalled parameters, and performs the
procedure on the server
Execution of RPC