CS241 Systems ProgrammingToday's TopicAbout SMP0About SMP0 (contd.)Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10SMP1Processesfork() creates a new process:Differences between parent and childfork() Example 1fork() example 2Example 2 cont’dSlide 18Orphans and ZombiesSlide 20Solution: wait(…)wait cont’dwait(...) macrosExample 3: waitexecexec variationsexec variations: passing parametersexec variations: command pathexec variations (cont’d)fork + execExample 4: execCS241 Systems ProgrammingDiscussion Section Week 2Original slides by: Stephen KloderToday's TopicSMP0 problemsTopics needed for SMP1ProcessForkWaitExecAbout SMP0Part 2, Task 5overlay_overlay(char *s1, char *s2, char *s3, char *s4)Part 3triple_three_step_step_step(char *first, char *second, char *third)the_end(void *orange, void *blue)About SMP0 (contd.)overlay_overlay(char *s1, char *s2, char *s3, char *s4)Find the overlay between s1 and s2 = o1Find the overlay between s3 and s4 = o2Find the overlay between o1 and o2 = resultHow to find the overlay between “father” and “mother”?Possible approach: generate all possible combination of “mother”strstr() with “father” to find the largest matchAbout SMP0 (contd.)void triple_three_step_step_step(char *first, char *second, char *third) {if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 &&third[3] == second[2]+8 && second[2] == first[1]+8)printf("10: Illinois\n");}first second thirdAbout SMP0 (contd.)void triple_three_step_step_step(char *first, char *second, char *third) {if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 &&third[3] == second[2]+8 && second[2] == first[1]+8)printf("10: Illinois\n");}0 4first second third8About SMP0 (contd.)void triple_three_step_step_step(char *first, char *second, char *third) {if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 &&third[3] == second[2]+8 && second[2] == first[1]+8)printf("10: Illinois\n");}0 4first second third8first[1]second[2]third[3]About SMP0 (contd.)void triple_three_step_step_step(char *first, char *second, char *third) {if (third == second+2 && second == first+2 && *third == *second+4 && *second == *first+4 &&third[3] == second[2]+8 && second[2] == first[1]+8)printf("10: Illinois\n");}00 4first second third8first[1]second[2]third[3]16About SMP0 (contd.)void the_end(void *orange, void *blue){if (orange != NULL && orange == blue && ((char *)blue)[0] == 1 && *((int *)orange) % 3 == 0)printf("12: Illinois\n");}1blue[0]orangeblue[1]blue[2]blue[3]About SMP0 (contd.)void the_end(void *orange, void *blue){if (orange != NULL && orange == blue && ((char *)blue)[0] == 1 && *((int *)orange) % 3 == 0)printf("12: Illinois\n");}1 001blue[0]orangeblue[1]blue[2]blue[3]*orange = ?(00000001 00000001)=257SMP1Simple Unix shellNon built-in commandsBuilt-in commandsChange DirectoryTerminationHistoryError handlingConcepts needed: process, fork, exec, waitProcessesA process is an instance of a running programA process contains:Instructions (i.e. the program)Resources (variables, buffers, links, etc.)State (identity, ready/running/locked, etc.)Processes can create other processesfork() creates a new process:Process Creation with fork()envstackfreeheapstaticcodeThe new (child) process is identical to the old (parent) process, except…envstackfreeheapstaticcodefork()createsParent ChildDifferences between parent and childProcess ID (getpid())Parent ID (getppid())Return value of fork()In parent, fork() returns child pidIn child, fork() returns 0fork() Example 1What does this do?fprintf(stdout,”%d\n”,fork());Try it!fork() example 2 #include <stdio.h>#include <sys/types.h>#include <unistd.h>int main(int argc, char** argv) {pid_t child_pid = fork();if (child_pid < 0) { // error codeperror(“Fork Failed”);return –1;} fprintf(stdout, “I am process %d\n”,getpid());if (child_pid == 0) { // child codefprintf(stdout,”I’m the child process.\n”);} else { // parent codefprintf(stdout,”I’m the parent of child process %d.\n”, child_pid);}return 0;}Example 2 cont’dThis exits too quickly; let’s slow it down:if (child_pid == 0) { // child codesleep(15);fprintf(stdout,”I’m the child process.\n”);} else { // parent codesleep(20);fprintf(stdout,”I’m the parent of child process %d.\n”, child_pid);}Example 2 cont’dIn a second window, run ps –a, and look for the pids from the program output.Periodically run ps –a again, as the program in the first window executes.What happens when the program runs?What happens when the child finishes?What happens when the parent finishes?What happens when you switch the parent and child sleep statements?Orphans and ZombiesWhen a process finishes, it becomes a z ombie until its parent cleans up after it.If its parent finishes first, the process becomes an or phan, and the init process (id 1) adopts it.How can a parent know when its children are done?Solution: wait(…) wait() allows a parent to wait for its child process, and save its return valuepid= wait(&status , options);pid= waitpid(pid , &status ,options);wait() waits for any child; waitpid() waits for a specific child.wait cont’dwait() blocks until child finisheswait() does not block if the option WNOHANG is included. When would we want to use this?The child’s return value is stored in *statuswait(...) macrosWIFEXITED(status) is true iff the process terminated normally.WEXITSTATUS(status) gives the last 8 bits of the process’s return value (assuming normal exit)Example 3: wait#include <sys/wait.h>……// add this to parent codeif (waitpid(child_pid, &result, 0) == -1) {perror(”Wait failed”);return -1;}if( WIFEXITED(result)) {fprintf(stdout, ”child %d returned %d\n”, child_pid, WEXITSTATUS(result));execexec replaces the current process image(code, variables, etc.) with that of a new program:envstackfreeheapstaticcodeenv*NewProgramexec* The program may choose to change the environmentBefore Afterexec variationsThere are 6 different ways of calling exec. Which one to use depends on three conditions:1. How arguments are passed2. How the path is specified3. Whether a new environment is usedexec variations: passing parametersexec can have parameters passed to it two different ways:List of parameters:execl(“/usr/bin/ls”, ”ls”, ”-l”, NULL);Argument Vector (like
View Full Document