C Program for single parent and 26 child process forked by the single parent
neelkanth_surekha#cat test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
FILE *f;
char c;
int a = 'A';
int i;
int r, pid;
int status;
remove("f.txt");
/* PARENT FORKS 26 CHILDREN AND WAITS TILL
ALL THE CHILDREN TERMINATE */
for ( i = 1; i
<= 26; i++ ){
pid
= fork();
if
((pid != 0) && (pid > 0))
{
/*parent process
pid != 0*/
printf("I am parent: pid :%d ppid: %d "
"waiting for child whose pid is
: %d\n", getpid(), getppid(), pid );
/* wait for
child to terminate */
waitpid(pid, &status, WUNTRACED | WCONTINUED);
a++; // increment
character
}
/* EACH CHILD OF PARENT WRITES A
CHARACTER INTO THE FILE */
else
{
printf("############################################################################\n");
/*child
process pid = 0*/
/*write next character to file*/
f = fopen("f.txt", "a");
fprintf(f, "%c", a);
printf("I am child number %d: pid :%d ppid: %d: Character
written into file : %c\n",
i, getpid(), getppid(), a);
fclose(f);
/* Here
every child writes a character into the file and exists here. */
exit(0);
}
}
/* THE BELOW PIECE OF CODE GETS ONLY
EXECUTED FOR PARENT AS CHILD WOULD EXIT ABOVE.
* SECONDLY,
THIS CODE BELOW WOULD ONLY GET EXECUTED AFTER ALL THE CHILDREN HAVE
* TERMINATED.
THIS IS DUE TO THE FACT THAT PARENT IS WAITING FOR EACH OF IT'S CHILD
* TO
TERMINATE.
* */
printf("\n");
printf("\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("Now
that all the 26 children have written a charater into f.txt file\n");
printf(",Parent who was waiting for all the children to terminate opens
the resultant file\n");
printf("and
prints it\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("\n");
printf("I am
the parent : my pid: %d my parent pid %d \n", getpid(), getppid());
f =
fopen("f.txt", "r");
printf("Text
read from the file: \n");
while (1){
fscanf(f, "%c", &c);
if
( feof(f))
{
break;
}
printf("%c", c);
}
fclose(f);
printf("\n");
return 0;
}
Output:neelkanth_surekha#./a.out
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12517
############################################################################
I am
child number 1: pid :12517 ppid: 12516: Character written into file : A
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12518
############################################################################
I am
child number 2: pid :12518 ppid: 12516: Character written into file : B
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12519
############################################################################
I am
child number 3: pid :12519 ppid: 12516: Character written into file : C
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12520
############################################################################
I am
child number 4: pid :12520 ppid: 12516: Character written into file : D
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12521
############################################################################
I am
child number 5: pid :12521 ppid: 12516: Character written into file : E
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12522
############################################################################
I am
child number 6: pid :12522 ppid: 12516: Character written into file : F
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12523
############################################################################
I am
child number 7: pid :12523 ppid: 12516: Character written into file : G
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12524
############################################################################
I am
child number 8: pid :12524 ppid: 12516: Character written into file : H
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12525
############################################################################
I am
child number 9: pid :12525 ppid: 12516: Character written into file : I
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12526
############################################################################
I am
child number 10: pid :12526 ppid: 12516: Character written into file : J
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12527
############################################################################
I am
child number 11: pid :12527 ppid: 12516: Character written into file : K
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12528
############################################################################
I am
child number 12: pid :12528 ppid: 12516: Character written into file : L
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12529
############################################################################
I am
child number 13: pid :12529 ppid: 12516: Character written into file : M
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12530
############################################################################
I am
child number 14: pid :12530 ppid: 12516: Character written into file : N
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12531
############################################################################
I am
child number 15: pid :12531 ppid: 12516: Character written into file : O
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12532
############################################################################
I am
child number 16: pid :12532 ppid: 12516: Character written into file : P
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12533
############################################################################
I am
child number 17: pid :12533 ppid: 12516: Character written into file : Q
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12534
############################################################################
I am
child number 18: pid :12534 ppid: 12516: Character written into file : R
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12535
############################################################################
I am
child number 19: pid :12535 ppid: 12516: Character written into file : S
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12536
############################################################################
I am
child number 20: pid :12536 ppid: 12516: Character written into file : T
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12537
############################################################################
I am
child number 21: pid :12537 ppid: 12516: Character written into file : U
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12538
############################################################################
I am
child number 22: pid :12538 ppid: 12516: Character written into file : V
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12539
############################################################################
I am
child number 23: pid :12539 ppid: 12516: Character written into file : W
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12540
############################################################################
I am
child number 24: pid :12540 ppid: 12516: Character written into file : X
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12541
############################################################################
I am
child number 25: pid :12541 ppid: 12516: Character written into file : Y
I am
parent: pid :12516 ppid: 14458 waiting for child whose pid is : 12542
############################################################################
I am
child number 26: pid :12542 ppid: 12516: Character written into file : Z
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Now
that all the 26 children have written a charater into f.txt file
,Parent
who was waiting for all the children to terminate opens the resultant file
and
prints it
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
I am
the parent : my pid: 12516 my parent pid 14458
Text
read from the file:
ABCDEFGHIJKLMNOPQRSTUVWXYZ