简体   繁体   English

每20秒发出一次打印消息的信号(C多线程程序)

[英]Signal to print message every 20 seconds (C Multithreaded program)

I am trying to print a message every 20 seconds in a multithreaded program. 我试图在多线程程序中每20秒打印一条消息。 I have a server that has two threads. 我有一台具有两个线程的服务器。 One thread waits for incoming connections and makes a new thread for the client when it connects. 一个线程等待传入连接,并在客户端连接时为客户端创建一个新线程。

I have looked at this: C: SIGALRM - alarm to display message every second but with my code, I'm not sure where I would put the loop. 我看了一下: C:SIGALRM-每秒显示一次消息的警报,但是使用我的代码,我不确定将循环放在何处。 I am not allowed to make a new thread or use sleep() or any variation of sleep() . 我不能让一个新的线程或使用sleep()或任何变化sleep()

Code for server acceptor thread: 服务器接受器线程的代码:

while((csock = accept(sock, (struct sockaddr *) &theClient, (socklen_t *) &cl)))
{
        pthread_t newClient;
        new_sock = malloc(sizeof(socket_t));
        *new_sock = csock;
        pthread_create(&newClient, NULL, getInput, (void *) new_sock)
}

The other thread is just handling the client's input. 另一个线程只是处理客户的输入。 I tried putting the loop inside the above loop but then it never accepts new connections. 我尝试将循环放在上面的循环中,但是它从不接受新的连接。

How would I go about doing this? 我将如何去做呢? Any help would be appreciated. 任何帮助,将不胜感激。

Your problem seems to be currently that accept() is blocking the thread until a new connection comes in; 您的问题似乎是当前accept()阻塞了线程,直到有新的连接进入为止。 therefore you can't print anything. 因此您无法打印任何内容。

You could use non-blocking accept() in a loop to check for connections, and in the same loop wait until 20 seconds has passed. 您可以在循环中使用非阻塞accept()来检查连接,然后在同一循环中等待20秒。 Note that this is very inefficient as this loop doesn't stop; 请注意,由于此循环不会停止,因此效率非常低。 it uses 100% of 1 cpu core. 它使用100%的1 cpu内核。

// sock is listening
fcntl(sock,F_SETFD,O_NONBLOCK);

time_t tm = time(); // Unix timestamp, in seconds.

while(true) {
  csock = accept(sock, (sockaddr*)&theClient, (socklen_t*)&cl);
  if (csock==-1) {
    if (errno==EAGAIN || errno==EWOULDBLOCK); // No connection at the
                                              // moment, we need to try
                                              // again later.
    else break; // Some other error occurred
  }
  else { // If it is connected
    pthread_t newClient;
    new_sock = malloc(sizeof(socket_t));
    *new_sock = csock;
    pthread_create(&newClient,NULL,getInput,(void*)new_sock);
  }

  if (time()-tm>=20) { // At least 20 seconds have elapsed since
                       // last message.
    printf("Hello World!!\n");
    tm = time(); // Start waiting for another 20 seconds.
  }
}

Using select() to wait for a new connection would be far more efficient - you can also set a timeout which expires so that you can print your message 使用select()等待新连接会效率更高-您还可以设置超时时间,以便可以打印消息

Edit: You don't want to use a signal, because, as it says in the article you linked, you can't use printf from inside a signal handler. 编辑:您不想使用信号,因为正如您在链接的文章中所说的那样,您不能从信号处理程序内部使用printf If you use the signal handler to set a flag, you won't be able to read the flag unless you use non-blocking accept() (because otherwise accept() could block for a minute but nothing prints). 如果使用信号处理程序设置标志,则除非使用非阻塞accept(),否则您将无法读取该标志(因为accept()可能会阻塞一分钟,但不会打印任何内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM