標籤:des style blog color os io 檔案 for 資料
SYNOPSIS /* According to POSIX.1-2001 */ #include <sys/select.h> /* According to earlier standards */ #include <sys/time.h> #include <sys/types.h> #include <unistd.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); void FD_CLR(int fd, fd_set *set); int FD_ISSET(int fd, fd_set *set); void FD_SET(int fd, fd_set *set); void FD_ZERO(fd_set *set);
DESCRIPTION select() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corre- sponding I/O operation (e.g., read(2)) without blocking.
select函數對資料結構fd_set進行操作,fd_set是一個bitmap,該集合由開啟的檔案描述符構成。
注意,對於管道而言,select是通過檢查管道是否阻塞,來進行監聽的。只要所監聽的管道非阻塞,select就能立馬獲知,並將其在fd_set中的相應為置1。管道非阻塞只有兩種情況,一種是用戶端向管道寫東西,另一種是用戶端將管道關閉(讀端)。當然,對於非阻塞的情況,select能監聽到,但它並不會知道非阻塞是因為用戶端關閉管道還是用戶端向管道寫訊息。這需要我們自己來判斷。通常是通過read系統調用的傳回值來判斷。read返回0,說明是用戶端關閉了管道,read返回正值,說明是用戶端向管道寫東西了。
代碼
實現多個用戶端向服務端發送訊息,服務端將訊息列印在標準輸出上。實現如下:
server.c
/************************************************************************* > File Name: server.c > Author: KrisChou > Mail:[email protected] > Created Time: Sat 23 Aug 2014 02:37:58 PM CST ************************************************************************/#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <sys/select.h>#include <sys/time.h>int main(int argc, char* argv[]) { /* 開啟管道 */ int fd1, fd2, fd3 ; fd1= open(argv[1], O_RDONLY); fd2= open(argv[2], O_RDONLY); fd3= open(argv[3], O_RDONLY); printf("server: %d \n", getpid()); /* 設定select需要監聽的集合fd_set */ fd_set readset ,bak_set ; FD_ZERO(&bak_set); FD_SET(fd1, &bak_set); FD_SET(fd2, &bak_set); FD_SET(fd3, &bak_set); /* 設定select輪巡時間 */ struct timeval tm ; tm.tv_sec = 10 ; tm.tv_usec = 0 ; /* 可以在程式開始,使得伺服器端同時讀到3個訊息,此處可有可無 */ sleep(10); /* select是通過檢查管道是否阻塞,來進行監聽的。如果均為阻塞,select在輪巡時間內返回0。 * 如果有管道不阻塞,則select返回不阻塞的管道個數。 * 注意:當某個用戶端向管道發送資料,或者關閉管道,均屬於非阻塞狀況。 * 我們可以根據read的傳回值來區分這兩種情況 */ int nret ; /* select傳回值 */ char buf[1024]; while(1){ /* 每次select後都會改變select所監聽集合的值,因此需要在每次select的開始重新設定監聽集合。 * 如果不進行設定,那麼上次select後監聽集合置1的描述符仍然為1。 * 注意:在Linux系統下,時間也需要重新設定。 */ readset = bak_set ; tm.tv_sec = 10 ; tm.tv_usec = 0 ; nret = select(6 , &readset, NULL, NULL, &tm ); if(nret > 0) { printf("%d active ! \n", nret); if(FD_ISSET(fd1, &readset)) /* select傳回值大於0時,必然有管道不阻塞了,此時需要查看是哪個管道*/ { memset(buf, 0 , 1024); if(0 == read(fd1, buf, 1024)) { FD_CLR(fd1, &bak_set); /* 如果一個使用者其寫端已經關閉,那麼將其描述符從監聽集合中去除 */ }else { write(1, buf, strlen(buf)); } } if(FD_ISSET(fd2, &readset)) { memset(buf, 0 , 1024); if(0 == read(fd2, buf, 1024) ) { FD_CLR(fd2, &bak_set); }else { write(1, buf, strlen(buf)); } } if(FD_ISSET(fd3, &readset)) { memset(buf, 0 , 1024); if(read(fd3, buf, 1024) == 0) { FD_CLR(fd3, &bak_set); }else { write(1, buf, strlen(buf)); } } }else if(nret == 0) { printf("time out ! \n"); } }
}
client.c
/************************************************************************* > File Name: client.c > Author: KrisChou > Mail:[email protected] > Created Time: Sat 23 Aug 2014 02:30:54 PM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <time.h>int main(int argc, char *argv[]){ /* 開啟管道 */ printf("%d start ! \n", getpid()); int fd_sent; fd_sent = open(argv[1],O_WRONLY); if(fd_sent == -1) { perror("open"); exit(1); } printf("connect success! \n"); /* 用戶端向管道發送訊息,按ctrl+D退出迴圈 */ char line[1024]; /* 從標準輸入獲得要發送的訊息 */ char msg[1024]; /* 給訊息頭加一個pid,表示身份 */ while(memset(line,0,1024), fgets(line,1024,stdin) != NULL) { memset(msg,0,1024); sprintf(msg, "%d : %s \n", getpid(), line); write(fd_sent, msg, strlen(msg)); } close(fd_sent); return 0;}
執行程式:
mkfifo 1.fifo 2.fifo 3.fifo;
./server.exe 1.fifo 2.fifo 3.fifo
./client 1.fifo./client 2.fifo./client 3.fifo
注意1
如果將以下代碼去除:
if(0 == read(fd1, buf, 1024)){ FD_CLR(fd1, &bak_set); /* 如果一個使用者其寫端已經關閉,那麼將其描述符從監聽集合中去除 */}
fd2與fd3的也去除,則程式運行,會陷入死迴圈。原因如下:我們假設用戶端關閉了管道1.fifo,那麼select就會監聽到1.fifo非阻塞,會執行while迴圈中的if語句,檢測到是fd1非阻塞(原因是用戶端在寫端關閉管道了),那麼會執行write(1, buf, strlen(buf)); 因為buf為空白,什麼也不輸出。此時,由於while(1)死迴圈,select接著監聽,由於沒有將fd1從監聽集合中去除,select立馬要監聽到其非阻塞…如此迴圈往複,陷入死迴圈。當有一個用戶端關閉寫端,退出後,程式輸出結果為:
1 active!1 active!1 active!...
注意2
如果不強制退出,服務端程式永遠不會退出去。用戶端可以關閉管道退出。也可以在關閉後繼續開啟管道,與伺服器相連。
Linux之select系統調用_1