Is there anybody who knows how to do this? This is an assignment for my Operatin
ID: 638859 • Letter: I
Question
Is there anybody who knows how to do this? This is an assignment for my Operating Systems course and I need help! Here's how it works:
Written below is a C++ program that creates a child process in Linux:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
#define MAX_COUNT 200
#define PROC_COUNT 1
void ChildProcess(void)
{
for (int i=1; i < MAX_COUNT; i++)
printf(" This line is from child, pid=%d, count=%d ", getpid(), i);
printf("** Child process complete ");
}
void ParentProcess(void)
{
for (int i=1; i < MAX_COUNT; i++)
printf(" This line is from parent, pid=%d, count=%d ", getpid(), i);
printf("** Parent process complete ");
}
int main()
{
pid_t pid;
printf("Parent process: pid=%d ", getpid());
pid = fork();
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
Now, here are the steps to implement and explore:
A. Modify the program to create four child processes. Capture the output of the program (In Ubuntu Linux Installation, you can type ./testfork >forktestoutput to save the output). What can you see about the relative execution of the five processes?
B. Modify the program to print out any available information on the child process after the loop completes
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
#define MAX_COUNT 200
#define PROC_COUNT 1
void ChildProcess(void)
{
for (int i=1; i < MAX_COUNT; i++)
printf(" This line is from child, pid=%d, count=%d ", getpid(), i);
printf("** Child process complete ");
}
void ParentProcess(void)
{
for (int i=1; i < MAX_COUNT; i++)
printf(" This line is from parent, pid=%d, count=%d ", getpid(), i);
printf("** Parent process complete ");
}
int main()
{
pid_t pid;
printf("Parent process: pid=%d ", getpid());
pid = fork();
if (pid == 0)
{
for(int i=0;i<4;i++){
ChildProcess();}
}
else
ParentProcess();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.