Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D. Using a layered approach, the operating system is divided into N levels or layers Layer 0 is the hardware Layer 1 is often the kernel Layer N is the top-level user interface (GUI) Each layer uses functions and services of the layer (or layers) beneath it Also view as a stack of services The core program running in an operating system is called the kernel When a computer is switched on, a bootstrap program executes from ROM The bootstrap program initializes the system, then loads the operating system kernel and starts its execution Program instructions run either in user mode or in kernel mode switch modes via system calls Kernel mode allows the operating system to protect itself and its system components Kernel gives control to a user process, but may set a timer to ensure a process does not run beyond its allotted time To avoid infinite loops, memory leaks, memory hogs, etc. Not always effective in practice... ▪ Can you stop a runaway process before your computer crashes? Aaaaaaaugghhhh! I’m going to take this computer and... OS services are available via system calls System calls are made via an interface called an Application Program Interface (API) Common operating system APIs: ▪ Win32 API for Windows ▪ POSIX API for POSIX-based systems, including UNIX, Linux, Mac OS X ▪ Java API for Java Virtual Machine ▪ C/C++ Standard Library Types of system calls include: Process control (e.g. start/suspend/stop a process) ▪ Debugging information, too File management Device management Information retrieval and maintenance ▪ Current date/time, number of current users, OS version, amount of free memory, process information, etc. Communications (e.g. IPC, network) An API hides the implementation details of the underlying operating system Programmers just need to abide by the API specifications How do we change the API or the operating system services that it offers? the dude abides... Example using the printf() function from C One API may call another, which may in turn call another, and so on... Use registers to pass parameters: Store memory location X that references a block of data somewhere in memory An operating system provides services: Program execution ▪ Load programs into memory, run/suspend/halt programs, handle/display errors I/O operations ▪ Seamlessly interact with I/O devices, including disks, networks connection, etc. Filesystem manipulation ▪ Read/write/traverse filesystem directories, read/write files, enforce permissions, search for files Other operating system services: Inter-Process Communications (IPC) ▪ Processes exchange information via shared memory, message passing, sockets, pipes, files, etc. ▪ Often spans multiple computers and networks Error detection and recovery ▪ Detect errors in CPU, memory, I/O devices, processes, network connections, etc. ▪ Recover from errors gracefully, ensuring correct and consistent operations Multiprogramming goals: Maximize CPU utilization Maximize number of processes in memory Timesharing goals: Switch CPU among processes such that users interact with each program simultaneously almost! Maximize fairness Processes are created by the operating system Processes initially added to a job queue, which contains all processes waiting to enter the system From the job queue, processes that are ready for execution are added to the ready queue A long-term scheduler (i.e. job scheduler) selects processes from the job queue, adding those processes to the ready queue A short-term scheduler (i.e. CPU scheduler) selects processes from the ready queue and allocates time with the CPU More on this later.... The long-term scheduler is invoked infrequently The degree of multiprogramming of an operating system is defined as the number of processes in memory In a stable operating system, the average process arrival rate equals the average process departure rate Processes are either I/O bound or CPU bound A CPU-bound process does little I/O and instead makes heavy use of the CPU An I/O-bound process spends a majority of its time performing (i.e. waiting for) I/O The long-term scheduler should select a good process mix of CPU-bound and I/Obound processes Most modern operating systems have no long-term scheduler (e.g. Windows, UNIX) All processes are admitted to the ready queue, regardless of whether the operating system can handle the load Often results in users changing their usage habits.... Modern operating systems implement a medium-term scheduler to swap processes to/from memory and disk A process is an active program in execution Requires CPU time, memory, file access, network access, other I/O access Operating system is responsible for: Creating/deleting processes Scheduling processes Allocating resources to processes Synchronizing communication between processes For each process, the operating system manages and executes processes by recording: heap Program counter (PC) Registers Data section (global data) Stack (temporary data) Heap (dynamically allocated memory) stack data text/code As a process executes, it changes its state Operating system represents each process via a process control block (PCB) Process state Process ID or number Program counter (PC) CPU registers CPU-scheduling and memory management information List of open file/resource handles context switch takes a few milliseconds The short-term scheduler frequently decides which process the CPU executes next Typical time slice (t) a process has with the CPU is 100 milliseconds How much CPU time is wasted if t is 100ms and it takes 10ms to schedule the next process and perform the context switch? Processes are created from other processes A parent process creates a child process, which in turn creates child processes of its own, etc. A tree of processes is the result: Operating system resources are sometimes shared amongst processes Possibilities: Parent and child processes share all resources Child shares a subset of its parent’s resources Parent and child processes share no resources Each process has its own unique process identifier (pid) When a new process is created, the parent has two options: Parent process continues to execute concurrently with its children Parent process waits for its children to terminate their execution The child process decides what it will do: Duplicate the parent or load a new program In Unix, a new child process is forked via the fork() system call Child optionally calls the exec() system call to load a new program