I have multi thread application in which I'm creating a thread like this:
int main(int argc,char *argv[])
{
pthread_t thread_id[argc-1];
int i;
struct parameter thread_data[argc-1];
int status;
for(i=0;i<argc-1;i++)
{
thread_data[i].ip_filename = argv[i+1];
strcpy (thread_data[i].op_filename,argv[i+1]);
strcat (thread_data[i].op_filename,".h264");
}
for(i=0;i<argc-1;i++)
{
pthread_create (&thread_id[i], NULL , &thread_function, &thread_data[i]);
}
}
Now in the thread function, I want to redirect stderr
& stdout
in one separate file as per thread. Something like a thread log file.
How can I do so?
Edit:
If thread specific prints can be displayed on different terminal..? I mean if there are 2 threads then it opens 2 terminals & prints each threads data on different terminals.
You will have to keep track of what
FILE*
/fd
to use for each thread and usefprintf
etc. There's no other way.For multiple terminals, you need to open each terminal in your program. There's no way to automatically figure out which one to open. Run
/bin/tty
in the shell of the terminal you want to open, and then open that terminal in your program.An alternative method would be to have a
AF_UNIX
socket listening for connections. Then you write a separate program which connects to that socket. You could run that program in the terminal where you wish output to appear.I don't think this is possible directly. stdin/stdout are global variables, shared between threads, and so are file descriptors.
You'll have to create your own files, and change the
printf
intofprintf
.I use a
fork()
inside the thread for redirect the stdout of the forked process while the "true" thread is inwaitpid()
. The problem is how to pass the file where you want to redirect stdout. I use a global thread pool, and the thread will find itself throughpthread_equal(pthread_self(),iterator)
, then in the global thread pool structure there is the outfile where the program should redirect the stdout. In my case I create atmpnam()
and write it to the thread struct, but you can use it how you wish.Here is some example code: (written on the fly)
I really wrote it on the fly, I haven't tested this code, so take care to fix any errors. I didn't post my real code because of calls to my own library. Here I didn't check the return values from
tmpnam()
,fork()
,open()
andmalloc()
, which you should do.stdout
andstderr
are unique streams, by definition. Imagine, how would shell redirect your streams if there were multiplestdout
s? You would need to create your own output streams/file variables and use them instead. Is there a problem with that?You might find it useful to use thread-specific storage.
You cannot make a process output something on a different terminal. The process doesn't know about terminal, it just outputs the stream, and the terminal picks it up and displays.
What you can however do is to direct output from one of the streams into a file, and run
tail -f <filename>
in a different terminal.If you really must do this...
First you need to create 2
pthread_key_t
s, one forstdout
and one forstderr
. These can be created usingpthread_key_create
, and they must be accessable from all threads. Let's call themstdout_key
andstderr_key
.When a thread is being created:
and then in your header file:
then just use:
I don't recommend this approach though.