/11
lOMoARcPSD| 61549570
NOI DUNG 1:
Bai1:
#include <stdio.h>
#include <sys/select.h>
int main() { fd_set
fdset;
FD_ZERO(&fdset);
FD_SET(3, &fdset); // Set bit thứ 3 FD_SET(4,
&fdset); // Set bit thứ 4 prin("fds_bits[0] = %ld\n",
fdset.__fds_bits[0] ); // Hiển thị giá trị nhị phân của
fds_bits[0] for (int i = sizeof(fdset.__fds_bits[0])*8 - 1; i
>= 0; i--) { prin("%d", (fdset.__fds_bits[0] >> i) & 1);
}
prin("\n");
return 0;
}
Bai2:
check = FD_ISSET(sockfd, &read_fds); if
(check == 1) { prin("Socket có sẵn để
đọc\n");
}
else if (check == 0 ) { prin("Khong co
socket san de doc "); }
lOMoARcPSD| 61549570
Bai3:
FD_CLR(sockfd, &read_fds);
Bai4:
fd_set read_fds;
FD_ZERO(&read_fds); FD_SET(sockfd, &read_fds);
select(sockfd + 1, &read_fds, NULL, NULL, NULL);
Bai5:
struct meval meout; meout.tv_sec = 5;
meout.tv_usec = 0; select(sockfd + 1, &read_fds, NULL,
NULL, &meout);
Bai6:
-meout = NULL: select() sẽ chờ vô thời hạn.
-meout có giá trị: select() chờ tối đa thời gian chỉ định.
Bai7:
int sockfd1, sockfd2; fd_set
read_fds;
FD_ZERO(&read_fds);
FD_SET(sockfd1, &read_fds);
lOMoARcPSD| 61549570
FD_SET(sockfd2, &read_fds); int maxfd = (sockfd1 >
sockfd2) ? sockfd1 : sockfd2; select(maxfd + 1,
&read_fds, NULL, NULL, NULL);
Bai8:
for (int i = 0; i <= maxfd; i++) {
if (FD_ISSET(i, &read_fds)) {
// Xử lý socket i
}
}
Bai9: int result = select(sockfd + 1, &read_fds, NULL, NULL,
&meout); if (result == 0) { prin("Timeout xảy ra\n");
}
Bai10: int result = select(maxfd + 1, &read_fds, NULL, NULL,
&meout); if (result > 0) {
// Có FD sẵn sàng }
else if (result == 0) {
prin("Timeout\n");
} else { perror("select");
}
Bai11:
Giải thích: select() thay đổi tập fd_set (chỉ giữ lại FD sẵn sàng, xóa bỏ các FD không sẵn
sàng). Nếu muốn ếp tục theo dõi các FD ban đầu, cần khởi tạo lại.
lOMoARcPSD| 61549570
Bai12:
FD_ZERO(&read_fds);
FD_SET(listener, &read_fds); FD_SET(client,
&read_fds); maxfd = (listener > client) ? listener :
client; select(maxfd + 1, &read_fds, NULL, NULL,
NULL);
Bai13:
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds); FD_SET(sockfd,
&read_fds); maxfd = (sockfd > 0) ? sockfd : 0;
select(maxfd + 1, &read_fds, NULL, NULL, NULL);
Bai14:
fd_set readfds; struct
meval meout; while
(1)
{
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
int t;
scanf("&d",t); meout.tv_sec = t; meout.tv_usec = 0; int
result = select(sockfd + 1, &readfds, NULL, NULL, &meout);
}
lOMoARcPSD| 61549570
Bai15: int ready = select(maxfd + 1, &read_fds, NULL, NULL,
&meout); prin("%d socket sẵn sàng\n", ready);
Bai16:
for (int i = 0; i <= maxfd; i++) { if
(!FD_ISSET(i, &read_fds)) {
close(i);
}
}
Bai17:
fd_set excepds;
FD_ZERO(&excepds); FD_SET(sockfd, &excepds);
select(sockfd + 1, NULL, NULL, &excepds, &meout);
Bai18:
- Mặc định, Select có thể theo dõi tối đa FD_SETSIZE ( thường là 1024 ) socket Bai19:
Select là hàm blocking , nó sẽ chặn luồng thực thi cho đến khi có sự kiện xảy ra. Do đó thời gian
thực thi của select chủ yếu là thời gian đợi . Tuy nhiên ta có thchỉnh giá trị meout về 0 , lúc
này có thể coi select là hàm non-blocking . Tuy nhiên giá trị thời gian thực thi của select phụ
thuộc vào chi phí kernel khi nó phải chạy qua tất cả các bit để kiểm tra các FD đã sẵn sàng.
Bai20:
int my_select(int maxfd, fd_set *readfds,int t) { struct meval
*meout; meout.tv_sec = t; meout.tv_usec = 0; return
select(maxfd + 1, readfds, NULL, NULL, meout);
lOMoARcPSD| 61549570
}
NOI DUNG2:
Bai1: struct pollfd fds[1];
fds[0].fd = 0;
fds[0].events = POLLIN;
Bai2:
int ret = poll(fds, 1, 5000); // 5 giây meout
if (ret == -1) { perror("poll error");
} else if (ret == 0) { prin("Timeout, không có
dữ liệu\n");
} else { if (fds[0].revents & POLLIN) { prin("Có
dữ liệu từ stdin\n");
}
}
Bai3:
struct pollfd fds[2]; fds[0].fd
= sockfd1; fds[0].events =
POLLIN; fds[1].fd = sockfd2;
lOMoARcPSD| 61549570
fds[1].events = POLLIN;
poll(fds, 2, 10000);
Bai4:
for (int i = 0; i < 2; i++) { if (fds[i].revents & POLLIN) {
prin("Socket %d có dữ liệu\n", fds[i].fd);
}
}
Bai5:
poll(fds, 2, 0);
Bai6:
struct pollfd fds[1]; fds[0].fd = -1; // FD
không hợp lệ fds[0].events = POLLIN; if
(poll(fds, 1, 1000) > 0) { if (fds[0].revents &
POLLNVAL) { prin("FD không hợp lệ\n");
}
}
Bai7:
fds[0].events = POLLOUT; int ret =
poll(fds, 1, 5000); if (fds[0].revents &
POLLOUT) { prin("Socket sẵn sàng
ghi\n");
}
lOMoARcPSD| 61549570
Bai8:
fds[0].events = POLLIN | POLLOUT; if (poll(fds, 1, 5000) > 0) { if
(fds[0].revents & POLLIN) prin("Có dữ liệu để đọc\n"); if
(fds[0].revents & POLLOUT) prin("Sẵn sàng ghi\n");
}
Bai9: if (fds[0].revents & POLLHUP) {
prin("Client đóng kết nối\n");
}
Bai10:
int listen_fd, new_socket;
struct sockaddr_in address;
socklen_t addrlen; struct pollfd
fds[MAX_FDS]; int nfds = 0;
for(int i = 0; i < MAX_FDS; i++) {
fds[i].fd = -1; fds[i].events =
0; fds[i].revents = 0;
}
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed"); exit(EXIT_FAILURE);
}
address.sin_family = AF_INET; address.sin_addr.s_addr =
INADDR_ANY; address.sin_port = htons(PORT); if (bind(listen_fd,
(struct sockaddr *)&address, sizeof(address)) < 0) {
lOMoARcPSD| 61549570
perror("bind failed");
close(listen_fd); exit(EXIT_FAILURE);
} if (listen(listen_fd, 10) <
0) { perror("listen failed");
close(listen_fd);
exit(EXIT_FAILURE);
}
fds[0].fd = listen_fd;
fds[0].events = POLLIN; nfds = 1;
while (1) { int ret = poll(fds,
nfds, -1); if (ret < 0) {
perror("poll failed"); if
(errno == EINTR) connue;
break;
}
for (int i = 0; i < nfds; i++) { if
(fds[i].fd == -1 || fds[i].revents == 0) {
connue;
}
if (fds[i].fd == listen_fd) {
if (fds[i].revents & POLLIN) {
addrlen = sizeof(address);
new_socket = accept(listen_fd, (struct sockaddr *)&address, &addrlen); if
(new_socket < 0) {
perror("accept failed");
lOMoARcPSD| 61549570
} else { for (int j = 0;
j < MAX_FDS; j++) { if
(fds[j].fd == -1) { fds[j].fd =
new_socket; fds[j].events
= POLLIN; fds[j].revents =
0; if (j >= nfds) {
nfds = j + 1;
}
break;
}
}
if (nfds > MAX_FDS) { fprin(stderr, "Max FDs reached.
Rejecng fd %d.\n", new_socket); close(new_socket);
}
}
} else if (fds[i].revents & (POLLERR | POLLNVAL | POLLHUP)) {
exit(EXIT_FAILURE);
}
fds[i].revents = 0;
}
else {
// Xu li
client ...
}
lOMoARcPSD| 61549570
}
}
close(listen_fd); for(int i
= 0; i < nfds; i++) { if
(fds[i].fd > 0) {
close(fds[i].fd);
}
}

Preview text:

lOMoAR cPSD| 61549570 NOI DUNG 1: Bai1: #include #include int main() { fd_set fdset; FD_ZERO(&fdset);
FD_SET(3, &fdset); // Set bit thứ 3 FD_SET(4,
&fdset); // Set bit thứ 4 printf("fds_bits[0] = %ld\n",
fdset.__fds_bits[0] ); // Hiển thị giá trị nhị phân của
fds_bits[0] for (int i = sizeof(fdset.__fds_bits[0])*8 - 1; i
>= 0; i--) { printf("%d", (fdset.__fds_bits[0] >> i) & 1); } printf("\n"); return 0; } Bai2:
check = FD_ISSET(sockfd, &read_fds); if
(check == 1) { printf("Socket có sẵn để đọc\n"); }
else if (check == 0 ) { printf("Khong co socket san de doc "); } lOMoAR cPSD| 61549570 Bai3:
FD_CLR(sockfd, &read_fds); Bai4: fd_set read_fds;
FD_ZERO(&read_fds); FD_SET(sockfd, &read_fds);
select(sockfd + 1, &read_fds, NULL, NULL, NULL); Bai5:
struct timeval timeout; timeout.tv_sec = 5;
timeout.tv_usec = 0; select(sockfd + 1, &read_fds, NULL, NULL, &timeout); Bai6:
-timeout = NULL: select() sẽ chờ vô thời hạn.
-timeout có giá trị: select() chờ tối đa thời gian chỉ định. Bai7: int sockfd1, sockfd2; fd_set read_fds; FD_ZERO(&read_fds);
FD_SET(sockfd1, &read_fds); lOMoAR cPSD| 61549570
FD_SET(sockfd2, &read_fds); int maxfd = (sockfd1 >
sockfd2) ? sockfd1 : sockfd2; select(maxfd + 1,
&read_fds, NULL, NULL, NULL); Bai8:
for (int i = 0; i <= maxfd; i++) {
if (FD_ISSET(i, &read_fds)) { // Xử lý socket i } }
Bai9: int result = select(sockfd + 1, &read_fds, NULL, NULL,
&timeout); if (result == 0) { printf("Timeout xảy ra\n"); }
Bai10: int result = select(maxfd + 1, &read_fds, NULL, NULL,
&timeout); if (result > 0) { // Có FD sẵn sàng } else if (result == 0) { printf("Timeout\n"); } else { perror("select"); } Bai11:
Giải thích: select() thay đổi tập fd_set (chỉ giữ lại FD sẵn sàng, xóa bỏ các FD không sẵn
sàng). Nếu muốn tiếp tục theo dõi các FD ban đầu, cần khởi tạo lại. lOMoAR cPSD| 61549570 Bai12: FD_ZERO(&read_fds);
FD_SET(listener, &read_fds); FD_SET(client,
&read_fds); maxfd = (listener > client) ? listener :
client; select(maxfd + 1, &read_fds, NULL, NULL, NULL); Bai13: FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds); FD_SET(sockfd,
&read_fds); maxfd = (sockfd > 0) ? sockfd : 0;
select(maxfd + 1, &read_fds, NULL, NULL, NULL); Bai14: fd_set readfds; struct timeval timeout; while (1) { FD_ZERO(&readfds);
FD_SET(sockfd, &readfds); int t;
scanf("&d",t); timeout.tv_sec = t; timeout.tv_usec = 0; int
result = select(sockfd + 1, &readfds, NULL, NULL, &timeout); } lOMoAR cPSD| 61549570
Bai15: int ready = select(maxfd + 1, &read_fds, NULL, NULL,
&timeout); printf("%d socket sẵn sàng\n", ready); Bai16:
for (int i = 0; i <= maxfd; i++) { if
(!FD_ISSET(i, &read_fds)) { close(i); } } Bai17: fd_set exceptfds;
FD_ZERO(&exceptfds); FD_SET(sockfd, &exceptfds);
select(sockfd + 1, NULL, NULL, &exceptfds, &timeout); Bai18:
- Mặc định, Select có thể theo dõi tối đa FD_SETSIZE ( thường là 1024 ) socket Bai19:
Select là hàm blocking , nó sẽ chặn luồng thực thi cho đến khi có sự kiện xảy ra. Do đó thời gian
thực thi của select chủ yếu là thời gian đợi . Tuy nhiên ta có thể chỉnh giá trị timeout về 0 , lúc
này có thể coi select là hàm non-blocking . Tuy nhiên giá trị thời gian thực thi của select phụ
thuộc vào chi phí kernel khi nó phải chạy qua tất cả các bit để kiểm tra các FD đã sẵn sàng. Bai20:
int my_select(int maxfd, fd_set *readfds,int t) { struct timeval
*timeout; timeout.tv_sec = t; timeout.tv_usec = 0; return
select(maxfd + 1, readfds, NULL, NULL, timeout); lOMoAR cPSD| 61549570 } NOI DUNG2: Bai1: struct pollfd fds[1]; fds[0].fd = 0; fds[0].events = POLLIN; Bai2:
int ret = poll(fds, 1, 5000); // 5 giây timeout
if (ret == -1) { perror("poll error");
} else if (ret == 0) { printf("Timeout, không có dữ liệu\n");
} else { if (fds[0].revents & POLLIN) { printf("Có dữ liệu từ stdin\n"); } } Bai3:
struct pollfd fds[2]; fds[0].fd = sockfd1; fds[0].events = POLLIN; fds[1].fd = sockfd2; lOMoAR cPSD| 61549570 fds[1].events = POLLIN; poll(fds, 2, 10000); Bai4:
for (int i = 0; i < 2; i++) { if (fds[i].revents & POLLIN) {
printf("Socket %d có dữ liệu\n", fds[i].fd); } } Bai5: poll(fds, 2, 0); Bai6:
struct pollfd fds[1]; fds[0].fd = -1; // FD
không hợp lệ fds[0].events = POLLIN; if
(poll(fds, 1, 1000) > 0) { if (fds[0].revents &
POLLNVAL) { printf("FD không hợp lệ\n"); } } Bai7:
fds[0].events = POLLOUT; int ret =
poll(fds, 1, 5000); if (fds[0].revents &
POLLOUT) { printf("Socket sẵn sàng ghi\n"); } lOMoAR cPSD| 61549570 Bai8:
fds[0].events = POLLIN | POLLOUT; if (poll(fds, 1, 5000) > 0) { if
(fds[0].revents & POLLIN) printf("Có dữ liệu để đọc\n"); if
(fds[0].revents & POLLOUT) printf("Sẵn sàng ghi\n"); }
Bai9: if (fds[0].revents & POLLHUP) {
printf("Client đóng kết nối\n"); } Bai10: int listen_fd, new_socket; struct sockaddr_in address;
socklen_t addrlen; struct pollfd fds[MAX_FDS]; int nfds = 0;
for(int i = 0; i < MAX_FDS; i++) {
fds[i].fd = -1; fds[i].events = 0; fds[i].revents = 0; }
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed"); exit(EXIT_FAILURE); }
address.sin_family = AF_INET; address.sin_addr.s_addr =
INADDR_ANY; address.sin_port = htons(PORT); if (bind(listen_fd,
(struct sockaddr *)&address, sizeof(address)) < 0) { lOMoAR cPSD| 61549570 perror("bind failed");
close(listen_fd); exit(EXIT_FAILURE);
} if (listen(listen_fd, 10) < 0) { perror("listen failed"); close(listen_fd); exit(EXIT_FAILURE); } fds[0].fd = listen_fd;
fds[0].events = POLLIN; nfds = 1;
while (1) { int ret = poll(fds, nfds, -1); if (ret < 0) { perror("poll failed"); if (errno == EINTR) continue; break; }
for (int i = 0; i < nfds; i++) { if
(fds[i].fd == -1 || fds[i].revents == 0) { continue; }
if (fds[i].fd == listen_fd) {
if (fds[i].revents & POLLIN) { addrlen = sizeof(address);
new_socket = accept(listen_fd, (struct sockaddr *)&address, &addrlen); if (new_socket < 0) { perror("accept failed"); lOMoAR cPSD| 61549570 } else { for (int j = 0; j < MAX_FDS; j++) { if
(fds[j].fd == -1) { fds[j].fd = new_socket; fds[j].events = POLLIN; fds[j].revents = 0; if (j >= nfds) { nfds = j + 1; } break; } }
if (nfds > MAX_FDS) { fprintf(stderr, "Max FDs reached.
Rejecting fd %d.\n", new_socket); close(new_socket); } }
} else if (fds[i].revents & (POLLERR | POLLNVAL | POLLHUP)) { exit(EXIT_FAILURE); } fds[i].revents = 0; } else { // Xu li client ... } lOMoAR cPSD| 61549570 } } close(listen_fd); for(int i = 0; i < nfds; i++) { if (fds[i].fd > 0) { close(fds[i].fd); } }