Docsity

Processes and Threads in Computer Operating Systems, Lecture notes of Operating Systems

Partial preview of the text

Download Processes and Threads in Computer Operating Systems and more Lecture notes Operating Systems in PDF only on Docsity!

CSE 380

Computer Operating Systems

Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture Note 2: Processes and Threads Lecture Note 2.1: Processes and System Calls

Process

q Consider a simple disk operating system (like MS-DOS) q User types command like “run foo” at Keyboard (I/O device driver for keyboard, screen) q Command is parsed by command shell q Executable program file (load module) “foo” is located on disk (file system, I/O device driver for disk) q Contents are loaded into memory and control transferred ==> process comes alive! (disk device driver, relocating loader, memory management) q During execution, process may call OS to perform I/O: console, disk, printer, etc. (system call interface, I/O device drivers) q When process terminates, memory is reclaimed (memory management) A process is a program in execution with associated data and execution context

Keeping track of processes

q For each process, OS maintains a data structure, called the

process control block (PCB). The PCB provides a way of

accessing all information relevant to a process:

ß This data is either contained directly in the PCB, or else the

PCB contains pointers to other system tables.

q Processes (PCBs) are manipulated by two main

components of the OS in order to achieve the effects of

multiprogramming:

ß Scheduler : determines the order in which processes will gain

access to the CPU. Efficiency and fair-play are issues here.

ß Dispatcher : actually allocates CPU to the process selected by

the scheduler.

Process Context

q The context (or image) of a process can be described by ß contents of main memory ß contents of CPU registers ß other info (open files, I/O in progress, etc.) q Main memory -- three logically distinct regions of memory: ß text region: contains executable code (typically read-only) ß data region: storage area for dynamically allocated data structure, e.g., lists, trees (typically heap data structure) ß stack region: run-time stack of activation records q Registers: general registers, PC, SP, PSW, segmentation registers q Other information: ß open files table, status of ongoing I/O ß process status (running, ready, blocked), user id, ...

When are processes created?

1. System initialization (many daemons for

processing email, printing, web pages etc.)

2. Execution of a process creation system call by a

running process (fork or CreateProcess)

3. User request to create a new process (executing

new applications)

List of all active processes: ps (on Unix), Ctl-Alt-Del (on Windows)

When are processes terminated?

1. Normal exit (voluntary)

2. Error exit (voluntary)

3. Fatal error (involuntary), due to bugs

4. Killed by another process (involuntary)

When does OS get invoked?

q Operating system gets control in two ways ß A user process calls OS by a system call (e.g. executes TRAP) ß An interrupt aborts the current user process q System stack can be used to exchange information between OS and user processes q Recall: Mode bit in PSW is set only when OS is running

Interrupts

q An interruption of the normal processing of processor. q Interrupts cause the CPU to suspend its current computation and take up some new task. Control may be returned to the original task at some time later. q Reasons for interrupts and/or traps: ß control of asynchronous I/O devices ß CPU scheduling ß exceptional conditions (e.g., div. by zero, page fault, illegal instruction) arising during execution q Interrupts are essentially what drives an OS. We can view the OS as an event-driven system, where an interrupt is an event. q Bounding interrupt handling latency is important for real-time systems.

Interrupts

On Intel Pentium, hardware interrupts are numbered 0 to 255 ß 0: divide error ß 1: debug exception ß 2: null interrupt ß 6: invalid opcode ß 12: stack fault ß 14: page fault ß 16: floating-point error ß 19-31: Intel reserved (not available?) ß 32-255: maskable interrupts (device controllers, software traps) Issue: can CPU be interrupted while an OS interrupt handler is executing? Maskable interrupts can be disabled, plus there is a priority among interrupts

14

Typical interrupt handling sequence

q Interrupt initiated by I/O device signaling CPU, by exceptional condition arising, through execution of special instruction, etc. q CPU suspends execution of current instruction stream and saves the state of the interrupted process (on system stack). q State typically refers to contents of registers: PC, PSW, SP, general- purpose registers. q The cause of the interrupt is determined and the interrupt vector is consulted in order to transfer control to the appropriate interrupt handler. q Interrupt handler performs whatever processing is necessary to deal with the interrupt. q Previous CPU state is restored (popped) from system stack, and CPU returns control to interrupted task.

Example: Servicing a Disk Interrupt

q When disk controller completes previous transfer, it generates an interrupt. q Interrupt handler changes the state of a process that was waiting for just-completed transfer from wait-state to ready-state. q It also examines queue of I/O requests to obtain next request. q I/O is initiated on next request. q CPU scheduler called. q Control returned to user mode.

System Calls

q Provide "direct access" to operating system services (e.g., file system, I/O routines, memory allocate & free routines) by user programs. q System calls are special, and in fact, are treated like interrupts. q Programs that make system calls were traditionally called "system programs" and were traditionally implemented in assembly language ß Win32 API in Windows q Each system call had a particular number. Instruction set has a special instruction for making system calls: SVC (IBM 360/370) trap (PDP 11) tw (PowerPC) - trap word tcc (Sparc) break (MIPS)

Steps in System Call

q User program pushes parameters to read on stack

q User program executes CALL instruction to invoke library

routine read in assembly language

q Read routine sets up the register for system call number

q Read routine executes TRAP instruction to invoke OS

q Hardware sets the mode-bit to 1, saves the state of the

executing read routine, and transfers control to a fixed

location in kernel

q Kernel code, using a table look-up based on system call

number, transfers control to correct system call handler

Steps in System Call (cont)

q OS routine copies parameters from user stack, sets up

device driver registers, and executes the system call using

privileged instructions

q OS routine can finish the job and return, or decide to

suspend the current user process to avoid waiting

q Upon return from OS, hardware resets the mode-bit

q Control transfers to the read library routine and all

registers are restored

q Library routine terminates, transferring control back to

original user program

q User program increments stack pointer to clear the

parameters