DOC PREVIEW
U of I CS 241 - Systems Programming

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 TopicSMP0 problemsTopics needed for SMP1ProcessForkWaitExecAbout SMP0Part 2, Task 5overlay_overlay(char *s1, char *s2, char *s3, char *s4)Part 3triple_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 = o1Find the overlay between s3 and s4 = o2Find the overlay between o1 and o2 = resultHow 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)=257SMP1Simple Unix shellNon built-in commandsBuilt-in commandsChange DirectoryTerminationHistoryError handlingConcepts 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()envstackfreeheapstaticcodeThe 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

U of I CS 241 - Systems Programming

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Systems Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Systems Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Systems Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?