Problem with fopen

V

vaddadi.chandu

I am writing a multithreaded web-server and multithreaded client in C.
The server is required to keep a log of the transactions in a local
file. I try to open a file using the following:

FILE *fd;
fd = fopen("file.log","w");

fprintf(fd, "%s\n",<a character string>);

This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it.

Can anyone help?

Thanks,
Vaddadi
 
W

Walter Roberson

I am writing a multithreaded web-server and multithreaded client in C.
The server is required to keep a log of the transactions in a local
file. I try to open a file using the following:
FILE *fd;
fd = fopen("file.log","w");
fprintf(fd, "%s\n",<a character string>);
This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it.
Can anyone help?

Threads are not part of standard C, so a newsgroup such as
comp.programming.threads or comp.programming.unix would be suggested.


<OT>
My -guess-, given what little you've said, is that you are attempting
to write to the file in a different thread than you opened it in,
and that in that other thread you have not taken the proper POSIX
precautions to be allowed to write. In POSIX, when you want to
write to the same descriptor from different processes or different
threads, you first have to synchronize the I/O between the threads.
The proper procedures are spelt out in the POSIX documentation, and
the details are not appropriate for comp.lang.c .
 
E

Eric Sosman

I am writing a multithreaded web-server and multithreaded client in C.
The server is required to keep a log of the transactions in a local
file. I try to open a file using the following:

FILE *fd;
fd = fopen("file.log","w");

fprintf(fd, "%s\n",<a character string>);

This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it.

Can anyone help?

The library and/or the operating system is probably
buffering the output you write to the file, accumulating
the data in memory until there's enough to make an actual
write worth while. If you wait until "enough" output has
been generated, it's likely that a whole bunch of data will
suddenly be written to the file all at once.

If you need to have the output committed to disk sooner,
there are at least two things to try:

- Use fflush(fd) after each output operation. This
tells the library to empty any buffers the file might
be using, sending their contents to the operating
system.

- Use setvbuf(fd, NULL, _IOLBF, BUFSIZ) immediately after
the fopen(). This tells the library to send data to the
operating system as soon as each line is completed (that
is, every time a '\n' character is written). For even
more immediacy (but perhaps less efficiency) you could
use _IONBF as the third argument.

Either of these will probably solve your problem. However,
there are no guarantees! Different platforms have different
I/O capabilities, and the C language Standard doesn't attempt
to regulate all their complexities. For example, you can be
sure that the library sends the data to the O/S, but you are
then at the mercy of whatever the O/S decides to do with it --
and this is a matter outside the scope of the C language. The
actions of fflush() and setvbuf() are "suggestions," really, not
commands. The suggestions are strong, but less than absolute.
 
R

Roberto Waltman

Eric Sosman said:
(e-mail address removed) wrote:

<...>
Different platforms have different
I/O capabilities, and the C language Standard doesn't attempt
to regulate all their complexities. For example, you can be
sure that the library sends the data to the O/S, but you are
then at the mercy of whatever the O/S decides to do with it --
and this is a matter outside the scope of the C language. The
actions of fflush() and setvbuf() are "suggestions," really, not
commands. The suggestions are strong, but less than absolute.

In addition to that, some file systems do
not update the file information structures
until the file is closed.

It is possible that the data was actually
written to disk, but the file size will
still be zero for any other process
attempting to read it.

You may have to close the file periodically
and then reopen and append data to it, or
implement a rotation mechanisms where the
log file is closed after reaching a size
or number of entries limit, and a new one
is created to continue logging data.

As mentioned before, these issues are
platform specific and off-topic for
comp.lang.c


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top