c - 选择系统调用
问题描述
我一直在做socket编程,下面是select系统调用。如果这个程序在 5 秒内没有得到输入,它将终止,否则它将在终端中执行命令。我不明白程序的哪个部分使给定的消息作为终端中的命令执行。例如,如果我们键入 ls 并输入它会在终端中执行 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
解决方案
我不明白代码的哪一部分负责执行 ls 命令。
代码的任何部分都没有执行命令。
当输入可用时,该命令将立即退出。它不会读取输入。相反,您启动程序的命令 shell 将在程序退出后读取输入并处理输入 - 即 shell 将执行ls
。
推荐阅读
- c++ - 如果只有 std::auto_ptr i 可用,我还应该使用智能指针吗?
- ruby-on-rails - actiondispatch cookieoverflow 发布请求
- webpack - Webpack Mix 不会在 Laravel 5.6 中更新文件
- android - 将 JSON 对象转换为 JSON 数组
- java - 在 Apache karaf 容器中启用码头请求日志记录/统计信息
- php - 尝试在 Laravel 5.5 中获取非对象的属性,同时从数组中获取对象
- typescript - TypeScript:任何实现特定接口的类
- mysql - Like查询优化
- java - Vaadin 中当前 UI 的访问方法
- java - 为什么 appium 停止在反应原生 android 应用程序中查找元素?