Download presentation
Presentation is loading. Please wait.
Published byKerrie Chambers Modified over 9 years ago
1
LIS245- Process_Management1 Process Management: Process Control Operating Systems
2
LIS245- Process Management- semester-22
3
3 What is a process? One of the fundamental functions of an OS is processes management A process is (in simple terms) a program in execution A program (an executable file) is a passive entity that resides on a disk (floppy, cd-rom, hard-disk) A process is an active entity that can run. It includes The program (executable file) loaded in memory (called memory (or core) image of the process) The state: set of register values in CPU (PC, SP, other registers) Open files: their descriptors and their status. Allocated I/O resource for this process, ….
4
LIS245- Process Management- semester-24 What is a process Modern computer can run multiple processes using a single CPU A scheduler coordinates the usage of CPU by several processes A user that is using a process perceives usually that the CPU is allocated to himself/herself. This is also pseudo-parallelism, where more than one process run in parallel. At any given time instant, only one process is running: using the CPU
5
LIS245- Process Management- semester-25 The Process Model Conceptually, each process has its own virtual CPU: virtual registers. In reality, a single physical CPU is shared. program A program B program C program D virtual CPU1 virtual CPU2 Virtual CPU3 virtual CPU4 time process A C D B CPU allocation to processes Real Situation Conceptual Model
6
LIS245- Process Management- semester-26 What is a process When a process is created, the respective program is loaded into memory from disk A program consists of 3 parts (segments) Text segment: includes sequence of instructions Never modified by the process Data segment: contains global variables Can be modified by the process during execution Stack segment: holds temporary variables Local variable of functions Function parameters Return addresses
7
LIS245- Process Management- semester-27 Process State In a multiprograming environment, a process changes state while in execution. The states that a process can be in are: Running Ready Blocked (waiting) running blocked ready 12 4 3 1.Process blocks for I/O 2.Scheduler picks an other process 3.Scheduler picks this process 4.I/O becomes available
8
LIS245- Process Management- semester-28
9
9 Process-structured OS Scheduler handles interrupts and scheduling 0 12n-1n-2 …… processes A process can be a system process doing for example a file service
10
LIS245- Process Management- semester-210 Process Implementation A process is represented in OS using a structure called process control block (PCB) A PCB is entry in a table, called process table When a process is suspended (changes from running state to one of blocked or ready states) All its state information is stored in the corresponding PCB in process table When a process is resumed (changed from blocked or ready state to running state) All its state information contained in PCB is loaded into CPU. The process can continue executing.
11
LIS245- Process Management- semester-211 What is in PCB? A PCB contains information about a process classified as follows Process state: ready, blocked, running. Program counter: address of the next instruction that is to be executed for this process CPU registers: vary depending on hardware Accumulators Index registers Stack pointers General purpose registers Condition code information CPU scheduling information Process priority Pointers to scheduling queues. Memory Management Information Base and limit register values Page tables Text, data and stack pointers, etc. Accounting Information Amount of CPU usage, time limits Process ID I/O status information List of I/O devices allocated to the process List of open files, etc.
12
LIS245- Process Management- semester-212 A typical PCB Process ManagementMemory ManagementFile Management Registers Program Counter Program status word Stack pointer Process state Priority Scheduling parameters Process ID Parent Process Process group Signals Time when process started CPU time used Children’s CPU time Time of next alarm Pointer to text segment Pointer to data segment Pointer to stack segment Root directory Working directory File descriptors User ID Group ID The above table contains come of the fields that may exist in a PCB. The list is not complete.
13
LIS245- Process Management- semester-213 Virtual address space of linux Address of the blocks The second column shows the flags Third, fourth, fifth and sixth column shows the offset, device, inode, and name of the file that is mapped to the block. File with read-write flag but contains no file are allocated for heap and stack.
14
LIS245- Process Management- semester-214 Process address space
15
LIS245- Process Management- semester-215
16
LIS245- Process Management- semester-216 How interrupts affect processes? A hardware interrupt may come to the CPU requesting some immediate service from CPU. CPU may be running some process at that time. CPU should resume process and should handle the interrupt.
17
LIS245- Process Management- semester-217 How interrupts affect processes? Steps to handle interrupts Hardware stacks program counter, psw, and possible one more other registers. Hardware loads new program counter from interrupt vector Interrupt vector keeps the address of ISR (interrupt service routine) for the incoming interrupt identified by a number. In ISR: An assembly language procedure saves the registers into the PCB of the process that was executing. An assembly language procedure sets up a new stack Information stored on the previous stack is removed Interrupt service runs (a C program) Scheduler decides which process is run next. C procedure returns to the assembly code Assembly language procedure starts up new current process
18
LIS245- Process Management- semester-218 Context switch Save state into PCB-0 Reload state from PCB-1 Save state into PCB-1 Reload state from PCB-0 executing idle ….. Interrupt or system call executing Process P 0 Process P 1 Operating System
19
LIS245- Process Management- semester-219 Context switch Switching the CPU to an other process requires saving the state of the old process and loading the saved state of the new process into CPU. This is called context switch The context of a process is represented in the PCB of that process System does not do any useful work during context switch. Hence it is waste of time. Context switch is usually costly. Context switch time may range from 1 s to 1000 s
20
LIS245- Process Management- semester-220 Process Scheduling There is scheduler component of OS that decides which process should use CPU and when, and how long. When a process enters the system, it is put into a process (job) queue. This queue constists of all processes in the system All processes in the ready state are put in ready queue. A linked list. OS has also other queues: Each I/O device may have a queue associated with it. This queue contains all the processes that want service from that device. This is called device queue. Each device has its own device queue.
21
LIS245- Process Management- semester-221 head tail head tail head tail head tail head tail Ready Queue Mag. Tape Unit 0 Mag. Tape Unit 1 Disk Unit 0 Terminal Unit 0 registers... registers... registers... …… registers... registers... registers... registers... PCB 7 PCB 2 PCB 14 PCB 6 PCB 13 PCB 3 PCB 5 Various Queues in an OS Queue header
22
LIS245- Process Management- semester-222 Representation of Process Scheduling ready queue I/O queue I/O CPU I/O request time slice expired time slice expired fork a child wait for an interrupt wait for an interrupt child executed child executed interrupt occurs interrupt occurs Queuing-diagram representation of process scheduling queue resource enters system terminates
23
LIS245- Process Management- semester-223 Three possible state with execution Once a process is allocated the CPU and is executing, one of the 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 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 queue has its PCB and resources deallocated.
24
LIS245- Process Management- semester-224 Schedulers A process goes through various scheduling queues during its lifetime. OS must select processes from these queues for serving them The selection process is carried out the appropriate scheduler.
25
LIS245- Process Management- semester-225 Schedulers There are 3 kinds of schedulers Long-term scheduler Short-term scheduler Medium-term scheduler Invocation intervals of these schedulers differ. Long-term scheduler: For batch systems, selects the set of jobs that needs to be loaded into memory and executed until they finish. When a terminated process leaves, long term scheduler will be invoked again. Invocation interval of seconds, minutes or longer. In some systems, there may not be a long-term scheduler: Unix for example.
26
LIS245- Process Management- semester-226 Schedulers Short-term scheduler: Selects from among the processes in ready queue. Invoked much more frequently. At least once every 100ms. Therefore, short-term scheduler should be fast in picking up the next process to execute. Midterm-schedulers: Used in some systems. Can be used to store executing programs to the disk for some time and then reload into memory: called swapping. This can increase the process mix that are executing. Can be used when processes’ memory requirements are more than the physical memory size.
27
LIS245- Process Management- semester-227 Medium-term scheduling model ready queue CPU I/O waiting queues I/O waiting queues I/O Partially executed swapped-out processes Partially executed swapped-out processes end swap out swap in
28
LIS245- Process Management- semester-228 Schedulers (Cont.) Short-term scheduler is invoked very frequently (milliseconds) (must be fast) IF it takes 10 milliseconds to decide to execute a process for 100 milliseconds, then of the CPU is wasted simply for scheduling. Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) The long-term scheduler controls the degree of multiprogramming Processes can be described as either: I/O-bound process – spends more time doing I/O than computations, many short CPU bursts CPU-bound process – spends more time doing computations; few very long CPU bursts
29
LIS245- Process Management- semester-229 Operations on Processes Some operations on processes are: Creating a process Terminating a process Waiting for a child to terminate OS must provide facilities (mechanisms) for these operations.
30
LIS245- Process Management- semester-230 Operations on Processes: Process Creation A process may create several other processes The created processes are called children The creating process is called the parent In this way, a tree of processes may be created. Each process has an ID (pid) The parent knows the its children's IDs.
31
LIS245- Process Management- semester-231 A tree of processes in UNIX system root pagedaemon swapper init login shell login shell login shell netscape emacs acrobat-reader matlab shell tetris
32
LIS245- Process Management- semester-232 Operations on Processes: Process Creation When a child created: In Unix, The memory image of a parent process is duplicated and the child own the duplicate. This means: All global variables of the child will have initial values set to the values of global variables of the parent at child creation time. When child and parent continue execution, these variables may be set to different values. This child will inherit all the open file descriptors of the parent. This means, descriptors of the parent is copied into the child. This means, parent and child can access the same files or devices.
33
LIS245- Process Management- semester-233 Operations on Processes: Process Creation When a process creates a new child process, there are two possibilities exists in terms of execution of the parent: The parent continues execute concurrently with its children The parent waits until some or all of its children have terminated. There are also two possibilities in terms of the address space of the child: The child process is duplicate of the parent process UNIX, Windows The child process has a program loaded into it (Windows) Windows
34
LIS245- Process Management- semester-234 Process Creation (Cont.) Address space Child duplicate of parent Child has a program loaded into it UNIX examples fork system call creates new process exec system call used after a fork to replace the process’ memory space with a new program
35
LIS245- Process Management- semester-235 Example: Unix process creation In Unix, the only way to create a new process is calling the fork () system call. In Unix, each process has a process identifier (which is an integer value). fork() system call creates a exact duplicate of the calling (parent) process. The child then can load a new program if it wants to execute some different program than what the parent process executes. Can do this by use of execlp system call execlp replaces the memory image of a process with some new program.
36
LIS245- Process Management- semester-236 UNIX Process Creation Every process, except process 0, is created by the fork() system call fork() allocates entry in process table and assigns a unique PID to the child process child gets a copy of process image of parent: both child and parent are executing the same code following fork() but fork() returns the PID of the child to the parent process and returns 0 to the child process
37
LIS245- Process Management- semester-237 Process creation - Example main () { int pid; cout << “ just one process so far”<<endl; pid = fork(); if (pid == 0) cout <<“I am the child “<< endl; else if (pid > 0) cout <<“I am the parent”<< endl; else cout << “fork failed”<< endl;}
38
LIS245- Process Management- semester-238 Example: Unix process creation #include int i, j, char a[1024]; int fd; main(int argc, char *argv[]) { int pid; fd = fopen(“myfile.txt”, R_ODLY); pid = fork(); /* create a new process */ if (pid < 0) {/* system call is not successful */ fprintf(stderr, “fork() failed\n”); exit(-1); /* termination with some error */ } else if (pid == 0) { /* this is the child process */ execlp (“/bin/ls”, “ls”, NULL); } else { /* this is the parent process */ wait(NULL); /* wait for the child to terminate */ printf(“child completed\n”); exit(0); /* normal termination */ }
39
LIS245- Process Management- semester-239 C Program Forking Separate Process int main() { Pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); }
40
LIS245- Process Management- semester-240 fork() main() { …. pid = fork(); …… } int i = 10; inj j; char a[1024] int fd = 5 pid = ? Parent pid = 2345 main() { …. pid = fork(); …… } int I = 10; inj j; char a[1024] int fd = 5 pid = 0 Child pid = 3210 Before calling fork() main() { …. pid = fork(); …… } int i; inj j; char a[1024] int fd = 5 pid = 3210 Parent pid = 2345 After calling fork() fork() executes newly created already exists PC register PC register stack data text stack data text
41
LIS245- Process Management- semester-241 execlp() main() { …. execlp(…); …… } int I = 10; inj j; char a[1024] int fd = 5 pid = 0 Child pid = 3210 main() { /* list the content of the current directory } …… Child pid = 3210 stack data text our program /bin/ls program execlp() executes Before calling execlp()After calling execlp() PC register PC register
42
LIS245- Process Management- semester-242 Example
43
LIS245- Process Management- semester-243 Example: Exercise
44
LIS245- Process Management- semester-244
45
LIS245- Process Management- semester-245
46
LIS245- Process Management- semester-246
47
LIS245- Process Management- semester-247
48
LIS245- Process Management- semester-248 Process termination When a process terminates, all the resources of the process are deallocated by the OS. Memory Open files. I/O buffers … A process may indicate to OS that it wants to terminate by calling the exit () system call. At that point, the process may return some information (such as the exit code) to its parent, if the parent is waiting for the process to terminate. A parent may wait for a child to terminate by calling the wait() or waitpid() system call. If a parent does not wait for a child to terminate, the parent can simply terminate after creating the child and optionally doing some more job. In this case, the parent of the child will be a default process ( init process in Unix)
49
LIS245- Process Management- semester-249 Difference The Difference between the two functions are as follows: The wait function can block the caller until a child process terminates, whereas waitpid has an option that prevents it from blocking. The waitpid function doesn’t wait for the child that terminates first; it has a number of options that control which process it waits for.
50
LIS245- Process Management- semester-250 parent calls fork() parent calls wait() child calls exit() and terminates parent calls exit() and terminates wait() returns with pid of child child created parent created ParentChild time process in ready state process in wait state Parent waits for child to terminate
51
LIS245- Process Management- semester-251 Wait and waitpid functions When a process terminates, either normally or abnormally, the kernel notifies the parent by sending the SIGCHLD signal to the parent. Block, if all of its children are running Return immediately with the termination status of a child, if a child has terminated and is waiting for its termination status to be fetched. Return immediately with an error, if it doesn’t have any child processes
52
LIS245- Process Management- semester-252 Two uses of fork When a process wants to duplicate itself. Common for network server. Parent calls fork and lets the child handle the request. The parent waits for the next service request to arrive. When a process wants to execute different program. Child does an exec right after it returns from the fork. Next we look for summarization…..
53
LIS245- Process Management- semester-253 Communications Models Shared Memory Vs Message Passing: Advantages and Disadvantages
54
LIS245- Process Management- semester-254 Cooperating Processes Independent process cannot affect or be affected by the execution of another process. no sharing of data and flow of information between processes. Cooperating process can affect or be affected by the execution of another process. processes work together to achieve a job. They may exchange data Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience
55
LIS245- Process Management- semester-255 Producer-Consumer Problem Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process Example: A compiler will produce assembly code which is consumed by an assembler. The assembler, in turn, may produce object modules, which is consumed by the loader. Client Server Paradigm.
56
LIS245- Process Management- semester-256 Cooperating Processes Concurrent execution requires Communication facilities Synchronization of actions of more than one processes. producer Producer-consumer problem: paradigm for programming concurrent and cooperating processes. consumer buffer
57
LIS245- Process Management- semester-257 Cooperating Processes Producer consumer problem examples: Print program – printer driver Compiler – assembler - loader compiler buffer assembler Assembly code buffer loader Object code Unbounded or bounded buffer
58
LIS245- Process Management- semester-258 Cooperating Processes Buffer could be A shared memory between consumer(s) and producer(s). Could be provided by OS through the use of interprocess-communication (IPC) facilities. We will later see: Interprocess communication Synchronization
59
LIS245- Process Management- semester-259 Buffer in Shared memory Available Buffer of items to be filled by the producer and emptied by the consumer. The buffer memory (shared) is shared by the producer and consumer process. Producer and Consumer must beSynchronized. Two types of Buffer unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size
60
LIS245- Process Management- semester-260 Shared data #define BUFFER_SIZE 10 Typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Solution is correct, but can only use BUFFER_SIZE-1 elements Bounded-Buffer – Shared-Memory Solution In points out the next free position in the buffer. Out points to the first full position in the buffer. The buffer is empty when in==out. The buffer is full when (in+1)%BUFFER_SIZE= = out.
61
LIS245- Process Management- semester-261 Bounded-Buffer – Producer process-Consumer process NextProduced New item to be produced is stored. NextConsumed Item to be consumed is stored.
62
LIS245- Process Management- semester-262 Interprocess Communication (IPC ) Mechanism for processes to communicate and to synchronize their actions Message system – processes communicate with each other without resorting to shared variables IPC facility provides two operations: send(message) – message size fixed or variable receive(message) If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive Implementation of communication link physical (e.g., shared memory, hardware bus) logical (e.g., logical properties)
63
LIS245- Process Management- semester-263 Implementation Questions How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes? What is the capacity of a link? Is the size of a message that the link can accommodate fixed or variable? Is a link unidirectional or bi-directional?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.