|
CS 456 - Systems Programming
Spring 2024
|
Displaying ./code/feb08/fork3.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv){
pid_t id = fork();
if (id == -1) exit(1); // fork failed
if (id > 0)
{
printf("I am the parent: pid = %d \n", id);
// I'm the original parent and
// I just created a child process with id 'id'
// Use waitpid to wait for the child to finish
} else { // returned zero
// I must be the newly made child process
printf("I am the child: pid = %d \n", id);
}
return 0;
}
|