You are on page 1of 24

08.

705 Real Time Operating System Module 1

Module 1

Introduction to Operating system: An operating system (OS) is a program that manages the computer hardware. OS also provides a basis for application programs and acts as an intermediary between the computer user and the computer hardware. Some OS are designed to be convenient, others to be efficient, and others some combination of the two. Consider abstract view of the components of a computer system:

The hardware constitutes CPU, memory and I/O units, which provides the basic computing resources for the system. The application programs such as word processors/ spreadsheets/ compilers and Web browsers, define the ways in which these resources are used to solve users computing problems. The OS controls the hardware and coordinates its use among the various application programs for the various users. In user view, OS is designed mostly for ease of use (Home PC) and efficient resource utilization (work stations). In such cases it is designed to maximize resource utilization- to assure that all available CPU time, memory, and I/O are used efficiently and that no individual user takes more than OSs fair share. In system view, OS the program most intimately involved with the hardware and act as a resource allocator. The OS acts as the manager of the resources like CPU time, memory space, file storage space, I/O devices, etc. OS acts as a control program. It controls the execution of programs to prevent errors and improper use of computer. OS is the one program running at all times on the computer usually called the kernel. Bootstrap program is loaded at power up or reboot. It is typically stored in ROM and generally known as firmware. It loads OS kernel and starts execution.

Dept. of ECE, VKCET

Page 1

08.705 Real Time Operating System

Module 1

OS structure: One of the most important aspects of OS is the ability to multiprogram. Single users frequently have multiple programs running. Multiprogramming increases CPU utilization by organizing jobs (code and data) so that CPU always has one to execute. OS operations: Modern OS are interrupt-driven. For each type of interrupt, separate segments of code in the operating system determine what action should be taken. An interrupt service routine is provided that is responsible for dealing with the interrupt. Since the OS and the users share the hardware and software resources of the computer system, we need to make sure that an error in a user program could cause problems only for the one program running. Dual-mode operation: To allows OS to protect it and other system components. It contains user mode and kernel mode. When the computer system is executing on behalf of a user application, the system is in user mode. However when a user application requests a service from the operating system (via a system call), it must transition from user to kernel mode to fulfill the request. This is shown below:

Timer: OS maintains control over the CPU and helps to avoid to get stuck in an infinite loop. A timer can be set to interrupt the computer after a specified period. A simple technique is OS initialize a counter with the amount of time that a program is allowed to run. Every second the timer interrupts and the counter is decremented by one. As long as the counter is positive, control is returned to the user program. When the counter becomes negative, the OS terminates the program for exceeding the assigned time limit.

Kernel: The OS kernel is a basic set of objects, primitive operations, data structures, and processes. In one view, the kernel is the lowest level of software, immediately above the hardware architectural level. In other view, it is the part of the OS that resides permanently in main memory. By another view, kernel is the part of the OS that runs in a protected mode. The set of possible functions and objects in a kernel can be divided into four classes: 1. Process and thread management: process creation, destruction, and basic interprocess communication and synchronization. 2. Interrupt and trap handling: Responding to signals triggered by various system events; among these are the termination of a process, completion of an I/O operation, a timer signal indicating a timeout or clock tick, an error caused by a program, or hardware malfunctioning, etc. Dept. of ECE, VKCET Page 2

08.705 Real Time Operating System

Module 1

3. Resource management: Primitives for maintaining, allocating, and releasing units of various resources such as CPUs, timers, main memory, secondary storage, I/O devices, or les etc. 4. Input and output: Read, write, and control operations for initiating and supervising the transfer of data between the various I/O devices and main memory or registers. Micro-kernels: OS kernels can be quite small, consisting of most of the rst two classes (Process and thread management and Interrupt and trap handling) and some limited resource management, usually processor scheduling and perhaps some low-level virtual memory allocation. Windows NT and Mach are examples of systems with such micro-kernels. Monolithic-kernels: OS kernel almost all of the functions listed in the four classes (Process and thread management, Interrupt and trap handling, Resource management and Input and output operation) can be incorporated into a very large kernel. Most versions of Unix and Linux, have monolithic-kernels. Single-user OS. It has a single processor, runs a single program and interacts with a single user at a time. The OS for this system is very simple to design and implement. Example: - MS-DOS Only one program resides in computer memory and it remains that till it is executed. It is also called uni-program OS. In this OS, the whole, memory space is allocated to one program to memory managements not very difficult task to do. The CPU has to execute only 1 program at a time, so that CPU management also does not have any problem. In a single user OS, a single user can access the computer at a particular time. The computer which are based on this OS have a single processor and able to execute only a single program at a particular time. This system provides all the resources to the users all the time. The single user OS into following categories: 1. Single user, single tasking: In a single user, single tasking OS; there is a single user to execute a program at a particular system. Eg. MS-DOS 2. Single user, multitasking OS: In single user, multitasking OS a single user can execute multiple programs. Eg. A user can program different programs such as making calculations in excel sheet, printing a word document & downloading into the file from internet at the same time. Advantages of single user OS: The CPU has to handle only one application program at a time so that process management is easy in this environment. Due to the limited number of programs allocation of memory to the process & allocation of resources to the process becomes any easy task to handle. Disadvantages of single user OS: As the OS is handling one application at a time most of the CPU time is wasted, because it has to sit idle most of the time while executing a single program. Resources like memory, CPU are not utilized at the maximum.

Dept. of ECE, VKCET

Page 3

08.705 Real Time Operating System

Module 1

Multi-user OS: Multiple users on different terminals (monitor/keyboard, PCs etc.) to access a single system with an OS. Allows access data and process of a single machine. Powerful system and acts as a server system. Usually called client-server system and the general structure is shown below:

Advantages of Multi-user OS: As the OS is handling multiple application at a time most of the CPU time is efficiently using. Resources like memory, CPU are utilizing at the maximum. Multiple users can use a system at a time. Networked, distributed system can be implemented. Can be reduce hardware cost comparatively Disadvantages of multi-user OS: Complicated OS I/O devices and resources management is difficult. Execution of programs is comparatively slow. Require lot of memory space. OS functions(services): OS provide services to programs and to users of those programs. It provide environment for the execution of programs. The common functions are: 1. Program execution: Loads the program into memory and executes the program. Also end its execution normally or abnormally. 2. I/O operation: Open or close any I/O device or any file. 3. File system management: Read a file or write a file. 4. Communications: Share the data between different processes. 5. Error detection: To detect and make appropriate action to correct the errors for consistent computing.

Dept. of ECE, VKCET

Page 4

08.705 Real Time Operating System

Module 1

OS components: The different OS components are: 1. Process Management 2. Main Memory Management 3. File Management 4. I/O System Management 5. Secondary Management 6. Networking 7. Protection System 8. Command-Interpreter System System call: Provides an interface to the OS services. Available as routines written in C and C++, also certain low-level tasks may be in assembly language instructions. Consider an example to illustrate how system calls are used for writing a simple program to read data from one file and copy them to another file.

Sequences require the following operations: 1. Acquire input and output file names: They are specified in many ways depending on OS. One approach is the program to ask the user for the file names. In an interactive system this require a sequence of system calls for writing prompting message on the screen and then to read from the keyboard. On a mouse based and icon-based systems, a menu of files displayed in a window. The user can use the mouse to select the source name and a window can be opened for the destination name to be specified. This sequence requires many I/O system calls. 2. Once two files are obtained, the program must open the input file and create the output file. This requires another system call. There may be possible error conditions for each operation. When a program try to open the input file, if it doesnt exists or if it is protected from access, the program should print a message (system call) and terminate abnormally (system call). If input file exists, then user must create a new output file. If Page 5

Dept. of ECE, VKCET

08.705 Real Time Operating System

Module 1

there is already an output file with the same name, the program abort (system call) or user may delete existing file (system call) and create a new one (system call). 3. Enter into a loop that reads from the input file (system call) and writes to the output file (system call). Each read and write return the status information regarding various possible error conditions. 4. When the program find the end of the file or any hardware failure to read, abort (system call) the operation. 5. After the entire file is copied, the program may close both files (another system call), write a message to the console or window (system call) and finally terminate normally (final system call). Types of system calls: Different group of system calls are: 1. Process control 2. File manipulation 3. Device manipulation 4. Information maintenance 5. Communication 6. Protection Process Control: End, abort: running program needs to be able to halt its execution either normally (end) or abnormally (abort) Load, execute: load new program and execute it. Create process, terminate process: create a new job or process, or terminate it normaly. Get process attributes, set process attributes: attributes including the job's priority, its maximum allowable execution time, and so on. Wait for time: wait for a certain amount of time to pass. Wait event, signal event: wait for a specific event to occur. Allocate and free memory File management Create file and delete file Open and close Read, write and reposition (rewinding or skipping to the end of the file) Get file attributes and set file attributes: attributes include the file name, file type, protection codes, accounting information, and so on. Device management (devices like main memory, disk drives, access to files, and so on) Request device and release device: A system with multiple users may require us to first request the device, to ensure exclusive use of it. After finished with the device, release it. Read, write and reposition Get device attributes and set device attributes: I/O devices are identified by special file names, directory placement, or file attributes. Logically attach or detach devices Information maintenance Get time or date and set time or date: Current time or date Dept. of ECE, VKCET Page 6

08.705 Real Time Operating System

Module 1

Get system data and set system data: Number of current users, the version number of the OS, the amount of free memory or disk space, and so on. Get process, file, or device attributes Set process, file, or device attributes Communications (Two models IPC: Message passing model and shared-memory model) Create and delete communication connection Send and receive messages Transfer status information Attach or detach remote devices Protection: Set permission and get permission: Permission settings of resources such as files and disks Allow user and deny user: Users can-or cannot-be allowed access to certain resources Process: OS executes variety of programs and is called jobs, tasks etc. Process is a program in execution and process execution must progress in sequential fashion. The structure of process in memory is:

A process is more than the program code, which is sometimes known as the text section. It includes the current activity, as represented by the value of the program counter (PC) and the contents of the processor's registers. A process generally also includes the process stack, which contains temporary data (such as function parameters, return addresses, and local variables), and a data section, which contains global variables. A process may also include a heap, which is memory that is dynamically allocated during process run time. Process is an active entity, but a program is passive entity, which is also referred as executable file. Process state: As a process executes, it changes state. The state of a process is defined as the current activity of that process. Each process may be in one of the following states: 1. New: The process is being created. Dept. of ECE, VKCET Page 7

08.705 Real Time Operating System

Module 1

2. Running: Instructions are being executed. 3. Waiting: The process is waiting for some event to occur (such as an I/O completion or reception of a signal). 4. Ready: The process is waiting to be assigned to a processor. 5. Terminated: The process has finished execution. The name of the states is arbitrary and varies from one OS to other. Diagram of process state transition is shown below:

Process Control Block (PCB): Each process in OS is represented by PCB and also called task control block (TCB). The content of typical PCB is shown below:

The contents in PCB are: 1. Process state: The current state of the process. 2. Program counter: The address of the next instruction to be executed for this process. 3. CPU registers: The registers vary in number and type, depending on the computer architecture. They include accumulators, index registers, stack pointers, and generalpurpose registers, plus any condition-code (status register) information. 4. CPU-scheduling information: This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. 5. Memory-management information: This information may include such information as the value of the base and limit registers, the page tables, or the segment tables etc.

Dept. of ECE, VKCET

Page 8

08.705 Real Time Operating System

Module 1

6. Accounting information: This information includes the amount of CPU and real time used, time limits, account numbers, job or process numbers, and so on. 7. I/O status information: This information includes the list of I/O devices allocated to the process, a list of open files, and so on. CPU switching from process to process is shown below:

Process scheduling: Scheduler provide multi-programming feature. Process scheduler selects the available process from a set of several available processes for execution on the CPU. As process enters the system, they are put into a job queue, which consists of all processes in the system. The process residing in main memory and ready or waiting to execute are kept on a list called the ready queue. This queue is generally stored as linked list. A ready queue has a header file, which contains pointer to the first and final PCBs in the list. Each PCB includes a pointer field that points to the next PCB in the ready queue. A common representation of process scheduling is a queuing diagram and is shown below:

Dept. of ECE, VKCET

Page 9

08.705 Real Time Operating System

Module 1

Where the rectangular box representation is for queue, and two types of queue: ready queue and set of devices queue. Circle represents resources that servers queue. Arrow represents the flow of process in the system. A new process is initially put in the ready queue, it waits these until it is selected for execution or is dispatched. Once the process is dispatched to the CPU and is executing, one of several events could occur: 1. The process could issue an I/O request and then placed in a I/O queue. 2. The process could create a new sub-process and wait for the sub-processs termination. 3. The process could be removed forcibly from the CPU, as result of an interrupt and put back in the ready queue. For first two cases the process switches from waiting state to ready state and is then put back in the ready queue. Operation on process: The processes in most system can execute concurrently, and they may be created and deleted dynamically. Therefore two mechanisms provided by OS for this are: 1. Process creation 2. Process termination Process creation: A process may create several new processes using create-process system call during the execution of process. The creating process is called parent process and the new processes are called children of that processes, forming a tree of processes. Most OS including UNIX and Windows family identify the processes according to a unique process identifier (pid), which is typically an integer number Parent and children can share all resources. Children share subset of parents resources, but parent and child share no resources. When a process creates a new process, two possibilities exist in terms of execution: Parent and children execute concurrently. Parent waits until children terminate. There are also two possibilities in terms of the address space of the new process: Child duplicate of parent. Child has a program loaded into it. Dept. of ECE, VKCET Page 10

08.705 Real Time Operating System

Module 1

A UNIX example: fork() is a system call and create a new process, which consists a copy of address space of the original process. This allows the parent process to communicate with child process. Both process continue execution after system call fork(), but one difference: the return code for the fork() is zero for child process, whereas the a nonzero pid of the child is returned to the parent. exec() is a system call after fork(). Parent can create more children or it has nothing else to while the child runs, it can issue a wait() system call to move itself in the ready queue until the termination of child. The illustration is shown below:

A tree of processes in a typical UNIX system:

Process Termination: Process executes last statement and asks the OS to delete it by using the exit system call. Output data from child to parent via wait system call. Processs resources are deallocated by OS Parent may terminate execution of children processes via abort system call for a variety of reasons, such as: Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting, and the operating system does not allow a child to continue if its parent terminates. A UNIX example to terminate a process is system call exit(). Inter Process Communication (IPC):

Dept. of ECE, VKCET

Page 11

08.705 Real Time Operating System

Module 1

Processes executing concurrently in OS may be either independent processes or cooperating processes. Independent processes cannot affect or be affected by the other processes executing in the system. Cooperating processes can affect or be affected by the other processes executing in the system. Several reasons for providing the environment that allows process cooperation are: 1. Information sharing: To share information between processes 2. Computation speedup: Increase the speed of the task execution. 3. Modularity: To construct the system in modular fashion. 4. Convenience: User has to work with many tasks. Cooperating processes require IPC mechanism, that will allow to exchange data and information. Two types model of IPC are: 1. Shared memory 2. Message-passing Shared memory: A region of memory is that is shared by cooperating processes is established. The processes can exchange information by reading and writing data to the shared region.

The form of the shared data and the location are determined by the processes and are not under the operating systems control. The processes are also responsible for ensuring that they are not writing to the same location simultaneously. Producer-consumer problem is similar to the concept of cooperating processes. Producer produces products (in process it is information) and consumer consumes the products. Buffer memory resides in the region of memory that is shared by the producer and consumer processes. Buffer can be filled by the producer and emptied by the consumer. The buffer resides in the memory that can be shared by the processes. The producer and consumer must be synchronized, so that the consumer does not try to consume an item that has not yet been produced. Two types of buffer: 1. Unbounded buffer No practical limit in size of the buffer. Consumer may have to wait for the new items, but the producer can always produce new items.

Dept. of ECE, VKCET

Page 12

08.705 Real Time Operating System

Module 1

2. Bounded buffer Fixed buffer size. Consumer must wait if the buffer is empty and producer must wait if the buffer is full. Otherwise both can run concurrently. Message-passing: This allows the processes to communicate and synchronize their actions without sharing same address space. Communication takes place by means of messages exchanged between the processes.

This is similar to chat program in www (World Wide Web). Require two operations, send message and receive message. Message sent over by a process can be either fixed or variable sized. In fixed-sized messages, system level implementation is straight forward, but programming is difficult. In variable-sized message system require complex system level implementation, but programming becomes simpler. If a process A and B want to communicate, they send messages to and receive messages using a communication link. The link can be established by: 1. Direct or indirect communication link 2. Synchronous or asynchronous communication link 3. Automatic or explicit buffering link. Examples of IPC: Three different types: 1. POSIX (Portable OS Interface for UNIX) shared memory IPC. 2. Mach messaged based IPC 3. Windows XP message based IPC Signals for processes: One way for messaging between processes and OS Signals are software generated interrupts that are sent to a process when an event happens. Process send a signal uses the function signal ( ) having an integer number n in the argument. Signal provides shortest communication and sends a output n for a process which enables the OS to unmask or mask a process as per the value of n. Signal function calling is similar to run code in ISR and masking and unmasking is equivalent to flag at a register. Dept. of ECE, VKCET Page 13

08.705 Real Time Operating System

Module 1

When a signal is sent from process, OS interrupt the process execution and calls the function for signal handling. On return, the process continues as before. For example; signal(5) the signal handler 5 is reset. The function represented by 5 is fixed run by the signal handler. An advantage of signal is it takes shortest possible CPU usage time to run a handler. Some signal related IPC functions are: 1. SigHandler( ): To create signal handler corresponding to a signal identified by the signal number and define a pointer to the signal context. 2. Connect: It is an interrupt vector to a signal number, with signaled handler function and arguments. 3. Signal( ): Function to send a signal identified by the number in the argument. 4. Mask: Masking a signal 5. Unmask: Unmasking a signal 6. Ignore: Ignore a signal Some signals in Linux are: 1. Term: Terminate the process 2. Ign: Ignore the signal 3. Stop: To stop a process 4. Cont: To continue the process, if it is currently stopped. Process scheduling: Basic concept of process scheduling: Provide multiprogramming feature Effective utilization of CPU time When one process has to wait, the OS takes the CPU away from that process and gives the CPU to another process Every time one process has to wait, another process can take over use of the CPU All computer resources are scheduled before use. CPUs scheduling is central to OS design CPU-I/O Burst cycle: The success of CPU scheduling depends on the property of processes: process execution which consists of a cycle of CPU execution and I/O wait. Processes alternate between these two states. Process execution begins with a CPU burst. And is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on. Finally CPU burst ends with a system request to terminate execution

Dept. of ECE, VKCET

Page 14

08.705 Real Time Operating System

Module 1

The durations of CPU bursts vary greatly from process to process and from computer to computer. There may be large number of short CPU bursts and a small number of long CPU bursts. An I/O-bound program typically has many short CPU bursts, but a CPU bound program might have a few long CPU bursts. CPU Scheduler Short-term scheduler (or CPU scheduler) selects a process from the processes in memory that are ready to execute and allocates the CPU to that process The ready queue is not necessarily a first-in, first-out (FIFO) queue. There are various scheduling algorithms like FIFO queue, a priority queue, a tree, or simply an unordered linked list All the processes in the ready queue are lined up waiting for a chance to run on the CPU. The records in the queues are generally PCBs of the processes Preemptive and non-preemptive Scheduling: CPU-scheduling decisions may take place under the following four circumstances: 1. When a process switches from the running state to the waiting state. 2. When a process switches from the running state to the ready state. 3. When a process switches from the waiting state to the ready state. 4. When a process terminates. For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready queue) must be selected for execution. This is called non-preemptive or cooperative scheduling There is a choice, however, for situations 2 and 3. This is called preemptive scheduling.

Dept. of ECE, VKCET

Page 15

08.705 Real Time Operating System

Module 1

In non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. This scheduling method was used by Microsoft Windows 3.x. Windows 95 introduced preemptive scheduling, and all subsequent versions of Windows OS have used preemptive scheduling. The Mac OS X OS for the Macintosh also uses preemptive scheduling; previous versions of the Macintosh OS on cooperative scheduling. Cooperative scheduling is the only method that can be used on certain hardware platforms, because it does not require the special hardware (for example, a timer) needed for preemptive scheduling. Preemptive scheduling need more care with access to shared data. Consider the case of two processes that share data. While one is updating the data, it is preempted so that the second process can run. The second process then tries to read the data, which are in an inconsistent state. In such situations, new mechanisms to coordinate access to shared data are required. Preemption also affects the design of the OS kernel Dispatcher: A component involved in the CPU-scheduling function. That gives control of the CPU to the process selected by the short-term scheduler. This function involves the following: 1. Switching context. 2. Switching to user mode. 3. Jumping to the proper location in the user program to restart that program. Dispatcher should be as fast as possible, since it is invoked during every process switch. The time dispatcher takes for the dispatcher to stop one process and start another running is known as the dispatch latency. Scheduling Criteria Different CPU-scheduling algorithms have different properties, and the choice of a particular algorithm may favor one class of processes over another. The criteria include the following: 1. CPU utilization: To keep the CPU as busy as possible. Conceptually, CPU utilization can range from 0 to 100%. In a real system, it should range from 40% (for a lightly loaded system) to 90% (for a heavily used system) 2. Throughput: One measure of work and is the number of processes that are completed per time unit, called throughput. For long processes, this rate may be one process per hour; for short transactions, it may be ten processes per second 3. Turnaround time: The important criterion of scheduler is how long it takes to execute a process. It is the interval from the time of submission of a process to the time of completion. It is the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O. 4. Waiting time: Waiting time is the sum of the periods spent waiting in the ready queue. 5. Response time: Another measure is the time from the submission of a request until the first response is produced. This measure, called response time, is the time it takes to start Dept. of ECE, VKCET Page 16

08.705 Real Time Operating System

Module 1

responding, not the time it takes to output the response. The turnaround time is generally limited by the speed of the output device It is desirable to maximize CPU utilization and throughput and to minimize turnaround time, waiting time, and response time Scheduling algorithms: Several scheduling algorithms are there. Few are: 1. First-Come, First-Served (FCFS) Scheduling 2. Shortest-Job-First (SJF) Scheduling 3. Priority Scheduling 4. Round-Robin Scheduling 5. Multilevel Queue Scheduling 6. Multilevel Feedback Queue Scheduling FCFS: Simplest CPU-scheduling algorithm. The process that requests the CPU first is allocated the CPU first. FCFS policy is easily managed with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. The average waiting time under the FCFS policy is often quite long Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds:

If the processes arrive in the order P1, P2, P3 and are served in FCFS order, we get the result shown by the Gantt chart (which is a bar chart that illustrates a particular schedule, including the start and finish times of each of the participating processes). Gantt chart for the example:

The waiting time is 0ms for process P1, 24ms for process P2, and 27ms for process P3. Thus, the average waiting time is (0+24+27)/3 = 17ms If the processes arrive in the order P2, P3, P1; Gantt chart:

Waiting time for P1 = 6ms Waiting time for P2 = 0ms Waiting time for P3 = 3ms Average waiting time = (6 + 0 + 3)/3 = 3ms Dept. of ECE, VKCET Page 17

08.705 Real Time Operating System

Module 1

Thus, the average waiting time under an FCFS policy is generally not minimal and may vary substantially if the processes CPU burst times vary greatly. Convoy effect in FCFS: Assume one CPU-bound process and many I/O-bound processes. As the processes flow around the system, the following scenario may result. The CPU-bound process will get and hold the CPU. During this time, all the other processes will finish their I/O and will move into the ready queue, waiting for the CPU. While the processes wait in the ready queue, the I/O devices are idle. Eventually, the CPU-bound process finishes its CPU burst and moves to an I/O device. All the I/O-bound processes, which have short CPU bursts, execute quickly and move, back to the I/O queues. At this point, the CPU sits idle. The CPU-bound process will then move back to the ready queue and be allocated the CPU. Again, all the I/O processes end up waiting in the ready queue until the CPU-bound process is done. There is a convoy effect as all the other processes wait for the one big process to get off the CPU. The convoy effect results in lower CPU and device utilization. The FCFS scheduling algorithm is non-preemptive. Once the CPU has been allocated to a process, that process keeps the CPU until it releases the CPU, either by terminating or by requesting I/O. The FCFS algorithm is thus particularly troublesome for time-sharing systems. SJF: Different approach to CPU scheduling. This algorithm associates with each process the length of the process's next CPU burst. When the CPU is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie. Also called shortest-next-CPU-burst algorithm, because scheduling depends on the length of the next CPU burst of a process, rather than its total length Consider the following set of processes, with the length of the CPU burst given in ms:

Gantt chart:

The waiting time of P1 = 3ms The waiting time of P2 = 16ms The waiting time of P3 = 9ms The waiting time of P4 = 0ms Thus the average waiting time = (3+16+9+0)/4 = 7ms

Dept. of ECE, VKCET

Page 18

08.705 Real Time Operating System

Module 1

By comparison, with FCFS scheduling scheme, the average waiting time would be (0+6+14+21)/4 = 10.25ms The SJF scheduling algorithm is provably optimal. Consequently, the average waiting time decreases. The real difficulty with the SJF algorithm is to know the length of the next CPU request. One approach is by approximate SJF scheduling. We may not know the length of the next CPU burst, but we may be able to predict its value. We can expect that the next CPU burst will be similar in length to the previous ones. By computing an approximation of the length of the next CPU burst, we can pick the process with the shortest predicted CPU burst. The SJF algorithm can be either preemptive or non-preemptive. The preemptive SJF algorithm will preempt the currently executing process, whereas a nonpreemptive SJF algorithm will allow the currently running process to finish its CPU burst. Preemptive SJF scheduling is sometimes called shortest-remaining-time-first scheduling. Consider the following four processes, with the length of the CPU burst given in ms:

Gantt chart:

P1 starts at 0ms, P2 arrives at 1ms and remaining time for P1 is 7ms and > time required for P2, so P1 is preempted and P2 is scheduled and so on. Average waiting time = [(10- 1)+(1 -1)+(17-2)+(5-3)]/4 = 26/4 = 6.5ms Non-preemptive SJF scheduling would result in an average waiting time is [0 +(8-1)+(17-2)+(12-3)]/4 = 7.75ms Priority Scheduling: The SJF algorithm is a special case of the general priority scheduling algorithm. A priority is associated with each process, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa. Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to 4,095. There is no general agreement on whether 0 is the highest or lowest priority. Some systems use low numbers to represent low priority; others use low numbers for high priority

Dept. of ECE, VKCET

Page 19

08.705 Real Time Operating System

Module 1

Consider the following set of processes, assumed to have arrived at time 0 in the order P1, P2,,P5, with the length of the CPU burst given in ms:

Gantt chart:

Average waiting time = (6+0+16+18+1)/5 = 8.2ms Priorities can be defined either internally or externally. Internally defined priorities use some measurable quantity or quantities to compute the priority of a process like time limits, memory requirements, the number of open files, and the ratio of average I/O burst to average CPU burst etc. External priorities are set by criteria outside the OS such as the importance of the process, the type and amount of funds being paid for computer use, the department sponsoring the work etc. Priority scheduling can be either preemptive or non-preemptive. When a process arrives at the ready queue, its priority is compared with the priority of the currently running process. A preemptive priority scheduling algorithm will preempt the CPU if the priority of the newly arrived process is higher than the priority of the currently running process. A non-preemptive priority scheduling algorithm will simply put the new process at the head of the ready queue. A major problem with priority scheduling algorithms is indefinite blocking. A process that is ready to run but waiting for the CPU can be considered blocked. A priority scheduling algorithm can leave some low-priority processes waiting indefinitely. A solution to the problem of indefinite blockage of low-priority processes is aging. Aging is a technique of gradually increasing the priority of processes that wait in the system for a long time Round-Robin Scheduling: Designed especially for time-sharing systems. It is similar to FCFS scheduling, but preemption is added to enable the system to switch between processes. A small unit of time, called a time quantum or time slice, is defined. A time quantum is generally from 10 to 100ms in length. The ready queue is treated as a circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time interval of up to 1 time quantum. To implement RR scheduling, we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue.

Dept. of ECE, VKCET

Page 20

08.705 Real Time Operating System

Module 1

The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process. If the process has a CPU burst of less than 1 time quantum, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue If the CPU burst of the currently running process is longer than 1 time quantum, the timer will go off and will cause an interrupt to the OS. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue. The average waiting time under the RR policy is often long. Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in ms:

If the time quantum of 4ms, then process P1 gets the first 4ms. Since it requires another 20ms, it is preempted after the first time quantum, and the CPU is given to the next process in the queue, P2. Process P2 does not need 4ms, so it quits before its time quantum expires. The CPU is then given to the next process, process P3. Once each process has received 1 time quantum, the CPU is returned to process P1 for an additional time quantum Gantt chart:

Average waiting time = [(10-4)+4+7]/3 = 5.66ms The RR scheduling algorithm is preemptive. The performance of the RR algorithm depends heavily on the size of the time quantum. If the time quantum is extremely large, the RR policy is the same as the FCFS policy. If the time quantum is extremely small (say, 1 millisecond), the RR approach is called processor sharing and (in theory) creates the appearance that each of n processes has its own processor running at 1/n the speed of the real processor. Small quantum time increases context switching and performance of RR scheduling. Let one process of 10 time units. If the quantum is 12 time units, the process finishes in less than 1 time quantum, with no overhead. If the quantum is 6 time units, the process requires 2 quanta, resulting in a context switch. If the time quantum is 1 time unit, then nine context switches will occur, slowing the execution of the process accordingly

Dept. of ECE, VKCET

Page 21

08.705 Real Time Operating System

Module 1

The time quantum to be large with respect to the context-switch time. If the context-switch time is approximately 10% of the time quantum, then about 10% of the CPU time will be spent in context switching. In practice, most modern systems have time quanta ranging from 10 to 100ms and context switching time is < 10ms. Multilevel Queue Scheduling: In this all processes are easily classified into different groups. For example, a common classification is made between foreground (interactive) processes and background (batch) processes. These two types of processes have different response-time requirements and so may have different scheduling needs. Also foreground processes may have priority (externally defined) over background processes. Multilevel queue scheduling algorithm partitions the ready queue into several separate queues. The processes are permanently assigned to one queue, generally based on some property of the process, such as memory size, process priority, or process type. Each queue has its own scheduling algorithm. Separate queues might be used for foreground and background processes. The foreground queue might be scheduled by an RR algorithm, while the background queue is scheduled by an FCFS algorithm. Multilevel queue scheduling algorithm with five queues is shown below:

Dept. of ECE, VKCET

Page 22

08.705 Real Time Operating System

Module 1

Each queue has absolute priority over lower-priority queues. No process in the batch queue, for example, could run unless the queues for system processes, interactive processes, and interactive editing processes were all empty. If an interactive editing process entered the ready queue while a batch process was running, the batch process would be preempted Each queue gets a certain portion of the CPU time, which it can then schedule among its various processes. For example, in the foreground-background queue can be given 80% of the CPU time for RR scheduling among its processes, whereas the background queue receives 20 percent of the CPU to give to its processes on an FCFS basis. Multilevel Feedback Queue Scheduling: In multilevel queue scheduling algorithm, processes are permanently assigned to a queue when they enter the system. This setup has the advantage of low scheduling overhead, but it is inflexible. In multilevel feedback queue scheduling algorithm, a process to move between queues. The idea is to separate processes according to the characteristics of their CPU bursts. If a process uses too much CPU time, it will be moved to a lower-priority queue. This scheme places I/O-bound and interactive processes in the higher-priority queues. In addition, a process that waits too long in a lower-priority queue may be moved to a higher priority queue. This form of aging prevents starvation. Consider a multilevel feedback queue scheduler with three queues, numbered from 0 to 2.

The scheduler first executes all processes in queue 0. Only when queue 0 is empty will it execute processes in queue 1. Similarly, processes in queue 2 will only be executed if queues 0 and 1 are empty. A process that arrives for queue 1 will preempt a process in queue 2. A process in queue 1 will in turn be preempted by a process arriving for queue 0. A process entering the ready queue is put in queue 0. A process in queue 0 is given a time quantum of 8ms. If it does not finish within this time, it is moved to the tail of queue 1. If queue 0 is empty, the process at the head of queue 1 is given a quantum of 16ms. If it does not complete, it is preempted and is put into queue 2. Processes in queue 2 are run on an FCFS basis but are run only when queues 0 and 1 are empty.

Dept. of ECE, VKCET

Page 23

08.705 Real Time Operating System

Module 1

In general, a multilevel feedback queue scheduler is defined by the following parameters: 1. The number of queues 2. The scheduling algorithm for each queue 3. The method used to determine when to upgrade a process to a higher priority queue 4. The method used to determine when to demote a process to a lower priority queue 5. The method used to determine which queue a process will enter when that process needs service

Dept. of ECE, VKCET

Page 24

You might also like