linux select 用法
select系统调用是用来让我们的程序监视多个文件句柄(file descriptor)的状态变化的。程序会停在select这里等待,直到被监视的文件句柄有某一个或多个发生了状态改变。
文件在句柄在Linux里很多,如果你man某个函数,在函数返回值部分说到成功后有一个文件句柄被创建的都是的,如man socket可以看到“On success, a file descriptor for the new socket is returned.”而man 2 open可以看到“open() and creat() return the new file descriptor”,其实文件句柄就是一个整数,看socket函数的声明就明白了:
int socket(int domain, int type, int protocol);
当然,我们最熟悉的句柄是0、1、2三个,0是标准输入,1是标准输出,2是标准错误输出。0、1、2是整数表示的,对应的FILE *结构的表示就是stdin、stdout、stderr,0就是stdin,1就是stdout,2就是stderr。
比如下面这两段代码都是从标准输入读入9个字节字符:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char ** argv)
{
char buf[10] = "";
read(0, buf, 9); /* 从标准输入 0 读入字符 */
fprintf(stdout, "%s\n", buf); /* 向标准输出 stdout 写字符 */
return 0;
}
/* **上面和下面的代码都可以用来从标准输入读用户输入的9个字符** */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char ** argv)
{
char buf[10] = "";
fread(buf, 9, 1, stdin); /* 从标准输入 stdin 读入字符 */
write(1, buf, strlen(buf));
return 0;
}
继续上面说的select,就是用来监视某个或某些句柄的状态变化的。select函数原型如下:
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
函数的最后一个参数timeout显然是一个超时时间值,其类型是struct timeval *,即一个struct timeval结构的变量的指针,所以我们在程序
相关文档:
linux 小技巧
前言:因为用Linux的时间越来越长,所需要做的事也越来越多,效率成了我必需突破的瓶颈。在此总结一下这段时间用过的一些好的Linux技巧。以后时常补充这样自己要用的时候就很方便了。 Author:Ajian[文本处理]1、 ...
前言:因为用Linux的时间越来越长,所需要做的事也越来越多,效率成了我必需突破的瓶颈。 ......
bool RemoveNode(string& szFileName)
{
TiXmlDocument myDocument(szFileName);
bool loadOkay = myDocument.LoadFile();
if(loadOkay == false)
return false;
//获得根元素
TiXmlElement *rootElemen ......
1.关机
init 0或者 halt poweroff
重新启动
init 6或者 reboot
关机的时候通知下其他用户 Shutdown
shutdown -r +5 (五分钟之后关机)
2.在Linux下可以使用长文件或目录名,需要遵循的规则
/ 禁止使用
后缀是没有实际意义的
3.touch a 建立一个文件
4.shell命令的一般格式
$cmd ......
how to install apache, PHP and MySQL on Linux
This tutorial explains the installation of Apache web server, bundled
with PHP and MySQL server on a Linux machine. The tutorial is primarily for SuSE
9.2, 9.3, 10.0 & 10.1, but most of the steps ought to be valid for all
Linux-like operating ......
用惯了Windows平台开发工具的人,转到Linux平台上肯定有一个适应的过程。
Windows下面直接使用VS20....等等,配合MSDN文档,开发查询轻松自如。
而Linux平台,由于种类繁多没有像MSDN这样全面的技术文档,对于技术资料的查询就要依靠
[root@localhost]#MAN [函数名]
的方式来查询。大部分的C++函数时可以查询到得,但是 ......