{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture 27 week7

lecture 27 week7 - Unix C GNU HTML Perl Python Teacher's...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 1 Teacher's Notes
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 2 Advanced C Comp-206 : Introduction to Software Systems Week 7 Joseph Vybihal Computer Science McGill University
Image of page 2
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 3 Announcements
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 4 Part 1 Advanced Libraries
Image of page 4
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 5 Invoking Programs int system(STRING); Create a child process and then wait for completion of child’s execution int fork(void); Concurrent execution of parent and child processes #include <sys/types.h> #include <unistd.h> wait(pid)
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 6 System() $ myprog Main() { : system(“ls”); : } Shell ls sleep
Image of page 6
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 7 fork() $ myprog Main() { int result; : result = fork(); : if (result == 0) { do child stuff } else { do parent stuff wait(pid); } : } original Main() { int result; : result = fork(); : if (result == 0) { do child stuff } else { do parent stuff wait(pid); } : } copy parent child concurrent Result = 0 Result = pid
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 8 About fork() The fork() system call will spawn a new child process which is an identical process to the parent except that it has a new system process ID number. The process is copied in memory from the parent and a new process structure is assigned by the kernel. The return value of the function is what discriminates the two threads of execution. A zero is returned by the fork function in the child's process. The environment, resource limits, umask, controlling terminal, current working directory, root directory, signal masks and other process resources are also duplicated from the parent in the forked child process.
Image of page 8
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 9 GNU Fork() Example #include <stddef.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> /* Execute the command using this shell program. */ #define SHELL "/bin/sh" int my_system (const char *command) { int status=0; pid_t pid; pid = fork (); if (pid == 0) { /* This is the child process. Execute the shell command. */ execl (SHELL, SHELL, "-c", command, NULL); exit (EXIT_FAILURE); } else if (pid < 0) /* The fork failed. Report failure. */ status = -1; else /* This is the parent process. Wait for the child to complete. */ if (waitpid (pid, &status, 0) != pid) status = -1; return status; } replace
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 10 Example 2 void main(){ int fork_return; int count = 0; printf("Process %d about to fork a child.\n", getpid() ); /* getpid() returns the process id of this process. */ fork_return = fork(); if( fork_return < 0) { printf("Unable to create child process, exiting.\n"); exit(-1); } /* BOTH processes will do this! */ system("ps"); if( fork_return > 0) { /*Then fork_return is the pid of the child process and I am the parent. Start printing a's.*/ printf("Created child process %d.\n", fork_return); while( count++ < 800) { putchar('a');
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}