首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

非废置模式O_NONBLOCK的作用是什么

2013-01-28 
非搁置模式O_NONBLOCK的作用是什么小弟在学习linux编程,知道open()里面有个非搁置模式O_NONBLOCK的参数模

非搁置模式O_NONBLOCK的作用是什么
小弟在学习linux编程,知道open()里面有个非搁置模式O_NONBLOCK的参数模式,但是不知道它的作用是什么,能不能举个具体点的例子告诉我???????非废置模式O_NONBLOCK的作用是什么
[解决办法]
以前写的,程序开始后自动计时,十秒后退出。如果输入stop,则立即退出。


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <fcntl.h>

static int thread_exit = 0;     /* running state of a thread */

void *showtime(void *arg)
{                               /* this is a long-time test */
        int *i = arg;
        time_t p;
        thread_exit = 0;
        while (--(*i) > 0) {
                printf("---%d---\n", *i);
                time(&p);
                printf("%s", ctime(&p));
                sleep(1);
        }
        thread_exit = 1;
        return ((void *)0);
}

int main(int argc, char **argv)
{
        pthread_t tid;
        pthread_attr_t attr;
        void *tret;
        char cmd[16] = { 0 };
        int err, times = 10;    /* default: test costs 10 secs */
        int flag;

        /* Use pthread */
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        err = pthread_create(&tid, NULL, showtime, &times);     /* showtime is a long-time test */
        if (err != 0) {
                perror("Can't create thread.\n");
                exit(1);
        }

        /* pthread_join(tid,&tret); *//* wait thread to terminate, pthread_join will block the main thread, so we give it up */

        flag = fcntl(0, F_GETFL, 0);


        flag 
[解决办法]
= O_NONBLOCK;
        if (fcntl(0, F_SETFL, flag) < 0) {      /* fgets no-block now */
                perror("Set stdin to non-block fails.");
                exit(1);
        }

        for (;;) {
                if (thread_exit)
                        break;
                if (fgets(cmd, sizeof(cmd) - 1, stdin) != NULL) {
                        if (strncmp(cmd, "stop", 4) == 0) {
                                pthread_cancel(tid);    /* cancel thread */
                                pthread_attr_destroy(&attr);
                                flag &= ~O_NONBLOCK;
                                if (fcntl(0, F_SETFL, flag) < 0) {      /* block stdin */
                                        perror("Set stdin to block fails.");
                                        exit(1);
                                }
                                break;
                        }
                } else {
                        usleep(10000);
                        continue;       /* not a must */


                }
        }

        return 0;
}


[解决办法]
叫非阻塞模式,很容易找到答案
#man 2 open
When possible, the file is opened in non-blocking mode.  Neither
the open() nor any subsequent operations on the file  descriptor
which  is  returned will cause the calling process to wait

大概意思就是open以及接下来在这个文件描述符上的操作,都不会让调用进程等待数据到来,而是直接返回-EAGAIN。
比如你操作一个video camera设备,
使用非阻塞模式打开/dev/video,读取数据时: v4l2 buffer队列有可用数据,返回数据;没有可用数据,则立即返回-EAGAIN
使用阻塞模式打开/dev/video,读取数据时: v4l2 buffer队列有可用数据,返回数据;没有可用数据,则当前进程休眠在这个buffer queue的等待队列上,直到新的一帧数据到达,唤醒等待队列上的进程才会返回。

热点排行