Add documentation to program to explain its functionality Ad
Add documentation to program to explain its functionality
Add a header comment to the top of the program explaining the overall purpose of the program.
Add internal comments within the program to explain the purpose of different sections of the program.
Create a short of description of WHY the program works the way it does. Do not just document the output of the program… you must also explain why the program provides that output.
Program:
fork2.c
/* fork2.c - Child process can run different code than parent process */
#include <stdio.h>
#include <unistd.h>
main()
{
int i,j;
j=0;
printf(\"Ready to fork...\ \");
i=fork();
if (i == 0) {
printf(\"The child executes this code.\ \");
for (i=0; i<5; i++) {
j=j+i;
}
printf(\"Child j=%d\ \",j);
}
else {
printf(\"The parent executes this code.\ \");
for (i=0; i<3; i++) {
j=j+i;
}
printf(\"Parent j=%d\ \",j);
}
}
Solution
Priogram:
#include <stdio.h>
#include <unistd.h>
main()
{
int i,j;//Here we are Initializing i and j are Integer variables
j=0; // Here we are Initializing value of j=0
printf(\"Ready to fork...\ \"); //This statement is printed it is following the fork() system call
i=fork();
/* fork() it is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call */
if (i == 0) {
printf(\"The child executes this code.\ \");
for (i=0; i<5; i++) {
j=j+i; // Here we are adding value of i i.e zero to value of j
/* The above Loop will be goes like this
j=0+0 j=0
j=0+1 j=1
j=1+2 j=3
j=3+3 j=6
j=6+4 j=10 */
}
printf(\"Child j=%d\ \",j); // Here we are printing the final value of j for Child
}
else {
printf(\"The parent executes this code.\ \");
//This statement is printed when value of i not equals to zero i.e i greater than Zero
for (i=0; i<3; i++) {
j=j+i;
/* The above Loop will be goes like this
j=0+1 j=1
j=1+2 j=3 */
}
printf(\"Parent j=%d\ \",j); // Here we are printing the final value of j for Parent
}
}
Output:
Ready to fork...
The parent executes this code.
Parent j=3
Ready to fork...
The child executes this code.
Child j=10


