09 - Processes (2)OutlineProcess CreationProcess StateProcess State TransitionsAnnouncementsReading: MOS 2.1Process CreationSystem initialization (init)UNIXUNIX forkUNIX execWindows CreateProcessPollEvPollEvFrom existing process (syscall)Windows11 2 3 5 1 3 2 3 42 3 1 2 3 5 1 3 41 2 2 3 5 3 1 4 3all of them234Program logicUser interactionDuplicate the current process (fork)Duplicate current process withreturns in both processes (twice):Both processes continue executing from after thefork syscall instruction.Each has a different PID number.Creating a clone:Different behaviors:Parent and children processes form a process treeIf process exits before its children, childrenare adopted by a system process like initDifferent PIDsparent: fork returns child's PIDincluding global variablesPC, SP, registers all separatechild: fork returns 0Separate copies of address space/memorySeparate CPU execution contextwipes the current address space and loadsthe new programExecuting in CPUchild begins execution from the new program'smain function (or entry point)shared program text (read-only)shared memory with copy-on-writeReady to execute, waiting for CPUWaiting for event (I/O)Parent process:Child process returns with 0Parent process returns with child's PIDcallscreated byChild process:Scheduled batch job (mainframe)Load another program (exec)Create a new process and immediatelyload target program (CreateProcess)When are processes created?How are processes created?2 main approaches:Process StateLate penalties will be applied on canvas.PollEverywhere today: pollev.com/rtsang11)1)1)2)2)A)A)B)C)D)B)C)D)----------------Runningblocked -> running:ready -> blocked:no event + unfairnot running, can't syscall---ReadyBlocked/Waiting-2)fork()fork()fork()fork()parentoutput:pid 100: hellopid 100: worldpid 101: worldoutput:childLinux:Windows:Parent and Child have:Loads another program in current processRepresents what a process is currently doing.3 basic states (OS-dependent)There are caveats about how system resourcesbehave when forked in some UNIX versions:Consider the following program:What does this program do?CreateProcess runs new program from the start.How many processes will executing this programcreate?Which of the following is a possible output of thisprogram?orProcess ExplorerExample output (partial):ps -elf --forestpslist.exe -t PID PPID CMD 1 0 /sbin/init1516 1 \_ /usr/lib/systemd/systemd --user2503 1516 \_ /usr/libexec/gnome-terminal-server2528 2503 \_ bash2533 2528 \_ ps -elf --forestroot[kthreadd]initsystemdlogindnetworkdparentcontextparentparent resumeschildcontextchildcontextmemorymemorymemoryregistersregistersregistersdemo2demo2xcalcforkexecpid = fork()exec()exit()wait()RunningBlockedReadychild(pid == 0)parent(pid > 0)012infiniteA)B)C)D)How many processes in the running state can therebe simultaneously on a single-CPU system?Missing transitions:Many processes live in blocked, waiting for input.waitingfor eventswitched byschedulereventoccursselected byschedulerbased on MOS Figure 2-2