Question

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 parent process waits for them to terminate before it can exit.
Note that you may have to create Child 2 first, followed by Child 1.
The parent program does the same thing as a shell that runs the command "ps -aux | sort -r -n -k 5".
You must use the fork(), pipe(), dup2(), close(), execlp() functions.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}

Add a comment
Know the answer?
Add Answer to:
Write a C program for Linux called pipes.c that does the following: In the main() function,...
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 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...

  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • Using either a UNIX or a Linux system, write a C program that forks a child...

    Using either a UNIX or a Linux system, write a C program that forks a child process that ultimately becomes a zombie process. Process states can be obtained from the command: ps -l The process states are shown below the S column; processes with a state of Z are zombies. The process identifier (pid) of the child process is listed in the PID column, and that of the parent is listed in the PPID column. Because you do not want...

  • How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can...

    How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can store one message. –Forks a process. –Creates 2+ messages, with contents of your choice, in the parent and sends them to the child via a named or anonymous pipe. –Prints the contents of all received messages in the child process. –If necessary, cleans up after the pipe. Note (the subjects about signal and Pipes )

  • 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...

  • DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER...

    DO NOT COPY PASTE THE SOLUTION FROM ANOTHER QUESTION! IT IS NOT THE RIGHT ONE, ANSWER WILL BE REPORTED IF NOT A UNIQUE SOLUTION. Write a C program called myshell.c, which, when compiled and run, will do what the shell does, namely, it executes in a loop (until user types exit on the keyboard), prints a prompt on the screen, reads the command typed on the keyboard (terminated by \n), creates a new process and lets the child execute the...

  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n", getpid());         }     return 0;}

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Arid No is 19-arid-898Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n",...

  • 1) Write a complete C or C++ program to print Hello World Greetings. 2) Using the...

    1) Write a complete C or C++ program to print Hello World Greetings. 2) Using the command line, compile and generate the executable for the above program. Let’s call helloWorld the target executable. 3) Write a C program that does the following: a) forks a child to execute helloWorld b) waits for the child execution to end 4) Reuse the above program to try a different variant of exec family of system calls. OPTIONAL: 1) write a program main that...

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