Class Notes (835,137)
Canada (508,956)
CSC209H1 (41)
Karen Reid (20)
Lecture

mar08c.docx

1 Page
129 Views
Unlock Document

Department
Computer Science
Course
CSC209H1
Professor
Karen Reid
Semester
Winter

Description
 Produce/consumer/dup2 moved to mar06c  Exec  ex. build a simple shell showexec.c o The exec system call replaces the program being run by a process bynclude a different one; i.e. replace a process's memory contents 2. #include 3. #include  The new program starts executing from the beginning 4. #include o On success, exec never returns, on failure, exec returns -1 5. #include 6. #include 7. 8. /* Read command arguments from stdin and return them 9. * as an array of strings where the last element is NULL 10. */ 11. char **read_args() { o Ex. Program X Program Y 12. char *buf = malloc(128); int i = 5; printf("hello\n", i); 13. char **args = malloc(sizeof(char *)*4); printf("%d\n", i); 14. exec("Y"); 15. fprintf(stderr, "Enter a command: "); printf("%d\n", i); // reach here = something wrong 16. fgets(buf, 128, stdin); 17. buf[strlen(buf)-1] = '\0'; // get rid of '\n' char  Expected ouput: Failure output: 18. 5 5 hello 5 19. int i = 0; 20. args[i] = strtok(buf, " "); // tokenize the input o After exec new process inherits from calling process: 21. while (args[i] != NULL) { 22. i++;  PID, PPID, Working Directory, Open files 23. args[i] = strtok(NULL, " ");  Pending signals and pending alarms 24. }  Just different instructions 25. 26. i = 0; o 6 versions of exec 27. while (args[i] != NULL) { // print the args 28. printf("args[%d] = %s\n", i, args[i]); execl(char *path, char *arg0, …, (char *)NULL); execv(char *path, char *argv[]); 29. i++;  suffix: v for vector, l for list 30. } 31. return args;  ways to pass in arguments for the executable 32. }  l – list out all the arguments 33.  v – pass in an array of arguments 34. int main() { 35. int r;  Last element of the argv array has to be NULL to indicate36.e char **args = read_args(); end for both l or v options 37. 38. if ((r = fork()) < 0) {  path is the absolut
More Less

Related notes for CSC209H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit