lecture 27 week7

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background image of page 2
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 3 Announcements
Background image of page 3

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 4 Part 1 Advanced Libraries
Background 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)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal Software Systems 6 System() $ myprog Main() { : system(“ls”); : } Shell ls sleep
Background 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
Background image of page 7

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

View Full DocumentRight 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.
Background 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
Background image of page 9

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

View Full DocumentRight 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. */
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '09
  • GNU HTML Perl, HTML Perl Python, Joseph Vybihal Software Systems, Joseph Vybihal Software, Vybihal Software Systems

Page1 / 57

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online