Lecture11-intro-to-processes-part3

Lecture11-intro-to-processes-part3 - Announcements CS 3214...

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

View Full Document Right Arrow Icon
1 CS 3214 Computer Systems Godmar Back Lecture 11 Announcements • Exercise 4 due Sep 30 • Project 3 due Oct 14 CS 3214 Fall 2011 THREADS AND PROCESSES Part 3 CS 3214 Fall 2011 fork() #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(int ac, char *av[]) { pid_t child = fork(); if (child < 0) perror(“fork”), exit(-1); if (child != 0) { printf ("I'm the parent %d, my child is %d\n", CS 3214 Fall 2011 getpid(), child); wait(NULL); /* wait for child (“join”) */ } else { printf ("I'm the child %d, my parent is %d\n", getpid(), getppid()); execl("/bin/echo", "echo", "Hello, World", NULL); } } fork() vs. exec() • fork(): – Clone most state of parent, including memory – Inherit some state, e.g. file descriptors – Keeps program, changes process – Called once, returns twice • exec(): – Overlays current process with new executable – Keeps process, changes program – Called once, does not return (if successful) CS 3214 Fall 2011 exit(3) vs. _exit(2) • exit(3) destroys current processes • OS will free resources associated with it – E.g., closes file descriptors, etc. etc. • Can have atexit() handlers – _exit(2) skips them • Exit status is stored and can be retrieved by parent – Single integer – Convention: exit(EXIT_SUCCESS) signals successful execution, where EXIT_SUCCESS is 0 CS 3214 Fall 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 wait() vs waitpid() • int wait(int *status) – Blocks until any child exits – If status != NULL, will contain value child passed to exit() – Return value is the child pid – Can also tell if child was abnormally terminated • int waitpid(pid_t pid, int *status, int options) – Can say which child to wait for CS 3214 Fall 2011 If multiple children completed, will take in arbitrary order
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

Page1 / 4

Lecture11-intro-to-processes-part3 - Announcements CS 3214...

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

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