- 论坛徽章:
- 0
|
本帖最后由 kgn28 于 2010-07-22 13:20 编辑
记得看过0.12的select实现:
1,检查文件描述符,调用fd->ops->poll,测试文件描述符是否准备好。
2,如果没有,则把自己放到一个wait queue中,然后schedule。
也就是让自己去睡眠,等待被唤醒,但是看2.6的代码时,有些不理解:
http://lxr.linux.no/linux+v2.6.33/fs/select.c- 396int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
- 397{
- 398 ktime_t expire, *to = NULL;
- 399 struct poll_wqueues table;
- 400 poll_table *wait;
- 401 int retval, i, timed_out = 0;
- 402 unsigned long slack = 0;
- 403
- 404 rcu_read_lock();
- 405 retval = max_select_fd(n, fds);
- 406 rcu_read_unlock();
- 407
- 408 if (retval < 0)
- 409 return retval;
- 410 n = retval;
- 411
- 412 poll_initwait(&table);
- 413 wait = &table.pt;
- 414 if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
- 415 wait = NULL;
- 416 timed_out = 1;
- 417 }
- 418
- 419 if (end_time && !timed_out)
- 420 slack = estimate_accuracy(end_time);
- 421
- 422 retval = 0;
- 423 for (;;) {
- 424 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
- 425
- 426 inp = fds->in; outp = fds->out; exp = fds->ex;
- 427 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
- 428
- 429 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
- 430 unsigned long in, out, ex, all_bits, bit = 1, mask, j;
- 431 unsigned long res_in = 0, res_out = 0, res_ex = 0;
- 432 const struct file_operations *f_op = NULL;
- 433 struct file *file = NULL;
- 434
- 435 in = *inp++; out = *outp++; ex = *exp++;
- 436 all_bits = in | out | ex;
- 437 if (all_bits == 0) {
- 438 i += __NFDBITS;
- 439 continue;
- 440 }
- 441
- 442 for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
- 443 int fput_needed;
- 444 if (i >= n)
- 445 break;
- 446 if (!(bit & all_bits))
- 447 continue;
- 448 file = fget_light(i, &fput_needed);
- 449 if (file) {
- 450 f_op = file->f_op;
- 451 mask = DEFAULT_POLLMASK;
- 452 if (f_op && f_op->poll) {
- 453 wait_key_set(wait, in, out, bit);
- 454 mask = (*f_op->poll)(file, wait);
- 455 }
- 456 fput_light(file, fput_needed);
- 457 if ((mask & POLLIN_SET) && (in & bit)) {
- 458 res_in |= bit;
- 459 retval++;
- 460 wait = NULL;
- 461 }
- 462 if ((mask & POLLOUT_SET) && (out & bit)) {
- 463 res_out |= bit;
- 464 retval++;
- 465 wait = NULL;
- 466 }
- 467 if ((mask & POLLEX_SET) && (ex & bit)) {
- 468 res_ex |= bit;
- 469 retval++;
- 470 wait = NULL;
- 471 }
- 472 }
- 473 }
- 474 if (res_in)
- 475 *rinp = res_in;
- 476 if (res_out)
- 477 *routp = res_out;
- 478 if (res_ex)
- 479 *rexp = res_ex;
- 480 cond_resched();
- 481 }
- 482 wait = NULL;
- 483 if (retval || timed_out || signal_pending(current))
- 484 break;
- 485 if (table.error) {
- 486 retval = table.error;
- 487 break;
- 488 }
- 489
- 490 /*
- 491 * If this is the first loop and we have a timeout
- 492 * given, then we convert to ktime_t and set the to
- 493 * pointer to the expiry value.
- 494 */
- 495 if (end_time && !to) {
- 496 expire = timespec_to_ktime(*end_time);
- 497 to = &expire;
- 498 }
- 499
- 500 if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
- 501 to, slack))
- 502 timed_out = 1;
- 503 }
- 504
- 505 poll_freewait(&table);
- 506
- 507 return retval;
- 508}
复制代码 看情形,在for( ; ; )之间,不停的调用:
1,测试
2,cond_resched()
那分明是:没有把当前进程放入到wait queue,然后叫其他进程唤醒,而是不停的执行,直到被抢占或者文件描述符准备好。不知道我哪里理解错了???如果是这样cpu周期不是浪费了吗???
也就是说调用select的进程一直是出于running状态的了。。。 |
|