Can't write to named pipe
A named pipe remains opened until you read it from some other place. This is to permit communication between different processes.
Try:
mkfifo fifo
echo "foo" > fifo
Then open another terminal and type:
cat fifo
If you return to you first terminal, you'll notice that you can now enter other commands.
See also what happends with the reverse :
# terminal 1
cat fifo
# terminal 2
echo "foo" > fifo
# and now you can see "foo" on terminal 1
If you want you terminal not to "hang on" when trying to write something to the fifo, attach to the fifo a file descriptor :
mkfifo fifo
exec 3<> fifo
echo "foo" > fifo
echo "bar" > fifo
Can't open Windows Named Pipe for writing?
the 4-th parameter of CreateNamedPipe
- nMaxInstances
- this is The maximum number of instances that can be created for this pipe.
so this is not instance count created in single call, but maximum count which can be created. single call to CreateNamedPipe
always create one (1) instance of pipe. if you want have 4 instance - you need call CreateNamedPipe
4 time. also initially pipe created in listening state, so client can just connect to it by call CreateFile
. but after connection is broken (because client close self handle) and you want accept new client connections for the same pipe instance - you need call DisconnectNamedPipe
and then ConnectNamedPipe
- only after this new client can again connect to the same pipe instance.
but anyway, even if you create only single pipe instance, by single call CreateNamedPipeA
- first client can connect to it. error 231 - i guess that real source of error is STATUS_PIPE_NOT_AVAILABLE
(An instance of a named pipe cannot be
found in the listening state) - you can check this by call RtlGetLastNtStatus()
after CreateFile
fail instead GetLastError()
say that pipe name is valid, no problems with access, but somebody already connect (may be already disconnect) to pipe - never first call to CreateFile
return this error
Can't make a named Pipe
You can get the error number this way:
#include <errno.h>
...
if (mkfifo(fifo_name, MODE) < 0) {
printf("Couldn't make the fifo, error = %d\n", errno);
return -1;
}
You can also get a text description for the error by using strerror()
.
#include <errno.h>
#include <string.h>
...
if (mkfifo(fifo_name, MODE) < 0) {
printf("Couldn't make the fifo, %s\n", strerror(errno));
return -1;
}
Important: always read errno
before any other library call! Subsequent calls may change it.
Why does writing to a named pipe continue after no one is reading?
"It's my understanding that the OS will block a program that writes to a named pipe until another program reads from the named pipe."
That understanding is incorrect. write
will not block unless the pipe/fifo is full. From the pipe manul:
A pipe has a limited capacity. If the pipe is full, then a write(2)
will block or fail, depending on whether the O_NONBLOCK flag is set
(see below).
As to why the first write
appears to block - it actually doesn't. It is the open
that blocks. From the fifo manaul:
The FIFO must be opened on both ends (reading and writing) before data
can be passed. Normally, opening the FIFO blocks until the other end
is opened also.
Update: Actually the above is true for the first write
. But there is probably more to explanation. Once the readbyte
program closes the fifo, subsequent write
calls should start failing.
C can't read from fifo (named pipe)
- Issue is your closing the file,
you can try this or fork another process and read the file, which is mostly why named fifos are used for i.e inter-process communication
This link explains in detail How to send a simple string between two programs using pipes?
pid_t p;
int fd;
char str[]="sample";
mkfifo("myfifo", S_IRUSR | S_IWUSR);
fd = open("myfifo", O_RDWR);
printf("write %d byte\n", write(fd, str, 5));
printf("read %d byte\n",read(fd, str, 6));
close(fd);
unlink("myfifo");
Duplex named pipe in .NET gets stuck when writing during a read (async write)
I just had to pass the PipeOptions.Asynchronous
flag to the constructor like so:
new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
Related Topics
List Directory Entries in The Svn Repository
How to Find Files Except Given Name
Using Output of Previous Commands in Bash
Loop Through Array of Arrays of String with Spaces
What Is The Linux Equivalent to Msvc++'s Option /D1Reportsingleclasslayout
Append to /Etc/Apt/Sources.List
How to Set Default Arguments for "Ls" in Linux
How to Idiomatically Package Dependencies for a Qt Application Using Cpack
Linux Asynch Io - Difference Between Aio.H and Libaio.H
Checking If a Screen of The Specified Name Exists
Can Upstart Expect/Respawn Be Used on Processes That Fork More Than Twice
Best Way to Build Cross Toolchains on MAC Os X
Checking Shared Libraries for Non Default Loaders
How to Let Users Run a Script with Root Permissions