Вы находитесь на странице: 1из 14

Yeditepe University

Department of Computer Engineering

CSE 232
Systems Programming



1. Introduction

A computer system can be divided into 4 components:

- the hardware (CPU, memory, input/output devices, etc.),

- the operating system,
- the system programs (word processors, spread sheets, accounting softwares,
- the application programs.

Application Programs

System Programs



Figure 1. Hierarchy of computer components

2. Computer System Structure

disk disk

disk printer tape-drive

CPU controller controller controller

system bus



Figure 2. Hardware of a computer system

2.1. Run-time Environment

- Contains a set of service routines that are available for use during program execution (eg. I/O
Program invokes a service routine, OS takes care of status testing, counting of bytes
transferred, error checking)

- Provides facilities for managing the resources

(eg. CPU is switched among user jobs according to some predefined policy)

2.2. Input and Output (I/O)

I/O devices:
block devices
character devices

I/O types:
Programmed I/O
Interrupt driven I/O (Interrupt service routine)

Direct memory access (DMA)

Example 1: Keyboard

Keyboard interface (controller)

Port 0
(Status) Keyboard
CPU electronics

Port 1

Figure 3. Keyboard controller

Port: an addressable register

- input only ports: CPU only reads from it
- output only ports: CPU only writes to it
- ports for both input and output

Every port has a number. A port can be referred to in an assembly language program by its
number, such as: IN 1

Status port: checked by the CPU to see if a key has been pressed and data is ready to be sent to
Data port: when a key is pressed, its ASCII code is found in the data port

Polling: CPU continually examines the status port until a key is pressed (Programmed I/O)

Ex: Reading a character from keyboard

KEYIN: IN 0 ; read status port into A

ANA A ; test A (10H key is pressed, 00H otherwise)
JZ KEYIN ; jump if 00H
IN 1 ; take data into A

Example 2: Printer

Printer interface (controller)

Port 2
(Control) Printer
CPU interface

Port 3

Figure 4. Printer controller

Data port: character to be printed is put in this port

Control port: when the character is ready in the data port, strobe signal (that tells the printer that
it can take the character in data port) is sent.

A timing loop must be used in the CPU so that the characters are never passed to the printer at a
rate faster than it can accept.

Special characters:
- carriage return: causes the printer head to move to the left side of the paper
- line feed: causes the printer to advance the paper by one line

Ex: Writing a character to printer

CHAROUT: OUT 3 ; put char. in accumulator into output port

CALL DELAY ; make sure printer is ready
MVI A, 10H ; set up strobe word (10H char. in data port is available for printing)
OUT 2 ; put it into control port to start strobe signal to printer
MVI A, 00H ; reset strobe (00H char. in data port is not available for printing)
OUT 2 ; put it into control port to finish strobe signal

Interrupt Processing

An interrupt is a signal that causes the computer to alter its normal flow of instruction execution.
eg. divide by zero, time out, I/O operation

The interrupt automatically transfers control to an interrupt processing routine which takes some
action in response to the condition that caused the interrupt, and then control is returned to the
program at which its execution was interrupted.


interrupt routine

interrupt occurs

Some interrupt types:

- I/O interrupt: Generated by an I/O channel or device.

- Program interrupt: Generated by some condition that occurs during program execution (eg.
divide by zero, illegal machine instruction, etc.)

- Timer interrupt: Generated by an internal timer within the CPU

- Supervisor call interrupt (SVC): A SVC instruction generates an interrupt that transfers control
to an OS service routine. The CPU switches from user mode to supervisor mode.

When an interrupt occurs, the status of the CPU is saved (registers, program counter etc.) and the
interrupt service routine is executed. After the interrupt is processed, they are restored by the
interrupt routine and control is transferred to the program. Saving and restoring the processor
status is called context switching.

Interrupt priority: During the processing of an interrupt, all interrupts of equal or lower priority
are inhibited, higher priority interrupts are allowed. At the end of an interrupt, if there are more
than one interrupt pending, one with the highest priority is executed.

Interrupt masking, is used to prevent certain interrupts from occurring while the first one is being

Example 3: Disk

Bus request line Disk controller

CPU RAM DMA other interface

R/W line Device electronics registers

Address bus
Data bus
Control bus

Figure 7. Disk controller

Interface registers

Status port

Control port

DMA address

DMA (Direct Memory Access): is a procedure for transferring data directly between controller
and memory without the help of CPU.

DMA device: is like a separate CPU with functions related to data transfer, such as:
- shuts the CPU down for short periods
- seizes control of the system bus
- transfers data between memory and external device without the intermediation of CPU

Disk controller: accepts commands from CPU such as:

- select disk drive
- set up DMA registers
- return current sector address
- return current track address
- move head in/out one track
- move head to track 0
- read/write current sector

Status port: contains the result of a query from CPU or status

Control port: commands for the controller are placed
DMA address registers: they receive addresses from CPU

3. Operating Systems

An operating system is a program that acts as an intermediary between a user of a computer and
the computer hardware.

Main functions of an operating system are:

1. The operating system is an interface between the user and the computer
provides access to the to the utility programs such as editors, debuggers, etc.
starts the execution of the programs
provides low level I/O routines to the user
controls access to the files
provides access functions for the protection of the resources and data from unauthorized
detects errors such as memory error, device failure, arithmetic overflow, etc. and provides
error handling routines
collects performance statistics

2. The operating system manages the resources of the system

controls and allocates resources
System resources are memory, CPU, I/O devices, etc.

3.1. Historical Review of Operating Systems


- Monitors are the simplest operating systems.

- Single user systems
- Allow user interaction

Simple Batch Systems

The user prepares a job (which consists of program, data and some control information), submits
it to the computer operator and receives the output after the program is executed. The job is
usually punched on cards and the output is usually printed. To speed up processing, jobs with
similar needs can be batched together and run as a group. Thus, the operator sorts the programs
with similar requirements into batches, runs each batch and sends the output to the programmer.
Therefore in a batch operating system there is a lack of interaction between the user and the job
while that job is executing.

The operating system is always in memory and its job is to transfer control automatically from
one job to the next.

As the I/O devices are slower than the speed of the CPU (the difference may be three orders of
magnitude or more), the CPU is often idle. For using the system more efficiently, cards can be
read from the card reader onto the disk. The operating system records their location on disk in a
table. When a job requests the printer, the output is copied into a system buffer and is written to
the disk. When the job is completed, the output is printed. This form of processing is called
spooling (simultaneous peripheral operation on line). Spooling is also used for processing data at
remote sites. The CPU send the data via communication paths to a remote site and the processing
is done with no CPU intervention. The CPU just needs to be notified when the processing is
completed, so that it can spool the next batch of data.

Spooling overlaps the I/O of one job with the computation of other jobs. During the execution of
one job, the spooler may be reading the input of another job while printing the output of a
different job.


card reader I/O line printer


Figure 8. Spooling

Multiprogrammed Batch Systems

Spooling provides a job pool on disk. In order to increase CPU utilization, jobs in the pool may
be scheduled in different ways, such as, first-come first-served, shortest job first, priority basis,

The most important aspect of job scheduling is the ability to multiprogram. The operating system
keeps several jobs (a subset of jobs in the job pool) in memory at a time. During the execution of
one job, if it waits for an I/O operation to complete, the operating system switches to and
executes another job. In this way, as long as there is always some job to execute, the CPU will
never be idle.

Time-Sharing Systems

In multiprogrammed batch systems, the user cannot interact with the program during its
execution. In time-sharing or multitasking systems, multiple jobs are executed by the CPU
switching between them, but the switching occurs so frequently that the user may interact with
each program while it is running. Time-sharing operating systems:
- uses CPU scheduling and multiprogramming,
- uses time-slice mechanism,
- allows interactive I/O,
- allows many users to share the computer simultaneously.

2.5. Personal Computer (PC) Systems

A computer system dedicated to a single user is referred to as a PC. In the first PCs, the operating
system was neither multiuser nor multitasking (eg. MS-DOS). The operating system concepts
used in mainframes and minicomputers, today, are also used in PCs (eg. UNIX, Microsoft
Windows NT, Macintosh OS).

2.6. Parallel Systems

Parallel systems have more than one processor. In multiprocessor systems (tightly coupled),
processors are in close communication, such as they share computer bus, clock, memory or

2.7. Distributed Systems

Distributed systems also have more than one processor. Each processor has its local memory.
Processors communicate through communication lines (eg. Telephone lines, high-speed bus,
etc.). Processors are referred to as sites, nodes, computers depending on the context in which they
are mentioned. Multicomputer systems are loosely coupled. Example applications are e-mail,
web server, etc.

2.8. Real-time Systems

Real-time systems are special purpose operating systems. They are used when there are rigid time
requirements on the operation of a processor or the flow of data, and thus it is often used
as a control device in a dedicated application (eg. fuel injection systems, weapon
systems, industrial control systems, ). It has well defined, fixed time constraints. The
processing must be done within the defined constraints, or the system fails.

Two types:
- Hard real-time systems guarantee that critical tasks complete on time.
- In Soft real-time systems, a critical real-time task gets priority over other tasks, and the
task retains that priority until it completes.

3.2. User Interfaces for Operating Systems

- simple command language
- a menu
- graphical representation of programs and data
- I/O routines (service routines)

Operating System Features

Bootstrap program: initializes CPU, loads OS kernel into memory

OS waits for event, signaled by an interrupt either from a hardware or software (system
call, monitor call), interrupt vector
Dual mode of operation: user mode, kernel mode (supervisor mode)
Hardware Protection (base, limit registers)

user program 1 User programs

run in user mode
user program 2
Kernel call

3 service Operating
1 procedure system
runs in
dispatch table kernel mode

Figure 9. How a system call can be made

BIOS (Basic Input Output System)

It is an interface between the operating system and hardware. Changes from one computer to

Ex: - transfers a single character to printer

- reads a sector from disk

BIOS routines of CP/M:


To call a BIOS routine:

Jump Table: (found at the beginning of BIOS routines)

1 byte 2 bytes




4. Some Operating System Functions

4.1. Process Scheduling

CPU is assigned to processes (tasks) by the operating system. In a multiprogramming system,

process scheduling is the management of the CPU by switching control among the various
processes according to some scheduling policy.


waiting for awaited event

some event has occurred

time slice expired

running ready

3.2. Memory Management

Real Memory

Real memory is divided into partitions, each process is assigned to a different partition:
- Fixed partitions
- Variable partitions
- Relocatable partitions

Virtual Memory

In a virtual memory system, user programs are allowed to use a large continuous virtual address
space (virtual memory), which may be even larger than the total amount of the real memory
available. Portions of the virtual memory are mapped to the real memory as they are needed by
the program. Demand paging is one of the mapping methods. This mapping process is
completely invisible to the user.

Virtual memory
Real memory



virtual address mapping real address

3.3. File Management

File management function of an operating system is an intermediate stage between the user
program and the I/O supervision.

user prog.

record logical request

file file
information manager

block physical request


I/O interrupt supervisor I/O


3.4. Job Scheduling

Job scheduling is the task of selecting the next user job to begin execution. In a
multiprogramming environment, job scheduler specifies the order of job execution.

input queue job active dispatcher CPU

scheduler jobs

3.5. Resource Allocation

The resources such as CPU, memory, I/O channels are needed by all user jobs, and their
allocation is handled automatically by the operating system. Some problems to be handled by the
operating system are:
- mutual exclusion
- process synchronization,
- deadlock avoidance
- security and protection