0% found this document useful (0 votes)
6 views

lecture_slides_08_082-processes-creating

The document discusses processes in computing, specifically focusing on the fork-exec model used to create new processes. It explains how the fork() system call generates a child process that is a copy of the parent process, and the execve() system call replaces the child's code with a different program. Additionally, it highlights the differences in process creation between Linux and Windows, along with other relevant system calls for process management.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lecture_slides_08_082-processes-creating

The document discusses processes in computing, specifically focusing on the fork-exec model used to create new processes. It explains how the fork() system call generates a child process that is a copy of the parent process, and the execve() system call replaces the child's code with a different program. Additionally, it highlights the differences in process creation between Linux and Windows, along with other relevant system calls for process management.

Uploaded by

yihuangece
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

University

 of  Washington  

Sec3on  8:  Processes  


¢ What  is  a  process  
¢ Crea3ng  processes  
¢ Fork-­‐Exec  

Processes  
University  of  Washington  

Crea3ng  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  crea:on  in  Windows  is  slightly  different  from  Linux’s  fork-­‐
exec  model  

¢ Other  system  calls  for  process  management:  


§ getpid()
§ exit()
§ wait()  /  waitpid()

Processes  
University  of  Washington  

fork:  Crea3ng  New  Processes  


¢ pid_t fork(void)  
§ creates  a  new  process  (child  process)  that  is  iden:cal  to  the  calling  
process  (parent  process)  
§ returns  0  to  the  child  process  
§ returns  child’s  process  ID  (pid)  to  the  parent  process  
pid_t pid = fork();
if (pid == 0) {
printf("hello from child\n");
} else {
printf("hello from parent\n");
}

¢ fork  is  unique  (and  oHen  confusing)  because  it  is  called  once  
but  returns  twice  

Processes  
University  of  Washington  

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

pid_t pid = fork(); pid_t pid = fork();


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

pid_t pid = fork(); pid_t pid = fork();


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

hello from parent Which  one  is  first?   hello from child
Processes  
University  of  Washington  

Fork  Example  
¢ Parent  and  child  both  run  the  same  code  
§ Dis:nguish  parent  from  child  by  return  value  from  fork()
§ Which  runs  first  aIer  the  fork()  is  undefined
¢ Start  with  same  state,  but  each  has  a  private  copy  
§ Same  variables,  same  call  stack,  same  file  descriptors…  
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);
}

Processes  

You might also like