Rechercher une page de manuel
select
Langue: ru
Version: 11 ÆÅ×ÒÁÌÑ 1996 (openSuse - 09/10/07)
Section: 2 (Appels système)
Sommaire
îáú÷áîéå
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - ÓÉÎÈÒÏÎÎÏÅ ÍÕÌØÔÉÐÌÅËÓÉÒÏ×ÁÎÉÅ ××ÏÄÁ-×Ù×ÏÄÁëòáôëáñ ó÷ïäëá
#include <sys/time.h>#include <sys/types.h>
#include <unistd.h>
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, sigset_t * sigmask);
FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);
FD_ZERO(fd_set *set);
ïðéóáîéå
æÕÎËÃÉÉ select É pselect ÖÄÕÔ ÉÚÍÅÎÅÎÉÑ ÓÔÁÔÕÓÁ ÎÅÓËÏÌØËÉÈ ÆÁÊÌÏ×ÙÈ ÄÅÓËÒÉÐÔÏÒÏ×.ïÎÉ ÐÏÞÔÉ ÉÄÅÎÔÉÞÎÙ, ÔÏÌØËÏ
- (i)
- select ÉÓÐÏÌØÚÕÅÔ ÔÁÊÍ-ÁÕÔ × ×ÉÄÅ struct timeval (Ó ÓÅËÕÎÄÁÍÉ É ÍÉËÒÏÓÅËÕÎÄÁÍÉ), ÔÏÇÄÁ ËÁË pselect ÉÓÐÏÌØÚÕÅÔ struct timespec (Ó ÓÅËÕÎÄÁÍÉ É ÎÁÎÏÓÅËÕÎÄÁÍÉ).
- (ii)
- æÕÎËÃÉÑ select ÍÏÖÅÔ ÏÂÎÏ×ÉÔØ ÐÁÒÁÍÅÔÒ timeout, ÞÔÏÂÙ ÓÏÏÂÝÉÔØ, ÓËÏÌØËÏ ×ÒÅÍÅÎÉ ÏÓÔÁÌÏÓØ. æÕÎËÃÉÑ pselect ÎÅ ÉÚÍÅÎÑÅÔ ÜÔÏÔ ÐÁÒÁÍÅÔÒ.
- (iii)
- æÕÎËÃÉÑ select ÎÅ ÓÏÄÅÒÖÉÔ ÐÁÒÁÍÅÔÒÁ sigmask, É ×ÅÄÅÔ ÓÅÂÑ ËÁË pselect Ó ÐÁÒÁÍÅÔÒÏÍ sigmask, ÒÁ×ÎÙÍ NULL.
ïÔÓÌÅÖÉ×ÁÀÔÓÑ ÔÒÉ ÎÅÚÁ×ÉÓÉÍÙÈ ÎÁÂÏÒÁ ÄÅÓËÒÉÐÔÏÒÏ×. ôÅ, ÞÔÏ ÐÅÒÅÞÉÓÌÅÎÙ × ÐÁÒÁÍÅÔÒÅ readfds, ÂÕÄÕÔ ÏÔÓÌÅÖÉ×ÁÔØÓÑ ÎÁ ÐÒÅÄÍÅÔ ÐÏÑ×ÌÅÎÉÑ ÎÏ×ÙÈ ÓÉÍ×ÏÌÏ×, ÄÏÓÔÕÐÎÙÈ ÄÌÑ ÞÔÅÎÉÑ (ÇÏ×ÏÒÑ ÔÏÞÎÅÅ, ÏÐÅÒÁÃÉÑ ÞÔÅÎÉÑ ÎÅ ÂÕÄÅÔ ÂÌÏËÉÒÏ×ÁÎÁ -- × ÞÁÓÔÎÏÓÔÉ, ÆÁÊÌÏ×ÙÊ ÄÅÓËÒÉÐÔÏÒ ÎÁÈÏÄÉÔÓÑ × ËÏÎÃÅ ÆÁÊÌÁ); ÔÅ, ÞÔÏ ÕËÁÚÁÎÙ × ÐÁÒÁÍÅÔÒÅ writefds, ÂÕÄÕÔ ÏÔÓÌÅÖÉ×ÁÔØÓÑ ÎÁ ÐÒÅÄÍÅÔ ÔÏÇÏ, ÞÔÏ ÏÐÅÒÁÃÉÑ ÚÁÐÉÓÉ ÎÅ ÂÕÄÅÔ ÚÁÂÌÏËÉÒÏ×ÁÎÁ; ÔÅ ÖÅ, ÞÔÏ ÕËÁÚÁÎÙ × ÐÁÒÁÍÅÔÒÅ exceptfds, ÂÕÄÕÔ ÏÔÓÌÅÖÉ×ÁÔØÓÑ ÎÁ ÐÒÅÄÍÅÔ ÉÓËÌÀÞÉÔÅÌØÎÙÈ ÓÉÔÕÁÃÉÊ. ðÒÉ ×ÏÚ×ÒÁÔÅ ÉÚ ÆÕÎËÃÉÉ ÎÁÂÏÒÙ ÄÅÓËÒÉÐÔÏÒÏ× ÍÏÄÉÆÉÃÉÒÕÀÔÓÑ, ÞÔÏÂÙ ÐÏËÁÚÁÔØ, ËÁËÉÅ ÉÚ ÎÉÈ ÉÚÍÅÎÉÌÉ Ó×ÏÊ ÓÔÁÔÕÓ.
äÌÑ ÍÁÎÉÐÕÌÑÃÉÊ ÎÁÂÏÒÁÍÉ ÓÕÝÅÓÔ×ÕÀÔ ÞÅÔÙÒÅ ÍÁËÒÏÓÁ: FD_ZERO ÏÞÉÝÁÅÔ ÎÁÂÏÒ. FD_SET É FD_CLR ÄÏÂÁ×ÌÑÀÔ ÉÌÉ ÕÄÁÌÑÀÔ ÚÁÄÁÎÎÙÊ ÄÅÓËÒÉÐÔÏÒ ÉÚ ÎÁÂÏÒÁ. FD_ISSET ÐÒÏ×ÅÒÑÅÔ, Ñ×ÌÑÅÔÓÑ ÌÉ ÄÅÓËÒÉÐÔÏÒ ÞÁÓÔØÀ ÎÁÂÏÒÁ; ÜÔÏÔ ÍÁËÒÏÓ ÐÏÌÅÚÅÎ ÐÏÓÌÅ ×ÏÚ×ÒÁÔÁ ÉÚ ÆÕÎËÃÉÉ select.
n ÎÁ ÅÄÉÎÉÃÕ ÂÏÌØÛÅ ÓÁÍÏÇÏ ÂÏÌØÛÏÇÏ ÎÏÍÅÒÁ ÄÅÓËÒÉÐÔÏÒÁ ÉÚ ×ÓÅÈ ÎÁÂÏÒÏ×.
timeout~-- ÜÔÏ ×ÅÒÈÎÑÑ ÇÒÁÎÉÃÁ ×ÒÅÍÅÎÉ, ËÏÔÏÒÏÅ ÐÒÏÊÄÅÔ ÐÅÒÅÄ ×ÏÚ×ÒÁÔÏÍ ÉÚ select. íÏÖÎÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÎÏÌØ, ÐÒÉ ÜÔÏÍ select ÚÁ×ÅÒÛÉÔÓÑ ÎÅÍÅÄÌÅÎÎÏ. (üÔÏ ÐÏÌÅÚÎÏ ÄÌÑ ÐÅÒÉÏÄÉÞÅÓËÏÇÏ ÏÐÒÏÓÁ.) åÓÌÉ timeout ÒÁ×ÅÎ NULL (ÎÅÔ ÔÁÊÍ-ÁÕÔÁ), ÔÏ select ÂÕÄÅÔ ÏÖÉÄÁÔØ ÉÚÍÅÎÅÎÉÊ ÎÅÏÐÒÅÄÅÌÅÎÎÏÅ ×ÒÅÍÑ.
sigmask~-- ÜÔÏ ÕËÁÚÁÔÅÌØ ÎÁ ÍÁÓËÕ ÓÉÇÎÁÌÏ× (ÓÍ. sigprocmask(2)); ÅÓÌÉ ÜÔÏÔ ÐÁÒÁÍÅÔÒ ÎÅ ÒÁ×ÅÎ NULL, ÔÏ pselect ÓÎÁÞÁÌÁ ÚÁÍÅÝÁÅÔ ÔÅËÕÝÕÀ ÍÁÓËÕ ÓÉÇÎÁÌÏ× ÎÁ ÔÕ, ÎÁ ËÏÔÏÒÕÀ ÕËÁÚÙ×ÁÅÔ sigmask, ÚÁÔÅÍ ×ÙÐÏÌÎÑÅÔ select, É ×ÏÓÓÔÁÎÁ×ÌÉ×ÁÅÔ ÉÓÈÏÄÎÕÀ ÍÁÓËÕ ÓÉÇÎÁÌÏ×.
éÄÅÑ pselect × ÔÏÍ, ÞÔÏ ÅÓÌÉ ÎÕÖÎÏ ÐÏÄÏÖÄÁÔØ ÓÏÂÙÔÉÑ: ÓÉÇÎÁÌÁ ÉÌÉ ÁËÔÉ×ÎÏÓÔÉ ÎÁ ÆÁÊÌÏ×ÏÍ ÄÅÓËÒÉÐÔÏÒÅ, ÔÏ ÔÒÅÂÕÅÔÓÑ ÁÔÏÍÁÒÎÁÑ ÐÒÏ×ÅÒËÁ, ÞÔÏÂÙ ÐÒÅÄÏÔ×ÒÁÔÉÔØ race condition. (ðÒÅÄÐÏÌÏÖÉÍ, ÏÂÒÁÂÏÔÞÉË ÓÉÇÎÁÌÁ ÕÓÔÁÎÁ×ÌÉ×ÁÅÔ ÇÌÏÂÁÌØÎÙÊ ÆÌÁÇ É ×ÏÚ×ÒÁÝÁÅÔ ÕÐÒÁ×ÌÅÎÉÅ. ôÏÇÄÁ ÐÒÏ×ÅÒËÁ ÜÔÏÇÏ ÇÌÏÂÁÌØÎÏÇÏ ÆÌÁÇÁ, ÚÁ ËÏÔÏÒÏÊ ÓÌÅÄÕÅÔ select(), ÍÏÖÅÔ ÐÒÉ×ÅÓÔÉ Ë ÐÏÄ×ÉÓÁÎÉÀ, ÅÓÌÉ ÓÉÇÎÁÌ ÐÏÑ×ÌÑÅÔÓÑ ÓÒÁÚÕ ÐÏÓÌÅ ÐÒÏ×ÅÒËÉ, ÎÏ ÐÒÑÍÏ ÐÅÒÅÄ ×ÙÚÏ×ÏÍ select. ó ÄÒÕÇÏÊ ÓÔÏÒÏÎÙ, pselect ÐÏÚ×ÏÌÑÅÔ ÓÎÁÞÁÌÁ ÚÁÂÌÏËÉÒÏ×ÁÔØ ÓÉÇÎÁÌÙ, ÏÂÒÁÂÏÔÁÔØ ÐÒÉÛÅÄÛÉÅ ÓÉÇÎÁÌÙ, Á ÚÁÔÅÍ ×ÙÚ×ÁÔØ pselect() Ó ÖÅÌÁÅÍÏÊ sigmask, ÔÅÍ ÓÁÍÙÍ ÉÚÂÅÇÁÑ race condition.) ôÁË ËÁË Linux × ÎÁÓÔÏÑÝÅÅ ×ÒÅÍÑ ÎÅ ÓÏÄÅÒÖÉÔ ÓÉÓÔÅÍÎÏÇÏ ×ÙÚÏ×Á pselect(), ÔÅËÕÝÁÑ ÒÅÁÌÉÚÁÃÉÑ ÜÔÏÊ ÐÒÏÃÅÄÕÒÙ × glibc ×ÓÅ ÅÝÅ ÓÏÄÅÒÖÉÔ race condition.
÷ïú÷òáýáåíïå úîáþåîéå
ðÒÉ ÕÓÐÅÛÎÏÍ ÚÁ×ÅÒÛÅÎÉÉ select É pselect ×ÏÚ×ÒÁÝÁÀÔ ËÏÌÉÞÅÓÔ×Ï ÄÅÓËÒÉÐÔÏÒÏ×, ÎÁÈÏÄÑÝÉÈÓÑ × ÎÁÂÏÒÁÈ ÄÅÓËÒÉÐÔÏÒÏ×, ÐÒÉÞÅÍ ÜÔÏ ËÏÌÉÞÅÓÔ×Ï ÍÏÖÅÔ ÂÙÔØ ÒÁ×ÎÙÍ ÎÕÌÀ, ÅÓÌÉ ÔÁÊÍ-ÁÕÔ ÉÓÔÅËÁÅÔ, Á ÉÎÔÅÒÅÓÕÀÝÉÅ ÎÁÓ ÓÏÂÙÔÉÑ ÔÁË É ÎÅ ÐÒÏÉÚÏÛÌÉ. ðÒÉ ÏÛÉÂËÅ ×ÏÚ×ÒÁÝÁÅÔÓÑ -1, Á errno ÕÓÔÁÎÁ×ÌÉ×ÁÅÔÓÑ ÄÏÌÖÎÙÍ ÏÂÒÁÚÏÍ; ÎÁÂÏÒÙ ÄÅÓËÒÉÐÔÏÒÏ× É ÚÎÁÞÅÎÉÅ timeout ÓÔÁÎÏ×ÑÔÓÑ ÎÅÏÐÒÅÄÅÌÅÎÙ, ÐÏÜÔÏÍÕ ÐÒÉ ÏÛÉÂËÅ ÎÅÌØÚÑ ÐÏÌÁÇÁÔØÓÑ ÎÁ ÉÈ ÚÎÁÞÅÎÉÅ.ïûéâëé
- EBADF
- ÷ ÏÄÎÏÍ ÉÚ ÎÁÂÏÒÏ× ÎÁÈÏÄÉÔÓÑ ÎÅ×ÅÒÎÙÊ ÆÁÊÌÏ×ÙÊ ÄÅÓËÒÉÐÔÏÒ.
- EINTR
- âÙÌ ÐÏÊÍÁÎ ÎÅÚÁÂÌÏËÉÒÏ×ÁÎÎÙÊ ÓÉÇÎÁÌ.
- EINVAL
- n ÏÔÒÉÃÁÔÅÌØÎÏ.
- ENOMEM
- æÕÎËÃÉÑ select ÎÅ ÓÍÏÇÌÁ ×ÙÄÅÌÉÔØ ÕÞÁÓÔÏË ÐÁÍÑÔÉ ÄÌÑ ×ÎÕÔÒÅÎÎÉÈ ÔÁÂÌÉÃ.
úáíåþáîéå
÷ ÎÅËÏÔÏÒÙÈ ÐÒÏÇÒÁÍÍÁÈ select ×ÙÚÙ×ÁÅÔÓÑ Ó ÔÒÅÍÑ ÐÕÓÔÙÍÉ ÎÁÂÏÒÁÍÉ ÆÁÊÌÏ×, n ÒÁ×ÎÙÍ ÎÕÌÀ, É ÎÅÎÕÌÅ×ÙÍ ÚÎÁÞÅÎÉÅÍ timeout, ÞÔÏ Ñ×ÌÑÅÔÓÑ ÄÏ×ÏÌØÎÏ ÐÅÒÅÎÏÓÉÍÙÍ ÓÐÏÓÏÂÏÍ ÓÄÅÌÁÔØ ÚÁÄÅÒÖËÕ Ó ÍÉÌÌÉÓÅËÕÎÄÎÏÊ ÔÏÞÎÏÓÔØÀ.ðÏÄ Linux timeout ÉÚÍÅÎÑÅÔÓÑ, ÞÔÏÂÙ ÓÏÏÂÝÉÔØ ËÏÌÉÞÅÓÔ×Ï ×ÒÅÍÅÎÉ, ËÏÔÏÒÏÅ ÎÅ ÂÙÌÏ ÉÓÐÏÌØÚÏ×ÁÎÏ; ÂÏÌØÛÉÎÓÔ×Ï ÄÒÕÇÉÈ ÒÅÁÌÉÚÁÃÉÊ ÎÅ ÄÅÌÁÀÔ ÜÔÏÇÏ. üÔÏ ÐÒÉ×ÏÄÉÔ Ë ÐÒÏÂÌÅÍÁÍ ËÁË × ËÏÄÅ ÐÏÄ Linux, ËÏÔÏÒÙÊ ÞÉÔÁÅÔ ÚÎÁÞÅÎÉÅ timeout É ÐÅÒÅÎÏÓÉÔÓÑ × ÄÒÕÇÉÅ ÏÐÅÒÁÃÉÏÎÎÙÅ ÓÉÓÔÅÍÙ, ÔÁË É ËÏÇÄÁ ËÏÄ ÐÅÒÅÎÏÓÉÔÓÑ ÐÏÄ Linux É ÉÓÐÏÌØÚÕÅÔ ÐÒÉ ÜÔÏÍ struct timeval ÄÌÑ ÎÅÓËÏÌØËÉÈ ÆÕÎËÃÉÊ select × ÃÉËÌÅ ÂÅÚ ÐÏ×ÔÏÒÎÏÊ ÉÎÉÃÉÁÌÉÚÁÃÉÉ. óÞÉÔÁÊÔÅ, ÞÔÏ ÐÁÒÁÍÅÔÒ timeout ÎÅÏÐÒÅÄÅÌÅÎ ÐÏÓÌÅ ×ÏÚ×ÒÁÔÁ ÉÚ ÆÕÎËÃÉÉ select.
ðòéíåò
#include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> int main(void) { fd_set rfds; struct timeval tv; int retval; /* öÄÅÍ, ÐÏËÁ ÎÁ ÓÔÁÎÄÁÒÔÎÏÍ ××ÏÄÅ (fd 0) ÞÔÏ-ÎÉÂÕÄØ ÐÏÑ×ÉÔÓÑ. */ FD_ZERO(&rfds); FD_SET(0, &rfds); /* öÄÅÍ ÎÅ ÂÏÌØÛÅ ÐÑÔÉ ÓÅËÕÎÄ. */ tv.tv_sec = 5; tv.tv_usec = 0; retval = select(1, &rfds, NULL, NULL, &tv); /* îÅ ÐÏÌÁÇÁÅÍÓÑ ÎÁ ÚÎÁÞÅÎÉÅ tv! */ if (retval) printf("äÁÎÎÙÅ ÄÏÓÔÕÐÎÙ.\n"); /* ôÅÐÅÒØ FD_ISSET(0, &rfds) ×ÅÒÎÅÔ ÉÓÔÉÎÎÏÅ ÚÎÁÞÅÎÉÅ. */ else printf("äÁÎÎÙÅ ÎÅ ÐÏÑ×ÉÌÉÓØ × ÔÅÞÅÎÉÅ ÐÑÔÉ ÓÅËÕÎÄ.\n"); exit(0); }
óïïô÷åôóô÷éå óôáîäáòôáí
4.4BSD (ÆÕÎËÃÉÑ select ×ÐÅÒ×ÙÅ ÐÏÑ×ÉÌÁÓØ × 4.2BSD). ïÂÙÞÎÏ ÐÅÒÅÎÏÓÉÔÓÑ Ó/ÎÁ ÎÅ-BSD ÓÉÓÔÅÍÙ, ÐÏÄÄÅÒÖÉ×ÁÀÝÉÅ ÕÒÏ×ÅÎØ BSD-ÓÏËÅÔÏ× (×ËÌÀÞÁÑ ×ÁÒÉÁÎÔÙ System~V). ïÄÎÁËÏ ÚÁÍÅÔØÔÅ, ÞÔÏ ×ÁÒÉÁÎÔÙ System V ÏÂÙÞÎÏ ÕÓÔÁÎÁ×ÌÉ×ÁÀÔ ÚÎÁÞÅÎÉÅ ÐÅÒÅÍÅÎÎÏÊ timeout ÐÅÒÅÄ ×ÙÈÏÄÏÍ, Á ×ÁÒÉÁÎÔ BSD~-- ÎÅÔ.æÕÎËÃÉÑ pselect ÏÐÒÅÄÅÌÅÎÁ × IEEE Std 1003.1g-2000 (POSIX.1g). åÅ ÍÏÖÎÏ ÎÁÊÔÉ × glibc2.1 É ÐÏÚÄÎÅÅ. Glibc2.0 ÓÏÄÅÒÖÉÔ ÆÕÎËÃÉÀ Ó ÔÁËÉÍ ÉÍÅÎÅÍ, ÎÏ ÂÅÚ ÐÁÒÁÍÅÔÒÁ sigmask.
óíïôòé ôáëöå
accept(2), connect(2), poll(2), read(2), recv(2), send(2), sigprocmask(2), write(2)ðåòå÷ïä
Copyright (C) Alexey Mahotkin <alexm@hsys.msk.ru> 2000-2001Contenus ©2006-2024 Benjamin Poulain
Design ©2006-2024 Maxime Vantorre