select没有把进程放入wait queue吗?
时间:2010-07-22
来源:互联网
本帖最后由 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
复制代码
看情形,在for( ; ; )之间,不停的调用:
1,测试
2,cond_resched()
那分明是:没有把当前进程放入到wait queue,然后叫其他进程唤醒,而是不停的执行,直到被抢占或者文件描述符准备好。不知道我哪里理解错了???如果是这样cpu周期不是浪费了吗???
也就是说调用select的进程一直是出于running状态的了。。。
记得看过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}
1,测试
2,cond_resched()
那分明是:没有把当前进程放入到wait queue,然后叫其他进程唤醒,而是不停的执行,直到被抢占或者文件描述符准备好。不知道我哪里理解错了???如果是这样cpu周期不是浪费了吗???
也就是说调用select的进程一直是出于running状态的了。。。
作者: kgn28 发布时间: 2010-07-22
在fd->ops->poll里面,如果对应fd没有需要的事件,就会把current加入等待队列。
也就是说,current会被加入所有的fd的等待队列(如果它们都没有需要的事件的话)。 加入等待队列的这些waiter管理在table结构里面,退回do_select之前会清理。
在完成对所有fd的poll之后,如果没有发现需要的事件,最后会跑到poll_schedule_timeout,current在这里进入睡眠。
也就是说,current会被加入所有的fd的等待队列(如果它们都没有需要的事件的话)。 加入等待队列的这些waiter管理在table结构里面,退回do_select之前会清理。
在完成对所有fd的poll之后,如果没有发现需要的事件,最后会跑到poll_schedule_timeout,current在这里进入睡眠。
作者: kouu 发布时间: 2010-07-22
你可以选择用pause()函数来实现
作者: 0vk0 发布时间: 2010-07-22
pause():
pause函数使调用进程挂起直到有信号递达。如果信号的处理动作是终止进程,则进程终止,pause函数没有机会返回;如果信号的处理动作是忽略,则进程继续处于挂起状态,pause不返回;如果信号的处理动作是捕捉,则调用了信号处理函数之后pause返回-1,errno设置为EINTR,所以pause只有出错的返回值
参考《apue2》
pause函数使调用进程挂起直到有信号递达。如果信号的处理动作是终止进程,则进程终止,pause函数没有机会返回;如果信号的处理动作是忽略,则进程继续处于挂起状态,pause不返回;如果信号的处理动作是捕捉,则调用了信号处理函数之后pause返回-1,errno设置为EINTR,所以pause只有出错的返回值
参考《apue2》
作者: 0vk0 发布时间: 2010-07-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28