mirror of
https://github.com/Hizenberg469/Inter-Process-Communication-IPC-.git
synced 2026-04-20 02:12:25 +03:00
IPC using Pipes
This commit is contained in:
@@ -18,6 +18,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||||||
# Add source to this project's executable.
|
# Add source to this project's executable.
|
||||||
add_executable (Pipes_demo "Pipes_demo.c" )
|
add_executable (Pipes_demo "Pipes_demo.c" )
|
||||||
add_executable (2ProcessPipes "2ProcessPipes.c")
|
add_executable (2ProcessPipes "2ProcessPipes.c")
|
||||||
|
add_executable (Challenge "Challenge.c")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
40
Pipes/Challenge.c
Normal file
40
Pipes/Challenge.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
To implement "ls | wc -l" in C.
|
||||||
|
This only work in linux system.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
|
int pfds[2];
|
||||||
|
|
||||||
|
pipe(pfds);
|
||||||
|
|
||||||
|
/*
|
||||||
|
dup(int fd) : it duplicates the specified fd to the free fd value with lowest
|
||||||
|
numeric value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!fork()) {
|
||||||
|
|
||||||
|
close(1); /* closing the stdout whose fd value is 1 for a process. */
|
||||||
|
dup(pfds[1]); /* setting the stdout of process to writing end of the pipe. */
|
||||||
|
close(pfds[0]); /* Reading end of the pipe is not needed by the child process. */
|
||||||
|
execlp("ls", "ls", NULL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
close(0); /* closing the stdin whose fd value 0 for a process. */
|
||||||
|
dup(pfds[0]); /* setting the stdin of process to reading end of the pipe. */
|
||||||
|
close(pfds[1]); /* Writing end of the pipe is not needed by the parent process. */
|
||||||
|
execlp("wc", "wc", "-l", NULL);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user