help need with non-working redirect stdout to file

I need to start a couple of processes, with each one redirect stdout to a log file of its own. I’ve tried:

process1 >/tmp/process1.log &
process2 >/tmp/process2.log &

but it does not seem to do as I expect. Nothing is written to the log files.

To test further I’ve written a tiny test process:

int main(int argc, char *argv[]) {
  int i;
  for (i = 0; i < 10; i++) {
    printf("%d. writing to stdout\n", i+1);
    sleep(2);  
  }
  printf("Writing done.\n");
}

Using that as process1/2, it appears the log file is not updated until the process exits!?

I would have expected the log file to be updated whenever new stuff was written to stdout.

How can I achieve that behaviour?

Thanks,
Jacob Dall

Oops, forgot to mention I’m using 6.3 (SP2)

It’s because the data is buffered. Write more data and you will see that the file will grow. I beleive the data is buffered by the shell, not sure something can be done about that.

Do you know what the buffer size is?

you can control the buffer size with setbuf or setvbuf
also you can flush the buffer with fflush()
note that stderr is not buffered by default
stdout is line buffered, so a ‘\n’ in the output will flush that line.

I know I can do those changes - but I don’t really want to go there (changing the source files, that is) - I just want the stdout to be redirected. Nothing more.
The ‘\n’ is already present in the printf (as one can see in the shown source) - I reckon the the file should be updated, but it just isn’t

Colin, are you sure about this. From my observation, the bufferering method changes whether the data is sent to the screen (no redirection) or to a file. I always assumed this was because the shell was involved in the redirection. I did a little test and the shell isn’t involved, BUT when sent to a file \n doesn’t flush the line, a fflush is required. Probably have to do with the fact that the file is not a terminal device.

I don’t see any other way then to put a setvbuf in the source code, or write a custom resmgr that behaves like a terminal.

sorry, you’re right Mario, if the output device is a tty (ie istty succeeds) then it will be linebuffered, else it will be normally buffered.

Thanks you everyone for the comments

It seems that I have no other means than to modify the sources, then.