網路程式設計3 多程序 多執行緒程式設計 IO模型

2021-08-19 04:25:31 字數 3614 閱讀 5599

select系統呼叫第乙個引數需要+

1先說明一下, 在windows中, 並不要求select函式的第乙個引數總應該是fdmax +

1(在windows下, 給定-

1就行), 那linux中為什麼又是呢?

這就涉及到linux select第乙個引數的函式: 待測試的描述集的總個數。 但要注意, 待測試的描述集總是從0, 1, 2, ...開始的。 所以, 假如你要檢測的描述符為8, 9, 10, 那麼系統實際也要監測0, 1, 2, 3, 4, 5, 6, 7, 此時真正待測試的描述符的個數為11個, 也就是max(8, 9, 10) +

1 有兩點要注意:

1. 如果你要檢測描述符8, 9, 10, 但是你把select的第乙個引數定為8, 實際上只檢測0到7, 所以select不會感知到8, 9, 10描述符的變化。

2. 如果你要檢測描述符8, 9, 10, 且你把select的第乙個引數定為11, 實際上會檢測0

-10, 但是, 如果你不把描述如0

set到描述符中, 那麼select也不會感知到0描述符的變化。

所以, select感知到描述符變化的必要條件是, 第乙個引數要合理, 比如定義為fdmax+

1, 且把需要檢測的描述符set到描述集中。

4種io模型

訊號驅動式io:開啟套接字的訊號驅動io功能,通過sigaction系統呼叫安裝乙個訊號處理函式,當資料報準備好時,核心為程序產生sigio訊號,在訊號處理函式中呼叫recvfrom讀取資料報

select系統呼叫資料準備好的條件:

1.套接字接受緩衝區中的資料位元組數大於等於套接字接受緩衝區低水位標記線

2.該鏈結的讀半關閉,接收到fin訊號,讀將不阻塞而是返回0

3.套接字是乙個監聽套接字,且已完成的連線數不為0,accept不會阻塞

4.套接字上的錯誤處理

標準程式例項:

void str_cli(file *fp, int sockfd)}}

}

/*多執行緒併發伺服器例項*/

/*該**不涉及執行緒安全函式*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define port 1234

#define backlog 5

#define maxdatasize 1000

void process_cli(int connfd, struct sockaddr_in client);

void *function(void *arg);

struct arg;

int main()

int opt = so_reuseaddr;

setsockopt(listenfd, sol_socket, so_reuseaddr, &opt, sizeof(opt));

bzero(&server,sizeof(server));

server.sin_family = af_inet;

server.sin_port = htons(port);

server.sin_addr.s_addr = htonl(inaddr_any);

if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)

if(listen(listenfd, backlog) == -1)

len = sizeof(client);

while(1)

arg = (struct arg *)malloc(sizeof(struct arg));

arg->connfd = connfd;

memcpy((void *)&arg->client, &client, sizeof(client));

//建立執行緒,每個執行緒呼叫function函式,引數為arg

if(pthread_create(&tid, null, function, (void *)arg))

} close(listenfd);

}//處理客戶請求函式,將客戶傳遞過來的字串逆序返回

void process_cli(int connfd, struct sockaddr_in client)

cli_name[num - 1] = '\0';

printf("client's name is %s.\n", cli_name);

while(num = recv(connfd, recvbuf, maxdatasize, 0))

sendbuf[num - 1] = '\0';

send(connfd, sendbuf, strlen(sendbuf), 0);

} close(connfd);

}void *function(void *arg)

#include 

#include

#include

#include

#include

#include

#include

#include

#define port 1234

#define maxdatasize 100

void process(file *fp, int sockfd);

char *getmessage(char *sendline, int len, file *fp);

int main(int argc, char *argv)

if(((he = gethostbyname(argv[1])) == null))

if((sockfd = socket(af_inet, sock_stream, 0)) == -1)

bzero(&server, sizeof(server));

server.sin_family = af_inet;

server.sin_port = htons(port);

server.sin_addr = *((struct in_addr *)he->h_addr);

if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)

process(stdin, sockfd);

close(sockfd);

}void process(file *fp, int sockfd)

send(sockfd, sendline, strlen(sendline), 0);

while(getmessage(sendline, maxdatasize, fp) != null)

recvline[num] = '\0';

printf("server message:%s\n", recvline);

}}char *getmessage(char *sendline, int len, file *fp)

Linux網路程式設計(3) 多程序 多執行緒

在我的 多程序 這裡多程序採用傳統的多程序模型。每當有client發來的連線時建立乙個程序來處理連線,乙個子程序相應乙個連線。有了上篇單一程序的基礎,此處僅僅做簡單的改動便能夠實現。while 1 close clientfd 僅僅須要在while裡面加入程序的建立就可以,然後在子程序裡先關閉父程序...

python多執行緒 多程序程式設計

def defin canv w,h canvas np.zeros h,w canvas y0 1 y1,x0 1 x1 1 return canvas result pool multiprocessing.pool 將物件放入到類別中,包含了返回的物件 for anno in box pool...

python多執行緒,多程序程式設計。

程序,是目前計算機中為應用程式分配資源的最小單位 執行緒,是目前計算機中執行應用程式的最小單位 在實際系統中,其實程序都是被分為執行緒來實現的,所以參與時間片輪轉的是執行緒 但是管理應用程式的資源的單位和任務排程的單位都是程序。更像是乙個邏輯概念。執行緒是程序分出來的更精細的單位,執行緒間的上下文切...