lab 5 process control

17
NCHU System & Network Lab NCHU System & Network Lab Lab 5 Lab 5 Process Control Process Control Operating System Lab

Upload: turner

Post on 14-Jan-2016

51 views

Category:

Documents


1 download

DESCRIPTION

Lab 5 Process Control. Operating System Lab. What is a process?. Informally, a process is a program in execution. A process includes data section, which contains global variables text section, which contains the program code stack section, which contains temporary data - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

Lab 5Lab 5Process ControlProcess Control

Operating System Lab

Page 2: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

What is a process?What is a process?• Informally, a process is a program in execution.• A process includes

– data section, which contains global variables– text section, which contains the program code– stack section, which contains temporary data– heap section, which contains dynamically allocated memory

• A process is more than the program code, it also includes the current activity– Represented by the value of the program counter and the contents of

the processor’s registers.

Page 3: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

Process CreationProcess Creation• Parent process create children processes, which, in turn

create other processes, forming a tree of processes.

Page 4: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

Process Creation (cont.)Process Creation (cont.)• Resource sharing

– Parent and children share all resources

– Children share subset of parent’s resources

– Parent and child share no resources

• Execution

– Parent and children execute concurrently

– Parent waits until children terminate

• Address space

– Child duplicate of parent

– Child has a program loaded into it

Page 5: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

Process IdentificationProcess Identification

• UNIX identifies processes by a unique integral value called the process ID.

• Each process also has a parent process ID, which is initially the process ID of the process that created it.

Page 6: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The fork()fork() Function Function

• UNIX examples– fork() system call creates new process

• The fork() copies the parent's memory image so that the new process receives a copy of the address space of the parent.

• Both processes continue at the instruction after the fork statement (executing in their respective memory images).

Page 7: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The fork()fork() Function (cont.) Function (cont.)

Memory

fork()Original image

New image

Copy from original image

Page 8: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The fork()fork() Function (cont.) Function (cont.)

Page 9: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The fork()fork() Function (cont.) Function (cont.)

Page 10: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The wait()wait() Function Function

• The parent can execute wait() to block until the child finishes.

• If wait() returns because the status of a child is reported, it returns the process ID of that child.

• Else if an error occurs, it returns –1.

Page 11: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The exec()exec() Family Family

• The fork() function creates a copy of the calling process, but many applications require the child process to execute code that is different from that of the parent.

• The exec() family provides a facility for overlaying the process image of the calling process with a new image.

• Use the fork()–exec() combination for the child to execute the new program while the parent continues to execute the original code.

Page 12: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The exec()exec() Family (cont.) Family (cont.)

Memory

fork()Original image

New image

Copy from original image

exec()Load

another image

Page 13: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

The The exec()exec() Family (cont.) Family (cont.)

• execlp– passes the command-line arguments in an explicit list and are

useful if you know the number of command-line arguments at compile time.

• Example– execlp ("/bin/ls", "ls", “-l” , NULL);

Page 14: Lab 5 Process Control

C Program Forking a Child ProcessC Program Forking a Child Process

Page 15: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

Lab ILab I• Create a child process

– Increases the value of a global variable

– Declare a local variable and increase its value

• Note that, both the global and local variables must be initialized and have the same values.

• Finally, both of the processes print their results of global and local variables and also show their process id and parent’s id.

Page 16: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

Lab IILab II• Write a program that creates 5 processes, forming a tree

configuration illustrated in the figure.

• Prints their own reports to show their process id and their parent’s id

• Make a number of wait() for all it’s children exiting. – Depend on the number of children

A

B

C

D E

Page 17: Lab 5 Process Control

NCHU System & Network LabNCHU System & Network Lab

ReferencesReferences

• Avi Silberschatz, Peter Baer Galvin and Greg Gagne, “Operating System Concepts,” John Wiley & Sons, 6th Edition, 2001

• “Unix Systems Programming: Communication, Concurrency, and Threads” by Kay A. Robbins, Steven Robbins

• Neil Matthew and Richard Stones, “Beginning Linux Programming,” Wiley publishing, 3rd Edition, 2004

• W. Richard Stevens, “Advanced Programming in the UNIX Environment,” Addison-Wesley, 1992