fork
play

fork pid_t fork() 1. Clone current parent process to create - PowerPoint PPT Presentation

11/12/15 fork pid_t fork() 1. Clone current parent process to create identical child process, including all state (memory, registers, program counter, ). 2. Processes Continue executing


  1. 11/12/15 fork pid_t fork() 1. Clone current ¡ parent process ¡to ¡ create identical ¡ child process, including ¡all ¡state ¡(memory, ¡registers, ¡ program ¡counter, …). 2. Processes Continue ¡executing ¡both ¡copies ¡with ¡ one ¡difference: returns ¡0 to ¡the ¡ child ¡process • returns ¡child’s ¡process ¡ID ¡( pid ) to ¡the ¡ parent ¡process • pid_t pid = fork(); if (pid == 0) { Focus: printf("hello from child\n"); Process ¡model } else { Process ¡management ¡case ¡study: ¡Unix/Linux/Mac ¡OS ¡X printf("hello from parent\n"); (Windows ¡is ¡a ¡little ¡different.) } fork is ¡unique: ¡called ¡ in ¡one ¡process, ¡ returns ¡ in ¡two ¡processes! ¡ ¡ (once ¡in ¡parent, ¡once ¡in ¡child) 3 fork ¡again fork ¡example Parent ¡and ¡child ¡continue ¡from ¡ private copies ¡of ¡same ¡state. Process ¡n Memory ¡contents ¡( code , ¡globals, ¡ heap , ¡ stack , ¡etc.), pid_t pid = f ork( ); Register ¡contents, ¡ program ¡counter , ¡file ¡descriptors… if (p id = = 0) { pr intf ("he llo from chi ld\n "); 1 } els e { pr intf ("he llo from par ent\ n"); Only ¡difference: ¡return ¡value ¡from ¡ fork() Child ¡Process ¡m } Relative ¡ execution ¡order ¡of ¡parent/child ¡ after ¡ fork() undefined à m à à à 0 pid_t pid = f ork( ); pid_t pid = f ork( ); if (p id = = 0) { if (p id = = 0) { pr intf ("he llo from chi ld\n "); pr intf ("he llo from chi ld\n "); 2 void fork1() { } els e { } els e { int x = 1; pr intf ("he llo from par ent\ n"); pr intf ("he llo from par ent\ n"); } } pid_t pid = fork(); if (pid == 0) { pid_t pid = f ork( ); pid_t pid = f ork( ); 3 printf("Child has x = %d\n", ++x); if (p id = = 0) { if (p id = = 0) { } else { pr intf ("he llo from chi ld\n "); pr intf ("he llo from chi ld\n "); } els e { } els e { printf("Parent has x = %d\n", --x); pr intf ("he llo from par ent\ n"); pr intf ("he llo from par ent\ n"); } } } printf("Bye from process %d with x = %d\n", getpid(), x); hello from parent Which ¡prints ¡first? hello from child } 4 5 1

  2. 11/12/15 When ¡you ¡run ¡the ¡command ¡ ls fork-­‑exec Exec-­‑ing ¡a ¡new ¡program in ¡a ¡shell: fork-­‑exec ¡model: Stack clone ¡current ¡process fork() 1 Code/state ¡of ¡shell ¡process. replace ¡process ¡code ¡and ¡context ¡(registers, ¡memory) execv() with ¡a ¡fresh ¡program. Heap See ¡ man ¡3 ¡execv , ¡man ¡2 ¡execve Replaced ¡by ¡code/state ¡of ¡ls. Copy ¡of ¡code/state ¡ Data // Example arguments: path="/usr/bin/ls”, of ¡shell ¡process. Code: ¡/usr/bin/bash // argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULL fork() : void fork_exec(char* path, char* argv[]) { child child parent pid_t pid = fork(); Stack if (pid != 0) { Stack Stack printf("Parent: created a child %d\n”, pid); 2 2 3 exec() : } else { printf("Child: exec-ing new program now\n"); execv(path, argv); Heap Heap } Data Data Data printf("This line printed by parent only!\n"); Code: ¡/usr/bin/bash Code: ¡/usr/bin/bash Code: ¡/usr/bin/ls } Code/state ¡of ¡shell ¡process. 6 7 exit : ¡end ¡a ¡process execv : load/start ¡program Stack ¡bottom Null-­‑terminated void exit(int status) int execv(char* filename, env var strings End ¡process with ¡status: ¡0 ¡= ¡normal, ¡nonzero ¡= ¡error. char* argv[]) Null-­‑terminated atexit() registers ¡functions ¡to ¡be ¡executed ¡upon ¡exit argument ¡strings loads/starts ¡ program ¡ in ¡current ¡process: unused Executable ¡ filename envp[n] ¡== ¡NULL With ¡argument ¡list ¡ argv envp[n-­‑1] … overwrites ¡code, ¡data, ¡ and ¡stack envp[0] Keeps ¡pid, ¡open ¡files, ¡a ¡few ¡other ¡items ¡ argv[argc] ¡ == ¡ NULL does ¡not ¡return argv[argc-­‑1] … unless ¡error argv[0] Linker ¡vars envp argv argc Also ¡sets ¡up environment . ¡ ¡See ¡also: ¡execve. Stack ¡ frame ¡for ¡ main Stack ¡top 8 9 2

  3. 11/12/15 Zombies! wait for ¡child ¡processes ¡to ¡terminate Terminated ¡ process ¡still ¡consumes ¡system ¡resources pid_t waitpid(pid_t pid, int* stat, int ops ) Various ¡tables ¡maintained ¡by ¡OS Suspend ¡current ¡process ¡(i.e. ¡parent) ¡until ¡child ¡with ¡ pid ends. A ¡living ¡corpse, ¡half ¡alive ¡and ¡half ¡dead On ¡success: Reaping ¡ with wait/waitpid Return ¡ pid when ¡child ¡terminates. Parent ¡ waits to ¡reap ¡child ¡once ¡child ¡terminates Reap ¡child. Parent ¡receives ¡child ¡exit ¡status. If ¡ stat != NULL , ¡ waitpid saves ¡termination ¡ reason ¡ Kernel ¡discards ¡process. where ¡ it ¡points. What ¡if ¡parent ¡doesn’t ¡reap? See ¡ also: ¡ man ¡3 ¡waitpid If ¡any ¡parent ¡terminates ¡without ¡reaping ¡a ¡child, ¡then ¡child ¡will ¡be ¡reaped ¡ by ¡ init process ¡(pid ¡== ¡1) But ¡in ¡long-­‑running ¡processes ¡we ¡need ¡ explicit reaping e.g., ¡shells ¡and ¡servers 10 11 HCBye waitpid example Error-­‑checking CTBye Check ¡return ¡results ¡of ¡system ¡calls ¡for ¡errors! ¡ (No ¡exceptions.) void fork_wait() { Read ¡documentation ¡ for ¡return ¡values. int child_status; Use ¡perror to ¡report ¡error, ¡ then ¡exit. pid_t child_pid == fork(); if (child_pid == 0) { void perror(char* message) printf("HC: hello from child\n"); Print ¡"message: ¡ reason ¡that ¡last ¡system ¡call ¡failed." } else { if (-1 == waitpid(child_pid, &child_status, 0) { perror("waitpid"); exit(1); } printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); } 12 3

  4. 11/12/15 Examining ¡Processes ¡on ¡Linux ¡(demo) Summary ps Processes System ¡has ¡multiple ¡active ¡processes pstree Each ¡process ¡appears ¡to ¡have ¡total ¡control ¡of ¡the ¡processor. top OS ¡periodically ¡“context ¡switches” ¡between ¡active ¡processes /proc Implemented ¡using ¡ exceptional ¡control ¡flow Process ¡management fork, ¡execv, ¡waitpid 14 16 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend