Question

Allow the main process to generate a text file containing the text of this assignment. The main p...

Allow the main process to generate a text file containing the text of this assignment.

The main process will then create two other processes and pass to them the name of the file and two codes (code1 and code2) using a pipe() system call. The codes are two different alphabetic letters such as “a” and “k”.

  • The child processes should search the file for the character in the interval received, compute their frequencies and return them through a separate pipe to the parent process.

  • The parent process should compute the total number of characters in the file, and the rate of the appearance of the character frequencies received from the two child process, through separate pipes.

  • The parent process should then form a table of alphabetic characters and their frequencies and print the table in a proper format on the screen.

Hint: You are expected to use fork(), pipe(), read(), write(), close(), waitpid(), and other appropriate system call, if needed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

#include<string.h>
#include<unistd.h>

#include<stdio.h>

int main()

{
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int waitpid;
char pipe1writemessage[20] = "a";
char pipe2writemessage[20] = "k";
char readmessage[20];
returnstatus1 = pipe(pipefds1);

if (returnstatus1 == -1)
{
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);

if (returnstatus2 == -1)
{
printf("Unable to create pipe 2 \n");
return 1;
}
waitpid = fork();

if (waitpid != 0) // Parent process
{
close(pipefds1[0]); // Close the unwanted pipe1 read side
close(pipefds2[1]); // Close the unwanted pipe2 write side
printf("In Parent: Writing to pipe 1 – Message is %s\n", pipe1writemessage);
write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("In Parent: Reading from pipe 2 – Message is %s\n", readmessage);
}
else
{
//child process
close(pipefds1[1]); // Close the unwanted pipe1 write side
close(pipefds2[0]); // Close the unwanted pipe2 read side
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("In Child: Reading from pipe 1 – Message is %s\n", readmessage);
printf("In Child: Writing to pipe 2 – Message is %s\n", pipe2writemessage);
write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
}
return 0;
}

input In Parent: Writing to pipe 1 - Message is a n Child: Reading from pipe 1-Message is a In Child: Writing to pipe 2- Mess

Add a comment
Know the answer?
Add Answer to:
Allow the main process to generate a text file containing the text of this assignment. The main p...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • 10) Unlike a signal, which conveys only the occurrence of a particular event and contains no...

    10) Unlike a signal, which conveys only the occurrence of a particular event and contains no information content, a pipe can be thought of as a scratch file created by a system call. It can be used as a communications channel between concurrently running processes. The interface call to a pipe is similar to that for any file. In fact, the process reads and writes to a pipe just like any file. Unlike files, however, pipes do not represent actual...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

    Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • Modify the template program below and include the following additional functionalities: - Instead of 3 children...

    Modify the template program below and include the following additional functionalities: - Instead of 3 children processes, create 6 children processes. - 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, .... - Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167). - Keyboard inputs need to be monitored as in the original code. - Parent keeps reading from the...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • C homework help pipes

    Round 2: pipeline.cThis program takes the same input as sequence.c, but executes the commands as a pipeline, where the output of each command is piped to the input of the next command in line. The input of the first command, and the output of the final command, should not be altered. For example, if the file "cmdpipe" contains the linesls -s1sort -ntail -n 5then running./pipeline < cmdpipeshould output the 5 largest files in the current directory, in order of size.Suggested approach: set...

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

  • Suppose we have three programs aaa, bbb, ccc which are already developed, compiled and ready to...

    Suppose we have three programs aaa, bbb, ccc which are already developed, compiled and ready to execute. The first two, namely aaa and bbb, simply do some random amount of computation and write a character (respectively, 'A' and 'B to STDOUT_FILENO in a loop that is repeated 1000 times. If we run both programs at the same time, we might see different numbers of As and Bs such as AABBBAAABBBBABB. . . on the screen. The third program, namely ccc,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT