库函数和系统调用的区别:
库函数有缓冲区,系统调用无缓冲区
系统调用每次都要从系统空间调用到内核空间,而库函数不需要(缓冲区)
库函数可以跨平台,而系统调用不可以
fcntl:
设置或者修改已打开的文件性质
int fcntl(int fd文件描述符,int cmd指定不同的宏来修改fd指向文件的属性,…)需要传参才需要第三个参数
宏:
F_DUPFD复制重定向
F_GETFL获取
F_SETFL设置
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char*argv[])
{
int fd;
if((fd = open(argv[1],O_RDWR | O_CREAT ,0655)) < 0)
{
perror("open file error!");
exit(1);
}
int flags ;
flags = fcntl(fd, F_GETFL);
flags = flags | O_APPEND | O_NONBLOCK; //注意:不能修改读写标志位,可以加其他标志位
fcntl(fd, F_SETFL, flags);
if (write(fd, "hello", 5) < 0)
{
perror("write data error!");
exit(1);
}
lseek(fd, 0, SEEK_SET);
close(0);
fcntl(fd, F_DUPFD, 0);
char buffer[1024];
scanf("%s",buffer);
printf("buffer = %s\n", buffer);
close(fd);
return 0;
}
阻塞与非阻塞方式读取:
非阻塞方式:有数据就读出来,没有就读不出来
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char **argv)
{
int fd;
if((fd = open(argv[1], O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open error");
exit(1);
}
char buffer[1024] = {0};
if(read(fd, buffer, sizeof(buffer)) < 0)
{
perror("read error");
exit(1);
}
close(fd);
printf("buffer = %s\n", buffer);
return 0;
}
阻塞:
int main(int argc, char **argv)
{
int fd;
if((fd = open(argv[1], O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open error");
exit(1);
}
int n_r;
char buffer[1024] = {0};
while(1)
{
if(n_r = read(fd, buffer, sizeof(buffer)) < 0)
{
perror("read error");
exit(1);
}
printf("buffer = %s\n", buffer);
if(n_r > 0)
{
break;
}
}
memset(buffer, 0, sizeof(buffer));
read(0,buffer,sizeof(buffer));
printf("buffer = %s\n", buffer);
close(fd);
return 0;
}
阻塞转为非阻塞:
int main(int argc, char **argv)
{
int fd;
if((fd = open(argv[1], O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open error");
exit(1);
}
int n_r;
char buffer[1024] = {0};
int flags = 0;
flags = fcntl(0,F_GETFL);
flags = flags | O_NONBLOCK;
fcntl(0, F_SETFL, flags);
read(0,buffer,sizeof(buffer));
printf("buffer = %s\n", buffer);
close(fd);
return 0;
}
I/O多路复用:
有数据可读就读,没有就打印hello world:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
int main(int agrc, char **argv)
{
struct timeval time;
time.tv_sec = 3;//秒
time.tv_usec = 0;//毫秒
fd_set r_set;
FD_SET(0, &fd_set);
select(1,&r_set,NULL,NULL,&time);
printf("hello world!\n");
return 0;
}
监听鼠标和键盘:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
int main(int agrc, char **argv)
{
struct timeval time;
time.tv_sec = 0;
time.tv_usec = 0;
fd_set r_set;
fd_set all_set;
int fd;
if ((fd = open("/dev/input/mouse0", O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open file error!");
exit(1);
}
FD_SET(fd, &all_set);
FD_SET(0, &all_set);
while (1)
{
r_set = all_set;
int ret = select(fd + 1, &r_set, NULL, NULL, &time); //#define NULL (void *)0
if (FD_ISSET(fd, &r_set) > 0)
{
int cor;
read(fd, &cor, sizeof(cor));
printf("cor = %d\n", cor);
}
if (FD_ISSET(0, &r_set) > 0)
{
char buffer[1024];
read(0, buffer, sizeof(buffer));
printf("buffer = %s\n", buffer);
}
printf("hello world!\n");
sleep(1);
//FD_SET(fd, &r_set);
//FD_SET(0, &r_set);
}
return 0;
}
听:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
int main(int agrc, char **argv)
{
struct timeval time;
time.tv_sec = 0;
time.tv_usec = 0;
fd_set r_set;
fd_set all_set;
int fd;
int fds[1024]; //保存监听的文件描述符
for (int i = 0; i < 1024; i++)
{
fds[i] = -1;
}
if ((fd = open("/dev/input/mouse0", O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open file error!");
exit(1);
}
//假设:打开100个设备文件, 100个文件描述符
FD_SET(fd, &all_set);
for (int i = 0; i < 1024; i++)
{
if (fds[i] == -1)
{
fds[i] = 0;
break;
}
}
for (int i = 0; i < FD_SETSIZE; i++)
{
if (fds[i] == -1)
{
fds[i] = fd;
break;
}
}
FD_SET(0, &all_set);
//FD_SET 100次
//select缺点:
//1\fd_set(1024):数组,静态分配空间,固定大小1024
//2\轮询:全盘轮询,效率最低;
//select优点: 跨平台 win32
while (1)
{
r_set = all_set;
int ret = select(fd + 1, &r_set, NULL, NULL, NULL); //#define NULL (void *)0
if (ret == -1)
{
perror("select error!\n");
exit(1);
}
for (int i = 0; i < 1024; i++)
{
if (fds[i] != -1)
{
// printf("fds[%d] = %d\n", i,fds[i]);
if (FD_ISSET(fds[i], &r_set) > 0)
{
if (fds[i] == fd)
{
printf("mouse!\n");
int cor;
read(fd, &cor, sizeof(cor));
printf("cor = %d\n", cor);
}
else if (fds[i] == 0)
{
printf("key!\n");
char buffer[1024];
read(0, buffer, sizeof(buffer));
printf("buffer = %s\n", buffer);
}
if (--ret == 0) //确保所有变化的文件描述符都被处理
{
printf("ret == 0!\n");
break;
}
}
}
}
//printf("hello world!\n");
//sleep(1);
//FD_SET(fd, &r_set);
//FD_SET(0, &r_set);
}
return 0;
}
poll函数:
原理与select一致,区别在于poll没有最大文件描述符的限制(使用链表的方式存储fd)
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <poll.h>
int main(int agrc, char **argv)
{
#if 0
struct timeval time;
time.tv_sec = 0;
time.tv_usec = 0;
fd_set r_set;
fd_set all_set;
#endif
int fd;
#if 0
int fds[1024]; //保存监听的文件描述符
for (int i = 0; i < 1024; i++)
{
fds[i] = -1;
}
#endif
if ((fd = open("/dev/input/mouse0", O_RDWR | O_CREAT, 0655)) < 0)
{
perror("open file error!");
exit(1);
}
struct pollfd fds[2];
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = fd;
fds[1].events = POLLIN;
while(1)
{
int ret = poll(fds, 2, -1);
for(int i = 0; i < 2; i++)
{
if(fds[i].events == fds[i].revents)
{
if(fds[i].fd == 0)
{
char buffer[1024];
read(fds[i].fd, buffer, sizeof(buffer));
printf("buffer = %s\n", buffer);
}
else if(fds[i].fd == fd)
{
int cor;
read(fds[i].fd, &cor, sizeof(cor));
printf("cor = %d\n", cor);
}
if(--ret == 0)
{
break;
}
}
}
//printf("hello world!\n");
}
return 0;
}