创建多个线程并通过 system() 调用 Cygwin 中的其他可执行文件?
我正在 Cygwin 中从事一个项目。尝试在 C 中创建多个线程,并且每个线程使用 system() 函数通过命令行调用另一个可执行文件,结果发现事情无法正常工作。具体来说,我的代码是这样的:
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
system("date ");
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
但它不起作用。我得到的错误是堆栈溢出的分段错误。无论如何,您知道如何通过创建多个线程来并行调用系统 shell 中的其他可执行文件吗? 谢谢。
I am working on a project in Cygwin. In an attempt to create multiple threads in C, and each thread calls another executable through the command line using the system() function, it turns out things are not working properly. Specifically, the code I have is like this:
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
system("date ");
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
But it does not work. The error I get is segmenetation fault with stack overflows. Anyway has an idea on how to call other executables in the system shell in parallel by creating multiple threads?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加此代码:
之前。
在
main()
中调用Add this code :
before
as called in
main()
.这里有几个错误:
在 main() 函数中,创建线程后,应该使用 pthread_exit() 退出所有单个线程。所以exit()的使用不适合这里。
在 main() 函数的末尾,在终止主线程之前,调用 pthread_join() 等待所有单个线程终止。
在所有子线程结束后,可以调用exit()来终止进程本身。
http://www.thegeekstuff.com/2012/04/terminate-c-线程/
Several bugs here:
In the main() function, after you create the thread, you should use pthread_exit() to exit from all the individual thread. So exit() use is not right here.
In the end of the main() function, just before you terminate the main thread, call pthread_join() to wait for all the individual thread to terminate.
At the end after all the child threads have terminated, you can call exit() to terminate the process itself.
http://www.thegeekstuff.com/2012/04/terminate-c-thread/
这个问题看起来值得向 Cygwin 邮件列表报告。
相反,您可以做的是取消线程并使用 fork()/exec() 或 spawn(_P_NOWAITO, ...) 创建子进程。
(spawn() 实际上是一个函数族;请参阅 /usr/include/process.h 详细信息,建议使用它,因为它可以避免 Cygwin 的高分叉开销。)
That issue looks worth reporting to the Cygwin mailing list.
What you can do instead is to do away with threads and use fork()/exec() or spawn(_P_NOWAITO, ...) to create the child processes.
(spawn() actually is a family of functions; see /usr/include/process.h for details. Its use is recommended as it avoids Cygwin's high fork overhead.)