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.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

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.

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.