Fundamentals• Algorithm:– It is the logical procedure to solve a certain problem– It is informally specified a a sequence of elementary stepsthat an “execution machine” must follow to solve theproblem– It is not necessarily expressed in a formal programminglanguage!

• Program:– It is the implementation of an algorithm in a programminglanguage– Can be executed several times with different inputs

• Process:– An instance of a program that given a sequence of inputsproduces a set of outputs

Operating System• An operating system is a program that– Provides an “abstraction” of the physical machine– Provides a simple interface to the machine– Each part of the interface is a “service”

• An OS is also a resource manager– The OS provides access to the physical resources of acomputing machine– The OS provides abstract resources (for example, a file, avirtual page in memory, etc.)

Levels of abstractionUser Level

ProgrammerLevel

Kim

Lisa

WebBrowser

Shell

SystemLevel

Bill

Videogame

PrinterDaemon

Interface (System API)

OperatingSystem

Virtual MemoryDeviceDriver

DeviceDriver

SchedulerDeviceDriver

DeviceDriver

Virtual File Sys.DeviceDriver

DeviceDriver

HW LevelMain BoardCPU

Keyboard

Network Card

Printer

Video Card

Printer

Hard disk

Abstraction mechanisms• Why abstraction?– Programming the HW directly has several drawbacks• It is difficult and error-prone• It is not portable

– Suppose you want to write a program that reads a text filefrom disk and outputs it on the screen• Without a proper interface it is virtually impossible!

Abstraction Mechanisms• Application programming interface (API)– Provides a convenient and uniform way to access to oneservice so that• HW details are hidden to the high level programmer• One application does not depend on the HW• The programmer can concentrate on higher level tasks

– Example• For reading a file, linux and many other unix OS provide theopen(), read() system calls that, given a “file name” allow toload the data from an external support

the need for concurrency• there are many reason for concurrency– functional– performance– expressive power

• functional– many users may be connected to the same system at thesame time• each user can have its own processes that executeconcurrently with the processes of the other users

– perform many operations concurrently• for example, listen to music, write with a word processor, burn aCD, etc...• they are all different and independent activities• they can be done “at the same time”

the need for concurrency (2)• performance– take advantage of blocking time• while some thread waits for a blocking condition, another threadperforms another operation

– parallelism in multi-processor machines• if we have a multi-processor machine, independent activities can becarried out on different processors are the same time

• expressive power– many control application are inherently concurrent– concurrency support helps in expressing concurrency, makingapplication development simpler

theoretical model• a system is a set of concurrent activities– they can be processes or threads

• they interact in two ways– they access the hardware resources• processor• disk• memory, etc.

– they exchange data

• these activities compete for the resources and/orcooperate for some common objective

Process• The fundamental concept in any operating systemis the “process”– A process is an executing program– An OS can execute many processes at the same time(concurrency)– Example: running a Text Editor and a Web Browser at thesame time in the PC

• Processes have separate memory spaces– Each process is assigned a private memory space– One process is not allowed to read or write in the memoryspace of another process– If a process tries to access a memory location not in itsspace, an exception is raised (Segmentation fault), andthe process is terminated– Two processes cannot directly share variables

Memory Protection• By default, two processes cannot share theirmemory– If one process tries to access a memorylocation outside its space, a processorexception is raised (trap) and theprocess is terminated– The “Segmentation Fault” error!!Any reference to thisAny reference to thismemory results in amemory results in asegmentation faultsegmentation fault

Other dataHeap

StackBSSInitialized DataText

Processes• We can distinguish two aspects in a process• Resource Ownership– A process includes a virtual address space, a process image(code + data)– It is allocated a set of resources, like file descriptors, I/Ochannels, etc

• Scheduling/Execution– The execution of a process follows an ececution path, andgenerates a trace (sequence of internal states)– It has a state (ready, Running, etc.)– And scheduling parameters (priority, time left in the round, etc.)

Multi-threading• Many OS separate these aspects, by providing theconcept of thread• The process is the “resource owner”• The thread is the “scheduling entity”– One process can consists of one or more threads– Threads are sometime called (improperly) lightweightprocesses– Therefore, on process can have many different (andconcurrent) traces of execution!

Multi-threaded process model• In the multi-threadedprocess model eachprocess can have manythreads––––

Threads• Generally, processes do not share memory– To communicate between process, it is necessary to userOS primitives– Process switch is more complex because we have tochange address space

• Two threads in the same process share the sameaddress space– They can access the same variables in memory– Communication between threads is simpler– Thread switch has less overhead

Threads support in OS• Different OS implement threads in different ways– Some OS supports directly only processes• Threads are implemented as “special processes”

– Some OS supports only threads• Processes are threads’ groups

– Some OS natively supports both concepts• For example Windows NT

• In Real-Time Operating Systems– Depending on the size and type of system we can haveboth threads and processes or only threads– For efficiency reasons, most RTOS only support• 1 process• Many threads inside the process• All threads share the same memory

The thread control block• In a OS that supports threads– Each thread is assigned a TCB (Thread Control Block)– The PCB holds mainly information about memory– The TCB holds information about the state of the thread

Thread Table

TIDPIDCRIPSPOther Reg.StatePriorityTime left...

Thread states• The OS can execute many threads at the same time• Each thread, during its lifetime can be in one of the followingstates–––––

StartingReadyExecutingBlockedTerminating

(the thread is being created)(the thread is ready to be executed)(the thread is executing)(the thread is waiting on a condition)(the thread is about to terminate)

Thread statesa)b)c)d)e)f)

CreationDispatchPreemptionWait on conditionCondition trueExit

The thread is createdThe thread is selected to executeThe thread leaves the processorThe thread is blocked on a conditionThe thread is unblockedThe thread terminates

bStart

a

Ready

Running

ceBlocked

fdTerminate

Thread queuesReady queueAdmit

Dispatch

CPUPreemption

Event occurs

Wait condition 1

Event occurs

Wait condition 2

Event occurs

Wait condition 3

Context switch• It happens when– The thread has been “preempted” by another higher prioritythread– The thread blocks on some condition– In time-sharing systems, the thread has completed its “round”and it is the turn of some other thread

• We must be able to restore the thread later– Therefore we must save its state before switching to anotherthread