Lecture 5 Process Creation 1 Review Linux process

  • Slides: 21
Download presentation
Lecture 5: Process Creation 1

Lecture 5: Process Creation 1

Review: Linux process memory layout • Stack: – Automatically allocated and deallocated – Local

Review: Linux process memory layout • Stack: – Automatically allocated and deallocated – Local variables, parameters • Heap: – Manually allocated (malloc) and deallocated (free) – Dynamic sized data structures • Static data – Global variables • Text – Program 2

In this lecture • Process context • Process creation 3

In this lecture • Process context • Process creation 3

Process context: registers Stack pointer (SP) Stack Heap Static data Program counter (PC) Text

Process context: registers Stack pointer (SP) Stack Heap Static data Program counter (PC) Text (program) 4

Process context: file management • Root directory • Working directory • Opened files 5

Process context: file management • Root directory • Working directory • Opened files 5

Process context: others • Process id • Parent process • … 6

Process context: others • Process id • Parent process • … 6

Process creation: fork() system call #include <unistd. h> pid_t fork(void); //prototype • fork() returns

Process creation: fork() system call #include <unistd. h> pid_t fork(void); //prototype • fork() returns a process id (a small integer) • fork() returns twice! – – In the parent – fork returns the id of the child process In the child – fork returns a 0 7

Parent and child processes • The child process is a copy of the parent

Parent and child processes • The child process is a copy of the parent process – Same core image – Same context (except process id) • Registers • Files • Others 8

Creating a process: before fork() Stack Heap Static data PC Text (program) Fork() parent

Creating a process: before fork() Stack Heap Static data PC Text (program) Fork() parent process 9

Creating a process: after fork() PC Stack Heap Static data Text (program) fork() //return

Creating a process: after fork() PC Stack Heap Static data Text (program) fork() //return p Text (program) fork() //return 0 parent process child process id = p PC 10

Example #include <unistd. h> #include <stdio. h> void main(void) { pid_t pid = fork();

Example #include <unistd. h> #include <stdio. h> void main(void) { pid_t pid = fork(); if (pid > 0) printf(“I am the parentn”); else if (pid == 0) printf(“I am the childn”); else printf(“ERROR!n”); } 11

Process hierarchies • UNIX: Parent creates a child process, child can create more processes

Process hierarchies • UNIX: Parent creates a child process, child can create more processes – Forms a hierarchy – UNIX calls this a "process group” • All processes within a group are logically related • Windows has no concept of process hierarchy – all processes are created equal 12

Death and destruction • All processes usually end at some time during runtime (with

Death and destruction • All processes usually end at some time during runtime (with the exception of init) • Processes may end either by: – executing a return from the main function – calling the exit(int) function – calling the abort(void) function • When a process exits, the OS delivers a termination status to the parent process. 13

Waiting • Parent processes often wait for their child processes to end • Parent

Waiting • Parent processes often wait for their child processes to end • Parent processes do that via a wait() call – pid_t wait(int * status); – pid_t waitpid( pid_t pid, int * status, …); 14

Switching programs • fork() creates a new process • This would be almost useless

Switching programs • fork() creates a new process • This would be almost useless if there was not a way to switch which program is associated with the new process • The exec() system call is used to load a new program into an existing process 15

exec(): Loading a new image Stack prog’s stack Heap prog’s heap Static data prog’s

exec(): Loading a new image Stack prog’s stack Heap prog’s heap Static data prog’s static data Text (program) prog’s Text exec(prog, args) before after 16

exec() example: #include <unistd. h> main() { printf(“executing lsn”); execl(“/bin/ls”, “l”, (char*)0); exit(1); }

exec() example: #include <unistd. h> main() { printf(“executing lsn”); execl(“/bin/ls”, “l”, (char*)0); exit(1); } 17

A stripped-down shell while (TRUE) { type_prompt( ); read_command (command, parameters) if (fork() !=

A stripped-down shell while (TRUE) { type_prompt( ); read_command (command, parameters) if (fork() != 0) { /* Parent code */ waitpid( -1, &status, 0); } else { /* Child code */ execve (command, parameters, 0); /* repeat forever */ /* display prompt */ /* input from terminal */ /* fork off child process */ /* wait for child to exit */ /* execute command */ } 18

More examples • How many processes does this piece of code create? int main()

More examples • How many processes does this piece of code create? int main() { fork(); } 19

Bad example (don’t try this!) #include <unistd. h> #include <stdio. h> fork bomb! void

Bad example (don’t try this!) #include <unistd. h> #include <stdio. h> fork bomb! void main(void) { while (!fork()) printf("I am the child %dn“, getpid()); printf("I am the parent %dn“, getpid()); } 20

What is the output of this? int main() { int i; for (i=0; i<2;

What is the output of this? int main() { int i; for (i=0; i<2; i++) { fork(); printf(“%dn”, i); } return (0); } 21