Embedded RealTime Systems Lecture Operating Systems and Processes
Embedded Real-Time Systems Lecture: Operating Systems and Processes Dimitris Metafas, Ph. D. Slide 1
What is an Operating System (OS)? n n An operating system is a program that acts as an intermediary between a user of a computer and the computer hardware. The purpose of an operating system is to provide an environment in which a user can execute programs in a convenient and efficient manner. n A. Silberschatz Computer software can be roughly divided into two kinds: the system programs, which manage the operation of the computer itself, and the applications, which solve problems for their users. The most fundamental of all system programs is the operating system, which controls all the computer’s resources and provides the base upon which the application programs can be written. n A. Tanenbaum 2 User Applications Operating System Hardware
What is the OS doing? n n n n Resource allocation and control n Manage computer system’s resources (hardware and software) n Allocate them to specific programs as necessary n CPU is the most important resource Efficient operation of the computer system n Particularly important for large, shared multi user systems Convenience for the user n Simplify the work with computers (especially for small personal computers) Memory and File Management Networking Security … 3
Operating System Structure n There is no universal definition of what is part of an operating system and what is not: n n An OS is all what the vendor ships when you order it, or An OS is the one program running all the times on the computer (usually called OS kernel) with all else being application programs (system applications, utilities) Processes Applications System Call I/F Operating System Hardware OS Kernel Device Driver 4 Device Driver Process, Memory, File, and Device Management
System Calls – System call I/F Process n for the moment consider it as a program at execution System Calls n They provide the interface between a process and the operating system n Through the system calls, programs can use the operating system’s services, e. g. n Input from a device, n Output to a device, n Reading and writing files, n Interprocess communication, n … System call I/F n The operating system provides specific system libraries with predefined functions and subroutine calls (system API) 5
OS Kernel n n n OS Kernel: OS part that manages tasks, services interrupts, allocates shared resources, etc. n Includes the scheduler that determines which process should be executed next Different approaches: microkernel vs. macrokernel Microkernel (μ kernel) n e. g. VAX n Kernel is minimal, it contains only essential OS functions n Process/thread management n Interprocess communications n Memory management primitives n Services are provided by System Processes (Servers) Macrokernel n e. g. UNIX n Originally the kernel was small… n …but additional services were integrated into it resulting in a big kernel Various mixtures 6
Process Management n n n n Creation and Deletion of Processes (both user and system processes) Suspension and resumption of processes Process Scheduling Inter process communications mechanisms Process synchronization mechanisms Deadlock handling … 7
POSIX n n n POSIX (Portable Operating System Interface) is a set of standard OS interfaces based on the UNIX operating system. Why standardization? n ability to develop programs that could be moved among different manufacturer's computer systems (and OS), without having to be recoded. Why UNIX? n partly because UNIX was "manufacturer neutral. " n several major versions of UNIX existed so there was a need to develop a common denominator system. Each standard in the POSIX set is defined by a decimal following the POSIX. Thus: n POSIX. 1: standard for an application program interface in C. n POSIX. 2: standard shell and utility interface (the user's command interface with the operating system). n POSIX. 4: standard for thread management. The POSIX interfaces were developed under the auspices of the Institute of Electrical and Electronics Engineers (IEEE). n POSIX is IEEE 1003 standard 8
Posix Threads (Pthreads) Interface n Pthreads: Standard interface for ~60 functions that manipulate threads from C programs. n Creating threads n n n Determining your thread ID n n pthread_self Terminating threads n n pthread_create pthread_join pthread_cancel pthread_exit [terminates all threads] , ret [terminates current thread] Synchronizing access to shared variables n n pthread_mutex_init pthread_mutex_[un]lock pthread_cond_init pthread_cond_[timed]wait 9
Interprocess communication (IPC) n n n OS kernel provides mechanisms so that processes can pass data between them Two types of IPC semantics: n blocking: after sending a message, the sending process waits for response; n non blocking: after sending a message, the sending process continues. Two mechanisms for IPC n Shared memory: processes have some memory in common; n must cooperate to avoid destroying / missing messages. n Message passing: processes send messages along a communication channel no common address space. process 1 process 2 memory CPU 10
IPC (cont. ) n n Message passing n send(message) n receive(message) Direct messaging n send(message, process. X) n receive(message, process. Y) Indirect messaging n Via mailboxes, which are objects on which messages can be placed or removed by processes Buffering n Messages queued in a communication link 11
Kernel / User Interface n n n User processes run in user mode Kernel processes are privileged, run in kernel mode n They have access to devices and special kernel data structures n Unauthorized access from the user mode is prevented Access to the kernel: n Processes: through system calls n Interrupts: n Hardware interrupts: with those, devices announce that they need the CPU n Software interrupts: not from devices but from the kernel itself 12
Kernel activities n n n Kernel activities are divided in top half and bottom half activities Top Half activities are initiated from system calls n Process changes to kernel mode Bottom half activities are caused by device interrupts, which: n Are independent from the currently running process n Take the CPU from the currently running process for a (short) time Processes top half OS Kernel 13 Device Driver Device bottom half
Interrupts P 1 OS P 2 intr OS P 3 For a certain device: n In the interrupt handler for that device, try to perform: n minimum work possible n minimal I/O n Get register values, put register values. n Most of the device function is performed in the corresponding process / thread n i. e. read data in the interrupt handler, put them in a buffer and do the processing later (outside the interrupt handler) 14
Foreground / Background Systems n n n Small, simple systems usually do not have an OS Instead, they consist of an infinite loop that calls modules (functions) to perform various actions in the “Background”. n Background = task level Interrupt Service Routines (ISRs) handle asynchronous events in the “Foreground” n Foreground = interrupt level 15
Foreground/Background System “Background” “Foreground” interrupts ISR while loop ISR 1 Code Execution ISR 2 time 16
What is a Process? n n n A process is an instance of a program at execution n Program is a passive entity (the code) – the content of a file stored in disk n Process is an active entity, it is more than just the code: n A Process is the code (text segment) plus the current activity of a program, as represented by: n the PC (program counter), n the contents of the processor's registers, n the process stack and data (data segment). A process is a unique execution of a program n Several copies of a program may run simultaneously or at different times. A process has its own memory space, its own state and needs certain resources including CPU time, memory, files, I/O devices, … to be executed. Processes can be: n Operating system processes (executing system code) n User processes (executing user code) The operating system kernel manages processes. 17
Process Control Block (PCB) n n A Process Control Block (PCB) is an OS structure which holds the pieces of information associated with a process: n Process state: new, ready, running, waiting, etc. n Program counter: contents of the PC n CPU registers: contents of the CPU registers n CPU scheduling information: information on priority and scheduling parameters n Memory management information : Pointers to page or segment tables n Accounting information: CPU and real time used, time limits, etc. n I/O status information: which I/O devices (if any) this process has allocated to it, list of open files, etc. The PCB is OS specific 18
PCB: a data structure maintained by the O. S. struct pcb { char *pcb_usp; /* User stack pointer */ char *pcb_ssp; /* System stack pointer */ int pcb_r 0; int pcb_r 1; int pcb_r 2; int pcb_r 3; int pcb_r 4; int pcb_r 5; int pcb_r 6; int pcb_r 7; int pcb_fp; int pcb_pc; /* program counter */ int pcb_modpsr; /* program status register and mod register */ #if MMAX_XPC || MMAX_APC short pcb_isrv; /* ISRV Register in ICU(interrupt state) */ #endif MMAX_XPC || MMAX_APC quad pcb_f 0; quad pcb_f 1; . . . quad pcb_f 7; int pcb_fsr; /* FPU status register */ struct pt_entry *pcb_ptbr; int pcb_sigc[5]; #if MMAX_XPC int pcb_dcr; /* Debug Condition Register */ int pcb_dsr; /* Debug Status Register */ int pcb_car; /* Compare Address Register */ int pcb_bpc; /* Breakpoint Program Counter */ #endif MMAX_XPC }; 19
Process State A process can be in any one of many different states event occurred process deleted wait for event Delayed process deleted delay expired process deleted Dormant Waiting for Event delay task for n ticks context switch Ready process created Running context switch interrupted task deleted Interrupted 20 • Ready: waiting for a processor • Running: instructions are executed
Process Queues n n n Processes are arranged in queues A process queue is a linked list of PCBs Various queues corresponding to process states NULL head tail PCB PCB PCB running ready PCB waiting 21 PCB
Process vs. Task vs. Thread n n The terms process and task are often used interchangeably Thread = lightweight process: a process that shares memory space with other processes Thread – Process Similarities: threads n Have their own private register set (including PC) and stack. n Have their own logical control flow. n Can run concurrently. n Are context switched. n Have a state (ready, running, etc. ). n Can create child threads/ processes. Thread – Process differences: process n A single process can have multiple threads (peer threads) n Threads of a process are much like child processes of the process, but sharing memory space with parent process. n Peer threads share code, data, file descriptors and process priority, while processes (typically) do not. n All threads can access all address in the process memory space. n A thread can access another (peer) thread’s stack. n Threads are somewhat less expensive than processes. 22
Process vs. Task vs. Thread (cont. ) n Usual terminology n Process is always something big n Task n n n same with process in big systems same with thread in small or embedded systems Embedded systems n There is one process: the kernel n Tasks = threads 23
Concurrent Thread Execution n Two threads run concurrently (are concurrent) if their logical flows overlap in time. Otherwise, they are sequential. Examples: n Concurrent: A B, A C n Sequential: B C Thread A Time 24 Thread B Thread C
Why multiple processes? n Processes help us manage timing complexity: n multiple rates n n n multimedia automotive n A B asynchronous input n n time C user interfaces communication systems Life without processes: Code turns into a mess: n interruptions of one task for another n spaghetti code 25 A C A_code(); … B_code(); … if (C) C_code(); … A_code(); … switch (x) { case C: C(); case D: D(); . . .
Process Creation n n Processes may be created: n statically before system starts n dynamically during execution Creating a new process with fork(): n A process makes a copy of itself by calling the fork() function n Causes the OS to create a new process (child process) which is a copy of the process that called fork() (parent process) n Parent process keeps executing; while child also starts executing The child process shares with its parent: n the same code (text segment) n files that were opened by the parent process n the same data (data segment), excluding the return value of fork() PID = fork() returns a process identifier (PID) n In the child process, the value of the PID is zero n In the parent process, PID contains the child's PID n fork() is called once but returns twice This PID is useful for the parent process to keep track of all its children n e. g. , who is alive, who terminated 26
What happens on a fork() call? PID = fork(); if (PID == 0) { /* child process code */ } else { children[current_child] = PID; current_child++; /* this is the rest of the parent *process; continue your code * here E. g. : wait for your child * process to come back. */ } process a process b 27
Can the child process run a different code than the parent? n n PID = fork(); if (PID == 0) { /* child process code */ execv(“mychild”, childargs); perror(“execv”); exit(1); } execv, a POSIX function, takes as an argument the name of the file that holds the child’s code and an array of arguments. It overlays the child process with the new code and starts executing from the main function perror(“execv”)and exit(1)take care of the case that execv fails, and returns to the parent process a process b 28 process c
Process creation in POSIX PID = fork(); if (PID == 0) { /* child process code */ execv(“mychild”, childargs); perror(“execv”); exit(1); } else { /* parent process code */ parent_stuff(); wait(&cstatus); exit(0); } wait(&cstatus) n Waits for the child process n Sets the integer cstatus variable to the child process status n Makes sure that the child’s resources (memory) are freed exit() n C function used to leave a process Note n POSIX does not implement lightweight processes n Each POSIX process runs its own address space and cannot directly access the code of other processes n POSIX supports a mechanism for shared memory 29
fork() example n In the following two pieces of code, how many new processes will be created (excluding the current process)? int main() { fork(); } Answer: 1+2+4+8 = 15 int main() { if(fork() == 0) fork(); } Answer: 4 (only child in each case will get in) 30
Multiple Processes stack process priority CPU registers . . . CPU registers Memory: PCBs Processor } context CPU registers 31 process priority
Context Switching n n Multiple processes => CPU’s replacement of the currently running process with a new one (called “context switching”) Simply saves the old context and “restores” the new one 1. Current process is interrupted 2. Processor’s registers for that particular process are saved in a process specific table PCB stored 3. Process is placed on the “ready” list to wait for the next time it gets the CPU 4. Process control block stores memory usage, priority level, etc. – PCB updated 5. New process’s registers and status are loaded into the processor 6. New process starts to run This generally includes changing the stack pointer, the PC and the PSR (program status register) Note: 3 PCs involved in a context switch: of the OS kernel, and of the 2 switching processes 32 process 1 process 2. . . memory PC registers CPU
Multitasking Systems n n Batch system: operating system technique where one job completes before the next one starts Multitasking: operating system technique for sharing –in time a single processor between multiple processes (giving the impression that they are executed in parallel) Multiprocessing: processes really executed in parallel, in multiple CPUs Features provided by the OS to facilitate multiple processes running on the same CPU: n Context switching : alternating between the different processes or tasks n Scheduling : deciding which process to run next n Various scheduling algorithms n n n First come first served Shortest job first Round robin Priority based Critical sections : providing adequate memory protection when multiple tasks/processes run concurrently n Various solutions to dealing with critical sections 33
Cooperative Multitasking n n Multi tasking: n Cooperative multi tasking n Preemptive multi tasking Cooperative multi tasking: n running process decides (voluntarily) when to yield the CPU n the scheduler chooses which process to run next. if (x>2) sub 1(y); else sub 2(y); cswitch(); proc(a, b, c); proc(r, s, t); cswitch(); do-something; process 2 process 1 save_state(current-proc); p=choose_next_process; load_and_go(p); 34 scheduler
Cooperative Multitasking (cont. ) n Problems with co operative multitasking: n Programming errors can keep other processes out: n process never gives up CPU; n process waits too long to switch, missing input. if (x>2) sub 1(y); else { sub 2(y); cswitch(); } proc(a, b, c); process 1 35
Preemptive multitasking n n Most powerful form of multitasking: n OS controls when context switches should take place; n the OS scheduler chooses which process to run next. Use timer to call OS to switch contexts. Preemptive OS kernel: a kernel that can take a process out of the processor (before that is finished) 36 interrupt CPU timer n
Multitasking n n n In both cooperative and preemptive multitasking cases: n Hardware interrupts might cause a process to yield the CPU prematurely n The OS Scheduler decides the next process to run on the CPU, and starts this next process How and when can a process get control of the processor (to be executed)? n Periodical Clock interrupts invoke the OS. n The corresponding period is called time slice / time quantum. n It is during a clock interrupt that the kernel (scheduler) can determine if another process than the currently executed should run; in that case a context switch has to be performed. A process can be stopped at some point so that other code can run on the CPU n A (time slice) clock interrupt can cause a preemptive kernel to reschedule the CPU n An interrupt can cause the processor to run an interrupt handler 37
When Can A Context Switch Occur? n n Time slicing n Time slice (or quantum): period of time a task can run before a context switch can replace it n Driven by periodic hardware interrupts from the system timer n During a clock interrupt, the kernel’s scheduler can determine if another process should run and perform a context switch n Of course, this doesn’t mean that there is a context switch at every time slice! Preemption n Currently running task can be halted and switched out by a higher priority active task n No need to wait until the end of the time slice 38 Time slice Context switches interrupt Context switches
Context Switch Overhead n n n How often do context switches occur in practice? n It depends on how often users interact, how many processes need attention, etc. System context switch vs. processor context switch n Processor context switch = amount of time for the CPU to save the current process’s context and restore the next process’s context n System context switch = amount of time from the point that the process was ready for context switching to when it was actually swapped in n Hence, System CS = Processor CS + … System context switch time is a measure of responsiveness How long does a system context switch take? n Time slicing : time slice period (at least one) + processor context switch time n Preemptive : a processor context switch time n Preemption is mostly preferred because it is more responsive (system context switch = processor context switch) Non zero context switch time can push limits of a tight schedule. n In practice, OS context switch overhead is small. 39
Multitasking vs. batch system O Multitasking has more overheads – saving the current process, selecting the next process, loading the next process O Multitasking needs to provide for inter process memory protection ü Multitasking allows for concurrency – if a process is waiting for an event, another process can grab the CPU and get some work done 40
- Slides: 40