TOS Arno Puder 1 Objectives Explain how to create new processes - - PowerPoint PPT Presentation

tos arno puder
SMART_READER_LITE
LIVE PREVIEW

TOS Arno Puder 1 Objectives Explain how to create new processes - - PowerPoint PPT Presentation

TOS Arno Puder 1 Objectives Explain how to create new processes under Unix Explain how fork() could be implemented under TOS 2 Review: create_process() New processes are created in TOS using create_process() create_process()


slide-1
SLIDE 1

1

TOS Arno Puder

slide-2
SLIDE 2

2

Objectives

  • Explain how to create new processes

under Unix

  • Explain how fork() could be

implemented under TOS

slide-3
SLIDE 3

3

Review: create_process()

  • New processes are created in TOS using

create_process()

  • create_process() does:

– Allocate a free PCB entry – Initialize the PCB entry – Setup the initial stack frame

  • The entry point of a TOS process is

defined via a function pointer

slide-4
SLIDE 4

4

Example: create_process()

void test_process (PROCESS self, PARAM param) { // assert (self->name == “Test process”) // assert (param == 42) } void kernel_main () { // … create_process (test_process, 5, 42, "Test process"); }

slide-5
SLIDE 5

5

Stack of the new process

1 MB TOS code 640 KB

{

30 K stack frame for process 0 30 K stack frame for process 1 self func PROCESS actual parameter Return address (dummy argument) Address of new process (EIP) EAX ECX EDX EBX EBP ESI EDI Address ∞ Address ∞ will be saved in PCB.esp param PARAM actual parameter

{

slide-6
SLIDE 6

6

Overview of fork()

  • In Unix, a new process is created via a call

to fork().

  • fork() creates an exact copy of the

calling process.

  • Exact copy of:

– Program code – Heap – Stack

slide-7
SLIDE 7

7

fork() man page

NAME fork - create a child process SYNOPSIS #include <sys/types.h> #include <unistd.h> pid_t fork(void); DESCRIPTION fork creates a child process that differs from the parent process only in its PID and PPID. RETURN VALUE On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and errno will be set appropriately. Process ID Parent Process ID

slide-8
SLIDE 8

8

fork() – Example under Unix

#include <iostream> #include <unistd.h> void main() { int x = 42; if (fork() != 0) cout << "Parent process. x=" << x << endl; else cout << "Child process. x=" << x << endl; } Parent process. x=42 Child process. x=42 Output:

slide-9
SLIDE 9

9

Before calling fork()

void f (int x) { int z; fork(); } void g() { char a; int i; f (42); } void kernel_main () { g(); } RET addr 1 RET addr 2 Before fork()

RET addr 1 a i 42 RET addr 2 z

%ESP

Unsued Stack Stack for Boot Process

slide-10
SLIDE 10

10

After calling fork()

void f (int x) { int z; fork(); } void g() { char a; int i; f (42); } void kernel_main () { g(); } RET addr 1 RET addr 2 Location 3

RET addr 1 a i 42 RET addr 2 z

%ESP

Stack for new Process

pcb[i].esp

RET addr 1 a i 42 RET addr 2 z 512 (%EFLAGS) 8 (%CS) Location 3 EAX ECX EDX EBX EBP ESI EDI Stack for Boot Process

slide-11
SLIDE 11

11

Loading Processes

  • If fork() only creates a copy of the

parent process, how can different programs be run under Unix?

  • Solution: execve() loads a new program.

– A call to execve() loads a new program into the running process. – A call to execve() therefore never returns (unless there is an error).

slide-12
SLIDE 12

12

fork/wait/execve Example

#include <iostream> #include <unistd.h> #include <sys/wait.h> void main() { int x = 42; if (fork() != 0) { int status; cout << "Parent process. x=" << x << endl; wait (&status); } else { cout << "Child process. x=" << x << endl; char* args[] = {"ls", NULL}; char* env[] = {NULL}; execve ("/bin/ls", args, env); cout << "Never reached" << endl; } }