What will be covered in MidtermI? CS 3733 Operating Systems Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio Basics of C programming language Processes, program and system call Scheduling File system and IO IPC communication 1 2 Lecture 1 - Outline Von Neumann Architecture Operating System: what is it? Evolution of Computer Systems and OS Concepts Different types/variations of Systems/OS Ø Parallel/distributed/real-time/embedded OS etc. OS as a resource manager Ø How does OS provide service? interrupt/system calls OS Structures and basic components Ø Process/memory/IO device managers 3 This describes a design architecture: Before, computers have fixed uses A processing unit containing an ALU and processor registers A control unit containing an instruction register or program counter A memory to store both data and instructions (Stored-program computer) External mass storage, and input and output mechanisms. 4 1
Operating Systems: Definitions A program that acts as an intermediary between the user/programs of computer and computer hardware. From system view Ø Kernel the program running at all times (all else being application programs) Ø Control program controls the execution of user programs and operations of I/O devices Ø Resource allocator manages and allocates resources Goals of Operating system Ø Convenience: Make the computer convenient to use. Ø Efficiency: Manage system resources in an efficient manner 5 OS Interface: APIs and System Calls For application programmers Application programming interface (API) Ø The run-time support system (run-time libraries) provides a system-call interface, that intercepts function calls in the API and invokes the necessary system call within the operating system System calls provide the interface between a running program and the operating system. Ø Generally available in routines written in C and C++ Ø Certain low-level tasks may have to be written using assembly language 6 Lecture 2: Outline Programs, Processes and Threads Process creation and its components States of a process and transitions PCB: Process Control Block Process (program image) in memory Argument Arrays Making Functions Safe Storage and Linkage Classes Programs vs. Processes Program: a set instructions/functions Ø To accomplish a defined task Ø Passive entity, stored as files on disk Process: a program in execution Ø Dynamic concept: running of a program Ø Unit of work in a system Multiple processes may be from a single program Ø Run/execute the program multiple times How do we run/execute a program? 7 8 2
Process: Address Space An Example: fork( ) In UNIX A process includes Ø Program code: text segment Ø Global variables: data section Ø Temporary data (local variables, function parameters, and return address etc.): Stack Ø Dynamically allocated data (malloc): Heap Ø Program counter (PC) and registers Process execution must progress in sequential fashion (a single thread) Ideally Text pid = 25 Data Stack Process Status < > int cpid = fork( ); if (cpid = = 0) { <child code> exit(0); } <parent code> wait(cpid); File File Resources Resources UNIX kernel Text pid = 26 Data Stack Process Status cpid = 26 < > int cpid = fork( ); if (cpid = = 0) { <child code> exit(0); } <parent code> wait(cpid); cpid = 0 9 10 Linkage Classes (cont.) Variables Ø Declared outside any function and function name identifiers have external linkage by default; however, they can be given internal linkage with the key word static Ø Declared inside a function are only known inside that function and are said to have no linkage Activity void main() { if (fork() == 0) { printf( a ); } else { printf( b ); waitpid(-1, NULL, 0); } printf( c ); exit(0); } List all the possible output sequences for this program. Solution: We can t make any assumption about the execution order of the parent and child. Thus, any topological sort of b -> a and a -> c is possible: acbc abcc bacc 11 3
Lecture 03: Outline Process queues and scheduling events Different levels of schedulers Preemptive vs. non-preemptive Context switches and dispatcher Performance criteria Ø Fairness, efficiency, waiting time, response time, throughput, and turnaround time; Classical schedulers: FIFO, SFJ, PSFJ, and RR CPU Gantt chart vs. process Gantt charts Scheduler A process is migrated among various queues Operating system must select, for scheduling purposes, processes from these queues in some fashion. Ø The selection process is called as Scheduler. 13 14 Non-Preemptive vs. Preemptive Non-preemptive scheduling: voluntarily give up CPU Ø Once a process has the CPU: until it finishes or needs I/O Ø Not suitable for time-sharing Ø Only IO (case 1) and process termination (case 4) can cause scheduler action Preemptive scheduling Ø Process may be taken off CPU non-voluntarily Ø Both occasions 2 and 3 may cause scheduler action Ø 2 is a special case of 3 Ø Time-sharing systems have to be preemptive! Context Switch When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch! Context of a process represented in the PCB" Context-switch time is overhead; the system does no useful work while switching" Hardware support" Ø Multiple set of registers then just change pointers" Other performance issues/problems" Ø Cache content: locality is lost" Ø TLB content: may need to flush" 15 4
CPU-bound vs. IO-Bound Performance Criteria Bursts of CPU usage alternate with periods of I/O wait CPU-bound: high CPU utilization, interrupts are processed slowly I/O-bound: more time is spending on requesting data than processing it Process 1: CPU bound" Process 2:" I/O bound" Time" CPU bursts" Total CPU usage" I/O waits" Total CPU usage" 17 Methods to measure performance of CPU schedulers Fairness Ø Each process gets a fair share of CPU in Multiprogramming Efficiency: CPU Utilization Ø Percentage of time CPU is busy; e.g., 40%, 80%,100%(?) Throughput Ø Number of processes completed per unit time Ø E.g., 10 per second; or 1 per hour Turnaround Time Ø Time from submission to termination Ø Sum of CPU time, I/O time, and waiting time 18 Performance Criteria (cont.) Waiting time Ø Time for a process waiting for CPU in a ready queue Ø Scheduling algorithms: no effect on CPU or I/O time Response time Ø Time between submission and the first response Ø Good metric for interactive systems Classical Scheduling Algorithms First-Come First Served (FCFS) Shortest Job First (SJF) Preemptive Shortest (remaining) Job First (PSJF) Round Robin Scheduling (RR) Response time variance Ø For interactive systems, response time should NOT vary too much 19 20 5
Lecture 05: Outline Basics of File Systems Directory and Unix File System: inodes UNIX I/O System Calls: open, close, read, write, ioctl File Representations: FDT, SFT, inode table fork and inheritance, Filters and redirection File pointers and buffering Directory operations Links of Files: Hard vs. Symbolic inode: Store File Information (SGG 12.4) Traditional UNIX inode structure. 21 22 Unix I/O Related System Calls Device independence: uniform device interface I/O through device drivers with standard interface Use file descriptors 3 file descriptors open when a program starts Ø STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO 5 main system calls for I/O Ø open, close, read, write, ioctl Ø Return -1 on error and set errno 23 File Representation File Descriptor Table: user program area Ø An array of pointers indexed by the file descriptors Ø The pointers point to entries in System File Table System File Table: kernel area Ø Contains entries for each open file Ø Entries contain pointers to a table of inodes kept in memory Ø Other information in an entry: current file offset; count of file descriptors that are using this entry; Ø When a file is closed, the count is decremented. The entry is freed when the count becomes 0. In-Memory Inode Table: copies of used inodes 24 6
File Representation (cont.) Redirection A program can modify the file descriptor table entry so that it points to a different entry in the system file table. This action is known as redirection. Figure 4.2 (page 120): Relationship between the file descriptor table, the system file table and the in-memory inode table. 25 26 Filters and Redirection Example 4.35 (p129) Ø cat > my.file 27 Links in Unix Link: association between a filename and an inode Two types of links in Unix: Hard vs. Symbolic/Soft Ø Hard link: A hard link just creates another file with a link to the same underlying inode. Ø Soft link: a link to another filename in the file system When a file is created Ø A new inode is assigned Ø A new directory entry is created: directly link the filename to its inode à hard link Ø inode tracks the number of hard links to the inode Additional hard links Ø Created : ln oldname newname 28 7
Lecture 06: Outline Inter-Process communication (IPC) Pipe and its operations FIFOs: named pipes Ø Allow un-related processes to communicate Ring of communicating processes Ø Steps for Ring Creation with Pipes Ø A Ring of n Processes Other issues in token ring Ø Which process to write/read: token management Pipe: Communication Buffer Create a pipe: system call pipe() #include <unistd.h> int pipe(int fildes[2]); Ø Create a unidirectional communication buffer with two file descriptors: fildes[0] for read and fildes[1] for write Ø Data write and read on a first-in-first-out base Ø No external or permanent name, and can only be accessed through two file descriptors Ø The pipe can only be used by the process that created it and its descendants (i.e., child & grand-child processes) 29 30 Program 6.1, parentwritepipe.c, page 185 Parent/Child File Descriptor Tables Which process write? Which process read? Which process write/read first? 31 32 8
Pipes and Simple Redirection Example 6.5, page 188: ls -l sort -n +4 After both dup2 functions 33 34 After Close Unnecessary File Descriptors Problems of Pipe Pros Ø simple Ø flexible Ø efficient communication Cons: Ø no way to open an already existing pipe. This makes it impossible for two arbitrary processes to share the same pipe, unless the pipe was created by a common ancestor process. 35 36 9
Named Pipes (FIFO) Named Pipes are more powerful than ordinary pipes " Communication is bidirectional " No parent-child/sibling relationship is necessary between the communicating processes " Several processes can use the named pipe for communication " Provided on both UNIX and Windows systems" Benefits of Named Pipes Named pipes are very simple to use. mkfifo is a thread-safe function. No synchronization mechanism is needed when using named pipes. write (using write function call) to a named pipe is guaranteed to be atomic. It is atomic even if the named pipe is opened in non-blocking mode. Named pipes have permissions (read and write) associated with them, unlike anonymous pipes. An Example of Unidirectional Ring Samples A ring of 5 nodes Ø Nodes represent processes Ø Links represent a unidirectional pipe How do the processes communicate? Ø Message with target PID Ø Receive targeted message Ø Forward other messages 39 40 10
Samples [5 pts] What is the difference between a process and a program? Samples3 Similar to Assignment2 Ø Compute the CPU utilization, finishing time and waiting time 41 42 11