Docsity

Process Scheduling and Synchronization in Operating Systems, Lecture notes of Operating Systems

Toggle sidebar
bg1
Prof(Dr) Anil Kumar
2023
1 | P a g e
Unit 2
• Interleaving is a process or methodology to make a system more efficient, fast and reliable by arranging
data in a non contiguous manner
• In disk storage and drum memory, interleaving is a technique used to improve access performance to
storage by putting data accessed sequentially into non-sequential sectors.
• There are several uses of it:
– Storage: hard disk and other storage devices are used to store user and system data, there is always
a need to arrange the stored data in appropriate manner
– Error Correction: error in data communication and memory can be correlated through interleaving
– Multidimensional data structure
• It divides the memory into small chunks.
• It is used a high level technique to solve memory issues for motherboard and chips.
In disk storage and drum memory, it is used to improve access performance to storage by putting data accessed
sequentially into non-sequential sectors
Concurrency
• Computer science defines concurrency as a property of systems where several processes are executing at the
same time, and may or may not interact with each other.
• The above definition are neither very precise nor complete!
• What does ”at the same time” mean?
• In a single processor it is obvious that only one process can execute at a time.
• This means that concurrent activities must be given time slices such that it seems that they are running in
parallel.
• Concurrency is the execution of several instruction sequence at the same time.
• In OS, this happens when there are several process threads running in parallel. These thread may
communicate with each other through either shared memory or message passing.
• Concurrency is also a programming design philosophy:
• In concurrent programming, programming attempts to break down a complex problem into several
simultaneous executing processes that can be addresses individually.
• Although concurrent programming offer better program structure then sequential programming, but it is not
always practical
• In concurrent system, computation being executed at the same time can diverge (move away/differ) giving
intermediate results.
• They system may end of Deadlock.
• Thus, to design for Robust Concurrency in an OS, a programmer needs to both reduce a problem into
individual, parallel task and coordinate the execution, memory allocation and data exchange of these tasks.
• Concurrency is the interleaving of process, in time to give the appearance of simultaneous execution.
• It is differed from parallelism, which offers genuine simultaneous execution.
• The basic idea behind the concurrency:
• A single process temporarily excludes all other from using a shared resource, in order to ensure the
system integrity. (ME)
• Updating of a shared variable may be regarded as a Critical Section.
• Critical section is a sequence of the instruction, with clearly marked beginning and end.
• It is usually safe guard updating of one or more shared variable.
• When a process enters a critical section, it must complete all instruction therein before any other process is
allowed to enter the same critical section.
• Only process executing the critical section is allowed to access to the shared variable, all other process
should be prevented from doing so, until the completion of critical section. This is often referred to Mutual
Exclusion.
•
Problem of Concurrency
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Process Scheduling and Synchronization in Operating Systems and more Lecture notes Operating Systems in PDF only on Docsity!

Unit 2

  • Interleaving is a process or methodology to make a system more efficient, fast and reliable by arranging data in a non contiguous manner
  • In disk storage and drum memory, interleaving is a technique used to improve access performance to storage by putting data accessed sequentially into non-sequential sectors.
  • There are several uses of it:
    • Storage: hard disk and other storage devices are used to store user and system data, there is always a need to arrange the stored data in appropriate manner
    • Error Correction: error in data communication and memory can be correlated through interleaving
    • Multidimensional data structure
  • It divides the memory into small chunks.
  • It is used a high level technique to solve memory issues for motherboard and chips. In disk storage and drum memory, it is used to improve access performance to storage by putting data accessed sequentially into non-sequential sectors Concurrency
  • Computer science defines concurrency as a property of systems where several processes are executing at the same time, and may or may not interact with each other.
  • The above definition are neither very precise nor complete!
  • What does ”at the same time” mean?
  • In a single processor it is obvious that only one process can execute at a time.
  • This means that concurrent activities must be given time slices such that it seems that they are running in parallel.
  • Concurrency is the execution of several instruction sequence at the same time.
  • In OS, this happens when there are several process threads running in parallel. These thread may communicate with each other through either shared memory or message passing.
  • Concurrency is also a programming design philosophy:
  • In concurrent programming, programming attempts to break down a complex problem into several simultaneous executing processes that can be addresses individually.
  • Although concurrent programming offer better program structure then sequential programming, but it is not always practical
  • In concurrent system, computation being executed at the same time can diverge (move away/differ) giving intermediate results.
  • They system may end of Deadlock.
  • Thus, to design for Robust Concurrency in an OS, a programmer needs to both reduce a problem into individual, parallel task and coordinate the execution, memory allocation and data exchange of these tasks.
  • Concurrency is the interleaving of process, in time to give the appearance of simultaneous execution.
  • It is differed from parallelism, which offers genuine simultaneous execution.
  • The basic idea behind the concurrency:
  • A single process temporarily excludes all other from using a shared resource, in order to ensure the system integrity. (ME)
  • Updating of a shared variable may be regarded as a Critical Section.
  • Critical section is a sequence of the instruction, with clearly marked beginning and end.
  • It is usually safe guard updating of one or more shared variable.
  • When a process enters a critical section, it must complete all instruction therein before any other process is allowed to enter the same critical section.
  • Only process executing the critical section is allowed to access to the shared variable, all other process should be prevented from doing so, until the completion of critical section. This is often referred to Mutual Exclusion.

Problem of Concurrency

  • Sharing global resources safely is difficult.
  • Optimal allocation of resources is difficult.
  • Locating programming errors can be difficult, because the contexts in which error occurs cannot be reproduced safely. Process
  • A process is basically a program in execution.
  • The execution of a process must progress in a sequential fashion.
  • Means: we write our computer programs in a text file and when we execute this program, it becomes a process which performs all the tasks mentioned in the program.
  • When a program is loaded into the memory and it becomes a process, it can be divided into four sections ─ stack, heap, text and data.
  • Stack: The process Stack contains the temporary data such as method/function parameters, return address and local variables.
  • Heap: This is dynamically allocated memory to a process during its run time.
  • Text: This includes the current activity represented by the value of Program Counter and the contents of the processor's registers.
  • Data: This section contains the global and static variables. Program
  • Start: This is the initial state when a process is first started/created.
  • Ready: The process is waiting to be assigned to a processor. Ready processes are waiting to have the processor allocated to them by the operating system so that they can run. Process may come into this state after Start state or while running it by but interrupted by the scheduler to assign CPU to some other process.
  • Running: Once the process has been assigned to a processor by the OS scheduler, the process state is set to running and the processor executes its instructions.
  • Waiting: Process moves into the waiting state if it needs to wait for a resource, such as waiting for user input, or waiting for a file to become available.
  • Terminated or Exit: Once the process finishes its execution, or it is terminated by the operating system, it is moved to the terminated state where it waits to be removed from main memory. Process Control Block (PCB)
  • A Process Control Block is a data structure maintained by the Operating System for every process.
  • The PCB is identified by an integer process ID (PID).
  • A PCB keeps all the information needed to keep track of a process as listed below:
  • Process State: The current state of the process i.e., whether it is ready, running, waiting, or whatever.
  • Process privileges: This is required to allow/disallow access to system resources.
  • Process ID :Unique identification for each of the process in the operating system.
  • Pointer: A pointer to parent process.
  • Program Counter: Program Counter is a pointer to the address of the next instruction to be executed for this process.
  • CPU registers: Various CPU registers where process need to be stored for execution for running state.
  • CPU Scheduling Information: Process priority and other scheduling information which is required to schedule the process.
  • Memory management information: This includes the information of page table, memory limits, Segment table depending on memory used by the operating system.
  • Accounting information: This includes the amount of CPU used for process execution, time limits, execution ID etc.
  • I/O status information: This includes a list of I/O devices allocated to the process.
  • The architecture of a PCB is completely dependent on Operating System and may contain different information in different operating systems. Here is a simplified diagram of a PCB − The PCB is maintained for a process throughout its lifetime, and is deleted once the process terminates. Context Switching
  • A context switch (also sometimes referred to as a process switch or a task switch ) is the switching of the CPU (central processing unit) from one process or thread to another.
  • A process (also sometimes referred to as a task) is an executing (i.e., running) instance of a program.
  • 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
  • The more complex the OS and the PCB ➔ the longer the context switch
  • Time dependent on hardware support
  • Some hardware provides multiple sets of registers per CPU ➔ multiple contexts loaded at once Dispatching
  • The dispatcher is the module that gives control of the CPU to the process selected by the short-time scheduler(selects from among the processes that are ready to execute).
  • The function involves : Switching context. Switching to user mode. Jumping to the proper location in the user program to restart that program.
  • The CPU-scheduling function is the Dispatcher, which is the module that gives control of the CPU to the process selected by the short-term scheduler. It receives control in kernel mode as the result of an interrupt or system call.

Hardware Interrupt Handling

  • Details are architecture dependent!
  • Interrupt controller signals CPU that interrupt has occurred, passes interrupt number
    • Interrupts are assigned priorities to handle simultaneous interrupts
    • Lower priority interrupts may be disabled during service
  • CPU senses (checks) interrupt request line after every instruction; if raised, then:
    • uses interrupt number to determine which handler to start
    • interrupt vector associates handlers with interrupts
  • Basic program state saved (as for system call)
  • CPU jumps to interrupt handler
  • When interrupt done, program state reloaded and program resumes Software Interrupt Handling
  • Typically two parts to interrupt handling
  • The part that has to be done immediately
  • So that device can continue working
  • The part that should be deferred for later
  • So that we can respond to the device faster
  • So that we have a more convenient execution context
  • What does that mean? Interprocess communication Interprocess communication is the mechanism provided by the operating system that allows processes to communicate with each other. This communication could involve a process letting another process know that some event has occurred or the transferring of data from one process to another. Synchronization in Interprocess Communication

Synchronization is a necessary part of interprocess communication. It is either provided by the interprocess control mechanism or handled by the communicating processes. Some of the methods to provide synchronization are as follows −

  • Semaphore A semaphore is a variable that controls the access to a common resource by multiple processes. The two types of semaphores are binary semaphores and counting semaphores.
  • Mutual Exclusion Mutual exclusion requires that only one process thread can enter the critical section at a time. This is useful for synchronization and also prevents race conditions. Thread Thread is a sequential flow of tasks within a process. Threads in OS can be of the same or different types. Threads are used to increase the performance of the applications. Each thread has its own program counter, stack, and set of registers. Multithreading is the ability of a program or an operating system to enable more than one user at a time without requiring multiple copies of the program running on the computer. Multithreading can also handle multiple requests from the same user. A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history. A thread shares with its peer threads few information like code segment, data segment and open files. When one thread alters a code segment memory item, all other threads see that. A thread is also called a lightweight process. Threads provide a way to improve application performance through parallelism. Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process. Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control. Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors. The following figure shows the working of a single-threaded and a multithreaded process.
  • It is more economical to create and context switch threads.
  • Threads allow utilization of multiprocessor architectures to a greater scale and efficiency. Disadvantages
  • In a typical operating system, most system calls are blocking.
  • Multithreaded application cannot take advantage of multiprocessing Thread Operation A thread refers to a single sequential flow of activities being executed in a process; it is also known as the thread of execution or the thread of control. Now, thread execution is possible within any OS's process. Apart from that, a process can have several threads. There are four basic thread management operations: thread creation, thread termination, thread join, and thread yield User Level Threads In this case, the thread management kernel is not aware of the existence of threads. The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. The application starts with a single thread. Advantages
  • Thread switching does not require Kernel mode privileges.
  • User level thread can run on any operating system.
  • Scheduling can be application specific in the user level thread.
  • User level threads are fast to create and manage. Disadvantages
  • In a typical operating system, most system calls are blocking.
  • Multithreaded application cannot take advantage of multiprocessing. Kernel Level Threads In this case, thread management is done by the Kernel. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process. The Kernel maintains context information for the process as a whole and for individuals threads within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management in Kernel space. Kernel threads are generally slower to create and manage than the user threads. Advantages
  • Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
  • If one thread in a process is blocked, the Kernel can schedule another thread of the same process.
  • Kernel routines themselves can be multithreaded. Disadvantages
  • Kernel threads are generally slower to create and manage than the user threads.
  • Transfer of control from one thread to another within the same process requires a mode switch to the Kernel. Multithreading Models Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. Multithreading models are three types
  • Many to many relationship.
  • Many to one relationship.
  • One to one relationship. Many to Many Model The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads. The following diagram shows the many-to-many threading model where 6 user level threads are multiplexing with 6 kernel level threads. In this model, developers can create as many user threads as necessary and the corresponding Kernel threads can run in parallel on a multiprocessor machine. This model provides the best accuracy on concurrency and when a thread performs a blocking system call, the kernel can schedule another thread for execution.

One to One Model There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more concurrency than the many-to-one model. It also allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on microprocessors. Disadvantage of this model is that creating user thread requires the corresponding Kernel thread. OS/2, windows NT and windows 2000 use one to one relationship model.

Difference between User-Level & Kernel-Level Thread S.N. User-Level Threads Kernel-Level Thread 1 User-level threads are faster to create and manage. Kernel-level threads are slower to create and manage. 2 Implementation is by a thread library at the user level. Operating system supports creation of Kernel threads. 3 User-level thread is generic and can run on any operating system. Kernel-level thread is specific to the operating system. 4 Multi-threaded applications cannot take advantage of multiprocessing. Kernel routines themselves can be multithreaded.

in to picture by removing process (It is doing the opposite work of Short Term Scheduler) So,the processor can run the processes smoothly. Schedulers Schedulers are special system software which handle process scheduling in various ways. Their main task is to select the jobs to be submitted into the system and to decide which process to run. Schedulers are of three types −

  • Long-Term Scheduler
  • Short-Term Scheduler
  • Medium-Term Scheduler Long Term Scheduler It is also called a job scheduler. A long-term scheduler determines which programs are admitted to the system for processing. It selects processes from the queue and loads them into memory for execution. Process loads into the memory for CPU scheduling. The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound and processor bound. It also controls the degree of multiprogramming. If the degree of multiprogramming is stable, then the average rate of process creation must be equal to the average departure rate of processes leaving the system. On some systems, the long-term scheduler may not be available or minimal. Time-sharing operating systems have no long term scheduler. When a process changes the state from new to ready, then there is use of long-term scheduler. Short Term Scheduler It is also called as CPU scheduler. Its main objective is to increase system performance in accordance with the chosen set of criteria. It is the change of ready state to running state of the process. CPU scheduler selects a process among the processes that are ready to execute and allocates CPU to one of them. Short-term schedulers, also known as dispatchers, make the decision of which process to execute next. Short-term schedulers are faster than long-term schedulers. Medium Term Scheduler Medium-term scheduling is a part of swapping. It removes the processes from the memory. It reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling the swapped out-processes. A running process may become suspended if it makes an I/O request. A suspended processes cannot make any progress towards completion. In this condition, to remove the process from memory and make space for other processes, the suspended process is moved to the secondary storage. This prsocess is called swapping , and the process is said to be swapped out or rolled out. Swapping may be necessary to improve the process mix. Comparison among Scheduler S.N. Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler 1 It is a job scheduler It is a CPU scheduler It is a process swapping scheduler. 2 Speed is lesser than short term scheduler Speed is fastest among other two Speed is in between both short and long term scheduler. 3 It controls the degree of multiprogramming It provides lesser control over degree of multiprogramming It reduces the degree of multiprogramming. 4 It is almost absent or minimal in time sharing system It is also minimal in time sharing system It is a part of Time sharing systems.

5 It selects processes from pool and loads them into memory for execution It selects those processes which are ready to execute It can re-introduce the process into memory and execution can be continued. A Process Scheduler schedules different processes to be assigned to the CPU based on particular scheduling algorithms. There are six popular process scheduling algorithms which we are going to discuss in this chapter −

  • First-Come, First-Served (FCFS) Scheduling
  • Shortest-Job-Next (SJN) Scheduling
  • Priority Scheduling
  • Shortest Remaining Time
  • Round Robin(RR) Scheduling
  • Multiple-Level Queues Scheduling These algorithms are either non-preemptive or preemptive. Non-preemptive algorithms are designed so that once a process enters the running state, it cannot be preempted until it completes its allotted time, whereas the preemptive scheduling is based on priority where a scheduler may preempt a low priority running process anytime when a high priority process enters into a ready state.

Wait time of each process is as follows − Process Wait Time : Service Time - Arrival Time P0 0 - 0 = 0 P1 5 - 1 = 4 P2 8 - 2 = 6 P3 16 - 3 = 13 Average Wait Time: (0+4+6+13) / 4 = 5. Shortest Job Next (SJN)

  • This is also known as shortest job first , or SJF
  • This is a non-preemptive, pre-emptive scheduling algorithm.
  • Best approach to minimize waiting time.
  • Easy to implement in Batch systems where required CPU time is known in advance.
  • Impossible to implement in interactive systems where required CPU time is not known.
  • The processer should know in advance how much time process will take.

Wait time of each process is as follows − Process Wait Time : Service Time - Arrival Time P0 3 - 0 = 3 P1 0 - 0 = 0 P2 16 - 2 = 14 P3 8 - 3 = 5 Average Wait Time: (3+0+14+5) / 4 = 5. Priority Based Scheduling

  • Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems.
  • Each process is assigned a priority. Process with highest priority is to be executed first and so on.
  • Processes with same priority are executed on first come first served basis.
  • Priority can be decided based on memory requirements, time requirements or any other resource requirement.