Washington fork creajng new processes pidt forkvoid

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: xecve() 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 25 University of Washington fork: CreaJng 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 ojen confusing) because it is called once but returns twice Processes 26 University of Washington Understanding fork Process n Child Process m 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"); } pid_t pid = fork(); if (pid == 0) { pid = m printf("hello from child\n"); } else { printf("hello from parent\n"); } pid_t pid = fork(); if (pid == 0) { 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"); } pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } hello from parent Which one is first? Processes hello from child 27 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 a]er 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 28 University of Washington Processes ¢  ¢  ¢  What is a process CreaJng processes Fork-­‐Exec Processes 29 University of Washington Fork-­‐Exec ¢  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 §  There is a whole family of exec calls – see exec(3) and execve(2) // Example arguments: path="/usr/bin/ls”, // argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULL void fork_exec(char *path, char *argv) { pid_t pid = fork(); if (pid != 0) { printf("Parent: created a child %d\n”, pid); } else { printf("Child: exec-ing new program now\n"); execv(path, argv); } printf("This line printed by parent only!\n"); } Processes 30 University of Washington Exec-­‐ing a new program Stack Very high...
View Full Document

Ask a homework question - tutors are online