创建多个线程并通过 system() 调用 Cygwin 中的其他可执行文件?

发布于 2024-09-29 18:32:57 字数 785 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-10-06 18:32:57

添加此代码:

for(t=0; t<NUM_THREADS; t++){
  pthread_join(threads[t], NULL);
}

之前。

pthread_exit(NULL);

main() 中调用

Add this code :

for(t=0; t<NUM_THREADS; t++){
  pthread_join(threads[t], NULL);
}

before

pthread_exit(NULL);

as called in main().

迷鸟归林 2024-10-06 18:32:57

这里有几个错误:

  1. 在 main() 函数中,创建线程后,应该使用 pthread_exit() 退出所有单个线程。所以exit()的使用不适合这里。

  2. 在 main() 函数的末尾,在终止主线程之前,调用 pthread_join() 等待所有单个线程终止。

  3. 在所有子线程结束后,可以调用exit()来终止进程本身。

http://www.thegeekstuff.com/2012/04/terminate-c-线程/

Several bugs here:

  1. 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.

  2. 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.

  3. 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/

夏花。依旧 2024-10-06 18:32:57

这个问题看起来值得向 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文