我一直在做套接字编程,下面是select系统调用。如果这个程序没有在5秒内得到一个输入,它将终止,否则它将在终端中执行命令。我不明白是哪一部分程序使给定的消息在终端中作为命令执行。例如,如果我们输入ls并输入enter,它将在终端中执行ls命令,但我不明白代码的哪一部分负责执行ls命令。这是密码。
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5; //in seconds
tv.tv_usec = 0; //in microseconds
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1) //select failed
perror("select()");
else if (retval) //user input
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\n");
exit(EXIT_SUCCESS);
}//program exit发布于 2021-09-10 19:57:36
我不明白代码的哪一部分负责执行ls命令。
代码中没有任何部分正在执行该命令。
当输入可用时,命令立即退出。它不会读取输入。相反,启动程序的命令shell将在程序退出后读取输入,并处理输入-即shell将执行ls。
https://stackoverflow.com/questions/69137230
复制相似问题