face=\"book antiqua, palatino\" size=\"4\"> Write code that forks into two proce
ID: 3540698 • Letter: F
Question
face="book antiqua, palatino" size="4">
Write code that forks into two processes, a parent process, and a child process.
The parent process will take the arguments to main() and send the characters in them one at a time to the child process through a pipe (one call to write for each character).face="courier new, courier, monospace">
Do not send characters from argv[0] through the pipe. Start with argv[1] and continue through the rest of the arguments.face="courier new, courier, monospace">class="code" face="courier new, courier, monospace">
The child process will read the characters sent by the parent process one at a time, and count the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever. The child process will return the number of characters counted to the parent by returning normally.style="font-style: italic">class="code">style="font-style: italic">style="font-style: italic">
The parent process will need to reap the child process to find out how many characters were counted by the child process.
It may be of use to know that read() will return immediately with a zero once the other end of the pipe is closed by the parent.face="courier">
If I call your code this way:
The parent process should print out:
Important: printing should be done only by the parent process. The child process should not print anything.
Your output should be formatted exactly as shown (small differences in whitespace are OK). Remember to replace %u201CI. Forgot%u201D with your own name.
------------------------------------------------------------------------
// Characters from command line arguments are sent to child process
// from parent process one at a time through pipe.
//
// Child process counts number of characters sent through pipe.
//
// Child process returns number of characters counted to parent process.
//
// Parent process prints number of characters counted by child process.
#include <stdio.h>
int main(int argc, char **argv)
{
// set up pipeclass="apple-tab-span">
// call fork()class="apple-tab-span">
if (0 /* replace 0 with test for parent vs child, delete this comment */) {class="apple-tab-span">
// -- running in child process --class="apple-tab-span">
int nChars = 0;class="apple-tab-span">
// Receive characters from parent process via pipeclass="apple-tab-span">
// one at a time, and count them.class="apple-tab-span">
// Return number of characters counted to parent process.class="apple-tab-span">
return nChars;class="apple-tab-span">
}class="apple-tab-span">
else {class="apple-tab-span">
// -- running in parent process --class="apple-tab-span">
int nChars = 0;class="apple-tab-span">
printf("CS201 - Assignment 3 - I. Forgot ");class="apple-tab-span">
// Send characters from command line arguments starting withclass="apple-tab-span">
// argv[1] one at a time through pipe to child process.class="apple-tab-span">
// Wait for child process to return. Reap child process.class="apple-tab-span">
// Receive number of characters counted via the valueclass="apple-tab-span">
// returned when the child process is reaped.class="apple-tab-span">
printf("child counted %d chars ", nChars);class="apple-tab-span">
return 0;class="apple-tab-span">
}class="apple-tab-span">
}
Write code that forks into two processes, a parent process, and a child process. The parent process will take the arguments to main() and send the characters in them one at a time to the child process through a pipe (one call to write for each character). Do not send characters from argv[0] through the pipe. Start with argv[1] and continue through the rest of the arguments. The child process will read the characters sent by the parent process one at a time, and count the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever. The child process will return the number of characters counted to the parent by returning normally. The parent process will need to reap the child process to find out how many characters were counted by the child process. It may be of use to know that read() will return immediately with a zero once the other end of the pipe is closed by the parent.Explanation / Answer
At least two things are glaringly wrong.
1. You allocate child as pid[2], and then write to the nonexistent child[2] entry, when only child[0] and child[1] actually exist.
2. You refer to child[0] in the first iteration of the following for loop, without ever having stored a value there. C does not initialize auto variables to anything.
Something to consider: Put your child process code in its own function "int runchild();" and then:
pid_t pid;
...
i = 0;
do
{
pid = fork();
if (pid == 0)
{ exit( runchild(); }
if (pid == -1)
{ /* fork() failed...do the Right Thing here */
else
{
child[i++] = pid;
}
} while (pid != -1 && i < 2);
That's a skeleton, of course. The general idea is to send the child process of the fork off to runchild(), which will only return if it wants to exit with a particular status. That makes runchild() sort of a main() without argc/argv for the child process. Only the parent process executes code after that "if (pid == 0) ..." statement.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.