week7 - Unix C + GNU HTML Perl Python A Advanced C Comp-206...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 1 Advanced C A Comp-206 : Introduction to Software Systems Week 7 Joseph Vybihal Computer Science McGill University
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 2007 2 Announcements
Background image of page 2
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 3 Part 1 Advanced Libraries
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 2007 4 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 4
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 5 System() $ myprog Main() { : system(“ls”); : } Shell ls sleep
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 2007 6 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 6
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 7 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 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 2007 8 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 8
Unix C + GNU HTML Perl Python COMP 206 - Joseph Vybihal 2007 9 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.*/
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 55

week7 - Unix C + GNU HTML Perl Python A Advanced C Comp-206...

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

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