是否可以在 select() 的无限循环中使用 nanosleep?

发布于 2024-08-07 07:04:21 字数 244 浏览 1 评论 0原文

我有一个 C 程序,它使用 for(;;) 循环和 select() 从套接字执行接收/发送操作监视文件描述符。我还需要这个程序每 80 毫秒发送一个数据包到一个数据包,我该如何实现呢? 也许我可以使用 fork() ,子进程只需每 80 毫秒在 select() 监视的文件描述符之一中写入一个 ack 即可。 有更好的解决方案吗?

I have a C program that do recv/send operations from/to socket using a for(;;) loop and a select() to monitor the file descriptor. I need also this program to send a packet every 80msec to a packet, how can I implement this?
Maybe I can use a fork() and the child process simply write an ack in one of the file descriptor monitored by the select() every 80msec.
Is there better solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

口干舌燥 2024-08-14 07:04:21

当调用 select() 时,您可以使用超时参数来限制选择等待时间。

 struct timeval {
           long    tv_sec;         /* seconds */
           long    tv_usec;        /* microseconds */
       };

int select(int nfds, fd_set *readfds, fd_set *writefds,
              fd_set *exceptfds, struct timeval *timeout);

将超时限制为 80 毫秒并发送所需的数据包相当容易。

When calling select() you can use the timeout argument to limit the selection waiting time.

 struct timeval {
           long    tv_sec;         /* seconds */
           long    tv_usec;        /* microseconds */
       };

int select(int nfds, fd_set *readfds, fd_set *writefds,
              fd_set *exceptfds, struct timeval *timeout);

It is rather easy to limit the timeout to 80msec and send the required packet.

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