Computer System
Programming
Moumen El-Melegy
Professor of Computer Engineering
Electrical Engineering Department
Assiut University, Assiut 71516
[email protected]
https://sites.google.com/site/EC427Spring20/
EC 427
4th Year, Computer & System Eng.
Operating System Concepts – 9th Edition 1.1 Silberschatz, Galvin and Gagne ©2013
Lecture 2
Computer System
Architecture
Operating System Concepts – 9th Edit9on Silberschatz, Galvin and Gagne ©2013
Computer System Structure
Computer system can be divided into four components:
Hardware – provides basic computing resources
CPU, memory, I/O devices
Operating system
Controls and coordinates use of hardware among
various applications and users
Application programs – define the ways in which the
system resources are used to solve the computing
problems of the users
Word processors, compilers, web browsers, database
systems, video games
Users
People, machines, other computers
Operating System Concepts – 9th Edition 1.3 Silberschatz, Galvin and Gagne ©2013
Computer Startup
bootstrap program is loaded at power-up or reboot
Typically stored in ROM or EPROM, generally
known as firmware
Initializes all aspects of system
Loads operating system kernel and starts
execution
Operating System Concepts – 9th Edition 1.4 Silberschatz, Galvin and Gagne ©2013
Interrupts
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
Hardware interrupt by one of the devices
An operating system is interrupt driven
Operating System Concepts – 9th Edition 1.5 Silberschatz, Galvin and Gagne ©2013
Interrupt Handling
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
Operating System Concepts – 9th Edition 1.6 Silberschatz, Galvin and Gagne ©2013
I/O Structure
Synchronous I/O: After I/O starts, control returns to user
program only upon I/O completion
Wait instruction idles the CPU until the next interrupt
Wait loop (contention for memory access)
At most one I/O request is outstanding at a time, no
simultaneous I/O processing
Asynchronous I/O: After I/O starts, control returns to user
program without waiting for I/O completion
System call – request to the OS 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
OS indexes into I/O device table to determine device
status and to modify table entry to include interrupt
Operating System Concepts – 9th Edition 1.8 Silberschatz, Galvin and Gagne ©2013
I/O Structure
I/O devices and the CPU can execute concurrently
Each device controller is in charge of a particular
device type
Each device controller has a local buffer
CPU moves data from/to main memory to/from local
buffers
I/O is from the device to local buffer of controller
Device controller informs CPU that it has finished its
operation by causing an interrupt
Operating System Concepts – 9th Edition 1.9 Silberschatz, Galvin and Gagne ©2013
Hardware Protection
OS must protect itself and users
Dual-Mode operation
I/O Protection
Memory Protection
CPU Protection
Operating System Concepts – 9th Edition 1.10 Silberschatz, Galvin and Gagne ©2013
Dual-Mode Operation
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
Operating System Concepts – 9th Edition 1.11 Silberschatz, Galvin and Gagne ©2013
I/O Protection
All I/O operation are privileged instructions
User cannot issue I/O instructions
Users can do I/O only through OS.
Operating System Concepts – 9th Edition 1.12 Silberschatz, Galvin and Gagne ©2013
Memory Protection
OS must protect interrupt vectors
OS must protect interrupt service routines
OS must protect its memory
OS must protect the user’s program memory from
others
OS uses Base and Limit registers.
Both can be loaded by only OS
Operating System Concepts – 9th Edition 1.13 Silberschatz, Galvin and Gagne ©2013
CPU Protection
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
Operating System Concepts – 9th Edition 1.14 Silberschatz, Galvin and Gagne ©2013
Chapter 2: Operating-System
Structures
Operating System Concepts – 9th Edit9on Silberschatz, Galvin and Gagne ©2013
System Components
Process Management
Memory Management
Storage Management
I/O Management
Protection and Security
Networking
Command-Interpreter System
Operating System Concepts – 9th Edition 1.17 Silberschatz, Galvin and Gagne ©2013
Process Management
A process is a program in execution. It is a unit of work within the
system. Program is a passive entity, process is an active entity.
Process needs resources to accomplish its task
CPU, memory, I/O, files
Initialization data
Process termination requires reclaim of any reusable resources
Single-threaded process has one program counter specifying
location of next instruction to execute
Process executes instructions sequentially, one at a time,
until completion
Multi-threaded process has one program counter per thread
Typically system has many processes, some user, some
operating system running concurrently on one or more CPUs
Concurrency by multiplexing the CPUs among the processes
/ threads
Operating System Concepts – 9th Edition 1.18 Silberschatz, Galvin and Gagne ©2013
Process Management Activities
The operating system is responsible for the following activities in
connection with process management:
Creating and deleting both user and system processes
Suspending and resuming processes
Providing mechanisms for process synchronization
Providing mechanisms for process communication
Providing mechanisms for deadlock handling
Operating System Concepts – 9th Edition 1.19 Silberschatz, Galvin and Gagne ©2013
Memory Management
To execute a program all (or part) of the instructions must
be in memory
All (or part) of the data that is needed by the program
must be in memory.
Memory management determines what is in memory and
when
Optimizing CPU utilization and computer response to
users
Memory management activities
Keeping track of which parts of memory are currently
being used and by whom
Deciding which processes (or parts thereof) and data
to move into and out of memory
Allocating and deallocating memory space as needed
Operating System Concepts – 9th Edition 1.20 Silberschatz, Galvin and Gagne ©2013
Storage Management
OS provides uniform, logical view of information storage
Abstracts physical properties to logical storage unit - file
Each medium is controlled by device (i.e., disk drive, tape
drive)
Varying properties include access speed, capacity, data-
transfer rate, access method (sequential or random)
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 directories
Mapping files onto secondary storage
Backup files onto stable (non-volatile) storage media
Operating System Concepts – 9th Edition 1.21 Silberschatz, Galvin and Gagne ©2013
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)
Operating System Concepts – 9th Edition 1.22 Silberschatz, Galvin and Gagne ©2013
I/O Subsystem
One purpose of OS is to hide peculiarities of hardware
devices from the user
I/O subsystem responsible for
Memory management of I/O including buffering
(storing data temporarily while it is being
transferred), caching (storing parts of data in faster
storage for performance), spooling (the overlapping
of output of one job with input of other jobs)
General device-driver interface
Drivers for specific hardware devices
Operating System Concepts – 9th Edition 1.23 Silberschatz, Galvin and Gagne ©2013
Protection and Security
Protection – any mechanism for controlling access of processes
or users to resources defined by the OS
Security – defense of the system against internal and external
attacks
Huge range, including denial-of-service, worms, viruses,
identity theft, theft of service
Systems generally first distinguish among users, to determine who
can do what
User identities (user IDs, security IDs) include name and
associated number, one per user
User ID then associated with all files, processes of that user to
determine access control
Group identifier (group ID) allows set of users to be defined
and controls managed, then also associated with each
process, file
Privilege escalation allows user to change to effective ID with
more rights
Operating System Concepts – 9th Edition 1.24 Silberschatz, Galvin and Gagne ©2013
Command Interpreter System
CLI or command line interpreter allows direct
command entry
Sometimes implemented in kernel,
sometimes by systems program
Sometimes multiple flavors implemented –
shells
Primarily fetches a command from user and
executes it
Sometimes commands built-in, sometimes
just names of programs
If the latter, adding new features doesn’t
require shell modification
Operating System Concepts – 9th Edition 1.25 Silberschatz, Galvin and Gagne ©2013
Bourne Shell Command Interpreter
Operating System Concepts – 9th Edition 1.26 Silberschatz, Galvin and Gagne ©2013
User Operating System Interface - GUI
User-friendly desktop metaphor interface
Usually mouse, keyboard, and monitor
Icons represent files, programs, actions, etc
Various mouse buttons over objects in the interface
cause various actions (provide information, options,
execute function, open directory (known as a folder)
Invented at Xerox PARC
Many systems now include both CLI and GUI interfaces
Microsoft Windows is GUI with CLI “command” shell
Apple Mac OS X is “Aqua” GUI interface with UNIX
kernel underneath and shells available
Unix and Linux have CLI with optional GUI interfaces
(CDE, KDE, GNOME)
Operating System Concepts – 9th Edition 1.27 Silberschatz, Galvin and Gagne ©2013
Touchscreen Interfaces
Touchscreen devices require
new interfaces
Mouse not possible or not
desired
Actions and selection based on
gestures
Virtual keyboard for text entry
Voice commands.
Operating System Concepts – 9th Edition 1.28 Silberschatz, Galvin and Gagne ©2013
The Mac OS X GUI
Operating System Concepts – 9th Edition 1.29 Silberschatz, Galvin and Gagne ©2013
System Calls
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)
Note that the system-call names used throughout this
text are generic
Operating System Concepts – 9th Edition 1.30 Silberschatz, Galvin and Gagne ©2013
Example of System Calls
System call sequence to copy the contents of one file to another file
Operating System Concepts – 9th Edition 1.31 Silberschatz, Galvin and Gagne ©2013
API – System Call – OS Relationship
Operating System Concepts – 9th Edition 1.32 Silberschatz, Galvin and Gagne ©2013
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
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
Operating System Concepts – 9th Edition 1.33 Silberschatz, Galvin and Gagne ©2013
Parameter Passing via Table
Operating System Concepts – 9th Edition 1.34 Silberschatz, Galvin and Gagne ©2013
Types of System Calls
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
Operating System Concepts – 9th Edition 1.35 Silberschatz, Galvin and Gagne ©2013
Types of System Calls (Cont.)
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
Operating System Concepts – 9th Edition 1.36 Silberschatz, Galvin and Gagne ©2013
Types of System Calls (Cont.)
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
Operating System Concepts – 9th Edition 1.37 Silberschatz, Galvin and Gagne ©2013
Types of System Calls (Cont.)
Protection
Control access to resources
Get and set permissions
Allow and deny user access
Operating System Concepts – 9th Edition 1.38 Silberschatz, Galvin and Gagne ©2013
Examples of Windows and Unix System Calls
Operating System Concepts – 9th Edition 1.39 Silberschatz, Galvin and Gagne ©2013
Standard C Library Example
C program invoking printf() library call, which calls write() system call
Operating System Concepts – 9th Edition 1.40 Silberschatz, Galvin and Gagne ©2013