SYSDA lecture 2

Introduction to operating systems

Important terms:
variable- item of data used by a program, stored in memory
Data structure- organised collection of related variables
Function- self-contained piece of code with clearly defined I/O- note, can be “called” from many places in a program

I/O generalities

I/O devices, like a hard disk will have an associated set of ports, through which device is controlled and data is transferred

Available ports have numbered addresses e.g. port 84
Each device is mapped to a consecutive range of ports- which are known as device registers
Control info and data for the device are written to individual ports, and data may be read from ports
^ so programs that control ports can spy!

Example below taken from lecture slide

Another example is register 0 on an ATA disk which reads and writes data
Whereas 1 has the read function od error and write function of write precompensation


Sending a disk command

Reading disk from data goes similar to example given below (which is from lecture)

  1. Read status register 7 to ensure device is not busy currently. When it is not:
  2. Write number of sectors you want to register 2
  3. Write addresses of sectors wanted into sectors 3,4,5,5
  4. Write a special value CMD_READ to command register, register, 7 Then disk controller will start reading the disk

IN and OUT instructions
If base address (lowest port number) for ATA controller was 170 Then instruction IN EAX 0x177 would read disk status register (register 7, which is 170+7) To CPU register EAX
OUT is used for writing to disk control “registers”

Interrupts
Electrical signal which is asserted to gain attention of CPU
Signal passes through interrupt controller
Diff I/O devices use different IRQ numbers



Interrupt Handlers
When interrupt occurs, CPU must (temporarily) abandon whatever programme it’s executing and instead execute specialised code to deal with the new event

During the start up, the BIOS or OS registers a set of functions which are to occur in response to specific types of functions- known as interrupt handlers

Privileged instructions
IN and OUT are examples of these
Can only be executed while CPU in special kernel mode
So only code running whilst the CPU is in kernel mode can directly perform I/O

Protection
This is to protect from user error or abuse

What is OS? Operating System as “kernel” code
So- application code runs in user mode
Most of the OS proper runs in kernel mode, meaning it has access to the full range of instructions

I/O operations can only be performed directly by the os on behalf of application progrms
What controls mode of CPU? Program status word does

Processes
The OS and processes

A typical CPU can only run one or very few small programs at a time
So OS is responsible for sharing this resource between processes
Done through time-sharing
Context switching- happens so frequently that everything appears to be running at once

How is this arranged?
Another app of interrupt systems but the I/O device involved is system clock

So

So every 10ms, interrupt controller sees table, picks next one at table
This is called and process dispatched by DP
Then next process
Scheduler chooses from front of the queue/table

System calls
Invoking the OS

How does the user ask OS to perform privileged instructions on its behalf?
Through system calls

Performs actions such as creating new processes and performing input or output

Function call by users cant directly change processor to kernel mode
Instead, software interrupt may be raised- similar to hardware interrupt

Note how similar it looks to prev example

For a read call, a system call handler may call into a device drive that may for example talk to ATA disk

Later, when data available, hardware interrupt goes through another entry in the interrup table and eventually the user process is rescheduled

Read begins with soft interrupt, ends with hard

Comments