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