Process Management, Exercises of Operating Systems

The operating system is responsible for the following activities in connection with process and thread management: the creation and deletion of both user and ...

Typology: Exercises

2021/2022

Uploaded on 09/27/2022

leyllin
leyllin 🇬🇧

4.3

(15)

241 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Operating Systems I- Lecture Lecturer: Dr. Sura Z. Alrashid
1
Process Management
A process can be thought of as a program in execution. A process will need certain
resources such as CPU time, memory, files, and I/O devices to accomplish its task.
These resources are allocated to the process either when it is created or while it is executing.
A process is the unit of work in most systems. Systems consist of a collection of processes:
Operating-system processes execute system code, and user processes execute user code.
All these processes may execute concurrently(parallel).
Although traditionally a process contained only a single thread of control as it ran, most
modern operating systems now support processes that have multiple threads.
The operating system is responsible for the following activities in connection with process
and thread management: the creation and deletion of both user and system processes; the
scheduling of processes; and the provision of mechanisms for synchronization,
communication, and deadlock handling for processes.
Table of Contents
1!PROCESSES( 2!
1.1!PROCESS!CONCEPT! 2!
1.1.1!The&Process& 3!
1.1.2!Process&State& 4!
1.1.3!Process&Control&Block& 5!
1.1.4
!Threads&
& 6!
1.2!PROCESS!SCHEDULING! 7!
1.2.1!Scheduling&Queues& 7!
1.2.2!Schedulers& 9!
1.2.3!Context&Switch&10!
1.3!OPERATIONS!ON!PROCESSES!10!
1.3.1!Process&Creation&10!
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Process Management and more Exercises Operating Systems in PDF only on Docsity!

Process Management

A process can be thought of as a program in execution. A process will need certain resources — such as CPU time, memory, files, and I/O devices —to accomplish its task. These resources are allocated to the process either when it is created or while it is executing. A process is the unit of work in most systems. Systems consist of a collection of processes: Operating-system processes execute system code, and user processes execute user code. All these processes may execute concurrently(parallel). Although traditionally a process contained only a single thread of control as it ran, most modern operating systems now support processes that have multiple threads. The operating system is responsible for the following activities in connection with process and thread management: the creation and deletion of both user and system processes; the scheduling of processes; and the provision of mechanisms for synchronization, communication, and deadlock handling for processes.

Table of Contents

1 PROCESSES 2 1.1 PROCESS CONCEPT 2 1.1.1 The Process 3 1.1.2 Process State 4 1.1.3 Process Control Block 5 1.1.4 Threads 6 1.2 PROCESS SCHEDULING 7 1.2.1 Scheduling Queues 7 1.2.2 Schedulers 9 1.2.3 Context Switch 10 1.3 OPERATIONS ON PROCESSES 10 1.3.1 Process Creation 10

1.4 SUMMARY 11 1 Processes Early computer systems allowed only one program to be executed at a time. This program had complete control of the system and had access to all the system’s resources. In contrast, current-day computer systems allow multiple programs to be loaded into memory and executed concurrently. This evolution required firmer control and more compartmentalization of the various programs, and these needs resulted in the notion of a process , which is a program in execution. A process is the unit of work in a modern time- sharing system. A system consists of a collection of processes: operating- system processes executing system code and user processes executing user code. Potentially, all these processes can execute concurrently, with the CPU (or CPUs) multiplexed among them. By switching the CPU between processes, the operating system can make the computer more productive. CHAPTER OBJECTIVES

  • To introduce the notion of a process—a program in execution that forms the basis of all computation.
  • To describe the various features of processes, including scheduling, creation and termination, and communication.
  • To describe communication in client–server systems.

1.1 Process Concept

A question that arises in discussing operating systems involves what to call all the CPU activities. A batch system executes jobs, whereas a time-shared system has user programs, or tasks. Even on a single-user system such as the original Microsoft Windows, a user may

Operating Systems I- Lecture Lecturer: Dr. Sura Z. Alrashid 4 Figure 3.1 Process in memory. Figure 3.2 Diagram of process state. Although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences. For instance, several users may be running different copies of the mail program, or the same user may invoke many copies of the Web browser program. Each of these is a separate process, and although the text sections are equivalent, the data, heap, and stack sections vary. 1.1.2 Process State As a process executes, it changes state. The state of a process is defined in part by the current activity of that process. Each process may be in one of the following states: New. The process is being created. Running. Instructions are being executed. entity, such as a file containing a list of instructions stored on disk (often called an executable file ), whereas a process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources. A program becomes a process when an executable file is loaded into memory. Two common techniques for loading executable files are double-clicking an icon representing the executable file and entering the name of the executable file on the command line (as in prog.exe or a.out). text 0 max data heap stack Figure 3.1 Process in memory. 3.1 Process Concept 105 new terminated ready running admitted interrupt I/O or event completion scheduler dispatch I/O or event wait exit waiting Figure 3.2 Diagram of process state. Although two processes may be associated with the same program, they are nevertheless considered two separate execution sequences. For instance, several users may be running different copies of the mail program, or the same user may invoke many copies of the Web browser program. Each of these is a separate process, and although the text sections are equivalent, the data, heap, and stack sections vary. It is also common to have a process that spawns many processes as it runs. We discuss such matters in Section 3.4. 3.1.2 Process State As a process executes, it changes state. The state of a process is defined in part by the current activity of that process. Each process may be in one of the following states:

  • New. The process is being created.
  • Running. Instructions are being executed.
  • Waiting. The process is waiting for some event to occur (such as an^ I/O completion or reception of a signal).
  • Ready. The process is waiting to be assigned to a processor.
  • Terminated. The process has finished execution. These names are arbitrary, and they vary across operating systems. The states that they represent are found on all systems, however. Certain operating systems also delineate process states more finely. It is important to realize that only one process can be running on any processor at any instant. Many processes may be ready and waiting, however. The state diagram corresponding to these states is presented in Figure 3.2.

Waiting. The process is waiting for some event to occur (such as an I/O completion or reception of a signal). Ready. The process is waiting to be assigned to a processor. Terminated. The process has finished execution. The state diagram corresponding to these states is presented in Figure 3.2. 1.1.3 Process Control Block Each process is represented in the operating system by a process control block ( PCB )— also called a task control block. A PCB is shown in Figure 3.3. It contains many pieces of information associated with a specific process, including these: Figure 3.3 Process control block (PCB). Process state. The state may be new, ready, running, waiting, halted, and so on. Program counter. The counter indicates the address of the next instruction to be executed for this process. CPU registers. The registers vary in number and type, depending on the computer architecture. They include accumulators, index registers, stack pointers, and general-purpose registers, plus any condition- code information. Along with the program counter, this state information must be saved when an interrupt occurs, to allow the process to be continued correctly afterward (Figure 3.4). CPU-scheduling information. This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. 106 Chapter 3 Processes process state process number program counter memory limits list of open files registers

- • • Figure 3.3 Process control block (PCB).

  • Process state. The state may be new, ready, running, waiting, halted, and so on.
  • Program counter. The counter indicates the address of the next instruction to be executed for this process.
  • CPU^ registers. The registers vary in number and type, depending on the computer architecture. They include accumulators, index registers, stack pointers, and general-purpose registers, plus any condition-code information. Along with the program counter, this state information must be saved when an interrupt occurs, to allow the process to be continued correctly afterward (Figure 3.4).
  • CPU-scheduling information. This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. (Chapter 5 describes process scheduling.)
  • 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, depending on the memory system used by the operating system (Chapter 8).
  • Accounting information. This information includes the amount of^ CPU

Figure 3.4 CPU switch from process to process.

1.2 Process Scheduling

The objective of multiprogramming is to have some process running at all times, to maximize CPU utilization. The objective of time sharing is to switch the CPU among processes so frequently that users can interact with each program while it is running. To meet these objectives, the process scheduler selects an available process (possibly from a set of several available processes) for program execution on the CPU. For a single- processor system, there will never be more than one running process. If there are more processes, the rest will have to wait until the CPU is free and can be rescheduled. 1.2.1 Scheduling Queues As processes enter the system, they are put into a job queue that consists of all processes in the system. The processes that are residing in main memory and are ready and waiting to execute are kept on a list called the ready queue. This queue is generally stored as a linked list.

Figure 3.6 The ready queue and various I/O device queues. The system also includes other queues. When a process is allocated the CPU, it executes for a while and eventually quits, is interrupted, or waits for the occurrence of a particular event, such as the completion of an I/O request. Suppose the process makes an I/O request to a shared device, such as a disk. Since there are many processes in the system, the disk may be busy with the I/O request of some other process. The process therefore may have to wait for the disk. The list of processes waiting for a particular I/O device is called a device queue. Each device has its own device queue (Figure 3.6). A common representation of process scheduling is a queueing diagram , such as that in Figure 3.7. Each rectangular box represents a queue. Two types of queues are present: the ready queue and a set of device queues. The circles represent the resources that serve the queues, and the arrows indicate the flow of processes in the system. A new process is initially put in the ready queue. It waits there until it is selected for 3.2 Process Scheduling 109 queue header PCB 7 PCB 3 PCB 5 PCB 14 PCB 6 PCB 2 head head head head head ready queue disk unit 0 terminal unit 0 mag tape unit 0 mag tape unit 1 tail registers registers tail tail tail tail

**-

-** - - - - - - Figure 3.6 The ready queue and various I/O device queues. The system also includes other queues. When a process is allocated the CPU, it executes for a while and eventually quits, is interrupted, or waits for the occurrence of a particular event, such as the completion of an I/O request. Suppose the process makes an I/O request to a shared device, such as a disk. Since there are many processes in the system, the disk may be busy with the I/O request of some other process. The process therefore may have to wait for the disk. The list of processes waiting for a particular I/O device is called a device queue. Each device has its own device queue (Figure 3.6). A common representation of process scheduling is a queueing diagram , such as that in Figure 3.7. Each rectangular box represents a queue. Two types of queues are present: the ready queue and a set of device queues. The circles represent the resources that serve the queues, and the arrows indicate the flow of processes in the system. A new process is initially put in the ready queue. It waits there until it is selected for execution, or is dispatched. Once the process is allocated the CPU and is executing, one of several events could occur:

  • The process could issue an I/O request and then be placed in an I/O queue.
  • The process could create a new subprocess and wait for the subprocess’s termination.
  • The process could be removed forcibly from the^ CPU, as a result of an interrupt, and be put back in the ready queue.

1.2.3 Context Switch The interrupts cause the operating system to change a CPU from its current task and to run a kernel routine. Such operations happen frequently on general-purpose systems. When an interrupt occurs, the system needs to save the current context of the process running on the CPU so that it can restore that context when its processing is done, essentially suspending the process and then resuming it. The context is represented in the PCB of the process; it includes the value of the CPU registers, the process state (see Figure 3.2), and memory- management information. Generically, we perform a state save of the current state of the CPU, be it in kernel or user mode, and then a state restore to resume operations. Switching the CPU to another process requires performing a state save of the current process and a state restore of a different process. This task is known as a context switch. When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run. Context-switch time is pure overhead, because the system does no useful work while switching. Context-switching speed varies from machine to machine, depending on the memory speed, the number of registers that must be copied, and the existence of special instructions (such as a single instruction to load or store all registers). Typical speeds are a few milliseconds.

1.3 Operations on Processes

1.3.1 Process Creation A process may create several new processes, via a create-process system call, during the course of execution. The creating process is called a parent process, and the new processes are called the children of that process. Each of these new processes may in turn create other processes, forming a tree of processes. Most operating systems (including UNIX and the Windows family of operating systems)

identify processes according to a unique process identifier (or pid ), which is typically an integer number.

1.4 Summary

A process is a program in execution. As a process executes, it changes state. The state of a process is defined by that process’s current activity. Each process may be in one of the following states: new, ready, running, waiting, or terminated. Each process is represented in the operating system by its own process control block (PCB). A process, when it is not executing, is placed in some waiting queue. There are two major classes of queues in an operating system: I/O request queues and the ready queue. The ready queue contains all the processes that are ready to execute and are waiting for the CPU. Each process is represented by a PCB, and the PCBs can be linked together to form a ready queue. Long-term (job) scheduling is the selection of processes that will be allowed to contend for the CPU. Normally, long-term scheduling is heavily influenced by resource- allocation considerations, especially memory management. Short-term (CPU) scheduling is the selection of one process from the ready queue. Operating systems must provide a mechanism for parent processes to create new child processes. The parent may wait for its children to terminate before proceeding, or the parent and children may execute concurrently. There are several reasons for allowing concurrent execution: information sharing, computation speedup, modularity, and convenience. The processes executing in the operating system may be either independent processes or cooperating processes. Cooperating processes require an interpro- cess communication mechanism to communicate with each other. Principally, communication is achieved through two schemes: shared memory and mes- sage passing. The shared-memory method requires communicating processes to share some variables. The processes are expected to exchange information through the use of these shared variables. In a shared-memory