代写代考 COMP 2432 2021/2022

Operating Systems
Lecture 4 Process Management

 Processes

Copyright By PowCoder代写 加微信 powcoder

 Scheduler for processes
 Process creation and termination
 Unix/Linux processes
 Process communication and synchronization
COMP 2432 2021/2022

 Recallthatanoperatingsystemexecutes a variety of programs for users.
 A process is a program in execution.
 The terms process and job are used almost interchangeably in most textbooks.
 Aprocessincludes
 Valueofprogramcounter
 Value of registers and processor status word
 Stack for temporary variables
 Textforprogramcode
 Data section for global variables
 Heap for dynamic storage of variables (those created using malloc)
COMP 2432 2021/2022

 In an OS, there are multiple processes executing at the same time.
 Some computer systems have multiple CPUs, but most smaller ones have only one CPU.
 When the number of processes is more than number of CPUs, each CPU can only be allocated to a process for execution at each moment.
 Not all process can receive the CPU service at any moment.
 Process would alternate between “served by a CPU” and “waiting”. There are other possibilities to the condition of a process. These conditions are called process state.
 To keep track of the state of a process, and the information that should be maintained when the process switches between CPUs, a data structure is allocated for each process. This data structure is process control block, to store the information (like your school bag or the profile / desktop for a user).
COMP 2432 2021/2022

Process State
As a process executes, it changes from one state to another, reflected by a state transition diagram. Here is a typical one.
 new: process is being created.
 running: process is running (program instructions are being executed).
 waiting: process is waiting for some event to occur.
 ready: process is waiting to be assigned to a processor or CPU for execution.
 terminated: process has finished execution.
COMP 2432 202

Process Control Block
 Each process is represented by a Process Control Block (PCB).
 Process state
 new, ready, waiting etc.
 Program counter
 CPU registers
 registers, stack pointer, PSW
 list of opened files
COMP 2432 2021/2022
 CPU scheduling information
 process priority, pointer to scheduling
 Memory-management information
 limit of memory boundary  Accounting information
 process id, CPU time used  I/O status information

CPU Switching
 An example that process P0 passes the CPU to process P1 when P0 is interrupted or executes a system call.
COMP 2432 2021/2022

Process Scheduling Queues
 Several types of queues in computer system:
 Job queue: set of all processes in the system.
 Ready queue: set of all processes residing in main memory, ready and waiting to be executed.
 Device queues: set of processes waiting for an I/O device.
 Processes migrate (i.e. move) among the various queues.
 A process on ready queue gets CPU and may later wait for I/O at the device queue.
 A process scheduler for each type of queues determines who will get service next.
COMP 2432 2021/2022

Ready and I/O Device Queues
Why do you want this pointer?
Does it affect correctness or efficiency?
Lecture 4 COMP 2432 2021/2022

Process Life Cycle
COMP 2432 2021/2022

 A process will move through different queues as between its birth (creation) and its death (termination).
 A scheduler selects the process to be served when it is waiting for different services.
 Common types of schedulers:
 Long-term scheduler
 Also called job scheduler.
Select which processes should be brought into the ready queue (only process in ready queue are eligible for CPU).
allocated the CPU. COMP 2432 2021/2022
 Short-term scheduler
 Also called CPU scheduler.
 Select which process should be executed next and be

 Short-term scheduler is concerned with the allocation of CPU.
 Decision to give a CPU to a process occurs many times every second.
 Decision making step must be very fast (so it would be simple).
 Long-term scheduler is concerned with admission of jobs or processes for execution.
 Decision to put a process in the ready queue occurs rarely, perhaps maybe once when the process is created.
 Decision making should be a quite good one (it can afford a longer running time).
 Long-term scheduler controls the degree of multi- programming (number of processes potentially competing for CPU).
COMP 2432 2021/2022

 Processes can roughly be classified into two types:
 I/O-bound process
 A process that spends more time doing I/O than computations.
 Between long duration of waiting, there are many short periods of using CPU (many short CPU bursts).
 Example: interactive programs, e.g. editor.  CPU-bound process
COMP 2432 2021/2022
 A process that spends more time doing computations than I/O.  Between occasional I/O, there are long periods of using CPU
(few very long CPU bursts).
 Example: computation programs, e.g. finding next move in
chess playing.
 A process may be I/O-bound initially and then become CPU-bound or vice versa.
 Intermediate process between I/O and CPU-bound, with moderate I/O.

 A computer system will not be effective if all processes are I/O-bound.
 No long-term scheduler in Unix and Windows.COMP 2432 2021/2022
 Poor use of CPU and heavy competition on I/O devices.
 The percentage of time that the CPU is used is called CPU
utilization. We want a high CPU utilization.
 The competition to access I/O devices is called device
contention. We want a low device contention.
 A system will not perform well if all processes are CPU-bound.
 CPU is used to an extreme, but I/O devices are idle.
 Poor device utilization.
 Processes need to wait for a long time to get the CPU and to complete their execution.
 Long-term scheduler makes decision to maintain a good mix of CPU-bound and I/O-bound processes.

 Short-term scheduler makes decision on which process to get CPU.
 Simple schedulers just submit the processes in a first-come-first-serve manner to the CPU.
 Better schedulers allocate the CPU to improve system performance.
 Waiting time for CPU.
 Completion time of processes.  Responsiveness.
 Different types of processes would need different treatment.
 This issue of CPU scheduling (process scheduling) will be studied in depth in Lecture 6. COMP 2432 2021/2022

 Medium-term scheduler
 Some systems provide medium-term scheduler.
 When there are too many processes competing for CPU, some should be temporarily removed from ready queue.
 When there are few processes using CPU, some removed processes should be returned to ready queue.
 Control multi-programming degree after process admission.
 If not done carefully, this could lead to thrashing.
2 2021/2022

Context Switching
 Short-term scheduler controls which process gets CPU next.
 Sequence of events to bring CPU from an executing process to another is called context switching.
 When CPU switches to another process, the system must save the state of old process and load the previously saved state for new process.
 State of old process will be put on stack when the “time-up” interrupt occurs. It also puts the scheduler in-charge.
 After deciding on successor of CPU, scheduler changes the PC and returns from interrupt to the new process.
 Context switching time is a kind of overhead.
 System does no useful work while switching from one
 Time cost is dependent on hardware support, e.g. amount of efforts to save the state of the old process. COMP 2432 2021/2022
process to another.

Context Switching
 Process P0 is switched to process P1.
COMP 2432 2021/2022
Assume that the interrupt is a time- expire interrupt or a system call to I/O.
CPU CPU wasted wasted

Process Creation
 A process is created when a program is run.
 Effectively, it is another process that creates the new
process for the program.
 When you type a.out in Linux, the Linux shell (CLI) creates a new process for a.out, loads the code into
the process and lets it run.
 Since there is no long-term scheduler, the new process is
 There is a relationship between a process that creates another process.
 The creating process is called the parent process.
automatically admitted and put in the ready queue.
 Processes are normally identified by an integer,
called process identifier or pid.
The created process is called the child process.
A child process could be the parent of another process, and
a tree or hierarchy of processes could be formed.
COMP 2432 2021/2022

Process Creation
 Relationship between parent and its children:
 Resourcesharing
 Parent and children share all resources.
 Children share subset of parent’s resources.
 Parent and children share no resources.
 Execution
 Parent and children execute concurrently.
 Parent waits until all children terminate.
 Addressspace
 Eachchildduplicatesthatofparent.
 Each child has an independent program loaded into it.
 Linuxexample
 To show the parent/children processes use -H option for ps
command (does not work for Mac Unix), e.g. ps -Hlf or ps -Helf COMP 2432 2021/2022

Process Creation
 Process hierarchy in Solaris Unix.
 In Linux, full hierarchy could be obtained by pstree command (does not work for Mac Unix).
systemd-+ModemManager—2*[{MM…}] |-NetworkManager—3*[{NM…}]
|-automount—5*[{automount}] …
| |-sshd-+-5*[sshd—sshd—bash]
|-firewalld—{firewalld} …
COMP 2432 2021/2022
|-sshd—csh
|-sshd—sshd—bash—

Process Creation
 Unix and Linux example  Use fork system call to
create new process.
To replace process memory with a new program, use exec and its family of system calls.
Parent uses wait to collect child and then continues.
COMP 2432 2021/2022

Process Termination
 After a process executes its last statement, it asks the OS to terminate it by calling exit.
 It passes return data back to parent process via wait.  Process resources are de-allocated when terminated.
 Parent may terminate the execution of children processes by calling abort if
 Child has exceeded usage of resources beyond its allocation.
 Task assigned to child is no longer required.
 A process without a parent is called an orphan process.
COMP 2432 2021/2022
 Parent itself is exiting.
 Some OS do not allow a child process to continue if its parent
terminates and the child will be terminated as well.
 A completed child process that is not collected or picked up by
its parent is called a zombie process. Zombie consumes PCB.
 Some OS allow a special arrangement of child to continue after
parent terminates.

Process Creation in Unix/Linux
#include #include int main() {
create a child process
pid = /* fork a child process */ if (pid < 0) { /* error occurred */ Lecture 4 } printf("Fork Failed\n"); program for child process } else if (pid == 0) { /* I am child process */ printf("I am child\n"); program for parent } else { /* I am parent process */ process printf("I am parent\n"); /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete\n"); wait to collect a child process COMP 2432 2021/2022 Process Creation in Unix/Linux #include #include int main() {
pid = /* fork a child process */
if (pid < 0) { /* error occurred */ printf("Fork Failed\n"); exit(1); } else if (pid == 0) { /* I am child process */ printf("I am child\n"); exit(0); /* I am parent process */ printf("I am parent\n"); /* parent will wait for the child if (pid < 0) { /* error occurred */ printf("Fork Failed\n"); exit(1); } else if (pid == 0) { /* I am child process */ printf("I am child\n"); exit(0); /* I am parent process */ printf("I am parent\n"); /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete\n"); exit(0); to complete */ wait(NULL); printf("Child Complete\n"); exit(0); COMP 2432 2021/2022 Process Creation in Unix/Linux  Relationship between parent and child:  Resource sharing  Parent and child share no resources.  Execution  Parent and child execute concurrently.  Address space  Parent is informed about the completion of a child.  Parent should wait for a child to collect it. COMP 2432 2021/2022  Child duplicates that of parent.  Child may have an independent program loaded into it, with special exec system calls. Process Creation in Unix/Linux #include #include #include int main() {
pid = /* fork a child process */ if (pid < 0) { /* error occurred */ printf("Fork Failed\n"); new program for child process exit(1); } else if (pid == 0) { /* I am child process */ execl("/bin/ls", "ls", NULL); } else { /* I am parent process */ /* some parent program code */ /* parent will wait for the child to complete */ printf("Child Complete\n"); program for parent process COMP 2432 2021/2022 wait(NULL); Process Creation in Unix/Linux #include #include #include int main() {
pid = /* fork a child process */
if (pid < 0) { /* error occurred */ printf("Fork Failed\n"); exit(1); } else if (pid == 0) { /* I am child process */ execl("/bin/ls", "ls", NULL); /* I am parent process */ /* some parent program code */ /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete\n"); exit(0); COMP 2432 2021/2022 if (pid < 0) { /* error occurred */ printf("Fork Failed\n"); exit(1); } else if (pid == 0) { /* I am child process */ execl("/bin/ls", "ls", NULL); /* I am parent process */ /* some parent program code */ /* parent will wait for the child to complete */ wait(NULL); printf("Child Complete\n"); exit(0); Process Creation in Unix/Linux  The exec family of system calls allow a Unix/Linux child process to execute another program (instead of the parent program).  Include header file unistd.h (#include ).
System call
int execl(const char *path, const char *arg0, …);
Execute a program at pathname path, last argument must be NULL.
int execlp(const char *file, const char *arg0, …);
Execute a program named by file, last argument must be NULL.
int execle(const char *path, const
char *arg0, …, char *const envp[]);
Same as execl, but access environment variables via *envp[].
int execv(const char *path, char *const
Same as execl, but arguments are stored in *argv[] instead.
int execvp(const char *file, char
*const argv[]);
Same as execlp, but arguments are stored in *argv[] instead.
int execve(const char *path, char
*const argv[], char *const envp[]);
Same as execv, but access
environment variables via *envp[]. COMP 2432 2021/2022

Process Creation in Unix/Linux
 A non-zero return value indicates an error from system call.
 List of arguments in execl, execlp and execle are terminated by a NULL pointer.
 The path is a string containing the file name, including the full path, to be executed.
 The second argument arg0 is the name of the program for execution, e.g. “ls”.
 For execlp and execvp, the first argument is a file name instead of the path name.
 If the string contains a “/”, it is considered as a path name. Otherwise, it is a file name and the system will search for the file according to the directories in environment variable PATH.
 Forexecv,execvpandexecve,argumentsfortheprogramare packed within *argv[].
 For execle and execve, there is a final argument *envp[] storing the environment variables and values for the program.
COMP 2432 2021/2022

Process Creation in Unix/Linux
#include
#include
#include new program for child int main() { process with argument
pid = fork(); /* fork a child process */ if (pid < 0) { /* error occurred */ printf("Fork Failed\n"); } else if (pid == 0) { /* I am child process */ } else { /* I am parent process */ /* some parent program code */ /* parent will wait for the child to complete */ execl("/bin/ps", "ps", "-ef", NULL); wait(NULL); COMP 2432 2021/2022 printf("Child Complete\n"); program for parent process Process Termination in Unix/Linux  Recall that we have mentioned about process termination arrangement in Unix/Linux.  Parent should be informed about the completion of a child.  Parent should wait for a child to collect it.  If parent does not wait for a child to complete, and if parent completes before child completes, the child will become an orphan (without parent).  On the other hand, a completed child process that is not collected or picked up by its parent is called a zombie (alive dead).  So wait is necessary. COMP 2432 2021/2022 Process Termination in Unix/Linux  Special arrangement in Unix and Linux for orphan.  An orphan process will be adopted by a new parent process.  Thespecialprocesswillingtobecomenewparentforall orphan processes has pid 1. It is the init process.  Process with pid 0 is the swapper for paging, and process with pid 1 is the first process running for starting up and shutting down Unix. It has the name sched.  In the example above, a process has its parent process 1 (see third column PPID).  Astudentprocessisdoingsomethingandsomehownever finishes (still running), but the original shell process for the session has terminated (logged out). The process becomes an orphan and is adopted by process 1 as the new parent.  Note that process 1 on apollo (CentOS Linux) is called systemd (i.e. system daemon) instead of init.  Process 1 on MacOS is called launchd (i.e. launch daemon) instead of init. COMP 2432 2021/2022 Process Termination in Unix/Linux #include #include #include int main() {
child returns a value
int pid, cid, retval;
pid = fork(); /* fork a child process */
if (pid < 0) { printf("Fork Failed\n"); exit(1); } else if (pid == 0) { /* I am child process */ printf("I am child, to return 12\n"); } else { /* I am parent process */ get actual return value get child id and return value /* some parent program code */ /* parent will wait for the child to complete */ printf("Child %d returns %d\n", cid, WEXITSTATUS(retval)); exit(34); /* check exit status with echo $? */ cid = wait(&retval); Lecture 4 } COMP 2432 2021/2022 Cooperating Processes  An independent process cannot affect or be affected by the execution of another process.  A cooperating process can affect or be affected by the execution of another process.  Most larger systems have a collection of processes cooperating with one another.  Web server and web browser (client) pair is a form of cooperating processes, residing over the network at different computers.  Advantages of process cooperation:  Information sharing: concurrent access to data.  Computation speed-up: break into subtasks for processes.  Modularity: better structuring of functionality.  Convenience: model a user in concurrent working mode. COMP 2432 2021/2022 Cooperating Processes  A very common view point of cooperating processes is the model of a producer and a consumer.  Producer process produces information (called item) that is consumed by a consumer process.  Information (item) produced by producer must be stored up for consumer usage later (since consumer may not be ru 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com