Contents

  1. .
  2. ./dueling_processes.c
  3. ./exec_example.c
  4. ./fork_problem.c
  5. ./index.html
  6. ./sixforks.c

. 1/6

[
top][prev][next]


./dueling_processes.c 2/6

[
top][prev][next]
// Another example using fork
// Look at how the process ids change

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h>
#include<sys/wait.h> 

int main() {
    int pid= fork();
    int i;
    int status;

    if (pid != 0 ) {
        for(i=0; i<10; i++) {
            printf("Parent process %d running.\n", getpid());
            sleep(1);
        }
        wait(&status);
    }
    else {
        for(i=0; i < 10; i++) {
            printf("Child process %d running.\n", getpid());
            sleep(1);
        }
    }
}

./exec_example.c 3/6

[
top][prev][next]
// Example using exec

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h>
#include<sys/wait.h> 

int main() {
    int pid = fork();
    int status;
    if (pid != 0) {
        wait(&status); 
        printf("Parent's child's status is %d\n", status);
    } else { // in the child
        printf("Child: My process id is %d\n", getpid() );
        printf("Child: My parent process id is %d\n", getppid());
        char *args[]={"ps",NULL}; 
        execvp(args[0],args); 
        printf("Did I get here?\n");
    }
}

./fork_problem.c 4/6

[
top][prev][next]
// Example using fork
// What will this program output?
// What order will the output be in?  (Do we necessarily know?)

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h>
#include<sys/wait.h> 

int main() {
    int x = 27;
    int pid = fork();
    int status;
    if (pid != 0) {
        printf("Parent's x before wait is %d\n",x);
        x = x + 5;
        wait(&status); 
        printf("Parent's x after wait is %d\n",x);
        printf("Parent's child's status is %d\n", status);
    } else {
        printf("Child's x before sleep is %d\n",x);
        sleep(5);
        x = x + 10;
        printf("Child's x after sleep is %d\n",x);
    }
}

./index.html 5/6

[
top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/09-processes</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/themes/spacelab.min.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/course.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/syntax.css" />
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/css/style.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/09-processes</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//dueling_processes.c">dueling_processes.c</a></li>
<li><a href=".//exec_example.c">exec_example.c</a></li>
<li><a href=".//fork_problem.c">fork_problem.c</a></li>
<li><a href=".//sixforks.c">sixforks.c</a></li>
</ul>
</body>

./sixforks.c 6/6

[
top][prev][next]
// How many processes will this generate?

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h>
#include<sys/wait.h> 

int main() {
    int status;
    fork();
    fork();
    fork();
    fork();
    fork();
    fork();
    printf("Process %d exiting.\n", getpid());
    wait(&status);
}

Generated by GNU Enscript 1.6.6.