#insmod scull_all.ko
2. ls -l > /dev/scull_pipe0
非阻塞测试:
/****************************
* function nbtest
* 测试非阻塞操作
* 用非阻塞性I/O把输入复制到输出,并在其间稍作延迟,延时时间由命令行传递
****************************/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int delay = 1, n,m =0;
char buffer[4096];
if(argc > 1)
delay = atoi(argv[1]);
fcntl(0, F_SETFL, fcntl(0, F_GETFL)|O_NONBLOCK);
fcntl(1, F_SETFL, fcntl(1, F_GETFL)|O_NONBLOCK);
while(1)
{
n = read(0, buffer, 4096);
if(n >= 0)
m = write(1, buffer, n);
if((n<0||m<0) && (errno != EAGAIN))
break;
sleep(delay);
}
perror(n<0 ? "stdin": "stdout");
exit(1);
打开两个终端:
1. cat /dev/scull_pipe02. ls -l > /dev/scull_pipe0
非阻塞测试:
/****************************
* function nbtest
* 测试非阻塞操作
* 用非阻塞性I/O把输入复制到输出,并在其间稍作延迟,延时时间由命令行传递
****************************/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int delay = 1, n,m =0;
char buffer[4096];
if(argc > 1)
delay = atoi(argv[1]);
fcntl(0, F_SETFL, fcntl(0, F_GETFL)|O_NONBLOCK);
fcntl(1, F_SETFL, fcntl(1, F_GETFL)|O_NONBLOCK);
while(1)
{
n = read(0, buffer, 4096);
if(n >= 0)
m = write(1, buffer, n);
if((n<0||m<0) && (errno != EAGAIN))
break;
sleep(delay);
}
perror(n<0 ? "stdin": "stdout");
exit(1);
}
附:/* 设备号 查看(cat /proc/devices)*/
/* 设备节点查看 (cat /dev/xxx) */
每一个设备节点与每个设备号一一对应,应用程序对设备节点的操作=对该设备的操作
fcntl即F_SETFL, F_GETFL的使用,设置文件的flags, 阻塞设置成非阻塞,非阻塞设置成阻塞:
1、获取文件的flags,即open函数的第二个参数:
flags = fcntl(fd,F_GETFL,0);
2、设置文件的flags:
fcntl(fd,F_SETFL,flags);
3、增加文件的某个flags,比如文件是阻塞的,想设置成非阻塞:
flags = fcntl(fd,F_GETFL,0);
flags |= O_NONBLOCK;
fcntl(fd,F_SETFL,flags);
4、取消文件的某个flags,比如文件是非阻塞的,想设置成为阻塞:
flags = fcntl(fd,F_GETFL,0);
flags &= ~O_NONBLOCK;
fcntl(fd,F_SETFL,flags);