一.非阻塞IO
1、简介
当应用程序对设备驱动进行操作的时候,如果不能获取到设备资源,非阻塞 IO,应用程序对应的线程不会挂起,它要么一直轮询等待,直到设备资源可以使用,要么就直接放弃。
应用程序使用非阻塞访问方式从设备读取数据,当设备不可用或数据未准备好的时候会立即向内核返回一个错误码,表示数据读取失败。应用程序会再次重新读取数据,这样一直往复循环,直到数据读取成功。
如果应用程序要采用非阻塞的方式来访问驱动设备文件,可以使用如下所示代码:
int fd;
int data = 0;
fd = open("/dev/xxx_dev", O_RDWR | O_NONBLOCK); /* 非阻塞方式打开 */
ret = read(fd, &data, sizeof(data)); /* 读取数据 */
2、轮询
如果用户应用程序以非阻塞的方式访问设备,设备驱动程序就要提供非阻塞的处理方式,也就是轮询。 poll、 epoll 和 select 可以用于处理轮询,应用程序通过 select、 epoll 或 poll 函数来查询设备是否可以操作,如果可以操作的话就从设备读取或者向设备写入数据。当应用程序调用 select、 epoll 或 poll 函数的时候设备驱动程序中的 poll 函数就会执行,因此需要在设备驱动程序中编写 poll 函数。
(1).select函数
select 函数在linux下原型如下:
#include <sys/select.h>
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
//参数说明:
nfds:所要监视的这三类文件描述集合中, 最大文件描述符加 1。
readfds:用于监视指定描述符集合的读变化,也就是监视这些文件是否可以读取,只要这些集合里面有一个文件可以读取那么 seclect 就会返回一个大于 0 的值表示文件可以读取。如果没有文件可以读取,那么就会根据 timeout 参数来判断是否超时。可以将 readfs设置为 NULL,表示不关心任何文件的读变化。
writefds、exceptfds:和 readfs 类似,只是 writefs 用于监视这些文件是否可以进行写操作。 exceptfds 用于监视这些文件的异常。
timerout:超时时间,当我们调用 select 函数等待某些文件描述符可以设置超时时间,超时时间使用结构体 struct timeval 表示,结构体定义如下所示:
struct timeval {
long tv_sec; /* 秒 */
long tv_usec; /* 微妙 */
};
返回值: 0,表示的话就表示超时发生,但是没有任何文件描述符可以进行操作; -1,发生错误;其他值,可以进行操作的文件描述符个数。
使用select函数对某个驱动文件进行非阻塞读应用程序示例:
void main(void)
{
int ret, fd; /* 要监视的文件描述符 */
fd_set readfds; /* 读操作文件描述符集 */
struct timeval timeout; /* 超时结构体 */
fd = open("dev_xxx", O_RDWR | O_NONBLOCK); /* 非阻塞式访问 */
FD_ZERO(&readfds); /* 清除 readfds */
FD_SET(fd, &readfds); /* 将 fd 添加到 readfds 里面 */
/* 构造超时时间 */
timeout.tv_sec = 0;
timeout.tv_usec = 500000; /* 500ms */
ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
switch (ret) {
case 0: /* 超时 */
printf("timeout!\r\n");
break;
case -1: /* 错误 */
printf("error!\r\n");
break;
default: /* 可以读取数据 */
if(FD_ISSET(fd, &readfds)) { /* 判断是否为 fd 文件描述符 */
/* 使用 read 函数读取数据 */
}
break;
}
}
(2).poll函数
在单个线程中, select 函数能够监视的文件描述符数量有最大的限制,一般为 1024,可以修改内核将监视的文件描述符数量改大,但是这样会降低效率!这个时候就可以使用 poll 函数,poll 函数本质上和 select 没有太大的差别,但是 poll 函数没有最大文件描述符限制, Linux 应用程序中 poll 函数原型如下所示:
#include <poll.h>
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
fds:要监视的文件描述符集合以及要监视的事件,为一个数组,数组元素都是结构体 pollfd类型的
pollfd 结构体如下所示:
struct pollfd {
int fd; /* 文件描述符 */
short events; /* 请求的事件 */
short revents; /* 返回的事件 */
};
fd :是要监视的文件描述符,如果fd无效的话那么events监视事件也就无效,并且revents
返回 0。revents是返回参数,也就是返回的事件,由 Linux 内核设置具体的返回事件。events是要监视的事件,可监视的事件类型如下所示:
POLLIN 有数据可以读取。
POLLPRI 有紧急的数据需要读取。
POLLOUT 可以写数据。
POLLERR 指定的文件描述符发生错误。
POLLHUP 指定的文件描述符挂起。
POLLNVAL 无效的请求。
POLLRDNORM 等同于 POLLIN
nfds:poll 函数要监视的文件描述符数量。
timeout:超时时间,单位为 ms。
返回值:返回 revents 域中不为 0 的 pollfd 结构体个数,也就是发生事件或错误的文件描述符数量; 0,超时; -1,发生错误,并且设置errno为错误类型。
使用 poll 函数对某个设备驱动文件进行非阻塞读访问的示例代码:
void main(void)
{
int ret;
int fd; /* 要监视的文件描述符 */
struct pollfd fds;
fd = open(filename, O_RDWR | O_NONBLOCK); /* 非阻塞式访问 */
/* 构造结构体 */
fds.fd = fd;
fds.events = POLLIN; /* 监视数据是否可以读取 */
ret = poll(&fds, 1, 500); /* 轮询文件是否可操作,超时 500ms */
if (ret) { /* 数据有效 */
......
/* 读取数据 */
......
} else if (ret == 0) { /* 超时 */
......
} else if (ret < 0) { /* 错误 */
......
}
}
(3).epoll函数
传统的selcet和poll函数都会随着所监听的fd数量的增加,出现效率低下的问题,而且poll 函数每次必须遍历所有的描述符来检查就绪的描述符,这个过程很浪费时间。为此,epoll应运而生,epoll 就是为处理大并发而准备的,一般常常在网络编程中使用 epoll 函数。
应用程序需要先使用epoll_create函数创建一个 epoll 句柄, epoll_create函数原型如下:
int epoll_create(int size)
size: 从Linux2.6.8开始此参数已经没有意义了,随便填写一个大于 0 的值就可以。
返回值: epoll 句柄,如果为-1 的话表示创建失败。
epoll 句柄创建成功以后使用 epoll_ctl 函数向其中添加要监视的文件描述符以及监视的事件, epoll_ctl 函数原型如下所示:
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
epfd:要操作的 epoll 句柄,也就是使用 epoll_create 函数创建的 epoll 句柄
op:表示要对 epfd(epoll 句柄)进行的操作,可以设置为:
EPOLL_CTL_ADD 向 epfd 添加文件参数 fd 表示的描述符。
EPOLL_CTL_MOD 修改参数 fd 的 event 事件。
EPOLL_CTL_DEL 从 epfd 中删除 fd 描述符。
fd:要监视的文件描述符。
event:要监视的事件类型,为 epoll_event 结构体类型指针, epoll_event 结构体类型如下所示:
struct epoll_event {
uint32_t events; /* epoll 事件 */
epoll_data_t data; /* 用户数据 */
};
events: 表示要监视的事件,可选的事件如下所示:
EPOLLIN 有数据可以读取。
EPOLLOUT 可以写数据。
EPOLLPRI 有紧急的数据需要读取。
EPOLLERR 指定的文件描述符发生错误。
EPOLLHUP 指定的文件描述符挂起。
EPOLLET 设置 epoll 为边沿触发,默认触发模式为水平触发。
EPOLLONESHOT 一次性的监视,当监视完成以后还需要再次监视某个 fd,那么就需要将
fd 重新添加到 epoll 里面
可以进行‘或’操作,耶尔就是可以设置监视多个事件
返回值: 0,成功; -1,失败,并且设置 errno 的值为相应的错误码。
一切都设置好以后应用程序就可以通过 epoll_wait 函数来等待事件的发生,类似 select 函数。 epoll_wait 函数原型如下所示:
int epoll_wait(int epfd, struct epoll_event *events,
int maxevents, int timeout)
epfd: 要等待的 epoll。
events: 指向 epoll_event 结构体的数组,当有事件发生的时候 Linux 内核会填写 events。调用者可以根据 events 判断发生了哪些事件。
maxevents: events 数组大小,必须大于 0。
timeout: 超时时间,单位为 ms。
返回值: 0,超时; -1,错误;其他值,准备就绪的文件描述符数量。
注:epoll 更多的是用在大规模的并发服务器上,因为在这种场合下 select 和 poll 并不适合。当设计到的文件描述符(fd)比较少的时候就适合用 selcet 和 poll。
二.Linux驱动下的poll操作函数
当应用程序调用 select 或 poll 函数来对驱动程序进行非阻塞访问的时候,驱动程序file_operations 操作集中的 poll 函数就会执行。所以驱动程序的编写者需要提供对应的 poll 函数, poll 函数原型如下所示:
unsigned int (*poll) (struct file *filp, struct poll_table_struct *wait)
//参数说明:
filp: 要打开的设备文件(文件描述符)。
wait:结构体 poll_table_struct 类型指针, 由应用程序传递进来的。一般将此参数传递给poll_wait 函数。
返回值:向应用程序返回设备或者资源状态,可以返回的资源状态如下:
POLLIN 有数据可以读取。
POLLPRI 有紧急的数据需要读取。
POLLOUT 可以写数据。
POLLERR 指定的文件描述符发生错误。
POLLHUP 指定的文件描述符挂起。
POLLNVAL 无效的请求。
POLLRDNORM 等同于 POLLIN,普通数据可读
我们需要在驱动程序的 poll 函数中调用 poll_wait 函数, poll_wait 函数不会引起阻塞,只是将应用程序添加到 poll_table 中, poll_wait 函数定义linux/poll.h里面原型如下:
static inline void poll_wait(struct file * filp,
wait_queue_head_t * wait_address,
poll_table *p)
filp:就是poll函数中的filp要打开的设备文件(文件描述符)。
wait_address:等待列表头。
p:就是file_operations 中 poll 函数的 wait 参数。
当应用程序使用poll或select函数时驱动对应的poll函数示例:
/* 第一步
* 第二步
* 第三步
*
*/
wait_queue_head_t r_wait
/* 读操作 */
ssize_t xxx_read (struct file *filep, char __user *buf, size_t cnt, loff_t *plot)
{
/* 判断阻塞读还是非阻塞读 */
if(条件 & O_NONBLOCK) { /* 非阻塞读 */
if(读数据没读到){ /* 无效数据 */
return -EAGAIN;
}
}else{ /* 阻塞读 */
wait_event_interruptible(r_wait, 条件); /* 可被信号打断 */
}
}
/* poll操作 */
unsigned int xxx_poll (struct file *filep, struct poll_table_struct *wait)
{
int mask = 0;
/* poll函数必须调用poll_wait函数 */
poll_wait(filep, &dev->r_wait, wait);
/* 是否可读 */
if (读到数据的条件){ /* 按键按下,可读 */
mask = POLLIN | POLLRDNORM;
}
return mask;
}
/* 驱动入口函数 */
int __init xxx_init(void)
{
init_waitqueue_head(&r_wait);
}
/* 中断处理函数 */
irqreturn_t xxx(int irq, void *dev_id)
{
if(唤醒条件){
wake_up(&r_wait);
}
}
=========================
驱动代码编写:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/ide.h>
#include <linux/poll.h>
#define IMX6UIRQ_CNT 1
#define IMX6UIRQ_NAME "imx6uirq"
#define KEY_NUM 1
#define KEY0VALUE 0X01
#define INVAKEY 0XFF
/* key结构体 */
struct irq_keydesc{
int gpio; /* io编号 */
int irqnum; /* 中断号 */
unsigned char value; /* 键值 */
char name[10]; /* 名字 */
irqreturn_t (*handler) (int, void *); /* 中断处理函数 */
};
/* imx6uirq设备结构体 */
struct imx6uirq_dev{
dev_t devid;
int major;
int minor;
struct cdev cdev;
struct class *class;
struct device *device;
struct device_node *nd;
struct irq_keydesc irqkey[KEY_NUM];
struct timer_list timer;
atomic_t keyvalue;
atomic_t releasekey;
wait_queue_head_t r_wait; /* 读等待队列头 */
};
struct imx6uirq_dev imx6uirq; /* irq设备 */
static int imx6uirq_open(struct inode *inode, struct file *filp)
{
filp->private_data = &imx6uirq;
return 0;
}
static ssize_t imx6uirq_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char keyvalue;
unsigned char releasekey;
struct imx6uirq_dev *dev = filp->private_data;
if(filp->f_flags & O_NONBLOCK) { /* 非阻塞 */
if(atomic_read(&dev->releasekey) == 0) {
return -EAGAIN;
}
} else /* 阻塞 */
{
/* 等待事件 */
wait_event_interruptible(dev->r_wait, atomic_read(&dev->releasekey)); /* 等待按键有效 */
}
#if 0
/* 等待事件 */
wait_event_interruptible(dev->r_wait, atomic_read(&dev->releasekey)); /* 等待按键有效 */
#endif
#if 0
DECLARE_WAITQUEUE(wait, current); /* 定义一个等待队列项 */
if(atomic_read(&dev->releasekey) == 0) { /* 按键没按下 */
add_wait_queue(&dev->r_wait, &wait); /* 将队列项添加到等待队列头 */
__set_current_state(TASK_INTERRUPTIBLE); /* 当前进程设置为可被打断的状态 */
schedule(); /* 切换 */
/* 唤醒以后从这里运行 */
if (signal_pending(current)) {
ret = -ERESTARTSYS;
goto data_error;
}
__set_current_state(TASK_RUNNING); /* 将当前任务设置为运行状态 */
remove_wait_queue(&dev->r_wait, &wait); /* 将对应的队列项从等待队列头删除 */
}
#endif
#if 0
DECLARE_WAITQUEUE(wait, current); /* 定义一个等待队列项 */
add_wait_queue(&dev->r_wait, &wait); /* 将队列项添加到等待队列头 */
__set_current_state(TASK_INTERRUPTIBLE); /* 当前进程设置为可被打断的状态 */
schedule(); /* 切换 */
/* 唤醒以后从这里运行 */
if (signal_pending(current)) {
ret = -ERESTARTSYS;
goto data_error;
}
#endif
keyvalue = atomic_read(&dev->keyvalue);
releasekey = atomic_read(&dev->releasekey);
if(releasekey) { /* 有效按键 */
if(keyvalue & 0x80) {
keyvalue &= ~0x80;
ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
} else {
goto data_error;
}
atomic_set(&dev->releasekey, 0); /* 按下标志清零 */
} else {
goto data_error;
}
data_error:
#if 0
__set_current_state(TASK_RUNNING); /* 将当前任务设置为运行状态 */
remove_wait_queue(&dev->r_wait, &wait); /* 将对应的队列项从等待队列头删除 */
#endif
return ret;
}
static unsigned int imx6uirq_poll(struct file *filp, struct poll_table_struct * wait)
{
int mask = 0;
struct imx6uirq_dev *dev = filp->private_data;
poll_wait(filp, &dev->r_wait, wait);
/* 是否可读 */
if (atomic_read(&dev->releasekey)) { /* 按键按下,可读 */
mask = POLLIN | POLLRDNORM; /* 返回POLLIN*/
}
return mask;
}
/* 操作集 */
static const struct file_operations imx6uirq_fops = {
.owner = THIS_MODULE,
.open = imx6uirq_open,
.read = imx6uirq_read,
.poll = imx6uirq_poll,
};
/* 按键中断处理函数 */
static irqreturn_t key0_handler(int irq, void *dev_id)
{
struct imx6uirq_dev *dev = dev_id;
dev->timer.data = (volatile long)dev_id;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(20)); /* 20ms定时 */
return IRQ_HANDLED;
}
/* 定时器处理函数 */
static void timer_func(unsigned long arg) {
int value = 0;
struct imx6uirq_dev *dev = (struct imx6uirq_dev*)arg;
value = gpio_get_value(dev->irqkey[0].gpio);
if(value == 0) { /* 按下 */
atomic_set(&dev->keyvalue, dev->irqkey[0].value);
} else if(value == 1) { /* 释放 */
atomic_set(&dev->keyvalue, 0X80 | (dev->irqkey[0].value));
atomic_set(&dev->releasekey, 1); /* 完成的按键过程 */
}
/* 唤醒进程 */
if(atomic_read(&dev->releasekey)) {
wake_up(&dev->r_wait);
}
}
/* 按键初始化 */
static int keyio_init(struct imx6uirq_dev *dev)
{
int ret = 0;
int i = 0;
/* 1,按键初始化 */
dev->nd = of_find_node_by_path("/key");
if(dev->nd == NULL) {
ret = -EINVAL;
goto fail_nd;
}
for(i = 0; i < KEY_NUM; i++) {
dev->irqkey[i].gpio = of_get_named_gpio(dev->nd, "key-gpios", i);
}
for(i = 0; i < KEY_NUM; i++) {
memset(dev->irqkey[i].name, 0, sizeof(dev->irqkey[i].name));
sprintf(dev->irqkey[i].name, "KEY%d", i);
gpio_request(dev->irqkey[i].gpio, dev->irqkey[i].name);
gpio_direction_input(dev->irqkey[i].gpio);
dev->irqkey[i].irqnum = gpio_to_irq(dev->irqkey[i].gpio); /* 获取中断号 */
#if 0
dev->irqkey[i].irqnum = irq_of_parse_and_map(dev->nd, i);
#endif
}
dev->irqkey[0].handler = key0_handler;
dev->irqkey[0].value = KEY0VALUE;
/* 2,按键中断初始化 */
for(i = 0; i < KEY_NUM; i++) {
ret = request_irq(dev->irqkey[i].irqnum, dev->irqkey[i].handler,
IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,
dev->irqkey[i].name, &imx6uirq);
if(ret) {
printk("irq %d request failed!\r\n", dev->irqkey[i].irqnum);
goto fail_irq;
}
}
/* 3、初始化定时器 */
init_timer(&imx6uirq.timer);
imx6uirq.timer.function = timer_func;
return 0;
fail_irq:
for(i = 0; i < KEY_NUM; i++) {
gpio_free(dev->irqkey[i].gpio);
}
fail_nd:
return ret;
}
/* 驱动入口函数 */
static int __init imx6uirq_init(void)
{
int ret = 0;
/* 注册字符设备驱动 */
imx6uirq.major = 0;
if(imx6uirq.major) { /* 给定主设备号 */
imx6uirq.devid = MKDEV(imx6uirq.major, 0);
ret = register_chrdev_region(imx6uirq.devid, IMX6UIRQ_CNT, IMX6UIRQ_NAME);
} else { /* 没给定设备号 */
ret = alloc_chrdev_region(&imx6uirq.devid, 0, IMX6UIRQ_CNT, IMX6UIRQ_NAME);
imx6uirq.major = MAJOR(imx6uirq.devid);
imx6uirq.minor = MINOR(imx6uirq.devid);
}
if(ret < 0) {
goto fail_devid;
}
printk("imx6uirq major = %d, minor = %d\r\n", imx6uirq.major, imx6uirq.minor);
/* 2,初始化cdev */
imx6uirq.cdev.owner = THIS_MODULE;
cdev_init(&imx6uirq.cdev, &imx6uirq_fops);
/* 3,添加cdev */
ret = cdev_add(&imx6uirq.cdev, imx6uirq.devid, IMX6UIRQ_CNT);
if (ret)
goto fail_cdevadd;
/* 4、创建类 */
imx6uirq.class = class_create(THIS_MODULE, IMX6UIRQ_NAME);
if(IS_ERR(imx6uirq.class)) {
ret = PTR_ERR(imx6uirq.class);
goto fail_class;
}
/* 5,创建设备 */
imx6uirq.device = device_create(imx6uirq.class, NULL, imx6uirq.devid, NULL, IMX6UIRQ_NAME);
if(IS_ERR(imx6uirq.device)) {
ret = PTR_ERR(imx6uirq.device);
goto fail_device;
}
/* 初始化IO */
ret = keyio_init(&imx6uirq);
if(ret < 0) {
goto fail_keyinit;
}
/* 初始化原子变量 */
atomic_set(&imx6uirq.keyvalue, INVAKEY);
atomic_set(&imx6uirq.releasekey, 0);
/* 等待队列头 */
init_waitqueue_head(&imx6uirq.r_wait);
return 0;
fail_keyinit:
fail_device:
class_destroy(imx6uirq.class);
fail_class:
cdev_del(&imx6uirq.cdev);
fail_cdevadd:
unregister_chrdev_region(imx6uirq.devid, IMX6UIRQ_CNT);
fail_devid:
return ret;
}
/* 驱动出口函数 */
static void __exit imx6uirq_exit(void)
{
int i = 0;
/*1、释放中断 */
for(i = 0; i < KEY_NUM; i++) {
free_irq(imx6uirq.irqkey[i].irqnum, &imx6uirq);
}
/* 2,释放IO */
for(i = 0; i < KEY_NUM; i++) {
gpio_free(imx6uirq.irqkey[i].gpio);
}
/* 3,删除定时器 */
del_timer_sync(&imx6uirq.timer);
/* 注销字符设备驱动 */
cdev_del(&imx6uirq.cdev);
unregister_chrdev_region(imx6uirq.devid, IMX6UIRQ_CNT);
device_destroy(imx6uirq.class, imx6uirq.devid);
class_destroy(imx6uirq.class);
}
module_init(imx6uirq_init);
module_exit(imx6uirq_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
============================
应用层代码编写:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <poll.h>
/*
*argc:应用程序参数个数
*argv[]:具体的参数内容,字符串形式
*./imx6uirqAPP <filename>
* ./imx6uirqAPP /dev/mx6uirq
*/
int main(int argc, char *argv[])
{
//fd_set readfds;
struct pollfd fds;
int fd, ret;
char *filename;
unsigned char data;
if(argc != 2) {
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR | O_NONBLOCK); /* 非阻塞打开 */
if(fd < 0) {
printf("file %s open failed!\r\n", filename);
return -1;
}
#if 0
/* 循环读取 */
while(1) {
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
timeout.tv_sec = 1;
timeout.tv_usec = 0; /* 1S */
ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
switch(ret) {
case 0: /* 超时 */
printf("select timeout!\r\n");
break;
case -1: /* 错误 */
break;
default: /* 可以读取数据 */
if(FD_ISSET(fd, &readfds)) {
ret = read(fd, &data, sizeof(data));
if(ret < 0) {
} else {
if(data)
printf("key value = %#x\r\n", data);
}
}
break;
}
}
#endif
/* 循环读取 */
while(1) {
fds.fd = fd;
fds.events = POLLIN;
ret = poll(&fds, 1, 500); /* 超时500ms */
if(ret == 0) { /* 超时 */
} else if( ret < 0) { /* 错误 */
} else { /* 可以读取 */
if(fds.revents | POLLIN) { /* 可读取 */
ret = read(fd, &data, sizeof(data));
if(ret < 0) {
} else {
if(data)
printf("key value = %#x\r\n", data);
}
}
}
}
close(fd);
return 0;
}