ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I use fifos to communicate between two simple programs, a client and a server. The client runs, sends three characters to the server on a fifo, reads three characters from the server on another fifo, and ends. When I use write and read with size (3'rd parameter) 3 or 4 I get strange characters when I read the fifos both in client and server. When I use write and read with size 64, everything looks right. As a beginner it took me a while to figure this out, and I would like to know what happens. I know that the client sends an EOF when it exits, but this does not seem to be a problem with size 64. Please throw to me some suggestions, as of what could cause this behaviour.
BUFSIZE should be four as each char array should allow room to be NUL terminated, thus, a char's size is always one more than you want. The reason is due to how the C programming language handles strings. The length of a C char is found by searching for the (first) NUL byte. Samples of C string handling code can be seen in strlen and strcpy. If there is no NUL character you'll see erroneous output.
The preferred method I would recommend is to NUL terminate based on the length of data read from the console. The read function returns the length of the data read, thus, you can use this value to terminate e.g.
Code:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
int main(void)
{
char t[11];
int len = 0;
len = read(0, t, 10);
t[len-1]=0;
printf("[%s]", t);
}
Of course, you should also check the value of len, as it could be zero.
I used this short program to test how can I write to a fifo from bash.
Code:
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define BFOUT 12
int main(void){
int fdo, fdi, a;
char *fifo1="fifo1", *fifo2="fifo2", *s, t[BFOUT];
if((fdi = open(fifo2, O_RDONLY)) == -1) err_sys("client fifo2 open error");
if(read(fdi, t, BFOUT) == -1) err_sys("client fifo2 read error");
write(1, t, strlen(t)); // javascript arrays do not use "\0", so no need for + 1
}
int err_sys(const char *s){
printf("%s\n", s);
exit(1);}
I do:
Code:
mkfifo fifo2;
./rdfifo&
echo abc$'\0' > fifo2
and again I get the special characters after abc. Can you please point me in the right direction as of why does this happen?
in man bash, search \\n and you will find that $'\0' is supposed to write a character with ASCII code 0 - the null character.
Still:
Code:
cat fifo2&
echo abc$'\0' > fifo2
works well.
Writing to fifo from C and reading from bash goes well, too.
I also wonder what happens to stdin and stdout of a process which was run in the background from bash, and then the bash was stopped. Such a process becomes a child of init, and I believe that its stdin and stdout are no more connected to any terminal.
I discovered that echo abc$'\0' > fifo does not really send $'\0' to the pipe. Probably bash thinks that this is silly, and it just does not send it. $'\0' is probably expanded in '\0', as it should, but then I don't see it in the pipe.
The solution is to use:
Code:
printf "abc\0" > fifo
printf is also a builtin and it works like a charm. I can see the '\0' character in the fifo.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.