CS计算机代考程序代写 scheme 60-256 System Programming: Process Control I

60-256 System Programming: Process Control I

Content

COMP 2560 System Programming:
Process Control I
Courtesy of Dr. B. Boufama
modified by Dan Wu
School of Computer Science University of Windsor

Instructor: Dr. Dan Wu

Process Control I
1

Copyright @ 2019, 2020, 2021 all rights reserved

Content

Content

1
Introduction

2
Unix Processes

3
Creating a new Process: fork()

4
Terminating a process: exit()

5
wait() and waitpid()

Process Control I
2

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Context

Context

single computer (single CPU)
under UNIX operating system

It is possible to have several simultaneously executing programs
→ It appears that the executions are done in Parallel!

Multiprogramming:
The CPU executes some instructions of one program then switches to another program giving the illusion that any program is continuously executing, this scheme is called time-slicing.

Process Control I
3

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Program vs. process

A program is an executable file residing on a disk.
A process is an executing (running) program, usually with a limited life-time.
Note that sometimes a process is called task.

Important notion
An executing program → a process.

Process Control I
4

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Unix Processes

Every process in Unix contains the following (Ch. 7*, optional reading):
A unique process ID (PID): a unique nonnegative integer assigned by Unix, used to identify a process.
The user ID of the owner.
Some code: instructions that are being executed (the code segment)
Some data: variables (the data segment)
A stack: a form of memory where it is possible to push and pop instructions (the stack segment).
An environment: registers’ contents, tables of open files,…

Process Control I
5

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

getpid() and getppid()

getpid() and getppid()
A process can obtain its PID by the getpid() system call. A process can also obtain its parent ID PPID by the getppid() system call.
//getpid.c
#include
int main(void){

printf(“Hello, my PID is %d\n”, getpid()); printf(“Hello, my PPID is %d\n”, getppid()); exit(0);
}

Shell-Prompt> a.out
Hello, my PID is 11723 Hello, my PPID is 5598

Process Control I
6

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

States of a process

Different states of a process
Throughout its lifetime, a process can have the following states:

New: the process has just been created.
Ready: waiting to be assigned to a processor (the processor is busy running other processes).
Running: while its instructions are being executed.
Blocked: waiting for an event (e.g. I/O).
Done: finished

A new process becomes ready and waits for its turn to be executed by the processor.
A ready process becomes running for a ”time slice” after which it is either done, blocked or ready to continue for another time slice.
A blocked process becomes ready again after the event it was waiting for occurs (e.g. I/O).

Process Control I
7

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

init

init
Unix starts as a single process, called init. The PID of
init is 1.
The only way to create a new process in Unix, is to duplicate an existing one.
→ the process init is the ancestor of all subsequent

processes.
In particular, process init never dies.

Process Control I
8

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Creation of a new process

The creation or spawning of new processes is done with two system calls:
fork(): duplicates the caller process
exec(): replaces the caller process by a new one.

Process Control I
9

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Example 1: Handling a login session

init forks a copy of itself.
the child process execs the getty program which prints the login prompt, waits for the user’s login name, and then execs the login program.
the login process, if successful, execs the shell program (e.g. csh).

Process Control I
10

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Example 2: Running a utility from a shell (csh).

csh forks a copy of itself.
the child process execs the utility program, for example
gedit.
the parent process waits for the termination(exit) of its child process by going asleep.
when the child process terminates, a signal is sent to the parent process (the shell program csh). The latter wakes-up and becomes ready to accept the next command.

Process Control I
11

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

Creating a new Process: fork()

fork()
Synopsis: pid t fork(void);

when successful, the fork() system call: creates a copy of the caller (parent) process.
returns the PID of the newly created process to the parent returns 0 to the new process (the child).
If not successful, the fork() returns -1.

fork() is a “strange” system call: called by a single process but returns twice, to two different processes.

Process Control I
12

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

The child process

A child process has:
its own unique PID, a different PPID,
its own copy of the parent’s data segment and file descriptors.

Both parent and children resume the execution after the call to
fork().

fork() is primarily used in two situations:

1
2
A process wants to execute another program.
A process has a main task and when necessary creates a child to handle an operation (Servers).

Process Control I
13

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

both parent and children resume the execution after the call to fork()

//fork1.c
#include

int main(int argc, char *argv[]){ int pid;

printf(“Only one process\n”); pid = fork();

if(pid == -1){ perror(“impossible to fork”); exit(1);
}
if(pid > 0)
printf(“I am the parent, pid=%d\n”, getpid()); else
if(pid == 0)
printf(“I am the child, pid=%d\n”, getpid());

exit(0);
}

Process Control I
14

Copyright @ 2019, 2020, 2021 all rights reserved

Introduction Unix Processes
Creating a new Process: fork() Terminating a process: exit() wait() and waitpid()

The child has its own copy of the parent’s data segment

//fork2.c
#include
#include
#include

int glb = 100; // global variable int main(){
int pid;
int var = 88; printf(“Before fork\n”); pid = fork();
if( pid < 0 ){ perror("fork"); exit(1); } if(pid == 0){ //child glb++; var++; } else{ // parent sleep(2); } printf("pid = %d, glob = %d, var = %d\n", getpid(), glb, var); return 0; } Process Control I 15 Copyright @ 2019, 2020, 2021 all rights reserved Introduction Unix Processes Creating a new Process: fork() Terminating a process: exit() wait() and waitpid() The child has its own copy of the parent’s file descriptors and share the same offset (fork3.c) #include
#include
#include
#include int main(){
int pid, fd, i; char c;

if ( (fd =open(“test”,O_RDWR|O_CREAT, 0644)) == -1 ){ perror(“test”);
}

if( (pid = fork())< 0 ){ perror("fork"); exit(1); } if(pid == 0) // child for( i =65; i < 85; i++){ c = i; write(fd, &c ,1); } else // parent for( i = 0 ; i< 20; i++){ c = 64; // character @ write(fd, &c ,1); } return 0; } Process Control I 16 Copyright @ 2019, 2020, 2021 all rights reserved Introduction Unix Processes Creating a new Process: fork() Terminating a process: exit() wait() and waitpid() What is the output? //q1.c int main(){ printf("before fork, my pid is %d\n" , getpid()); fork(); fork(); fork(); printf("done, my pid is %d\n", getpid()); } //q2.c int main(){ int i; printf("before fork, my pid is %d\n" , getpid()); for (i=0; i<3; i++){ if ( fork()== 0 ) printf("Hi, I am child. My pid is %d\n", getpid()); } } Process Control I 17 Copyright @ 2019, 2020, 2021 all rights reserved Introduction Unix Processes Creating a new Process: fork() Terminating a process: exit() wait() and waitpid() Process termination: exit() Synopsis: void exit(int status); This call terminates a process and never returns The status value is available to the parent process through the wait() system call. When invoked by a process, the exit() system call: closes all the process’s file descriptors frees the memory used by its code, data and stack sends a SIGCHLD signal to its parent and waits for the parent to accept its return code. Process Control I 18 Copyright @ 2019, 2020, 2021 all rights reserved Introduction Unix Processes Creating a new Process: fork() Terminating a process: exit() wait() and waitpid() wait() and waitpid() wait() Synopsis: pid t wait(int *status); Allows the parent to wait for the termination of one of its children and accept its termination code. waitpid() Synopsis: pid t waitpid(pid t pid, int *status, int options); waitpid() is similar to wait() except that it has a number of options that control which process it wait for. For example: If pid > 0 then, waitpid() waits for the child process identified by pid.with
If pid = -1 then, waitpid() waits for any child process to return.

Process Control I
19

Copyright @ 2019, 2020, 2021 all rights reserved