Operating Systems 24CSCI03I Lecture Notes PDF

Document Details

Uploaded by Deleted User

British University in Egypt

Wessam El-Behaidy

Tags

operating systems computer science processes computer architecture

Summary

These lecture notes provide an overview of Operating Systems, focusing on fundamental concepts like processes, their states, and scheduling algorithms. It explores context switching and the roles of different components within the system. This document's key topics are in computer science and will be useful to any Operating System learner.

Full Transcript

OPERATING SYSTEMS 24CSCI03I By Wessam El-Behaidy Associate Professor, Computer Science Main Reference Operating System Concepts, Abraham Silberschatz, 10th Edition Do you remember? MULTI-PROGRAMMING, MULTI-TASKING/ TIME...

OPERATING SYSTEMS 24CSCI03I By Wessam El-Behaidy Associate Professor, Computer Science Main Reference Operating System Concepts, Abraham Silberschatz, 10th Edition Do you remember? MULTI-PROGRAMMING, MULTI-TASKING/ TIMESHARING Multiprogramming ▪ The Idea is: The operating system keeps several processes in memory simultaneously. – The number of processes currently in memory is known as the degree of multiprogramming. The operating system picks and begins to execute one of these processes. Eventually, the process may have to wait for some task, such as an I/O operation, to complete. – In a multiprogrammed system, the operating system simply switches to, and executes, another process. Advantage: – To increase CPU utilization – Also to keep users satisfied Multitasking or Timesharing ▪ Multitasking/ timesharing is a logical extension of multiprogramming. ▪ In multitasking systems, the CPU executes multiple processes by switching among them after a timeslice, but the switches occur frequently, providing the user with a fast response time. Process Process Concept ▪ Process is a program in execution. Program is passive entity stored on disk (executable file); process is active Program becomes process when an executable file is loaded into memory Program Process Process Concept ▪ One program can have several processes The same user may invoke many copies of an application. Each of these is a separate process. Although the text sections are equivalent, the data, heap, and stack sections vary. ▪ A process can itself be an execution environment for other code, as Java virtual machine Process in Memory Process in Memory ▪ The executable code - text section ▪ Data section - global variables Process in Memory ▪ Stack - temporary data ▪ Function parameters, return addresses, local variables Process in Memory ▪ Heap - memory dynamically allocated during run time Process in Memory ▪ Text and data sections have fixed size Process in Memory ▪ The stack and heap sections can shrink and grow dynamically during program execution - never overlap Process State ▪ As a process executes, it changes state New: The process is being created Ready: The process is waiting to be assigned to a processor Running: Instructions are being executed Process State ▪ As a process executes, it changes state Waiting: The process is waiting for some event to occur Terminated: The process has finished execution Process State ▪ Only one process can be running on any processor core at any instant. ▪ Many processes may be ready and waiting Process Control Block (PCB) Each process is represented in the operating system by a data structure called PCB (also called task control block) ▪ Process state – running, waiting, etc. ▪ Process number (PID) ▪ Program counter – location of instruction to next execute ▪ CPU registers  Alongwith the program counter, this state information must be saved when an interrupt occurs ▪ CPU scheduling information- priorities, scheduling queue pointers the PCB simply serves as the repository for all the data ▪... needed to start, or restart, a process Process Scheduling ▪ CPU scheduler selects among processes that are in the ready queue and allocate a CPU core to one of them. ▪ Goal: Maximize CPU use Quickly switch processes onto CPU core ▪ It maintains scheduling queues of processes Ready queue – set of all processes residing in main memory, ready and waiting to execute Wait queue – set of processes waiting for an event (i.e., I/O) Ready and Wait Queues Process Scheduling ▪ A new process is initially put in the ready queue. It Waits until it is selected for execution, or dispatched. Process Scheduling ▪ Once the process is allocated a CPU core and is executing, one of several events could causing processes migration among the various queues Process Scheduling ▪ A process continues this cycle until it terminates, It is removed from all queues Its PCB and resources are deallocated. Context Switch ▪ Switching the CPU core to another process is known context switch ▪ Context of a process represented in the PCB ▪ 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. ▪ A dispatcher is responsible for the context switching. Context Switch Context Switch ▪ Context-switch time is pure overhead; the system does no useful work while switching The more complex the OS and the PCB ➔ the longer the context switch Also speed depends on memory speed, the number of registers that must be copied,… ▪ Switching time dependent on hardware support Operations on Processes ▪ Process creation ▪ Process termination Process Creation ▪ During execution, a process may create several new processes through appropriate system calls, such as fork (). Parent process create children processes, which, in turn create other processes, forming a tree of processes. A parent process may have multiple child processes but a child process only one parent process. ▪ Execution options Parent and children execute concurrently Parent waits until children terminate EX: A Tree of Processes in Linux ▪ The systemd process (which always has a pid of 1) serves as the root parent process for all user processes and is the first user process created when the system boots. ▪ Once the system has booted, the systemd process creates processes which provide additional services EX: A Tree of Processes in Linux ▪ The logind process is responsible for managing clients that directly log onto the system. In this example, a client has logged on and is using the bash shell, which has been assigned pid 8416. Using the bash command-line interface, this user has created the process ps as well as the vim editor. Process Creation ▪ The child process will need certain resources (CPU time, memory, files, …) to accomplish its task. It may be able to obtain its resources directly from the operating system, Or it may be constrained to a subset of the resources of the parent process. The parent may have to partition its resources among its children, or it may be able to share some resources (such as memory or files) among several of its children. Restrictinga child process to a subset of the parent’s resources prevents any process from overloading the system by creating too many child processes. Process Termination ▪ Events which cause process termination: Normal exit (voluntary). ▪Process terminates its work; it asks the OS to delete it using the exit() system call Error exit (voluntary). ▪The process discovers an error and exits voluntary. For example, if a user types the command cc file.c ▪to compile the program file.c and no such file exists, the compiler simply announces this fact and exits. Fatal error (involuntary). ▪Due to an error caused by the process, often due to a program bug. Examples include executing an illegal instruction, referencing nonexistent memory, or dividing by zero. Killed by another process (involuntary). ▪A process executes a system call telling the operating system to kill some other process. In UNIX this call is kill. The corresponding Win32 function is TerminateProcess(). Process Termination ▪ Parent may terminate the execution of its children. Some reasons for doing so: Child has exceeded allocated resources Task assigned to child is no longer required ▪ Some operating systems do not allow child to exist if its parent has terminated. If a process terminates, then all its children must also be terminated (cascading termination). ▪ Process’resources are deallocated by operating system, when it is terminated. Threads Threads - Overview ▪ Previously, a process was an executing program with a single thread. ▪ However, modern OSs, provide features enabling a process to contain multiple threads of control. Most modern applications are multithreaded Threads run within application ▪ A process can have multiple threads of control, ▪ To perform more than one task at a time. Examples Of Multithreaded Applications ▪ An application that creates photo thumbnails from a collection of images may use a separate thread to generate a thumbnail from each separate image. ▪ A web browser might have one thread display images or text while another thread retrieves data from the network. ▪ A word processor may have: a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background. Threads - Overview ▪ Threads are lightweight processes as: They have their own ID, PC, a register set, and stack But they share with other threads in the same process code, data, heap sections, and other OS resources, such as open files, permissions… Program counter Threads - Overview ▪ Each thread belongs to exactly one process and no thread can exist outside a process. ▪ A thread in process P cannot reference a thread in process Q. Threads - Overview ▪ Because threads share the same address space, The operational cost of communication between the threads is low, which is an advantage. The disadvantage is that a problem with one thread in a process will certainly affect other threads and the viability of the process itself. ▪ Applications can be designed to utilize processing capabilities on multicore systems. can perform several CPU-intensive tasks in parallel across the multiple computing cores. Thank You

Use Quizgecko on...
Browser
Browser