#C language help actually
43 messages · Page 1 of 1 (latest)
int main(void){
FILE* const pipe = popen("cat /path/to/a/file", "r");
// normal read operation as with a file
pclose(pipe);
}
obviously if you to write to that pipe instead you'd use "w" as the second argument
what's the use of popen why not use pipe with an fd as usual
also should the path to a file be any file or a fifo?
this function is higher level
you don't to explicitly call pipe, and fork
that's the advantage
the path is just an example because i used catas an example
you can use any other command
ohh
lets say I want to pass a message from parent to a child process can u give me a psuedo code or code for it
is the child process an external command?
what is people using pipe for? shared memory?
also if I interpreted the above code correctly pipe holds the file pointer for path with read permission
interprocess communication
yeah sure
can u give example
client-client communication?
like a c++ project to another C++ project
not path, it's a wrapper around the read-end of the pipe
i used cat, but it could just be ls for example
have you ever run a unix command with piping?
|
yeah
processes can communicate via pipes but they must be somehow "related" (either "siblings" or "father-son" relationship)
nope
also communication is unidirectional
ping me if u do this
half duplex
$ ls -1 my-directory | grep '\.jpg'
like what is that code for
i mean, the code i posted above is pretty much that
it consists of two processes
ls lists the contents of a directory
grep is essentially a filter command
the | is the pipe operator interpreted by your shell
so, the output of ls, which would normally be printed to the terminal is fed into grep's stdin
and grep will only print entries which contain the string '.jpg'
int main(void){
FILE* const pipe = popen("some command that will read from stdin", "w");
fwrite("abc", 3, 1, pipe);
pclose(pipe);
}
i dont remember if this is how you use fwrite