Processesand OS basics
RHS – SOC 2
OS Basics
• An Operating System (OS) is essentially an abstraction of a computer
• As a user or programmer, I do not care too much about the specifics of a given com-puter – I think of a computer in abstract terms
• A bit like interfaces and implementations…
RHS – SOC 3
OS Basics
A computer monitor…
RHS – SOC 4
OS Basics
• An Operating System shields us from dealing with concrete details of a computer
• We can think of a computer in terms of– A file system– Memory– Input/output devices
• …and not worry about details
RHS – SOC 5
OS Basics
• As application deve-lopers, we interface with the OS, not the hardware
• Still need a basic understanding of the way an OS works
RHS – SOC 6
OS Basics
• Main tasks for an OS:– Hardware operation– Software operation– Memory management– File system management– Security– Networking
RHS – SOC 7
Process management
• Software operation more specifically means process management
• What is a process…?
• A process is a running instance of a computer program
• Similar to the relation between a class and an object
RHS – SOC 8
Process management
• Class / Program– A specification of
behavior– Passive collection of
instructions– Only one definition– Resides in secondary
storage (hard disk)
• Object / Process– A ”living” entity– Active execution of
instructions– Multiple instances can
coexist (usually)– Resides in primary
storage (RAM)
RHS – SOC 9
Process management
• The OS manages the life-cycle of a process– Starting the process– Managing the process
while active– Terminating the process
RHS – SOC 10
Process management
• The complexity of life-cycle management depends on the OS category
• Single-tasking OS – only one process can be active at any time
• Multi-tasking OS – many processes can be active at any time
• Almost all modern OS are multi-tasking – we will focus on that category
RHS – SOC 11
Process management
• In a multi-tasking OS, we can start many tasks, but only have one CPU available
• CPU resources – and other resources – must thus be shared among processes
• Managing this is a key OS task!
• A process may thus be in more states than just ”running” or ”not running”
RHS – SOC 12
Process life-cycle
• A multi-tasking OS will always include a process scheduler
• The process scheduler must decide when resources can be assigned to a specific process, thus making it able to execute
RHS – SOC 13
Process life-cycle
• Process scheduling is not trivial!
• What is the overall goal…?– Fairness– Responsiveness– Meeting a deadline– Minimising waiting time– …and other possible objectives
RHS – SOC 14
Process life-cycle
• In a modern OS, processes can be assigned a priority
• The lower priority, the fewer resources assigned to the process
• Enables the OS to do certain tasks ”in the background”, like– Virus scan– Disk defragmentation
RHS – SOC 15
Process life-cycle
• Starting a process:– First, the process is created – this involves
loading a copy of the program from secondary storage into RAM
– The process is then put in a waiting state by the process scheduler
– The process will remain in the waiting state, until the resources needed by the process become available
RHS – SOC 16
Process life-cycle
• Running a process:– At some point, the needed resources ar
assigned to the process, and the process can start executing. The process is now running
– During the execution, the process can become blocked or again become waiting
– A process becomes blocked if it has to wait for some other action to complete, like opening a file
RHS – SOC 17
Process life-cycle
• Terminating a process– At some point, the process has completed its
task (perhaps stopped by user)– The state of the process then becomes terminated
– The OS can then reclaim the memory used by the (now terminated) process
RHS – SOC 18
Process life-cycle
Created
Waiting Blocked
Running Terminated
RHS – SOC 19
Exercises
• What is the primary purpose of an operating system?• What is the relation between a program and a process?• Try to press Ctrl+Shift+Esc, which brings up the task manager. Go
to Processes – how many processes are (approximately) running on your PC? How many of them can you recognise?
• What makes a multi-tasking OS complex?• What type of priority should a Virus Scanner run with (high or low)?
RHS – SOC 20
Memory management
• A very important part of managing a process is memory management
• The OS has to make sure that memory is available in a transparent and efficient manner for the process
• The OS uses a technique called virtual memory for enable this
RHS – SOC 21
Memory management
• When a process is started, the OS will set up a virtual memory address space for the process
• The process only interacts with the virtual memory adress space
• The OS maps the virtual memory adress space to physical memory (either RAM or secondary storage, e.g hard drive)
RHS – SOC 22
Memory management
RHS – SOC 23
Memory management
• Since the OS cannot predict how much memory a process will need, it is typically given a large virtual address space
• 32-bit OS: Up to 4 GB
• Sum of virtual address spaces often much larger than available RAM
RHS – SOC 24
Memory management
• In the virtual address space, we have three types of data– Program data; the program itself– Stack data; data which is allocated when
methods are called, etc. (local variables)– Heap data; data which is dynamically
allocated, using the new statement
RHS – SOC 25
Memory management
Program data
Stack data
Heap data
RHS – SOC 26
Memory management
• Using RAM as physical memory is much more efficient than using the hard drive
• OS will continuously try to map as much virtual memory to RAM as possible
• Memory is divided into pages (typically less than 1 Mb) – RAM can be considered a cache of most used pages
RHS – SOC 27
Memory management
• Whenever a process accesses (virtual) memory, the OS looks up the correspon-ding page of physical memory– If the page is already in RAM, fine (page hit)– If the page is in secondary storage, it is
swapped into RAM (page fault)
• What page is then swapped out…?
RHS – SOC 28
Memory management
• A page fault is expensive, since it involves copying data from secondary storage
• OS tries to minimise number of page faults
• Usually, the OS will swap out the Least Recently Used (LRU) page
RHS – SOC 29
Memory management
• In general, data placement is always a compromise between speed and volume
• Most modern CPUs have several layers of internal memory, with a similar strategy for memory management
• Managed by the CPU, not the OS
RHS – SOC 30
Memory management
RHS – SOC 31
Memory management
• Other aspects of memory management– Security; preventing exploits such as buffer
overruns or other malicious attacks– Inter-process communication; when two
processes need to exchange data– Optimisation; OS cleans up memory when
processes are terminated, and relocates memory to larger contiguous blocks
RHS – SOC 32
Exercises
• What are the advantages of using Virtual Memory address spaces?• What happens if the running processes use more virtual memory
than the amount of available physical memory?• What is a page hit? a page fault?• Why should the OS try to minimise the number of page faults?• Can you think of other strategies for swapping out memory pages
than the LRU (Least Recently Used) strategy?• See if you can find some information about a modern CPU on the
Internet (e.g Intel Core i7). How many layers of memory cache are on the chip? How much memory is in each layer?