1
Module 2
Dr. Manas Ranjan Prusty, SCOPE
VIT Chennai
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)
System Calls - Example 3
System call sequence to copy the contents of one file
to another file
Example of a Standard API 4
System Call – Implementation 5
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 6
Relationship Standard API
System Call – Parameter Passing 7
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 8
System Call – Types 9
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.) 10
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.) 11
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.) 12
Protection
Control access to resources
Get and set permissions
Allow and deny user access
Examples of Windows and Unix System Calls
13
Standard C Library Example 14
C program invoking printf() library call, which calls write() system call
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
A von Neumann Architecture 19
Process Concept 20
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost interchangeably
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 21
Process States 22
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) 23
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 24
Threads 25
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 26
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 27
28
User and Kernel Threads 29
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
30
31
32
n s ?
s t i o
Q u e
A n y
33
References
Silberschatz, Gagne, Galvin: Operating System Concepts, 6th Edition
34
Yo u !
h a n k
T