在多线程应用程序中如何重定向 stderr &标准输出在每个线程的单独文件中?
我有一个多线程应用程序,在其中创建一个如下所示的线程:
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]);
}
}
现在在线程函数中,我想重定向stderr
& stdout
在每个线程的一个单独的文件中。 类似于线程日志文件。
我怎样才能这样做呢?
编辑:
线程特定的打印是否可以显示在不同的终端上..?我的意思是,如果有 2 个线程,那么它会打开 2 个终端在不同的终端上打印每个线程的数据。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您真的必须这样做...
首先您需要创建 2 个
pthread_key_t
,一个用于stdout
,一个用于stderr< /代码>。这些可以使用
pthread_key_create
创建,并且它们必须可以从所有线程访问。我们将它们称为stdout_key
和stderr_key
。当创建线程时:
然后在头文件中:
然后只需使用:
但我不推荐这种方法。
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.
我认为这不可能直接实现。 stdin/stdout 是全局变量,在线程之间共享,文件描述符也是如此。
您必须创建自己的文件,并将
printf
更改为fprintf
。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
.根据定义,
stdout
和stderr
是唯一的流。想象一下,如果有多个 stdout,shell 将如何重定向您的流?您需要创建自己的输出流/文件变量并使用它们。这有问题吗?您可能会发现使用线程特定存储很有用。
您不能使进程在不同的终端上输出某些内容。该进程不知道终端,它只是输出流,然后终端接收它并显示。
然而,您可以做的是将其中一个流的输出定向到文件中,并在不同的终端中运行
tail -f
。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.您必须跟踪每个线程使用的
FILE*
/fd
并使用fprintf
等。没有其他方法。对于多个终端,您需要在程序中打开每个终端。无法自动确定要打开哪一个。在要打开的终端的 shell 中运行
/bin/tty
,然后在程序中打开该终端。另一种方法是使用 AF_UNIX 套接字侦听连接。然后编写一个连接到该套接字的单独程序。您可以在希望出现输出的终端中运行该程序。
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.我在线程内使用
fork()
来重定向分叉进程的标准输出,而“true”线程位于waitpid()
中。问题是如何将文件传递到要重定向标准输出的位置。
我使用全局线程池,线程会通过pthread_equal(pthread_self(),iterator)找到自己,然后在全局线程池结构中有一个outfile,程序应该在其中重定向stdout。
在我的例子中,我创建了一个 tmpnam() 并将其写入线程结构,但您可以按照自己的意愿使用它。
这是一些示例代码:(即时编写)
我确实是即时编写的,我尚未测试此代码,因此请注意修复任何错误。由于调用我自己的库,我没有发布我的真实代码。这里我没有检查
tmpnam()
、fork()
、open()
和malloc()< 的返回值/code>,你应该这样做。
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 through
pthread_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 a
tmpnam()
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.