1
Module 2
System Calls 2
Programming interface to the services provided by the
OS
Typically written in a high-level language (C or C++)
Mostly accessed by programs via a high-level
Application Programming Interface (API) rather than
direct system call use
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)
Standard C Library Example 3
C program invoking printf() library call, which calls write() system call
System Calls - Example 4
System call sequence to copy the contents of one file
to another file
Example of a Standard API 5
System Call – Implementation 6
Typically, a number associated with each system call
System-call interface maintains a table indexed according to
these numbers
The system call interface invokes the intended system call
in OS kernel and returns status of the system call and any
return values
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 7
Relationship Standard API
System Call – Parameter Passing 8
Often, more information is required than simply identity of
desired system call
Exact type and amount of information vary according to OS
and call
Three general methods used to pass parameters to the OS
Simplest: pass the parameters in registers
• In some cases, may be more parameters than registers
Parameters stored in a block, or table, in memory, and address
of block passed as a parameter in a register
• This approach taken by Linux and Solaris
Parameters placed, or pushed, onto the stack by the program
and popped off the stack by the operating system
Block and stack methods do not limit the number or length of
parameters being passed
Parameter Passing via Table 9
System Call – Types 10
Process control
create process, terminate process
end, abort
load, execute
get process attributes, set process attributes
wait for time
wait event, signal event
allocate and free memory
Dump memory if error
Debugger for determining bugs, single step execution
Locks for managing access to shared data between
processes
System Call – Types (Cont.) 11
File management
create file, delete file
open, close file
read, write, reposition
get and set file attributes
Device management
request device, release device
read, write, reposition
get device attributes, set device attributes
logically attach or detach devices
System Call – Types (Cont.) 12
Information maintenance
get time or date, set time or date
get system data, set system data
get and set process, file, or device attributes
Communications
create, delete communication connection
send, receive messages if message passing model to host
name or process name
• From client to server
Shared-memory model create and gain access to memory
regions
transfer status information
attach and detach remote devices
System Call – Types (Cont.) 13
Protection
Control access to resources
Get and set permissions
Allow and deny user access
Examples of Windows and Unix System Calls
14
Protection - Modes 15
Dual-mode operation allows OS to protect itself and
other system components
User mode and kernel mode
Mode bit provided by hardware
Provides ability to distinguish when system is running user
code or kernel code
Some instructions designated as privileged, only executable in
kernel mode
System call changes mode to kernel, return from call resets it
to user
Increasingly CPUs support multi-mode operations
i.e. virtual machine manager (VMM) mode for guest VMs
Protection – Modes (Cont.) 16
Timer to prevent infinite loop / process hogging resources
Timer is set to interrupt the computer after some time period
Keep a counter that is decremented by the physical clock.
Operating system set the counter (privileged instruction)
When counter zero generate an interrupt
Set up before scheduling process to regain control or terminate program
that exceeds allotted time
Interrupt 17
Interrupt transfers control to the interrupt service routine generally,
through the interrupt vector, which contains the addresses of all the
service routines
Interrupt architecture must save the address of the interrupted
instruction
A trap or exception is a software-generated interrupt caused either by
an error or a user request
An operating system is interrupt driven
Interrupt driven (hardware and software)
Hardware interrupt by one of the devices
Software interrupt (exception or trap):
Software error (e.g., division by zero)
Request for operating system service
Other process problems include infinite loop, processes modifying each other or the
operating system
Interrupt Handling 18
The operating system preserves the state of the CPU by storing
registers and the program counter
Determines which type of interrupt has occurred:
polling
vectored interrupt system
Separate segments of code determine what action should be
taken for each type of interrupt
Process Concept 19
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Process – a program in execution; process execution must progress in sequential
fashion
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 stored on disk (executable file), 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 20
Process States 21
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
Process Control Block (PCB) 22
Information associated with each process
(also called task control block)
Process state – running, waiting, etc Program counter –
location of instruction to next execute
CPU registers – contents of all process-centric registers
CPU scheduling information- priorities, scheduling queue
pointers
Memory-management information – memory allocated to
the process
Accounting information – CPU used, clock time elapsed
since start, time limits
I/O status information – I/O devices allocated to process, list
of open files
CPU Switch From Process to Process 23
Operations on Processes
System must provide mechanisms for:
process creation,
process termination,
Process Creation
Parent process create children processes, which, in turn create
other processes, forming a tree of processes
Generally, process identified and managed via a process
identifier (pid)
Resource sharing options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
A Tree of Processes in Linux
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
C Program Forking Separate Process
Process Termination
Process executes last statement and then asks the operating
system to delete it using the exit() system call.
Returns status data from child to parent (via wait())
Process’ resources are deallocated by operating system
Parent may terminate the execution of children processes using
the abort() system call. Some reasons for doing so:
Child has exceeded allocated resources
Task assigned to child is no longer required
The parent is exiting and the operating systems does not allow a
child to continue if its parent terminates
Process Termination
Some operating systems do not allow child to exists if its parent has terminated. If a
process terminates, then all its children must also be terminated.
cascading termination. All children, grandchildren, etc. are terminated.
The termination is initiated by the operating system.
The parent process may wait for termination of a child process by using the
wait()system call. The call returns status information and the pid of the
terminated process
pid = wait(&status);
If no parent waiting (did not invoke wait()) process is a zombie
If parent terminated without invoking wait , process is an orphan
A process which has finished the execution but still has entry in the process table to
report to its parent process is known as a zombie process
Multiprocess Architecture – Chrome Browser
Many web browsers ran as single process (some still do)
If one web site causes trouble, entire browser can hang or crash
Google Chrome Browser is multiprocess with 3 different types of
processes:
Browser process manages user interface, disk and network I/O
Renderer process renders web pages, deals with HTML, Javascript. A new
renderer created for each website opened
Runs in sandbox restricting disk and network I/O, minimizing effect of security
exploits
Plug-in process for each type of plug-in
Threads 32
So far, process has a single thread of execution
Consider having multiple program counters per process
Multiple locations can execute at once
• Multiple threads of control -> threads
Must then have storage for thread details, multiple program
counters in PCB
Process creation is heavy-weight while thread creation is light-
weight
Kernels are generally multithreaded
Multiple tasks with the application can be implemented by
separate threads
Update display
Fetch data
Spell checking
Answer a network request
Threads - Benefits 33
Responsiveness – may allow continued execution if part
of process is blocked, especially important for user
interfaces
Resource Sharing – threads share resources of process,
easier than shared memory or message passing
Economy – cheaper than process creation, thread
switching lower overhead than context switching
Scalability – process can take advantage of
multiprocessor architectures
Single and Multithreaded Processes 34
Single and Multithreaded Processes 35
User and Kernel Threads 36
User threads - management done by user-level threads library
Three primary thread libraries:
POSIX Pthreads
Windows threads
Java threads
Kernel threads - Supported by the Kernel
Examples – virtually all general purpose operating systems,
including:
Windows
Solaris
Linux
Tru64 UNIX
Mac OS X
Multithreading Models 37
Many-to-One
One-to-One
Many-to-Many
38
References
Silberschatz, Gagne, Galvin: Operating System Concepts, 6th Edition