[問題] select() system call問題

看板C_and_CPP (C/C++)作者時間12年前 (2013/11/19 23:20), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
以下程式碼,在linux系統下 使用gcc編譯 編譯後會出現 line 16: storage size of "master_set" isn't known "working_set" 查了一下該include也include了 為什麼這邊編譯後會出現錯誤呢 #include <stdio.h> #include <sys/types.h> #include <sys/unistd.h> #include <sys/select.h> #include <sys/time.h> int main(){ int i; struct timeval timeout; struct fd_set master_set, working_set; char buf[1024]; FD_ZERO(&master_set); FD_SET(0, &master_set); timeout.tv_sec = 5; timeout.tv_usec = 0; i = 0; while(1){ memcpy(&working_set, &master_set, sizeof(master_set)); select(1, &working_set, NULL, NULL, &timeout); if(FD_ISSET(0, &working_set)){ read(0, buf, sizeof(buf)); write(1, buf, strlen(buf)); } printf("iteration: %d\n", i++); } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.42.101.127

11/19 23:26, , 1F
fd_set master_set; 而不是 struct fd_set master_set;
11/19 23:26, 1F

11/19 23:27, , 2F
fd_set 應該已經是型態名稱了
11/19 23:27, 2F

11/19 23:44, , 3F
了解 謝謝喔
11/19 23:44, 3F
可以執行了,但是執行時 當我輸入某字串 + ENTER後 他應該會輸出我輸入的字串 而後我沒輸入的話應該是會等5秒 印一次 ITERATION 但是奇怪的是 我輸入完後 他第一次會等5秒 5秒後,就開始瘋狂印ITERATION了 不知哪裡邏輯有誤? 謝謝 ※ 編輯: kumusou 來自: 114.42.101.127 (11/19 23:52)

11/20 00:09, , 4F
每次都重設 timeout, select 可能會改 timeout
11/20 00:09, 4F

11/20 00:13, , 5F
太強了 謝謝你
11/20 00:13, 5F
文章代碼(AID): #1IYu6rCJ (C_and_CPP)
文章代碼(AID): #1IYu6rCJ (C_and_CPP)