Contents
- .
- ./forkpuzzle2.c
- ./forkpuzzle.c
- ./index.html
- ./sixforks.c
. 1/5
[top][prev][next]
./forkpuzzle2.c 2/5
[top][prev][next]
/*
* Fork Puzzle2
*/
#include<stdio.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(.5);
}
wait(&status);
printf("Parent done %d\n", status);
}
else {
for(i=0; i < 10; i++) {
printf("Child process %d running.\n", getpid());
sleep(.5);
}
printf("Child done\n");
}
}
./forkpuzzle.c 3/5
[top][prev][next]
/*
* Fork Puzzle.
* Guarantees in the output: Parent's x after wait is last.
* No other guarantees in output between Parent and Child
*/
#include<stdio.h>
int main() {
int x = 27;
int pid = fork();
if (pid != 0) {
printf("Parent's x before wait is %d\n",x);
x = x + 5;
wait(NULL);
printf("Parent's x after wait is %d\n",x);
} else {
printf("Child's x before sleep is %d\n",x);
sleep(1);
x = x + 10;
printf("Child's x after sleep is %d\n",x);
}
}
./index.html 4/5
[top][prev][next]
<html>
<head><title>Examples for /home/faculty/sprenkle/public_html/cs330/examples/11-processes</title>
<link rel="stylesheet" type="text/css" href="http://www.cs.wlu.edu/~sprenkle/cs330/assignments/assign.css" />
</head>
<body>
<h1>Examples for /home/faculty/sprenkle/public_html/cs330/examples/11-processes</h1>
<ul>
<li><a href=".//code.html">All IN ONE FILE (pretty syntax)</a>
<li><a href=".//forkpuzzle.c">forkpuzzle.c</a></li>
<li><a href=".//forkpuzzle2.c">forkpuzzle2.c</a></li>
<li><a href=".//sixforks.c">sixforks.c</a></li>
</ul>
</body>
./sixforks.c 5/5
[top][prev][next]
/*
* Number of processes created: 2^{number of forks}
*/
#include<stdio.h>
int main(int argc, char* argv) {
fork();
fork();
fork();
fork();
fork();
fork();
printf("Process %d exiting from %d.\n", getpid(), getppid());
}
Generated by GNU Enscript 1.6.6.