Q: What is the header file to use with the select system
call? I included /sys/types.h but that is not working on AIX 4.14.
A: Try sys/select.h.
Q:
I got my client and server talking and being merry. The only problem
is if I cntrl C one of them to quit the other one just keeps scrolling
off of the screen and if I hit return I am greeted with a - Broken
Pipe - Unix error. My question is, is there any way to prevent this,
especially in my server?
A:
read() will return a 0 when the writing end of a FIFO has
exited or closed the FIFO (and there is no data left).
If you write to a pipe the is no longer open for reading by any
process, you will get a SIGPIPE which by default will terminate
your program.
Q:
If one of the processes shuts down can we exit the other
process, or do we have to restart the server? Also is it necessary to
delete the fifo's our processes created before they shut down?
A: You can do whatever you want (neither is required), but it
makes sense to exit when the other end of the connection has closed
the FIFO (possibly by exiting) and to clean up FIFOs before
exiting.
Q:
If I open a fifo in blocking mode does it block right there or will
it block when I try to read from it.
A:
a named pipe opened for reading will block on the open() until somebody
opens it for writing, and one opened for writing will block until
it's opened for reading.
Q:
Do we have to put the terminal in character mode or can we read the
typed data one line at a time?
A:
As we discussed in class, a line at a time is fine.
Q:
What is the command to flush stdout and which header file should I
include to use it.
A:
fflush(). I think you just need stdio.h but you might want to check
the man page.
(The setvbuf() command turns off buffering so you don't need to flush
anything.)
Q:
We're working on the project, and we are reading the names of the two fifos
from a well known fifo and the second name, when we try to open it, it
somehow has a lot of extra junk at the end, even though we've read in the
exact number of characters. So, when I try to open the fifo, it's saying
it's not there, however, when we print out the name to the screen, it looks
correct.
When we did a trace, it said
open("/tmp/write.4\201\3\1\13", 0, .....
instead of just "/tmp/write.4" like it should be. Do you have any ideas what
could be causing this? The other fifo, whose name is read in first, opened
fine.
A:
It looks like you're writing the name into the pipe with something
like
write(fd, pipename, strlen(pipename))instead of
write(fd, pipename, strlen(pipename)+1)Note that strlen doesn't count the terminating \0 character, which you need to send.
Q:
Is there a function in the C libraries that converts an integer to a
character string, so 123 becomes "123"?
A: sprintf().
... char buffer[25]; int n = 123; sprintf(buffer, "%d", n); ...
Q:
We've got our program working fine now except one little thing: when we write
some thing long, then the next time write something shorter, the end of the
first message is left. We can't figure out a way to clear out the variable
so that the extra garbage is not there. Any hints?
A:
The problem is that you are treating what you read from a fifo as
null-terminated strings, although you are not sending the null. You
could just use write to send output to the screen - write does not
care about null termination, it just needs to known how many
characters it should send to stdout. You could also just send the null
over with each string:
You are probably doing something like this:
write(thefifo,string,strlen(string));which does not send the null. The following does send the null
write(thefifo,string,strlen(string)+1);