I was trying to solve this problem for school project but I couldn't understand how to open and close the read and write within the pipe: the question is: Create a C program that executes the following command ls| sort -r using pipe and creation of a fork() and dup()
How to execute the command ls|sort -r in C using pipe and fork()?
554 Views Asked by mary At
2
There are 2 best solutions below
0
mary
On
int main(int argc, char*argv[])
{
int fd[2];
pid_t p1,p2;
pipe(fd);
if (pipe(fd)==-1){
perror("Erreur pipe");
exit();
}
p1=fork();
if (p1==-1){
perror("Erreur fork");
exit();
}
else if (p1==0) {
close(fd[0]);
dup2(fd[1],1);
execlp("ls","ls",argv[1],0);
}
p2=fork();
if (p2==-1){
perror("Erreur fork");
exit();
}
else if (p2==0){
close(fd[1]);
dup2(fd[0],0);
execlp("sort","-r",NULL);
}
waitpid(p1,nullptr,0);
close(fd[1]);
waitpid(p2,nullptr,0);
return 0;
}
// can you have a look at this and tell me if it would work?
Related Questions in C
- How to call a C language function from x86 assembly code?
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- How to crop a BMP image in half using C
- How can I get the difference in minutes between two dates and hours?
- Why will this code compile although it defines two variables with the same name?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Why can't I use the file pointer after the first read attempt fails?
- #include Header files in C with definition too
- OpenCV2 on CLion
- What is causing the store latency in this program?
- How to refer to the filepath of test data in test sourcecode?
- 9 Digit Addresses in Hexadecimal System in MacOS
- My server TCP doesn't receive messages from the client in C
- Printing the characters obtained from the array s using printf?
Related Questions in PIPE
- Creating a GUI application for creating graphs
- Dynamic Piping in C
- Redirecting stdout with execvp
- how to keep the colored output of a command after piping it through grep
- How do I summarize data and bind new rows to an existing data frame?
- Using imported HTML with own CSS an JS in Angular
- multiproccessing broken pipe error when i am trying to send plot data from process
- Sending standard output of one child to the standard output of another via a pipe
- Executing sed via execvp makes other pipes blocked
- Race condition in pipe/dup2/exec*
- Disable stdout in popen process
- Can close of pipe's write end fail leaving reading process blocked?
- grep for string and open matching files in text editor
- Capture Stderr and redirect it to Stdout in service with goroutine in go
- Is it possible to use an Eventbridge pipe to target and SNS topic in another account
Related Questions in FORK
- Forking vs Cloning in GitHub
- Execute JUnit suite with Maven Fork in Customized runOrder
- Multiple child processes accessing the same vector
- Parent process doesn't wait for the child process to terminate
- Implementation Bash pipes and redirections in c
- Why is fork() accepted in strace if the actual syscall is clone()?
- Which child will execute first when you call fork() and wait() multiple times?
- intercommunicating using stdin and stdout between forked C processes
- Ant Junit ForkMode with Suites
- How to enable a PR originating from a forked repo to cause a GitHub workflow to push to a branch (gh-pages) in the original repo?
- Executing sed via execvp makes other pipes blocked
- Python progress bar in conjunction with fork processes
- Race condition in pipe/dup2/exec*
- Git how to pull the update from original repository and keep my own changes
- Using SQLite as object store in a forking web server; can it be done safely?
Related Questions in EXEC
- Unexpected argument on sqlcmd command line
- How does Python's exec function work when sending an empty dictionary in the locals parameter?
- Executing sed from php script
- Parent process doesn't wait for the child process to terminate
- Reading and writing from pipe after execvp using dup2
- Many QPushButtons clicked connecting using exec()
- Dynamic Piping in C
- intercommunicating using stdin and stdout between forked C processes
- Can an imported module use modules already imported?
- Execute a comand for each file within a directory
- pyzmq doesn't receive messages when using exec
- Running exec() via PHP (Laravel) on IIS and Windows Server
- PHP exec/passthru/system produces different result than executing manually from command line, why?
- Start a bash terminal with C using execl / execv and add timeout
- How to get bash find exec to directly execute commands instead of using a temp file?
Related Questions in DUP
- intercommunicating using stdin and stdout between forked C processes
- Issues with file redirection when building a simple shell
- How to supply input to a thread which is polling for stdin, form another thread in the same process?
- STM32 .hex file convert to .dup file
- Executing the ls | wc linux command in c program using 2 processes communicating trough a pipe
- C program prints to terminal instead of file even after using dup2/dup
- How do I use 2 child processes one for executing command and the other one for reading output and passing it to the next?
- In C how to read the output of a process and write it in the input of another?
- In C how can i get the size of a pipe file descriptor without using READ or Write?
- Restore contents of a file descriptor after reading it?
- C restore stdin to the actual standard input after using dup2
- Redirecting stdout to pipe and reading from it from a single process
- piping and redirection for execv command
- How to implement pipes and redirections in my shell?
- Rails 6 Dup in nested controller
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
I don't normally condone the posting of solutions to homework problems, but there's enough bad code available on the internet that I thought I would post what I consider to be not terrible code. Perhaps I'm being presumptuous to think that, but: