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

60-256 System Programming: Process Control II

Content

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

Instructor: Dr. Dan Wu

Process Control II
1

Copyright @ 2019, 2020, 2021 all rights reserved
Content

Content

1
Terminating a process

2
Waiting for a child

3
Orphan and zombie Processes

Process Control II
2

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

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. It flushes all output streams and closes all open streams,
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 II
3

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example (terminate.c)

#include
#include
#include

#include
#include

int main() { int newpid;
printf(“before: mypid is %d\n”, getpid()); if ((newpid = fork()) == -1 )
perror(“fork”); else if (newpid == 0){
printf(“I am the child %d now sleeping…\n”,getpid()); sleep(2);
exit(47);
printf(“I am gone”);
}
else{
printf(“I am the parent %d\n”,getpid()); sleep(10);
printf(“My child %d must be gone by now. I am leaving…\n”,newpid); exit(1);
printf(“I am gone too\n”);
}
}

What is the output produced?

Process Control II
4

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

The output of the previous program is:
before: mypid is 5067 I am the parent 5067
I am the child 5068 now sleeping…
My child 5068 must be gone by now. I am leaving…

The lines printf(“I am gone”); and printf(“I am gone too”); are not executed because exit() never returns.

Process Control II
5

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

wait() and waitpid()
wait()
Synopsis: pid t wait(int *status);
Allows the parent to wait for the termination of one of its children and to 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 waits for.
For example:
If pid > 0 then, waitpid() waits for the child process identified by pid.
If pid = -1 then, waitpid() waits for any child process to return.

Process Control II
6

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Termination

When successful, wait(…) returns the pid of the terminating child process.
The value in status is encoded as follow:
if the rightmost byte of status is zero, then the leftmost byte contains the status returned by the child: a value between 0 and 255 (passed as an argument to exit).
This represents a normal termination of the child process.

if the rightmost byte of status is nonzero, then the rightmost 7 bits are equal to the signal number, that caused the process to terminate. The remaining bit of the rightmost byte is set to 1 if a core dump was produced by the child process.

Process Control II
7

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Termination

Process Control II
8

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example (termiante2.c)

#include
#include
#include

#include
#include

int main() { int newpid;
printf(“before: mypid is %d\n”, getpid()); if ((newpid = fork()) == -1 )
perror(“fork”); else if (newpid == 0){
printf(“I am the child %d now sleeping…\n”,getpid()); sleep(5);
exit(47);
}
else{
printf(“I am the parent %d\n”,getpid()); int status;
int child_pid = wait(&status);
printf(“My child %d has terminated\n”,child_pid);
printf(“I have received the status = %d\n”,status); int child_status = status >> 8;
int signal = status & 0x7F; int core = status & 0x80;

printf(“Child status = %d Signal = %d Core = %d\n”,child_status, signal, core);
}
}

Process Control II
9

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example of a normal termination
Example
Consider the previous program. The output resulting from a normal execution:
before: mypid is 4602 I am the parent 4602
I am the child 4603 now sleeping… My child 4603 has terminated
I have received the status = 12032 Child status = 47 Signal = 0 Core = 0

Process Control II
10

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example of a premature termination
Example

1

2`

3
Consider the previous program running. When the child is sleeping, the output is as follows:
before: mypid is 4602 I am the parent 4602
I am the child 4603 now sleeping…
Before the child makes the exit call, we kill the child process. At the command prompt, we run:
kill 4603 (kill –l)
The parent wakes up and displays:
My child 4603 has terminated
I have received the status = 15 Child status = 0 Signal = 15 Core = 0

Process Control II
11

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Bit-manipulation macros

Some bit-manipulation macros have been defined to deal with the value in the variable status(you need to include ).

WIFEXITED(status): true for normal child termination.

WEXITSTATUS(status): used only when
WIFEXITED(status) is true, it returns the exit status as an integer(0-255).

Process Control II
12

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Bit-manipulation macros

Some bit-manipulation macros have been defined to deal with the value in the variable status(you need to include
).

WIFSIGNALED(status): true for abnormal child termination

WTERMSIG(status): used only when WIFSIGNALED(status) is true, it returns the signal number that caused the abnormal child death.

Process Control II
13

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Bit-manipulation macros

Some bit-manipulation macros have been defined to deal with the value in the variable status(you need to include
).

WCOREDUMP(status): true if a core file was generated.

Process Control II
14

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Termination

Process Control II
15

Based on which of these four macros is true, other macros are used to obtain the
exit status, signal number, and the like.

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Termination

Process Control II
16

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

wait() and waitpid()

waitpid()
Synopsis: pid t waitpid(pid t pid, int *status, int options);

Process Control II
17

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

wait() and waitpid()(wait_demo.c status1.c, status2.c)

waitpid()
Synopsis: pid t waitpid(pid t pid, int *status, int options);

Process Control II
18

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

About core dump
Core dump
A core dump typically refers to a file (named core) containing the memory image of a particular process, or parts of it, along with other information such as the values of processor registers.
The file is created when a program has terminated abnormally, i.e. crashed.
It used to be important in debugging programs.

Process Control II
19

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

abort()

abort()
Synopsis: void abort(void);
The abort() function causes abnormal process termination to occur. It sends the signal SIGABRT to the parent process. It is declared in stdlib.h.
abort() causes a core dump (ulimit –a).

Process Control II
20

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example using abort (abort.c)

#include
#include
#include

#include
#include

int main() { int newpid;
printf(“before: mypid is %d\n”, getpid()); if ((newpid = fork()) == -1 )
perror(“fork”); else if (newpid == 0){
printf(“I am the child %d now sleeping…\n”,getpid()); sleep(5);
abort();
}
else{
printf(“I am the parent %d\n”,getpid()); int status;
int child_pid = wait(&status);
printf(“My child %d has terminated\n”,child_pid);
printf(“I have received the status = %d\n”,status); int child_status = status >> 8;
int signal = status & 0x7F; int core = status & 0x80;

printf(“Child status = %d Signal = %d Core = %d\n”,child_status, signal, core);
}
}

What is the output produced?

Process Control II
21

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example using abort()

The output of the previous program:
before: mypid is 5367 I am the parent 5367
I am the child 5368 now sleeping… My child 5368 has terminated
I have received the status = 134 Child status = 0 Signal = 6 Core = 128

Process Control II
22

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Orphan and zombie processes

A process that terminates does not leave the system before its parent accepts its return.

Special situations
There are 2 interesting situations:

1

2
a parent exits (for example, the parent has been killed prematurely) while its children are still alive. The children become orphans.
The parent is alive but never makes the call to wait(). The children become zombies.

Process Control II
23

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Orphan processes

The kernel changes the PPID of the orphan processes to 1.
→ orphan processes are systematically adopted by the

process init (whose PID is 1).
init accepts all its children returns.

Process Control II
24

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example of making an orphan process (orphan.c)

int main(){ printf(“Before fork\n”);

if ( fork() == 0 ){ // child
printf(“My parent is %d\n”, getppid()); sleep(6);
printf(“My parent is %d\n”, getppid()); exit(2);
}
// parent
printf(“had a child…\n”);

sleep(3);

exit(1);
}

What is the output produced?

Process Control II
25

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Zombie processes

Zombie processes remain in the system’s process table waiting for the acceptance of their return. However, they loose their resources (data, code, stack…).
Because the system’s process table has a fixed-size, too many zombie processes can require the intervention of the system administrator.

Process Control II
26

Copyright @ 2019, 2020, 2021 all rights reserved
Terminating a process Waiting for a child
Orphan and zombie Processes

Example of making a zombie process (zombie.c)

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

pid = fork();
if (pid){ // means pid !=0
printf(“I am the parent process, pid=%d\n”, getpid()); while(1)
sleep(5);
}
printf(“I am the child process, pid=%d\n”, getpid()); exit(0);
}

The program will display:
I am the parent process, pid=5585 I am the child process, pid=5586
If you look at the running processes: ps -u your user id PID TTY TIME CMD
5585 pts/4 0:00 make zom
5586 ? 0:00

Process Control II
27

Copyright @ 2019, 2020, 2021 all rights reserved