This question already has answers here:
Interprocess communication using pipe in Linux
(2 answers)
Program based on pipe in Linux
(2 answers)
Closed 8 years ago.
Does any one know how to write a number to pipe for that first of all i have to open the pipe, and then write suitable number to it.
Also,after writing also i have to read the number and print it.
I have tried to open the pipe with 'popen' command but i am uncertain what to give as the 1st argument to popen i.e:-
popen(const char *command, const char *type)
i want to ask what command should i give here if i want to write a number to pipe..??
you should use fd=open(FIFO_NAME,O_RDONLY); for reading fifo file.
mkfifo(FIFO_NAME,S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO);
fd=open(FIFO_NAME,O_WRONLY);
fifo is also called pipe.Here mkfifo create a fifo file and the write into a file.
Note:- that to read or write into fifo there should be a reader and writer process, Else it would be blocking one of the process.
if you don't want to write a process for reading the use cat command and writing process to see the fifo file content.
Related
This question already has answers here:
Strange behavior performing library functions on STDOUT and STDIN's file descriptors
(2 answers)
Closed 5 years ago.
I'm having difficulity understanding the read function in C.
len = read(fd, buf, 32);
when I assign fd as 0,1,2 and run the program, its basically doing the same thing, can someone tell me what difference does this make?
read() attempts to read up to count bytes from file descriptor fd.
fd = 0
fd = 1
fd = 2
Is reading from different file descriptors. The difference is, you are reading from different files, and the data read into the buffer is different.
What is the difference in reading from Book A and reading from Book B ? it is the same process of reading a book... it is the content that changes.
As far as I understand your question it is why nothing changes if you read from file descriptors 0, 1, 2.
In a normal program the file descriptor 0 is stdin, 1 is stdout and 2 is stderr. stdin is where you should read your input, 1 is where you should write your output and 2 is where you should write your error messages.
It is not uncommon that all three file descriptors may point to the same underlying file (a file can also be the console, network connection, etc.) behind the scenes. If you're just running your program from the command line this is actually quite likely. In that case you may be able to read from all of them and get the exact same result.
But. Then you decide that you want to save the output of the program in a file and run it like this: program > output. Now file descriptor 1 is no longer pointing to the same file as stdin and your program would break. Same thing happens if you point stderr to some error logging facility. Or get the input from a file or a pipe. Or run the program in some debuggers. Or a different terminal. This is why you should only read from 0 and no other file descriptors, even if you might get away with it sometimes.
This question already has answers here:
Can I share a file descriptor to another process on linux or are they local to the process?
(6 answers)
Closed 6 years ago.
int fd = socket(//arguments);
Can this descriptor be passed to another process via IPC and still be valid or is it local to the process that created it?
File descriptors are local to the process. For instance, every process will have its standard input, output, and error on file descriptors 0, 1, and 2.
It is possible to pass a file descriptor to another process over a UNIX domain socket. When this is done, each process will have a copy of the file descriptor, possibly under a different FD number. It's a kind of messy API, though, and is rarely used.
Yes, file descriptors are local to the process. When a process forks a child, however, the parent and child have the same file descriptor table. This is great because it allows for IO redirection, which is a pretty handy trick.
This question already has answers here:
Difference between fclose and close
(4 answers)
Closed 6 years ago.
I want to redirect STDOUT to a file on the disk. The point is to make the printf on my program write to a file instead of the console.
I saw some articles on the web where they used:
dup2(fileno(outputFile), STDOUT_FILENO);
Or:
close(STDOUT_FILENO);
dup(fileno(outputFile));
In every tutorial they use close() and it actually works. But I was curious and I tried to use fclose(stdout) instead but some error happened when I tried to use printf:
fclose(STDOUT_FILENO);
dup(fileno(outputFile));
Error:
Bad file descriptor
My question is, why does fclose() not work but close() does?
Thanks.
STDOUT_FILENO is a numeric file descriptor (usually 1). When you use close, you release the descriptor, but then reassign it with dup2. Any output to that descriptor will now go to the new file.
stdout on the other hand is a FILE*, an object of sorts that contains a file descriptor. Doing printf formats output into a buffer associated with the FILE, and then writes the buffer to the descriptor (depending upon the buffering mode). When you fclose a FILE*, it (normally) closes the underlying descriptor, and frees the FILE object. The dup2 does not ressurect stdout, so printf fails.
This question already has answers here:
replace system() with non-blocking function
(5 answers)
Closed 8 years ago.
Please tell me how to open two files simultaneouly using system();
I want to open two file
and I am doing it like
system("C:\temp\file1.doc");
system("C:\temp\file2.doc");
But here till the file 1 is open file 2 is not opening as the control is not able to reach the second system call , Is there a way to do open them simultaneously.
Thanks
Shashank
I would suggest two ways.
Run two threads. Call system() from each thread.
Create a child process using fork() and run system() from both parent and child processes.
This question already has answers here:
Portable way to pass file descriptor between different processes
(2 answers)
Closed 9 years ago.
The answer to this question shows how to share a file descriptor between two processes. I am unclear on whether this is possible with any process or only related processes. For example, is it possible to open an unnamed pipe and pass the read or write file descriptor to another unrelated process on a unix system?
Yes, you can pass a file descriptor to any other process. The only requirement is that you open a Unix domain socket to communicate between the two processes.