source: liacs/os/assignment1/parent1.c@ 145

Last change on this file since 145 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 1.1 KB
Line 
1/* a simple parent; version1; sys name: parent1.c
2Now we better create a program, which will replace the replica
3of the parent. We have named this replacing program child1.c
4and have made an executable out of it carrying the name child1
5(use the -o option). In parent process fork() returns the pid
6of the child; in the child process fork returns 0. */
7#include <sys/wait.h>
8#define NULL 0
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12
13int main(void)
14{
15 int pidValue = fork();
16 if (pidValue > 0) {
17/* Parent code here */
18 printf("Process[%d]: Parent in execution ... \n", getpid());
19 sleep(2);
20 if (wait(NULL) > 0) { /* Child terminating */
21 printf("Process[%d]: Parent detects terminating child \n", getpid());
22 }
23 printf("Process[%d]: Parent terminating ... \n", getpid());
24 }
25 else {
26 if (pidValue == 0) {
27/* this is the child's process */
28 execve("child1", NULL, NULL);
29 exit(0); /* should never get here, terminate */
30 }
31 else {
32/* should never get here; things such as not
33enought mem */
34 exit(0);
35 }
36 }
37 return(0);
38}
Note: See TracBrowser for help on using the repository browser.