03 Creating New Processes

background image

University of Washington

Section 8: Processes

What is a process

Creating processes

Fork-Exec

Processes

background image

University of Washington

Creating New Processes &

Programs

fork-exec model:

fork() creates a copy of the current process

execve() replaces the current process’ code & address
space with the code for a different program

fork() and execve() are system calls

Note: process creation in Windows is slightly different
from Linux’s fork-exec model

Other system calls for process management:

getpid()

exit()

wait() / waitpid()

Processes

background image

University of Washington

fork: Creating New Processes

pid_t fork(void)

creates a new process (child process) that is identical to
the calling process (parent process)

returns 0 to the child process

returns child’s process ID (pid) to the parent process

fork is unique (and often confusing) because
it is called

once

but returns

twice

Processes

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

background image

University of Washington

Understanding fork

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

Process n

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

Child Process m

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid = m

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid = 0

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

hello from parent

hello from child

Which one is first?

Processes

background image

University of Washington

Fork Example

Parent and child both run the same code

Distinguish parent from child by return value from fork()

Which runs first after the fork() is undefined

Start with same state, but each has a private
copy

Same variables, same call stack, same file descriptors…

Processes

void fork1()
{
int x = 1;
pid_t pid = fork();
if (pid == 0) {

printf("Child has x = %d\n", ++x);

} else {

printf("Parent has x = %d\n", --x);

}
printf("Bye from process %d with x = %d\n", getpid(), x);
}


Document Outline


Wyszukiwarka

Podobne podstrony:
03 Creating New Processes
03 ulotka new age
03 Skutki malpiego procesu (2007)
03-Funkcja planowania w procesie zarzdzania, materiaynaegzaminzpodst zarzdzaniaprzykadowytest
03 17 odpady z procesów wytwarzania dwutlenku tytanu
03 ulotka new age
2004 03 18 Ewolucja procesow zarzadzania Srodowiskowy kontekst zarzadzania
2019 03 19 Rusza proces Elżbiety P , aktywistki Strajku Kobiet Do Rzeczy
2 Prefixes creating new meanings
03 Skutki malpiego procesu (2007)
05 DFC 4 1 Sequence and Interation of Key QMS Processes Rev 3 1 03
Creative Writing New York Times Essay Collection Writers On Writing
03 modelowanie procesu
03 1 2 Pełnomocnictwo procesowe szczególne
03 Przebieg procesu technologicznego i kwas mlekowy
Zarządzanie systemami i przedsiębiorstwami- WYKŁAD ZARZĄDZANIE PROCESAMI 08.03.2010., zimar
Identyfikacja Procesów Technologicznych 03.Obiekt oscylacyjny
WSEIZ STATYSTYKA W 03 new kopia
03 1 Proces

więcej podobnych podstron