poll() 은 select()의 여러가지 부족한 부분들을 해결해 주며 select() 에 비해서 우수하다.
1. 사용자가 (highest-number fd + 1) 을 인자로 전달할 필요가 없다.
2. large-valued file descriptor 들에 대해서 좀더 효율적이다.
3. 단일 구조체 배열로 fd 집합을 정환ㄱ한 크기로 생성할 수 있다.
4. 입력 (events 필드)과 출력(revents 필드)이 분리되어 있어서, 변경없이, 배열을 재사용할 수 있다.
하지만 select()는 여전이 사용되고 있는데 이유는 아래와 같다.
1. 매우 간편하고, 몇몇 Unix 시스템은 poll() 지원하지 않는다.
2. 좀더 좋은 timeout 처리 능력을 제공한다.
int poll(struct pollfd *ufds, unsigned int nfds, int time-out);
struct pollfd {
int fd; //장치 식별 번호
short events; //이벤트 종류
short revents; //수신 이벤트
};
#include <stdio.h>
#include <unistd.h>
#include <sys/poll.h>
#define TIMEOUT 5 //poll timeout, in seconds
int main (void)
{
struct pollfd fds[2];
int ret;
//watch stdin for input
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;
//watch stdout for ability to write (almost always true)
fds[1].fd = STDOUT_FILENO;
fds[1].events = POLLOUT;
//All set, block!
ret = poll (fds, 2, TIMEOUT * 1000);
if (ret == -1) {
printf("poll(): error\n");
return 1;
}
if (!ret)
{
printf("%d seconds elapsed. \n", TIMEOUT);
return 0;
}
if (fds[0].revents & POLLIN)
printf("stdin is readable\n");
if (fds[1].revents & POLLOUT)
printf("stdout is writeable\n");
return 0;
}
'Linux_system' 카테고리의 다른 글
우분투 히든파일(숨김파일) 보이기/감추기 (0) | 2022.12.09 |
---|---|
c 에서 pthread 를 사용하여 shell script 실행 (0) | 2022.12.06 |
Multiplexed I/O - select() (0) | 2022.09.18 |
QT (0) | 2022.09.07 |
POSIX.1 interfaces (0) | 2022.08.23 |