

Two programs that communicate with FIFOs work with the loop, but not with the wh...
source link: https://www.codesd.com/item/two-programs-that-communicate-with-fifos-work-with-the-loop-but-not-with-the-while-loop.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Two programs that communicate with FIFOs work with the loop, but not with the while loop
I am trying to write two programs that will communicate via FIFOs in C. I am experimenting with FIFOs for an assignment I have.
When I know the number of messages and read them with a for loop, it prints out all the messages that were sent from the other side. If I use a while loop, it only sends two of them. The code is slightly changed from this question How to send a simple string between two programs using pipes?
This works:
/* writer */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
/* create the FIFO (named pipe) */
/* write "Hi" to the FIFO */
fd = open(myfifo, O_WRONLY);
int i;
for(i = 0; i < 10; i++)
write(fd, "Hi", sizeof("Hi"));
close(fd);
return 0;
}
And: (edited)
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
mkfifo(myfifo, 0666);
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
for(i = 0; i < 10; i++)
{
int n = read(fd, buf, MAX_BUF);
printf("n = %d , Received: %s\n",n, buf);
}
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
Edit: This now prints
n = 18 , Received: Hi
n = 12 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
n = 0 , Received: Hi
When I change the reader to this, it doesn't work:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_BUF 1024
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
char buf[MAX_BUF];
mkfifo(myfifo, 0666);
/* open, read, and display the message from the FIFO */
fd = open(myfifo, O_RDONLY);
int i;
while(read(fd, buf, MAX_BUF))
printf("Received: %s\n", buf);
close(fd);
/* remove the FIFO */
unlink(myfifo);
return 0;
}
I am running the two programs in two separate terminals and all. When I run them with the second reader, it only prints out:
Received: Hi
Received: Hi
Any help would be appreciated.
Pipes are stream based, not message based. While the number of bytes read should match the number written, the number of read
calls is not necessarily the same as the number of write
calls.
If we modify the reader to print the number of bytes received:
int len;
while((len=read(fd, buf, MAX_BUF)) > 0) {
printf("Received %d: %s\n", len, buf);
}
I get the following output:
Received 30: Hi
So in the second case, there are 10 writes of 3 bytes (2 for the letters H
and i
and one for the null terminating byte) and 1 read of 30 bytes. The reason 3 bytes is being written on each write
call is because the string constant "Hi"
has type char [3]
.
You only see one instance of "Hi" printed because the third byte is a null byte which terminates the string, so nothing past that is printed.
Recommend
-
98
Overview The StarCraft II API is an interface that provides full external control of StarCraft II. This API exposes functionality for developing software for: Scripted bots. Machine-learning based bots.
-
35
Automatically finding a program that implements a given specification is called program synthesis . The main difficulty is that the search space is huge: the number of programs of size \(n\) grows exponentially....
-
6
Why Companies Need Returnship Programs (Back to Work, Better) HBR IdeaCast...
-
3
-
8
How AI is Changing The Way Businesses Communicate and WorkOctober 28th 2021 new story
-
12
Microsoft Loop is a new Office app for the hybrid work eraThe biggest change to Microsoft’s Office documents in decades is expanding into Microsoft Loop, a hub for a new way of working in Office. Microsoft Loop is the new branding for
-
0
Y Combinator is hiring for a programs/ops person to grow Work at a Startup Y Combinator is hiring for a programs/ops person to grow Work at a Startup...
-
4
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
7
Gun Buy Back Programs Probably Don’t Work When I was still a criminology professor, I remember one day while out getting groceries receiving a cold call from a police department interested in collaborating. They asked if I could prov...
-
8
The Feature Work → Maintenance Work Loop February 15, 2023 #process
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK