linux select與poll實現機制與執行個體分析,poll執行個體分析

來源:互聯網
上載者:User

linux select與poll實現機制與執行個體分析,poll執行個體分析
    我們直到上層對檔案操作結合select與poll可以實現阻塞操作,那麼究竟是如何?的呢?

select介面:
    int select(int nfds, fd_set *readset, fd_set *writeset,
               fd_set *exceptset, struct timeval *timeout);
      
其中:

nfds     
     需要檢查的檔案描述符個數,數值應該比是三組fd_set中最大數
     更大,而不是實際檔案描述符的總數。
readset    
     用來檢查可讀性的一組檔案描述符。
writeset
     用來檢查可寫性的一組檔案描述符。
exceptset
     用來檢查意外狀態的檔案描述符。(註:錯誤並不是意外狀態)
timeout
     NULL指標代表無限等待,否則是指向timeval結構的指標,代表最
     長等待時間。(如果其中tv_sec和tv_usec都等於0, 則檔案描述符
     的狀態不被影響,但函數並不掛起)

執行個體:
JNIEXPORT jint JNICALL nativeTtySelect(JNIEnv* env, jclass jclazz, int fd) {

int select_ret = 0;

fd_set rfds;

struct timeval tv;

FD_ZERO(&rfds);

FD_SET(fd, &rfds);

tv.tv_sec = 1;

tv.tv_usec = 0;


select_ret = select(fd + 1, &rfds, NULL, NULL, &tv);

return select_ret;

}

上面調用select後,在核心中會調用到do_select,裡面會阻塞:
int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
{
ktime_t expire, *to = NULL;
struct poll_wqueues table;
poll_table *wait;
int retval, i, timed_out = 0;
unsigned long slack = 0;

rcu_read_lock();
retval = max_select_fd(n, fds);
rcu_read_unlock();

if (retval < 0)
return retval;
n = retval;

poll_initwait(&table);
wait = &table.pt;
if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
wait->_qproc = NULL;
timed_out = 1;
}

if (end_time && !timed_out)
slack = select_estimate_accuracy(end_time);

retval = 0;
for (;;) {
unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;

inp = fds->in; outp = fds->out; exp = fds->ex;
rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;

for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
unsigned long in, out, ex, all_bits, bit = 1, mask, j;
unsigned long res_in = 0, res_out = 0, res_ex = 0;
const struct file_operations *f_op = NULL;
struct file *file = NULL;

in = *inp++; out = *outp++; ex = *exp++;
all_bits = in | out | ex;
if (all_bits == 0) {
i += __NFDBITS;
continue;
}

for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
int fput_needed;
if (i >= n)
break;
if (!(bit & all_bits))
continue;
file = fget_light(i, &fput_needed);
if (file) {
f_op = file->f_op;
mask = DEFAULT_POLLMASK;
if (f_op && f_op->poll) {
wait_key_set(wait, in, out, bit);
mask = (*f_op->poll)(file, wait);//調用驅動poll介面,裡面會調用poll_waite 來add 等待隊列到鏈表中
}
fput_light(file, fput_needed);
if ((mask & POLLIN_SET) && (in & bit)) {
res_in |= bit;
retval++;
wait->_qproc = NULL;
}
if ((mask & POLLOUT_SET) && (out & bit)) {
res_out |= bit;
retval++;
wait->_qproc = NULL;
}
if ((mask & POLLEX_SET) && (ex & bit)) {
res_ex |= bit;
retval++;
wait->_qproc = NULL;
}
}
}
if (res_in)
*rinp = res_in;
if (res_out)
*routp = res_out;
if (res_ex)
*rexp = res_ex;
cond_resched();//sleep and wait for wake up,那麼究竟在哪裡被wakeup的呢?下面會有分析。
}
wait->_qproc = NULL;
if (retval || timed_out || signal_pending(current))
break;
if (table.error) {
retval = table.error;
break;
}

/*
* If this is the first loop and we have a timeout
* given, then we convert to ktime_t and set the to
* pointer to the expiry value.
*/
if (end_time && !to) {
expire = timespec_to_ktime(*end_time);
to = &expire;
}

if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
to, slack))
timed_out = 1;
}

poll_freewait(&table);

return retval;
}


核心中fs poll:

struct sysfs_open_dirent {
atomic_t refcnt;
atomic_t event;
wait_queue_head_t poll;
struct list_head buffers; /* goes through sysfs_buffer.list */
};

static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
{
struct sysfs_buffer * buffer = filp->private_data;
struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
struct sysfs_open_dirent *od = attr_sd->s_attr.open;

/* need parent for the kobj, grab both */
if (!sysfs_get_active(attr_sd))
goto trigger;

poll_wait(filp, &od->poll, wait);//add poll wait queue

sysfs_put_active(attr_sd);

if (buffer->event != atomic_read(&od->event))
goto trigger;

return DEFAULT_POLLMASK;

trigger:
buffer->needs_read_fill = 1;
return DEFAULT_POLLMASK|POLLERR|POLLPRI;
}

wakeup:

void sysfs_notify_dirent(struct sysfs_dirent *sd)
{
struct sysfs_open_dirent *od;
unsigned long flags;

spin_lock_irqsave(&sysfs_open_dirent_lock, flags);

od = sd->s_attr.open;
if (od) {
atomic_inc(&od->event);
wake_up_interruptible(&od->poll);//在這裡喚醒的
}

spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
}
EXPORT_SYMBOL_GPL(sysfs_notify_dirent);

/* wakeup the userspace poll */

sysfs_notify(kobj, NULL, "xxxx");


那麼上面方法能否實現當前進程阻塞呢?看看下面的常用的wait_event_interruptible,也是先將等待隊列加入list之後,schedule()切換cpu執行其他進程,當前

進程休眠,喚醒是另外進程來wake up的,那麼實現方法一致,所以select與poll結合完全可以實現進程的阻塞。

#define __wait_event_interruptible(wq, condition, ret)            \
do {                                    \
     DEFINE_WAIT(__wait);                        \
                                     \
     for (;;) {                            \
         prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);  \   //將wq
加入等待隊列list
         if (condition)                        \
             break;                        \
         if (!signal_pending(current)) {                \
             schedule();                    \ //sleep and waite to wake up
             continue;                    \
         }                            \
         ret = -ERESTARTSYS;                    \
         break;                            \
     }                                \
     finish_wait(&wq, &__wait);                    \
} while (0)




執行個體:串口如何?阻塞讀取的?包含底層分析。

tty poll:
static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
poll_table *wait)
{
unsigned int mask = 0;

//add read and write wait queue
poll_wait(file, &tty->read_wait, wait);
poll_wait(file, &tty->write_wait, wait);
if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
mask |= POLLIN | POLLRDNORM;
if (tty->packet && tty->link->ctrl_status)
mask |= POLLPRI | POLLIN | POLLRDNORM;
if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
mask |= POLLHUP;
if (tty_hung_up_p(file))
mask |= POLLHUP;
if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
if (MIN_CHAR(tty) && !TIME_CHAR(tty))
tty->minimum_to_wake = MIN_CHAR(tty);
else
tty->minimum_to_wake = 1;
}
if (tty->ops->write && !tty_is_writelocked(tty) &&
tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
tty_write_room(tty) > 0)
mask |= POLLOUT | POLLWRNORM;
return mask;
}



wakeup:
通過wake_up(&tty->read_wait),來喚醒讀取進程的。
下面的函數flush_to_ldisc,是在每次有資料過來中斷接收讀取完buferr後會調用tty_insert_flip_string(),
之後會調度flush_to_ldisc從核心緩衝區push bufer給上層。

/**
 *    flush_to_ldisc
 *    @work: tty structure passed from work queue.
 *
 *    This routine is called out of the software interrupt to flush data
 *    from the buffer chain to the line discipline.
 *
 *    Locking: holds tty->buf.lock to guard buffer list. Drops the lock
 *    while invoking the line discipline receive_buf method. The
 *    receive_buf method is single threaded for each tty instance.
 */

static void flush_to_ldisc(struct work_struct *work)
{
    struct tty_struct *tty =
        container_of(work, struct tty_struct, buf.work);
    unsigned long     flags;
    struct tty_ldisc *disc;

    disc = tty_ldisc_ref(tty);
    if (disc == NULL)    /*  !TTY_LDISC */
        return;

    spin_lock_irqsave(&tty->buf.lock, flags);

    if (!test_and_set_bit(TTY_FLUSHING, &tty->flags)) {
        struct tty_buffer *head;
        while ((head = tty->buf.head) != NULL) {
            int count;
            char *char_buf;
            unsigned char *flag_buf;

            count = head->commit - head->read;
            if (!count) {
                if (head->next == NULL)
                    break;
                tty->buf.head = head->next;
                tty_buffer_free(tty, head);
                continue;
            }
            /* Ldisc or user is trying to flush the buffers
               we are feeding to the ldisc, stop feeding the
               line discipline as we want to empty the queue */
            if (test_bit(TTY_FLUSHPENDING, &tty->flags))
                break;
            if (!tty->receive_room)
                break;
            if (count > tty->receive_room)
                count = tty->receive_room;
            char_buf = head->char_buf_ptr + head->read;
            flag_buf = head->flag_buf_ptr + head->read;
            head->read += count;
            spin_unlock_irqrestore(&tty->buf.lock, flags);
            disc->ops->receive_buf(tty, char_buf,
                            flag_buf, count);
            spin_lock_irqsave(&tty->buf.lock, flags);
        }
        clear_bit(TTY_FLUSHING, &tty->flags);
    }

    /* We may have a deferred request to flush the input buffer,
       if so pull the chain under the lock and empty the queue */
    if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
        __tty_buffer_flush(tty);
        clear_bit(TTY_FLUSHPENDING, &tty->flags);
        wake_up(&tty->read_wait);
    }
    spin_unlock_irqrestore(&tty->buf.lock, flags);

    tty_ldisc_deref(disc);
}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.