Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Operating System Principles AUA CS331 Albert Minasyan Process Management Process Concept o Process Table. Process Identifier. Process Tree. o The process. Process Control Block. Process Context. Process Scheduling o Process State. Scheduling Queues o Parallel and Concurrent processing. Context Switch Textbook Silberschatz, Chapter 4 Process Concept A process is a program in execution. A process is more than the program code, which is sometimes known as the text section. It also includes the current activity, as represented by the value of the program counter and the contents of the processor's registers. In addition, a process generally includes the process stack, which contains temporary data (such as method parameters, return addresses, and local variables), and a data section, which contains global variables. A process is a program in execution. a program is a passive entity process is an active entity Process consists of: Program binary code Data Section (global variables) Process Stack o function parameters o return addresses o local variables Registers content Open I/O Files o Descriptors (not the content) Current activity information (program counter, status, signals) Above information forms the Process Context CPU First Process One program becomes to two processes Second Process editor.exe Several processes share the computer simultaneously. Reasons: o Better utilization o Convenience 1 Operating System Principles AUA CS331 Albert Minasyan We emphasize that a program by itself is not a process; a program is a passive entity, such as the contents of a file stored on disk, whereas a process is an active entity, with a program counter specifying the next instruction to execute and a set of associated resources. 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 editor program. Each of these is a separate process, and, although the text sections are equivalent, the data sections vary. 2 Operating System Principles AUA CS331 Albert Minasyan Process Table. Process Identifier. Process Tree. The operating system is responsible for managing all the processes that are running on a computer and allocates each process a certain amount of time to use the processor. In addition, the operating system also allocates various other resources that processes will need such as computer memory or disks. To keep track of the state of all the processes, the operating system maintains a table known as the process table. Inside this table, every process is listed along with the resources the process is using and the current state of the process. During the course of execution, a process may create several new processes. 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, Linux, and Windows) identify processes according to a unique process identifier (or pid), which is typically an integer number. The pid provides a unique value for each process in the system, and it can be used as an index to access various attributes of a process within the kernel. Figure 3.8 illustrates a typical process tree for the Linux operating system, showing the name of each process and its pid. (We use the term process rather loosely, as Linux prefers the term task instead.) The init process (which always has a pid of 1) serves as the root parent process for all user processes. Once the system has booted, the init process can also create various user processes, such as a web or print server, an ssh server, and the like. In Figure 3.8, we see two children of init— kthreadd and sshd. The kthreadd process is responsible for creating additional processes that perform tasks on behalf of the kernel (in this situation, khelper and pdflush). The sshd process is responsible for managing clients that connect to the system by using ssh (which is short for secure shell). The login 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 emacs editor. Parent process C h i l d r e n Parent C h i l d r e n Figure 3.8 A tree of processes on a typical Linux system. On UNIX and Linux systems, we can obtain a listing of processes by using the ps command. For example, the command ps -el 3 Operating System Principles AUA CS331 Albert Minasyan will list complete information for all processes currently active in the system. It is easy to construct a process tree similar to the one shown in Figure 3.8 by recursively tracing parent processes all the way to the init process. ps –ef UID PID PPID C Process Status (Linux) or ps –ef | less - The Effective User Identifier of process. - The process identification number - PID of the parent process. - CPU usage (in percents). STIME - The start time of task TTY - The terminal on which the process originated. TIME - Cumulative CPU time the process used, in minutes and seconds (usually in HH.MM.SS format) CMD - The name of the command being executed UID PID PPID C STIME TTY TIME CMD root 1 0 0 Feb11 ? 00:00:04 init [3] root 2 1 0 Feb11 ? 00:00:00 [keventd] root 3 1 0 Feb11 ? 00:00:00 [kapm-idled] root 4 0 0 Feb11 ? 00:00:00 [ksoftirqd_CPU0] root 5 0 0 Feb11 ? 00:00:00 [kswapd] root 6 0 0 Feb11 ? 00:00:00 [kreclaimd] root 7 0 0 Feb11 ? 00:00:00 [bdflush] root 8 0 0 Feb11 ? 00:00:00 [kupdated] root 9 1 0 Feb11 ? 00:00:00 [mdrecoveryd] root 13 1 0 Feb11 ? 00:00:00 [kjournald] root 592 1 0 Feb11 ? 00:00:00 syslogd -m 0 root 597 1 0 Feb11 ? 00:00:00 klogd -2 root 686 1 0 Feb11 ? 00:00:00 xinetd -stayalive -reuse root 704 1 0 Feb11 ? 00:00:00 crond root 711 1 0 Feb11 tty1 00:00:00 login -- root root 713 1 0 Feb11 tty3 00:00:00 login -- stud2 root 714 1 0 Feb11 tty4 00:00:00 /sbin/mingetty tty4 root 715 1 0 Feb11 tty5 00:00:00 /sbin/mingetty tty5 root 716 1 0 Feb11 tty6 00:00:00 /sbin/mingetty tty6 root 719 711 0 Feb11 tty1 00:00:00 -bash root 805 1 0 Feb11 ? 00:00:04 sshd root 1189 1 0 Feb11 tty2 00:00:00 login -- stud1 stud1 1197 1189 0 Feb11 tty2 00:00:00 -bash stud2 1277 713 0 Feb11 tty3 00:00:00 -bash stud2 1319 1277 0 Feb11 tty3 00:00:00 /usr/bin/mc -P stud2 1321 1319 0 Feb11 pts/4 00:00:00 bash -rcfile .bashrc root 2384 805 0 10:00 ? 00:00:02 sshd studnt 2385 2384 0 10:00 pts/0 00:00:00 -bash root 2422 2385 0 10:00 pts/0 00:00:00 su root 2423 2422 0 10:00 pts/0 00:00:00 bash root 2468 805 0 10:10 ? 00:00:10 sshd PID - The2469 process identification number.00:00:00 Every process invoked by kernel gets studnt 2468 0 10:10 pts/1 -bash identification number beginning from 0. We see 2 PIDsps– –ef 2469 and 2879. root 3557 2423 0 21:22 pts/0 00:00:00 unique TTY - The terminal on which the process originated. bash and ps processes are originated from the local tty1 terminal. TIME - Cumulative CPU time the process used, in minutes and seconds (usually in HH.MM.SS format) CMD - The name of the command being executed (bash and ps) UID - The Effective User Identifier of process. This defines the privileges of process while it is running. This EUID usually is the same as the user ID created the process. Only if the command has SetUID then the process runs with the UID of the owner of command. The processes with PID 1,2,3, 2384 are run now with superuser root privileges. The process with PID 1197 have the rights of stud1 user. The processes with PID 1277,1319,1321 have the privileges of user stud2. There are also GID for processes which are similar to UID but concern to group privileges of processes. PPID - is the PID of the parent process which created this new process with the current PID. 4 Operating System Principles AUA CS331 Albert Minasyan The init process created [keventd], [kapm-idled] and other processes. The 1319 Midnight Commander process is launched by user stud2 from the 1277 bash shell. This one is launched by 713 process which is launched by kernel on tty3 and allowed login stud2 to the tty3 terminal. All processes are created by init daemon which is the first and main program running on Linux after initialization. C - CPU usage (in percents). STIME - The starting time of task Look at the Process Tree (Linux). 1. Login as studnt on the 1st virtual terminal. 2. Run the mc command mc 3. Call some manual page inside MC. man ls 4. Run pstree tool to look at the process tree. pstree [studnt@linuxhost studnt]$ pstree init-+-crond |-kapm-idled |-keventd |-kjournald |-klogd |-login---bash---su---bash |-2*[login---bash] |-mdrecoveryd |-3*[mingetty] |-sshd---sshd-+-bash---mc---bash---man---sh---sh-+-less | | `-nroff---groff---grotty | `-bash---pstree `-syslogd 4. Another form of process tree. ps -ejH 5 4 1 2 3 9 13 458 463 574 26970 26971 27081 27171 27208 27209 27454 594 1 1 0 1 1 1 1 458 463 574 574 26971 27081 27171 27208 27209 27454 594 1 1 0 1 1 1 1 458 463 574 574 26971 27081 27171 27171 27171 27171 594 ? ? ? ? ? ? ? ? ? ? ? pts/0 ? pts/1 pts/1 pts/1 pts/1 ? 00:00:00 kswapd 00:00:00 ksoftirqd_CPU0 00:00:04 init 00:00:00 keventd 00:00:00 kapm-idled 00:00:00 mdrecoveryd 00:00:02 kjournald 00:00:00 syslogd 00:00:00 klogd 00:00:00 sshd 00:00:01 sshd 00:00:00 bash 00:00:00 sftp-server 00:00:00 bash 00:00:00 su 00:00:00 bash 00:00:00 ps 00:00:00 crond 5 Operating System Principles AUA CS331 Albert Minasyan Process Control Block The operating system is responsible for managing all the processes that are running on a computer and allocates each process a certain amount of time to use the processor. In addition, the operating system also allocates various other resources that processes will need such as computer memory or disks. To keep track of the state of all the processes, the operating system maintains a table known as the process table. Inside this table, every process is listed along with the resources the process is using and the current state of the process. These table entries are called Process Control Blocks (PCB). What is PCB ? Are there any other similar structures in OS ? The PCB simply serves as the repository for any information that may vary from process to process to identify and manage the processes PCB is the repository of process. o Everything about the processes in the system is kept in PCBs o PCB is a container of the Process Context If the OS kernel supports threads then the system could manage also TCBs. The whole PCB collection is called Process Table. Each process is represented in the operating system by a process control block (PCB)-also called a task control block. PCB contains many pieces of information associated with a specific process, including these shown below. PCB Pointer to the next PCB i.e. pointer to the PCB of the next process to run. Process state: The state may be new, ready, running, waiting, halted, and so on. Process number: The identifier of the process (PID). 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. Memory-management information: The memory translation map. This information may include 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. CPU-scheduling information: This information includes a process priority, pointers to scheduling queues, and any other scheduling parameters. Process accounting information, such as when the process was last run, how much CPU and real time it has accumulated, time limits. I/O Information (i.e. I/O devices allocated to this process, list of opened files, etc) 6 Operating System Principles AUA CS331 Albert Minasyan Process Scheduling The responsibility of determining how to allocate processor time among all the ready processes is known as scheduling (CPU time scheduling). The objective of multiprogramming is to have some process running at all times, so as 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. A uniprocessor system can have only one running process. If more processes exist, the rest must wait until the CPU is free and can be rescheduled. Process State Processes can be in one of three main states: running, ready, or waiting. The running state means that the process has all the resources it needs for execution and it has been given permission by the operating system to use the processor. Only one process can be in the running state at any given time. The remaining processes are either in a waiting state (i.e., waiting for some external event to occur such as user input or a disk access) or a ready state (i.e., waiting for permission to use the processor). In a real operating system, the waiting and ready states are implemented as queues which hold the processes in these states. The state names are arbitrary, and they vary across operating systems. 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. Process State Diagram Dispatch Choose a task to run on CPU Queues Interrupt the running task After I/O request (Blocked) http://courses.cs.vt.edu/~csonline/OS/Lessons/Processes/index.html The OS itself is a process and while the other process is running the OS should wait its order and take control by interrupt or call. This is why if something is wrong in the system and there are no proper system calls or interrupts to 7 pass the control to OS then the system “hangs”. Operating System Principles AUA CS331 Albert Minasyan ps -aux | more USER root root root root root root studnt studnt studnt studnt studnt root PID %CPU %MEM 1 0.0 0.8 2 0.0 0.0 4 0.0 0.0 9 0.0 0.0 13 0.0 0.0 592 0.0 0.9 2469 0.0 2.1 3606 0.0 1.2 3609 0.0 1.4 3610 0.0 1.5 3614 0.0 1.6 3734 0.0 1.3 VSZ RSS TTY 1412 520 ? 0 0 ? 0 0 ? 0 0 ? 0 0 ? 1472 576 ? 2468 1312 pts/1 1872 788 pts/1 2208 924 pts/1 2216 964 pts/1 2228 1016 pts/1 2788 832 pts/0 STAT S SW SWN SW< SW S S S S S S R START Feb11 Feb11 Feb11 Feb11 Feb11 Feb11 10:10 21:42 21:42 21:42 21:42 22:02 TIME 0:04 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 0:00 COMMAND init [3] [keventd] [ksoftirqd_CPU0] [mdrecoveryd] [kjournald] syslogd -m 0 -bash man top sh -c (cd /usr/share/man sh -c (cd /usr/share/man /bin/sh /usr/bin/nroff–m ps -aux The new fields appeared in the result of ps –aux command have the following meanings: %CPU The task's share of the CPU time expressed as a percentage of total CPU time. We see there are not too many processes using much CPU time because our Linux computers are not loaded servers. %MEM The task's share of the physical memory. The studnt’s bash takes 2.1% of whole memory (more than other processes) VSZ RSS The total amount of virtual memory used by the task in kilobytes. The total amount of physical memory used by the task in kilobytes. The studnt’s bash takes 2468 kilobytes of virtual memory 1312 kilobytes of which Is resided in RAM (more than other processes). START STAT is the same as STIME - start time of task. The process state. PROCESS STATE CODES D - uninterruptible (unkillable) sleep (usually I/O processes) – waiting queue R - runnable - these processes now are working – ready queue S - sleeping - these processes are not running but they are active and wait to serve requests –waiting queue T - traced or stopped Z - a defunct ("zombie") process - this process tried to die, partially is dead but its name exists in process list For BSD formats and when the "stat" keyword is used, additional letters may be displayed: W - has no resident pages < - high-priority process N - low-priority task L - has pages locked into memory (for real-time and custom I/O to be very fast) As we see almost all processes are sleeping (S). Only our ps is running (R) at the time we view the processes. Some of them haven’t resident pages in RAM (W). This is the result of captured 0 size memory by these processes. Just now they don’t use the memory. 8 Operating System Principles AUA CS331 Albert Minasyan Scheduling Queues (Queues which contain the scheduled tasks) As processes enter the system, they are put into a job queue. This queue 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. A ready-queue header contains pointers to the first and final PCBs in the list. We extend each PCB to include a pointer field that points to the next PCB in the ready queue. The operating system also has 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. In the case of an I/O request, such a request may be to a dedicated tape drive, or to a shared device, such as a disk. Since the system has many processes, 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. Processes Waiting to run on CPU Processes waiting to write/read on HDD I/O Two types of queues are present: the ready queue and a waiting queue. The waiting queue could be represented as a set of device queues. 9 Operating System Principles AUA CS331 Albert Minasyan Another view how the processes are put on different queues. HDD Queue Flush Disk Queue Device Queues Printer Queue On below picture 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 in the ready queue until it is selected for execution (or dispatched). Once the process is assigned to 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 its termination. The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue. In the first two cases, the process eventually switches from the waiting state to the ready state, and is then put back in the ready queue. A process continues this cycle until it terminates, at which time it is removed from all queues and has its PCB and resources deallocated. 10 Operating System Principles AUA CS331 Albert Minasyan a. Explanation of the changes in the queues or states when the “I/O request branch” is activated? Who requests I/O? What happens with the process until it reaches to ready queue? How long it takes? The process itself issues the request. Process is put by OS into waiting queue as long as the I/O is being done. After I/O finishes the process is put into ready queue. When the Process issues the I/O request to read some data from the disk then you put it to the waiting queue (blocking I/O call). When the data comes back then you put the PCB to the ready queue. b. “Time Slice Expired” – In time sharing system the process is removed from the CPU after predefined period. Explanation of the changes in the queues or states when the “fork a child” part of the scheme is activated? Who issues fork? What happened the next? What happened eventually (the final result)? The process in CPU issues request to create a child – fork(). OS removes the process from CPU and puts it into the ready queue. Child is created and also put into the ready queue. 2 processes the newly created child and the parent are put into the ready queue. (In case if the call is non blocking ). If the call is blocking then the parent is put into the waiting queue. c. d. Explanation of the branch “wait for an interrupt”. All other types of interrupts except of mentioned 3 branches can cause the process to leave the CPU by interrupt. Particularly if another process finishes I/O then OS should interrupt the current process in CPU to move the process from waiting queue to ready queue. 11 Operating System Principles AUA CS331 Albert Minasyan Context Switch Switching the CPU to another process requires saving the state of the old process and loading the saved state for the new process. This task is known as a context switch. 3 different processes (P0, P1 and the OS itself) The context of a process is represented in the PCB of a process; it includes the value of the CPU registers, the process state, and memory-management information. 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. Its 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 range from 1 to 1000 microseconds. Context-switch times are highly dependent on hardware support. For instance, some processors (such as the Sun Ultra SPARC) provide multiple sets of registers. A context switch simply includes changing the pointer to the current register set. Of course, if active processes exceed register sets, the system resorts to copying register data to and from memory, as before. Also, the more complex the operating system, the more work must be done during a context switch. As we will see, advanced memory-management techniques may require extra data to be switched with each context. For instance, the address space of the current process must be preserved as the space of the next task is prepared for use. How the address space is preserved, and what amount of work is needed to preserve it, depend on the memory-management method of the operating system. As we will see, context switching has become such a performance bottleneck that programmers are using new structures (threads) to avoid it whenever possible. 12 Operating System Principles AUA CS331 Albert Minasyan Concurrent and parallel processes. What is the difference between the concurrency and the parallelism? Parallelism is when the processes run simultaneously. Concurrent processes could run simultaneously or interleaving. What is the main problem of concurrency and how it’s resolved ? o The hardware is single. Multiplex the processes in time on the same hardware is the solution to make illusion of multiple CPUs each of which is dedicated for the appropriate process. How it’s done that each process thinks that it has its own address space on real memory at the running time ? o The new translation map is loaded for each process during the switch. How the Kernel/User duality protects the I/O devices from processes? Who talks to device – the process or the kernel? How? o Kernel arbitrary multiplexes I/O through system calls coming from processes. Only kernel talks to devices. What will happen if the Context Switching is done very frequently? o The overhead of OS will become significant and the time will be spent almost only on OS work. What is the context switch? What is that “context” that is switched? Why the “context” is not damaged during the switching. o Context switching is the way making illusion that the process has full computer resources at the moment. The context is kept in the PCB and switching PCBs you perform context switching. Contexts are kept before deactivating and restored before activating. 13 Operating System Principles AUA CS331 Albert Minasyan Assume a single processor. How we can create an illusion of multiple CPUs and simultaneous processing ? Multiplex in time. Perform Context Switching. I/O Devices All virtual CPUs share same non-CPU resources I/O devices are the same Memory is the same Multiprogrmming vs. Multiprocessing Batch Processing Multiprogrmming Time Sharing 14 Operating System Principles AUA CS331 Albert Minasyan Context Switch implementation on x86 systems Is there any similarity with MIPS ? 15 Operating System Principles AUA CS331 Albert Minasyan General Purpose Registers 4 (X) Index Registers (Pointers) (P or I) Base Registers or Segment (S) Registers The long list of “push” assembly operations provides context saving into the stack before jumping to the new subroutine by “call” (“call” is equal to MIPS “jal”) instruction. As we can see the last instruction done by debugger is push eax It’s saving EAX general purpose register (value= 0x 00 00 00 04) into the stack’s top (the address is 0x0012F7A0). The stack address is kept in the Stack Pointer register ESP= 0x0012F7A0. Thus all pushes provide all local variable and other information saving in the Stack and ability to restore them later. 16