c - 在三个线程之间发送信号

标签 c linux multithreading

我必须编写一个使用 3 个线程的程序 - 一个用于读取字母,第二个用于计算字符,第三个用于输出它们。这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/stat.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>

int first[2];
int second[2];

void *input(void *ptr)
{
   char str[100];
   int length;

   while(1)
   {
      printf("Enter the message: ");
      fflush(stdout);
      length = read(STDIN_FILENO, str, sizeof(str));

      if(str[0] == ';')
         exit(2);

      if(length <= 0)
      {
         if(length == -1)
            perror("read");
         close(first[1]);
         exit(2);
      }

      if(write(first[1], str, length) != length)
      {
         perror("write");
         exit(2);
      }
   }
}

void *countChars(void *ptr)
{
   char str[100];
   int length, count = 0;

   while(1)
   {
      length = read(first[0], str, sizeof(str));
      if(length <= 0)
      {
         if(length == -1)
            perror("read");
         close(first[0]);
         close(second[1]);
         exit(2);
      }
      if(write(STDOUT_FILENO, str, length) != length)
      {
         perror("write");
         exit(2);
      }

      while(str[count] != '\n') count++;

      write(second[1], &count, sizeof(count));

      count = 0;
   }
}

void *output(void *ptr)
{
   int length, count = 0;

   while(1)
   {
      length = read(second[0], &count, sizeof(count));
      if(length < sizeof(count))
      {
         close(second[0]);
         exit(2);
      }

      printf("Number of characters: %d\n", count);
   }
}

int main()
{
   pthread_t t1, t2, t3;

   if(pipe(first) == -1)
   {
      printf("First pipe error");
      exit(1);
   }

   if(pipe(second) == -1)
   {
      printf("Second pipe error");
      exit(1);
   }

   pthread_create(&t1, NULL, input, NULL);
   pthread_create(&t2, NULL, countChars, NULL);
   pthread_create(&t3, NULL, output, NULL);

   pthread_join(t1, NULL);
   pthread_join(t2, NULL);
   pthread_join(t3, NULL);

   return 0;
}

它可以工作,但现在我必须在这里实现信号。发送 SIGUSR1 信号应该停止程序执行,直到发送 SIGUSR2 信号。

问题是当我发送信号时,只有一个线程收到它。因此,我必须使用 FIFO 来通知其他线程执行了哪个信号,并在其余线程中执行它。

我该怎么做?

最佳答案

信号传递给进程,而不是线程。因此,任何能够处理信号的线程都可以用来调用信号处理程序。您需要做的是弄清楚如何处理信号,然后决定如何将其传达给所有线程。您还没有真正描述“停止程序执行”的意思,所以我不得不猜测。

我建议结合使用 pthread_sigmasksigwait。您可以使用 pthread_sigmask 禁用工作线程中 SIGUSR1 和 SIGUSR2 的自动处理。然后在第四个信号处理程序线程中调用 sigwait 以显式处理这些信号。当信号处理程序线程收到 SIGUSR1 时,它会设置一个全局标志。工作线程定期检查该标志并在设置时进入休眠状态(可能是在条件变量上?)。然后信号处理程序线程循环并再次调用 sigwait。当它收到 SIGUSR2 时,它会唤醒工作线程,然后循环并再次调用 sigwait

关于c - 在三个线程之间发送信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25752006/

相关文章:

c - 定义 C 结构的一种令人困惑的方式

regex - 包含 Perl 一行的 Shell 脚本有空白结果

c++ - 在 std::thread 或 pthread_create 中创建的 QObject 的 QThreadData 是什么

c - Matlab 是否曾经复制传递给 mex 函数的数据?

c - 海湾合作委员会 C 错误 : expected ')' before numeric constant

c++ - 在 C++ (linux) 中设置 GPIO 的最快方法

java - 用户第二次启动线程时出现 IllegalStateException

c# - ThreadStatic 和 ASP.NET

c++ - 将代码从 C++ 转换为 C : `a+b > b+a` where a and b are `string`

c - 为什么输出后多了一个百分号?