march 1, 2002serguei a. mokhov, [email protected] 1 brief introduction to system calls and...

13
March 1, 2002 Serguei A. Mokhov, mokhov @cs.concordia.ca 1 Brief Introduction to Brief Introduction to System Calls and System Calls and Process Management Process Management COMP229 - System Software Edition 1.1, March 11, 2002 Slides and examples could be found at: http://alcor.concordia.ca/~sa_mokho/comp229/

Upload: raymond-stevens

Post on 30-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

1

Brief Introduction to System Brief Introduction to System Calls and Process Calls and Process

ManagementManagementCOMP229 - System Software

Edition 1.1, March 11, 2002

Slides and examples could be found at:http://alcor.concordia.ca/~sa_mokho/comp229/

Page 2: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

2

Overview

• System Call Interface

• Process Management– fork()– exec()– wait()– exit()

Page 3: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

3

System Call Interface

User-running

Kernel-running

Blocked Ready

Zombieexit()

syscall - trap

read(), write(),wait(), sleep()

App Software

Sys Software

OS

Hardware

API

Sys Call (OS) Interface

Sw-Hw I-face (drivers)

“You are here”

Page 4: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

4

The fork() System Call (1)

• A process calling fork() spawns a child process

• The child is almost an identical clone of the parent:– Text– Stack– PCB– Data

Page 5: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

5

The fork() System Call (2)

• fork() is one of the those system calls, which is called once, but returns twice!

• After fork() both the parent and the child are executing the same program.

• On error, fork() returns -1PID=28

p1

PID=28

p1

PID=34

c1

fork()

Consider a piece of program(see fork_pid.c example):...pid_t pid = fork();printf(“PID: %d\n”, pid);...

The parent will print:PID: 34

And the child will always print:PID: 0

Page 6: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

6

The fork() System Call (3)

• Remember, after fork() the execution order is not guaranteed.

• Simple fork() examples:– fork_pid.c prints out return from fork()– fork_child.c distinguishes between the parent

and the child

Page 7: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

7

The exec()System Call (1)• The exec() call replaces a current process’ image with a new one

(i.e. loads a new program within current process).• The new image is either regular executable binary file or a shell script.• There’s no a syscall under the name exec(). By exec() we usually

refer to a family of calls: – int execl(char *path, char *arg, ...);– int execv(char *path, char *argv[]);– int execle(char *path, char *arg, ..., char *envp[]);– int execve(char *path, char *argv[], char *envp[]);– int execlp(char *file, char *arg, ...);– int execvp(char *file, char *argv[]);

• Here's what c, l, e, and p mean:– l means an argument list,– v means an argument vector,– e means an environment vector, and– p means a search path.

Page 8: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

8

The exec()System Call (2)

• Upon success, exec() never returns to the caller. If it does return, it means the call failed. Typical reasons are: non-existent file or bad permissions.

• Arguments passed via exec() appear in the argv[] of the main() function.

• For more info: man -S 3 exec; Example is: exec_pid.cPID=28

p1

PID=28

p1

exec()

Old Program

New Program

Legend:

Page 9: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

9

Environment• The e exec calls use the environment when attempt to invoke a new

program.• Name = Value

– HOME– PATH– SHELL– USER– LOGNAME– ...

• set or env - will display current environment, which you can modify with:– the export command in a shell or a shell script;– getenv(), setenv(), putenv(), etc in C

Page 10: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

10

fork() and exec() Combined

• Often after doing fork() we want to load a new program into the child. E.g.: a shell.

PID=28

p1

PID=28

p1

PID=34

c1

fork()

tcsh tcsh tcsh

PID=34

c1

PID=34

c1

exec(ls)

tcsh ls

Page 11: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

11

The System wait() Call

• Forces the parent to suspend execution, i.e. wait for its children or a specific child to die (terminate is more appropriate terminology, but a bit less common).

• Example: fork_exec.c

Page 12: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

12

The exit() System Call

• This call gracefully terminates process execution. Gracefully means it does clean up and release of resources, and puts the process into the zombie state.

• By calling wait(), the parent cleans up all its zombie children.

• exit() specifies a return value from it’s program, which a parent process might want to examine as well as status of the dead process.

• _exit() call is another possibility of quick death without cleanup.

Page 13: March 1, 2002Serguei A. Mokhov, mokhov@cs.concordia.ca 1 Brief Introduction to System Calls and Process Management COMP229 - System Software Edition 1.1,

March 1, 2002 Serguei A. Mokhov, [email protected]

13

Process Overview

User-running

Kernel-running

Blocked Ready

Zombieexit()

syscall() - trap

wait()

PID=1INIT

?