日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【APUE】Chapter17 Advanced IPC sign extension 结构体内存对齐

發布時間:2025/3/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【APUE】Chapter17 Advanced IPC sign extension 结构体内存对齐 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

17.1 Introduction

這一章主要講了UNIX Domain Sockets這樣的進程間通訊方式,并列舉了具體的幾個例子。

?

17.2 UNIX Domain Sockets

這是一種特殊socket類型,主要用于高效的IPC,特點主要在于高效(因為省去了很多與數據無關的格式的要求)。

int socketpair(int?domain, int?type, int?protocol, int?sockfd[2]) 這個函數用于構建一對unix domain sockets;并且與之前的pipe函數不同,這里構建fd都是full-duplex的。

下面列舉一個poll + message queue + 多線程 的例子。

為什么要舉上面的例子?因為沒辦法直接用poll去管理多個message queue。

message queue在unix系統中有兩種標示方法:1. 全局用一個key 2. 進程內部用一個identifier

而poll關注的對象只能是file descriptor;所以,用unix domain sockets作為二者的橋梁。

例子包含兩個部分,reciver端和sender端。

reciver掛起來幾個message queue,每個queue單獨開一個線程去處理;主線程跟每個queue線程的關聯方式就是unix domain sockets。代碼如下:

1 #include "../apue.3e/include/apue.h" 2 #include <sys/poll.h> 3 #include <pthread.h> 4 #include <sys/msg.h> 5 #include <sys/socket.h> 6 7 #define NQ 3 8 #define MAXMSZ 512 9 #define KEY 0x123 10 11 struct threadinfo{ 12 int qid; 13 int fd; 14 }; 15 16 struct mymesg{ 17 long mtype; 18 char mtext[MAXMSZ]; 19 }; 20 21 void * helper(void *arg) 22 { 23 int n; 24 struct mymesg m; 25 struct threadinfo *tip = arg; 26 27 for(; ;) 28 { 29 memset(&m, 0, sizeof(m)); 30 if ((n = msgrcv(tip->qid, &m, MAXMSZ, 0, MSG_NOERROR))<0) { 31 err_sys("msgrcv error"); 32 } 33 /*來自一個消息隊列的內容 就特定的file desrciptor中*/ 34 if (write(tip->fd, m.mtext, n)<0) { 35 err_sys("write error"); 36 } 37 } 38 } 39 40 int main(int argc, char *argv[]) 41 { 42 int i, n, err; 43 int fd[2]; 44 int qid[NQ]; /*message queue在process內部的identifier*/ 45 struct pollfd pfd[NQ]; 46 struct threadinfo ti[NQ]; 47 pthread_t tid[NQ]; 48 char buf[MAXMSZ]; 49 50 /*給每個消息隊列設定處理線程*/ 51 for (i=0; i<NQ; i++) { 52 /*返回消息隊列的identifier 類似file descriptor*/ 53 if ((qid[i] = msgget((KEY+i), IPC_CREAT|0666))<0) { 54 err_sys("msgget error"); 55 } 56 printf("queue ID %d is %d\n", i, qid[i]); 57 /*構建unix domain sockets*/ 58 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)<0) { 59 err_sys("socketpair error"); 60 } 61 pfd[i].fd = fd[0]; /*main線程把住fd[0]這頭*/ 62 pfd[i].events = POLLIN; /*有data要去讀*/ 63 /* qid[i]在同一個process都可以用來表示同一個message queue */ 64 ti[i].qid = qid[i]; /*在每個線程中記錄要處理的消息隊列的id*/ 65 ti[i].fd = fd[1]; /*每個隊列的線程把住fd[1]這頭*/ 66 /*為每個消息隊列創建一個處理線程 并將對應的threadinfo參數傳入線程*/ 67 if ((err = pthread_create(&tid[i], NULL, helper, &ti[i]))!=0) { 68 err_exit(err, "pthread_create error"); 69 } 70 } 71 72 for (;;) { 73 /*一直輪詢著 直到有隊列可以等待了 再執行*/ 74 if (poll(pfd, NQ, -1)<0) { 75 err_sys("poll error"); 76 } 77 /*由于能進行到這里 則一定是有隊列ready了 找到所有ready的隊列*/ 78 for (i=0; i<NQ; i++) { 79 if (pfd[i].revents & POLLIN) { /*挑出來所有滿足POLLIN條件的*/ 80 if ((n=read(pfd[i].fd, buf, sizeof(buf)))<0) { 81 err_sys("read error"); 82 } 83 buf[n] = 0; /* 這個末尾賦'\0'是必要的 因為接下來要執行printf*/ 84 printf("queue id %d, message %s\n",qid[i],buf); 85 } 86 } 87 } 88 exit(0); 89 }

sender端,用command-line argument的方式讀入message的外部key,以及寫入message queue的數據,具體代碼如下:

#include "../apue.3e/include/apue.h" #include <sys/msg.h>#define MAXMSZ 512struct mymesg{long mtype;char mtext[MAXMSZ]; };int main(int argc, char *argv[]) {key_t key;long qid;size_t nbytes;struct mymesg m;if (argc != 3) {fprintf(stderr, "usage: sendmsg KEY message\n");exit(1);}key = strtol(argv[1], NULL, 0);if ((qid = msgget(key,0))<0) {err_sys("can't open queue key %s", argv[1]);}memset(&m, 0, sizeof(m));strncpy(m.mtext, argv[2], MAXMSZ-1);nbytes = strlen(m.mtext);m.mtype = 1;if (msgsnd(qid, &m, nbytes, 0)<0) {err_sys("can't send message");}exit(0); }

執行結果如下:

分析:

(1)unix socket domain在上述代碼中的好處主要是方便了多個message queue的管理

(2)引入unix socket domain雖然帶來了方便,但也在reciver中引入了兩次額外的cost:一個是line34的write,向unix domain socket多寫了一次;一個是line80的read,從unix domain socket多讀了一次。如果這種cost在可接受范圍內,那么unix socket domain就可以應用。

?

17.2.1 Naming UNIX Domain Sockets

上面介紹的這種socketpair的方式構造unix domain sockets,輸出是幾個fd,因此只能用于有親屬關系的process中。

如果要unrelated process之間用unix domain sockets通信,得從外面process能找到這個unix domain socket。

struct sockaddr_un{

  sa_family_t sun_family; ?/*AF_UNIX*/

  char sun_path[108]; ?/*pathname*/

}

這個結構體可以用來被構造成一個“可以被外面process找到的”的unix domain socket的地址,類似于“ip+port”的作用

具體需要如下三個步驟的操作:

1)fd = socket(AF_UNIX, SOCK_STREAM, 0) // 產生unix domain socket

2)un.sun_family = AF_UNIX?strcpy(un.sun_path, pathname)

3)bind(fd, (struct sockaddr *)&un, size) ?// 將unix domain socket與fd綁定

另,這里的pathname需要是一個獨一無二的文件名。后面的一系列內容,都把sockaddr_un按照ip+port進行理解就順暢了

有了結構體中sun_path這個文件名,這個unix domain socket就有了自己獨一無二的標識,其他進程就可以通過這個標識找到它。

1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <string.h> 5 6 int main(int argc, char *argv[]) 7 { 8 int fd, size; 9 struct sockaddr_un un; 10 11 un.sun_family = AF_UNIX; 12 memset(un.sun_path, 0, sizeof(un.sun_path)); 13 strcpy(un.sun_path, "foo.socket"); 14 15 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0))<0) { 16 err_sys("socket fail"); 17 } 18 size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 19 if (bind(fd, (struct sockaddr *)&un, size)<0) { 20 err_sys("bind failed"); 21 } 22 printf("UNIX domain socket bound\n"); 23 exit(0); 24 }

這里“foo.socket"不需要事先真的存在,它只需要是一個獨特的名稱就可以了。

執行結果如下:

程序執行的當前文件夾下是沒有foo.socket這個文件的

執行如上程序:

可以看到執行完程序后:

(1)foo.socket這個文件自動生成了,而且文件類型是socket(srwxrwxr-x中的s)

(2)如果foo.socket已經被占用了是沒辦法再綁定其他的unix domain socket的

?

17.3 Unique Connections

基于17.2.1的naming unix domain socket技術,就可以針對unix domain socket展開listen, accept, connect等一些列用于network socket的操作;用這樣的方式來實現同一個host內部的IPC。

具體的示意圖,如下所示:

apue中分別給出了listen accept connect三個函數的unix domain socket版。

int serv_listen(const char *name);

int serv_accpet(int listenfd, uid_t *uidptr);

int cli_conn(const char *name);

具體實現如下:

serv_listen函數(返回一個unix domain socket專門用于監聽client發送來的請求)

1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <errno.h> 5 6 #define QLEN 10 7 8 /*只要傳入一個well known name 就可返回fd*/ 9 int serv_listen(const char *name) 10 { 11 int fd; 12 int len; 13 int err; 14 int rval; 15 struct sockaddr_un un; 16 17 /*對name的長度上限有要求*/ 18 if (strlen(name) >= sizeof(un.sun_path)) { 19 errno = ENAMETOOLONG; 20 return -1; 21 } 22 /*這里創建的方式是SOCK_STREAM*/ 23 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0))<0) { 24 return -2; 25 } 26 /*防止name已經被占用了 這是一種排他的做法*/ 27 unlink(name); 28 /*初始化socket address structure*/ 29 memset(&un, 0, sizeof(un.sun_path)); 30 un.sun_family = AF_UNIX; 31 strcpy(un.sun_path, name); 32 len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 33 /*執行bind操作 因為有name所以可以綁定*/ 34 if (bind(fd, (struct sockaddr *)&un, len)<0) { 35 rval = -3; 36 goto errout; 37 } 38 /*執行listen的操作 并設置等待隊列的長度*/ 39 if (listen(fd, QLEN)<0) { 40 rval = -4; 41 goto errout; 42 } 43 return fd; 44 errout: 45 err = errno; 46 close(fd); 47 errno = err; 48 return rval; 49 }

serv_accpet函數(這里有一點沒看懂 為什么client's name有30s的限制)

1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <time.h> 5 #include <errno.h> 6 7 #define STALE 30 /*client's name can't be older than this sec*/ 8 9 int serv_accept(int listenfd, uid_t *uidptr) 10 { 11 int clifd; 12 int err; 13 int rval; 14 socklen_t len; 15 time_t staletime; 16 struct sockaddr_un un; 17 struct stat statbuf; 18 char *name; /*name中存放的是發起請求的client的地址信息*/ 19 20 /*因為sizeof不計算結尾的\0 所以在計算分配內存的時候要考慮進來*/ 21 if ((name = malloc(sizeof(un.sun_path+1)))==NULL) { 22 return -1; 23 } 24 len = sizeof(un); 25 /*就在這里阻塞著 等著client端發送來請求*/ 26 if ((clifd = accept(listenfd, (struct sockaddr *)&un, &len))<0) { 27 free(name); 28 return -2; 29 } 30 /*再讓len為path的實際長度 并存到name中*/ 31 len -= offsetof(struct sockaddr_un, sun_path); 32 memcpy(name, un.sun_path, len); 33 name[len] = 0; /*最后補上\0*/ 34 if (stat(name, &statbuf)<0) { /*讓statbuf獲得client關聯的文件的status*/ 35 rval = -3; 36 goto errout; 37 } 38 39 /*1. 驗證與client端關聯的文件類型是不是socket file*/ 40 #ifdef S_ISSOCK 41 if (S_ISSOCK(statbuf.st_mode)==0) { 42 rval = -4; 43 goto errout; 44 } 45 #endif 46 /*2. 驗證與clinet端關聯的文件的權限*/ 47 /*G for group O for owner U for user */ 48 /*驗證permission只有user-read user-write user-execute*/ 49 /*注意 ||運算符的優先級 要高于 !=運算符的優先級*/ 50 if ((statbuf.st_mode & (S_IRWXG | S_IRWXO)) || 51 (statbuf.st_mode & S_IRWXU) != S_IRWXU) { 52 rval = -5; 53 goto errout; 54 } 55 /*3. 驗證與client端關聯的文件被創建的時間*/ 56 staletime = time(NULL) - STALE; /**/ 57 if (statbuf.st_atim < staletime || 58 statbuf.st_ctim < staletime || 59 statbuf.st_mtim < staletime) { 60 rval = -6; 61 goto errout; 62 } 63 if (uidptr != NULL) { 64 *uidptr = statbuf.st_uid; 65 } 66 unlink(name); 67 free(name); 68 return clifd; 69 70 errout: 71 err = errno; 72 close(clifd); 73 free(name); 74 errno = err; 75 return rval; 76 }

cli_conn

1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <errno.h> 5 6 #define CLI_PATH "/var/tmp" /*客戶端標示*/ 7 #define CLI_PERM S_IRWXU /*權限設置*/ 8 9 int cli_conn(const char *name) 10 { 11 int fd; 12 int len; 13 int err; 14 int rval; 15 struct sockaddr_un un, sun;// un代表client端 sun代表server端 16 int do_unlink = 0; 17 /*1. 驗證傳入的name是否合理 18 * 這個name是server的name 先校驗server name的長度 */ 19 if (strlen(name) >= sizeof(un.sun_path)) { 20 errno = ENAMETOOLONG; 21 return -1; 22 } 23 /*2. 構建client端的fd 24 * 這個fd是client的專門發送請求的fd*/ 25 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0))<0) { 26 return -1; 27 } 28 /*3. 構建client端的地址*/ 29 /* 將文件名+進程號共寫進un.sun_path 并記錄長度 這里約定了path的格式*/ 30 memset(&un, 0, sizeof(un)); 31 un.sun_family = AF_UNIX; 32 sprintf(un.sun_path, "%s%05ld", CLI_PATH, (long)getpid()); 33 printf("file is %s\n", un.sun_path); 34 len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 35 /*4. 將構建的fd與構建的client端地址綁定*/ 36 unlink(un.sun_path); /*防止CLI_PATH+pid這個特殊的文件名已經被占用了*/ 37 if (bind(fd, (struct sockaddr *)&un, len)<0) { 38 rval = -2; 39 goto errout; 40 } 41 /* 為什么要先綁定再設定權限?因為如果不能綁定 修改權限就是無用功*/ 42 if (chmod(un.sun_path, CLI_PERM)<0) { 43 rval = -3; 44 do_unlink = 1; 45 goto errout; 46 } 47 /*5. 告訴client通過name去找server*/ 48 /* 通過這個name這個key與'server'的process建立連接*/ 49 memset(&sun, 0 ,sizeof(sun)); 50 sun.sun_family = AF_UNIX; 51 strcpy(sun.sun_path, name); 52 len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 53 if (connect(fd, (struct sockaddr *)&sun, len)<0) { 54 rval = -4; 55 do_unlink = 1; 56 goto errout; 57 } 58 return fd; 59 errout: 60 err = errno; 61 close(fd); 62 if (do_unlink) { 63 unlink(un.sun_path); 64 } 65 errno = err; 66 return raval; 67 }

?

?

17.4 Passing File Descriptors

在進程間傳遞file descriptor是也是unix domain socket的一種強大的功能。文件打開的各種細節,都隱藏在server端了。

至今在apue上已經有三種進程間的file descriptor的傳遞方式:

(1)figure3.8的情況,不同的process分別打開同一個file,每個process中的fd有各自的file table,這兩個fd基本沒有什么關系:

  

(2)figure8.2的情況,parent通過fork產生child,整個parent的memory layout都copy到child中,這兩個fd屬于不同的地址空間,但是值是相同的,并且共享同一個file table:

  

(3)17.4節的情況,通過unix domain socket的方式傳遞fd,這兩個fd屬于不同的地址空間,除了共享同一個file table沒有其他的不同:

  

這一部分還講了其他一些相關的結構體內容,這些細節為了看懂代碼而用,關鍵記住上面的三種fd方式就可以了。

apue這部分自己設定了一個protocol,設定通過unix domain socket傳遞fd的協議,這個協議的細節不用關注太多;重點看如何告訴系統,發送的是一個fd。

利用unix domain socket發送和接收fd的代碼如下:

send_fd的代碼(如何告訴系統發送的是一個fd?先把struct cmsghdr cmptr設定好line43~45,將cmptr賦值給struct msghdr msg中的msg.msg_control,這樣系統就知道發送的是一個fd)

1 #include "../apue.3e/include/apue.h" 2 #include <bits/socket.h> 3 #include <sys/socket.h> 4 5 /* 由于不同系統對于cmsghdr的實現不同 CMSG_LEN這個宏就是計算cmsghdr+int 6 * 所需要的memory大小是多少 這樣動態分配內存的時候才知道分配多少大小*/ 7 #define CONTROLLEN CMSG_LEN(sizeof(int)) 8 9 static struct cmsghdr *cmptr = NULL; 10 11 int send_fd(int fd, int fd_to_send) 12 { 13 struct iovec iov[1]; 14 struct msghdr msg; 15 char buf[2]; /*這是真正的協議頭的兩個特征bytes*/ 16 /*scatter read or gather write 具體參考14.6 17 * 具體到這里的情景比較簡單 因為iovec的長度只有1 相當于就調用了一個write 18 * 但是Unix domain socket的格式要去必須是struct iovec這種數據格式*/ 19 iov[0].iov_base = buf; 20 iov[0].iov_len = 2; 21 msg.msg_iov = iov; 22 msg.msg_iovlen = 1; 23 msg.msg_name = NULL; 24 msg.msg_namelen = 0; 25 /*調用send_fd分兩種情況: 26 * 1. 正常調用傳遞fd, 則fd_to_send是大于零的 27 * 2. 在send_err中調用send_fd, 則fd_to_send表示的是errorcode*/ 28 if (fd_to_send<0) { 29 msg.msg_control = NULL; 30 msg.msg_controllen = 0; 31 buf[1] = -fd_to_send; /*出錯的fd_to_send都是負數*/ 32 if (buf[1] == 0) { /*這個protocol并不是完美的 如果fd_to_send 33 是-256 則沒有正數與其對應 協議在這里特殊處理-1與-256都代表 errorcode 1*/ 34 buf[1] = 1; 35 } 36 } 37 else { 38 /*這里cmptr獲得的memory大小是由CMSG_LEN算出來的*/ 39 if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL ) { 40 return -1; 41 } 42 /*通過Unix domain socket發送fd 就如下設置*/ 43 cmptr->cmsg_level = SOL_SOCKET; 44 cmptr->cmsg_type = SCM_RIGHTS; 45 cmptr->cmsg_len = CONTROLLEN; 46 /*將cmptr融進要發送的msg*/ 47 msg.msg_control = cmptr; 48 msg.msg_controllen = CONTROLLEN; 49 /*得搞清楚strut cmsghdr的結構 50 * struct cmsghdr{ 51 * socklen_t cmsg_len; 52 * int cmsg_level; 53 * int cmsg_type; 54 * } 55 * // followed by the actual control message data 56 * CMSG_DATA做的事情就是在cmsghdr緊屁股后面放上'fd_to_send'這個內容 57 * ubuntu系統上查看<sys/socket.h>文件中的這個宏的具體實現 58 * 這個宏的具體實現就是struct cmsghdr結構體的指針+1, 然后將這個位置*/ 59 *(int *)CMSG_DATA(cmptr) = fd_to_send; 60 buf[1] = 0; 61 } 62 buf[0] = 0; /*這就是給recv_fd設定的null byte flag recv_fd()函數中就是靠這個位來判斷的*/ 63 /*這里校驗的sendmsg返回值是不是2 就是char buf[2]中的內容 64 * struct msghdr msg中 只有msg_iov中的數據算是被校驗的內容 65 * 而msg_control這樣的數據 都叫ancillary data 即輔助數據 66 * 輔助數據雖然也跟著發送出去了 但是不在sendmsg返回值的校驗標準中*/ 67 if (sendmsg(fd, &msg, 0)!=2) { 68 return -1; 69 } 70 return 0 71 }

接收端的代碼recv_fd如下(代碼不難理解,有個坑是line56是apue勘誤表中才修改過來,否則有問題;勘誤表的鏈接:http://www.apuebook.com/errata3e.html)

1 #include "open_fd.h" 2 #include <sys/socket.h> /* struct msghdr */ 3 4 /* size of control buffer to send/recv one file descriptor */ 5 #define CONTROLLEN CMSG_LEN(sizeof(int)) 6 7 static struct cmsghdr *cmptr = NULL; /* malloc'ed first time */ 8 9 /* 10 * Receive a file descriptor from a server process. Also, any data 11 * received is passed to (*userfunc)(STDERR_FILENO, buf, nbytes). 12 * We have a 2-byte protocol for receiving the fd from send_fd(). 13 */ 14 int 15 recv_fd(int fd, ssize_t (*userfunc)(int, const void *, size_t)) 16 { 17 int newfd, nr, status; 18 char *ptr; 19 char buf[MAXLINE]; 20 struct iovec iov[1]; 21 struct msghdr msg; 22 23 status = -1; 24 for ( ; ; ) { 25 iov[0].iov_base = buf; 26 iov[0].iov_len = sizeof(buf); 27 msg.msg_iov = iov; 28 msg.msg_iovlen = 1; 29 msg.msg_name = NULL; 30 msg.msg_namelen = 0; 31 if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL) 32 return(-1); 33 msg.msg_control = cmptr; 34 msg.msg_controllen = CONTROLLEN; 35 if ((nr = recvmsg(fd, &msg, 0)) < 0) { 36 err_ret("recvmsg error"); 37 return(-1); 38 } else if (nr == 0) { 39 err_ret("connection closed by server"); 40 return(-1); 41 } 42 43 /* 44 * See if this is the final data with null & status. Null 45 * is next to last byte of buffer; status byte is last byte. 46 * Zero status means there is a file descriptor to receive. 47 */ 48 for (ptr = buf; ptr < &buf[nr]; ) { 49 if (*ptr++ == 0) { 50 if (ptr != &buf[nr-1]) 51 err_dump("message format error"); 52 status = *ptr & 0xFF; /* prevent sign extension */ 53 if (status == 0) { 54 printf("msg.msg_controllen:%zu\n", msg.msg_controllen); 55 printf("CONTROLLEN:%zu\n", CONTROLLEN); 56 if (msg.msg_controllen < CONTROLLEN) 57 err_dump("status = 0 but no fd"); 58 newfd = *(int *)CMSG_DATA(cmptr); 59 } else { 60 newfd = -status; 61 } 62 nr -= 2; 63 } 64 } 65 if (nr > 0 && (*userfunc)(STDERR_FILENO, buf, nr) != nr) 66 return(-1); 67 if (status >= 0) /* final data has arrived */ 68 return(newfd); /* descriptor, or -status */ 69 } 70 }

?

?

17.5 An Open Server, Version 1

這一節正是利用17.4中的passing file descriptor的技術來構建一個"open" server:

這個server專門用來接收client發送的請求(即打開哪個文件,怎么打開),然后在server端把文件打開,再利用unix domain socket的技術把file descriptor給傳遞過去。

具體用到的技術就是client運行起來,通過fork+execl的方式調用opend(相當于server端的程序),并且通過socketpair的方式建立進程間的通信。

將書上的代碼整理了一下(main.c表示client端,maind.c表示server端,lib文件夾中包含用到的一些函數,include文件夾中的.h文件包括各種公用的lib)

main.c代碼如下:

1 #include "open_fd.h" 2 #include <fcntl.h> 3 #include <sys/uio.h> 4 5 #define BUFFSIZE 8192 6 #define CL_OPEN "open" // client's request for server 7 8 int csopen(char *name, int oflag) 9 { 10 pid_t pid; 11 int len; 12 char buf[10]; 13 struct iovec iov[3]; 14 static int fd[2] = {-1, -1}; 15 /*首次需要建立child parent的鏈接*/ 16 if (fd[0] < 0) { 17 printf("frist time build up fd_pipe\n"); 18 /*構建一個全雙工的pipe*/ 19 if (fd_pipe(fd) < 0) { 20 err_ret("fd_pipe error"); 21 return -1; 22 } 23 printf("fd[0]:%d,fd[1]:%d\n",fd[0],fd[1]); 24 if((pid = fork())<0){ 25 err_ret("fork error"); 26 return -1; 27 } 28 else if (pid ==0) { /*child*/ 29 close(fd[0]); 30 /*這個地方需要注意 這種full-duplex的fd 可以把in和out都掛到這個fd上面 之前只掛了stdin沒有掛out所以有問題*/ 31 /*將child的stdin 銜接到fd[1]上面*/ 32 if (fd[1] != STDIN_FILENO && dup2(fd[1],STDIN_FILENO)!=STDIN_FILENO) { 33 err_sys("dup2 error to stdin"); 34 } 35 /*將child的stdout 銜接到fd[1]上面*/ 36 if (fd[1] != STDOUT_FILENO && dup2(fd[1],STDOUT_FILENO)!=STDOUT_FILENO) { 37 err_sys("dup2 error to stdout"); 38 } 39 /*執行opend這個程序 這時opend這個程序的stdin就指向fd[1] child和parent通過pipe連接了起來*/ 40 if (execl("./opend", "opend", (char *)0)<0) { 41 err_sys("execl error"); 42 } 43 } 44 close(fd[1]); /*parent*/ 45 } 46 47 /*iov三個char array合成一個char array 每個array以空格分開*/ 48 sprintf(buf, " %d", oflag); 49 iov[0].iov_base = CL_OPEN " "; /* string concatenation */ 50 iov[0].iov_len = strlen(CL_OPEN) + 1; 51 iov[1].iov_base = name; /*傳入的filename在中間的io*/ 52 iov[1].iov_len = strlen(name); 53 iov[2].iov_base = buf; 54 iov[2].iov_len = strlen(buf) + 1; /* +1 for null at end of buf */ 55 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; 56 /*通過fd[0] fd[1]這個通道 由client向server發送數據*/ 57 /*writev在會把緩沖區的輸出數據按順序集合到一起 再發送出去*/ 58 if (writev(fd[0], &iov[0], 3) != len) { 59 err_ret("writev error"); 60 return(-1); 61 } 62 /* read descriptor, returned errors handled by write() */ 63 return recv_fd(fd[0], write); 64 } 65 66 /*這是client端調用的程序*/ 67 int main(int argc, char *argv[]) 68 { 69 int n, fd; 70 char buf[BUFFSIZE], line[MAXLINE]; 71 /*每次從stdin cat進來filename*/ 72 while (fgets(line, MAXLINE, stdin)!=NULL) { 73 /*替換把回車替換掉*/ 74 if (line[strlen(line)-1] == '\n') { 75 line[strlen(line)-1] = 0; 76 } 77 /*打開文件*/ 78 if ((fd = csopen(line, O_RDONLY))<0) { 79 continue; 80 } 81 /*把fd這個文件讀寫完成*/ 82 printf("fd obtained from other process : %d\n",fd); 83 while ((n = read(fd, buf, BUFFSIZE))>0) { 84 if (write(STDOUT_FILENO, buf, n)!= n) { 85 err_sys("write error"); 86 } 87 } 88 if (n<0) { 89 err_sys("read error"); 90 } 91 close(fd); 92 } 93 exit(0); 94 }

maind.c的代碼如下:

1 #include <errno.h> 2 #include <fcntl.h> 3 #include "open_fd.h" 4 5 #define CL_OPEN "open" 6 #define MAXARGC 50 7 #define WHITE " \t\n" 8 9 char errmsg[MAXLINE]; 10 int oflag; 11 char *pathname; 12 13 /* cli_args和buf_args兩個函數起到把讀進來的buf解析的功能 14 * 了解大體功能即可 不用細看*/ 15 16 int cli_args(int argc, char **argv) 17 { 18 if (argc != 3 || strcmp(argv[0], CL_OPEN) != 0) { 19 strcpy(errmsg, "usage: <pathname> <oflag>\n"); 20 return(-1); 21 } 22 pathname = argv[1]; /* save ptr to pathname to open */ 23 oflag = atoi(argv[2]); 24 return(0); 25 } 26 27 int buf_args(char *buf, int (*optfunc)(int, char **)) 28 { 29 char *ptr, *argv[MAXARGC]; 30 int argc; 31 32 if (strtok(buf, WHITE) == NULL) /* an argv[0] is required */ 33 return(-1); 34 argv[argc = 0] = buf; 35 while ((ptr = strtok(NULL, WHITE)) != NULL) { 36 if (++argc >= MAXARGC-1) /* -1 for room for NULL at end */ 37 return(-1); 38 argv[argc] = ptr; 39 } 40 argv[++argc] = NULL; 41 42 /* 43 * Since argv[] pointers point into the user's buf[], 44 * user's function can just copy the pointers, even 45 * though argv[] array will disappear on return. 46 */ 47 return((*optfunc)(argc, argv)); 48 } 49 50 void handle_request(char *buf, int nread, int fd) 51 { 52 int newfd; 53 if (buf[nread-1] != 0) { 54 send_err(fd, -1, errmsg); 55 return; 56 } 57 if (buf_args(buf, cli_args) < 0) { /* parse args & set options */ 58 send_err(fd, -1, errmsg); 59 return; 60 } 61 if ((newfd = open(pathname, oflag)) < 0) { 62 send_err(fd, -1, errmsg); 63 return; 64 } 65 if (send_fd(fd, newfd) < 0) /* send the descriptor */ 66 err_sys("send_fd error"); 67 close(newfd); /* we're done with descriptor */ 68 } 69 70 /*server端*/ 71 int main(void) 72 { 73 int nread; 74 char buf[MAXLINE]; 75 for (; ; ){ 76 /*一直阻塞著 等著stdin讀數據*/ 77 if ((nread = read(STDIN_FILENO, buf, MAXLINE))<0) { 78 err_sys("read error on stream pipe"); 79 } 80 else if (nread == 0) { 81 break; 82 } 83 handle_request(buf, nread, STDOUT_FILENO); 84 } 85 exit(0); 86 }

其余lib和include中的代碼有的是apue書上這個章節的,有的是apue源代碼提供的lib,這些不再贅述了。

直接看運行結果(在當前文件夾下面設定了一個xbf的文本文件,流程是讓client發送以只讀方式打開這個文件的請求,由server打開這個文件,然后再將fd返回)

先得注意msg.msg_controllen與CONTROLLEN是不等的,這是原書勘誤表中的一個bug。

server中打開的xbf文件的fd就是存在了msg這個結構體的最后的位置發送過來的。

如果將main.c中的line91注釋掉,結果如下:

可以看到,真正client接收到的fd的值,與server端發送時候的fd的值是沒有關系的,只是client端哪個最小的fd的值可用,就會用這個fd的值對應上server打開的xbf這個文件。

總結一下,流程是這樣的:

(1)server打開xbf文件 →

(2)server將與xbf文件對應的fd掛到cmsghdr的最后 →

(3)server通過fd_pipe產生的unix domain socket將msghdr發送到client端 →

(4)在發送的過程中kernel記錄的應該是這個fd對應的file table信息 →

(5)在client接收到這個file table時候,kernel分配一個client端可用的最小fd →

(6)client端獲得了一個fd并且這個fd已經指向開打的xbf文件

其余的具體protocol不用細看,但是一些技術細節后面再單獨記錄。

?

17.6 An Open Server Version 2

這里主要用到的是naming unix domain socket的技術,為的是可以在unrelated process之間傳遞file descriptor。

理解這個部分的重點是書上17.29和17.30兩個loop函數的實現:一個用的是select函數,一個用的是poll函數。(還需要熟悉守護進程的知識以及command-line argument的解析的套路)

要想迅速串起來這部分的代碼,還得回顧一下select和poll函數,這二者的輸入參數中都有value-on return類型的,先理解好輸入參數。

loop.select.c代碼如下:

1 #include "opend.h" 2 #include <sys/select.h> 3 4 void 5 loop(void) 6 { 7 int i, n, maxfd, maxi, listenfd, clifd, nread; 8 char buf[MAXLINE]; 9 uid_t uid; 10 fd_set rset, allset; 11 12 /* 與poll的用法不同 這里喂給select的fd_set是不預先設定大小的 13 * 而是靠maxfd來標定大小*/ 14 FD_ZERO(&allset); 15 /* obtain fd to listen for client requests on */ 16 if ((listenfd = serv_listen(CS_OPEN)) < 0) 17 log_sys("serv_listen error"); 18 /* 將server這個用于監聽的fd加入集合*/ 19 FD_SET(listenfd, &allset); 20 /* 需要監聽的最大的fd就是剛剛分配的listenfd*/ 21 maxfd = listenfd; 22 maxi = -1; 23 24 for ( ; ; ) { 25 rset = allset; /* rset gets modified each time around */ 26 /* select中的&rset這個參數 返回的時候只保留ready的fd*/ 27 if ((n = select(maxfd + 1, &rset, NULL, NULL, NULL)) < 0) 28 log_sys("select error"); 29 /* 處理有client發送請求的case*/ 30 if (FD_ISSET(listenfd, &rset)) { 31 /* accept new client request */ 32 if ((clifd = serv_accept(listenfd, &uid)) < 0) 33 log_sys("serv_accept error: %d", clifd); 34 i = client_add(clifd, uid); 35 FD_SET(clifd, &allset); /*A 向allset中增加需要監聽的內容*/ 36 if (clifd > maxfd) /* 更新select監控的最大的fd大小*/ 37 maxfd = clifd; /* max fd for select() */ 38 if (i > maxi) /* 更新Client array的大小*/ 39 maxi = i; /* max index in client[] array */ 40 log_msg("new connection: uid %d, fd %d", uid, clifd); 41 continue; 42 } 43 /* 沒有新的client 處理Client array中ready的client */ 44 for (i = 0; i <= maxi; i++) { /* go through client[] array */ 45 if ((clifd = client[i].fd) < 0) /*沒被占用的*/ 46 continue; 47 if (FD_ISSET(clifd, &rset)) { /*在監聽的set中*/ 48 /* read argument buffer from client */ 49 if ((nread = read(clifd, buf, MAXLINE)) < 0) { 50 log_sys("read error on fd %d", clifd); 51 } else if (nread == 0) { /* nread=0表明client已經關閉了*/ 52 log_msg("closed: uid %d, fd %d", 53 client[i].uid, clifd); 54 client_del(clifd); /* client has closed cxn */ 55 FD_CLR(clifd, &allset); /* B 從allset中刪除需要監聽的內容*/ 56 close(clifd); 57 } else { /* process client's request */ 58 handle_request(buf, nread, clifd, client[i].uid); 59 } 60 } 61 } 62 } 63 }

loop.pool.c的代碼如下:

#include "opend.h" #include <poll.h>#define NALLOC 10 /* # pollfd structs to alloc/realloc */static struct pollfd * grow_pollfd(struct pollfd *pfd, int *maxfd) {int i;int oldmax = *maxfd;int newmax = oldmax + NALLOC;if ((pfd = realloc(pfd, newmax * sizeof(struct pollfd))) == NULL)err_sys("realloc error");for (i = oldmax; i < newmax; i++) {pfd[i].fd = -1;pfd[i].events = POLLIN;pfd[i].revents = 0;}*maxfd = newmax;return(pfd); }void loop(void) {int i, listenfd, clifd, nread;char buf[MAXLINE];uid_t uid;struct pollfd *pollfd;int numfd = 1;int maxfd = NALLOC;/* 先分配10個fd槽 */if ((pollfd = malloc(NALLOC * sizeof(struct pollfd))) == NULL)err_sys("malloc error");for (i = 0; i < NALLOC; i++) {pollfd[i].fd = -1;pollfd[i].events = POLLIN; /*read*/pollfd[i].revents = 0;}/* obtain fd to listen for client requests on */if ((listenfd = serv_listen(CS_OPEN)) < 0)log_sys("serv_listen error");client_add(listenfd, 0); /* we use [0] for listenfd */pollfd[0].fd = listenfd;for ( ; ; ) {/* 這里控制的是numfd而不是maxfd*/if (poll(pollfd, numfd, -1) < 0)log_sys("poll error");/* 1. 先判斷是否有新的client請求 */if (pollfd[0].revents & POLLIN) {/* accept new client request */if ((clifd = serv_accept(listenfd, &uid)) < 0)log_sys("serv_accept error: %d", clifd);client_add(clifd, uid);/* possibly increase the size of the pollfd array *//* 如果Client array數量超過了pollfd的數量 就realloc*/if (numfd == maxfd)pollfd = grow_pollfd(pollfd, &maxfd);pollfd[numfd].fd = clifd;pollfd[numfd].events = POLLIN;pollfd[numfd].revents = 0;numfd++;log_msg("new connection: uid %d, fd %d", uid, clifd);/* 與select不同 這里沒有continue 而是可以直接向下進行* 為什么可以直接向下進行 而select就不可以* 因為poll使用pollfd來標定需要等著的fd的* 每個struct pollfd中* a. 既有關心ready的事件* b. 又有真正ready的事件* 處理一個fd并不會影響其他fd的狀態*/}/* 2. 再判斷有哪些ready的client*/for (i = 1; i < numfd; i++) {if (pollfd[i].revents & POLLHUP) {goto hungup;} else if (pollfd[i].revents & POLLIN) {/* read argument buffer from client */if ((nread = read(pollfd[i].fd, buf, MAXLINE)) < 0) {log_sys("read error on fd %d", pollfd[i].fd);} else if (nread == 0) { hungup:/* the client closed the connection */log_msg("closed: uid %d, fd %d",client[i].uid, pollfd[i].fd);client_del(pollfd[i].fd);close(pollfd[i].fd);if (i < (numfd-1)) { /* 這個應該是corner case的判斷*//* 這么做是為了節約空間* 把末端的fd及相關信息頂到i這個位置上 *//* pack the array */pollfd[i].fd = pollfd[numfd-1].fd;pollfd[i].events = pollfd[numfd-1].events;pollfd[i].revents = pollfd[numfd-1].revents;/* 由于把末位的頂到i這個位置上* 所以要再check一遍這個位置 */i--; /* recheck this entry */}numfd--;} else { /* process client's request */handle_request(buf, nread, pollfd[i].fd,client[i].uid);}}}} }

?

===================================分割線===================================

記錄幾個遇到的技術細節問題

1. sign extension的問題

上面recv_fd中的line54有一個不是很直觀的做法

int status;

char *ptr;

status = *ptr & 0xFF;

ptr是char類型,可以代表0~255的值,代表不同的返回狀態。比如*ptr為128的值用二進制表示為1000000。

由于status是int類型占4bytes 32bits,如果直接status = *ptr,就涉及到位擴展的問題,最高位到底是當成符號位還是取值位呢?

(1)首先,char到底是有符號還是無符號的,取決于編譯器,見這篇文章(http://descent-incoming.blogspot.jp/2013/02/c-char-signed-unsigned.html)

(2)0xFF默認是無符號int型,高位8都為0

因此,無論char是不是有符號的,一旦與0xFF做了與運算,則相當于把char類型的最高位自動當成了取值位了。就避免了上面提到的符號位擴展的問題。

為了方便記憶,寫了一個小例子記錄這種sign extension帶來的影響:

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(int argc, char *argv[]) 5 { 6 /*驗證int的byte數目*/ 7 int status = -1; 8 char c1 = 254; /*默認254是int類型占4bytes 轉換成char類型占1bytes 直接截取低8位*/ 9 unsigned char c2 = 254; 10 /*gcc編譯器 默認的char是有符號的 因為直接從char轉換到int是用char的符號位補齊高位*/ 11 status = c1; 12 printf("status converted from c1 : %d\n", status); 13 /*如果是unsigned char是沒有符號位的 因此從unsigned char轉換到int是高位直接補0*/ 14 status = c2; 15 printf("status converted from c2 : %d\n", status); 16 /*驗證默認的0xFF是4 bytes 32 bits的*/ 17 printf("size of defalut int : %ld\n", sizeof(0xFF)); 18 status = c1 & 0xFF; 19 printf("status converted from c1 & 0xFF : %d\n", status); 20 /*如果是1 byte 8 bits的int類型*/ 21 int8_t i8 = 0xFF; 22 status = c1 & i8; 23 printf("status converted from c1 & int8_t i8 : %d\n", status); 24 }

執行結果如下:

上面的例子應該可以包含絕大多數情況了。

這是當時看過的一個不錯的資料:http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/signExt.html

?

2. sizeof與strelen的問題

http://www.cnblogs.com/carekee/articles/1630789.html

?

3. 結構體內存對齊問題

send_fd和recv_fd代碼中都用到了一個宏定義CMSG_LEN:查看這個宏在socket.h中的定義,引申出CMSG_ALIGN這個內存對齊的宏定義。

(1)要想回顧CMSG_ALIGN怎么做到內存對齊的,可以參考下面的blog:http://blog.csdn.net/duanlove/article/details/9948947

(2)要想理解為什么要進行內存對齊,可以參考下面的blog:http://www.cppblog.com/snailcong/archive/2009/03/16/76705.html

(3)從實操層面,學習如何計算結構體的內存對齊方法,可以參考下面的blog:http://blog.csdn.net/hairetz/article/details/4084088

把上面的內容總結起來,可得結構體內存對齊如下的結論:

1 A元素是結構體前面的元素 B元素是結構體后面的元素,一般結構體開始的偏移量是0,則:A元素必須讓B元素滿足 B元素的尋址偏移量是B元素size的整數倍大小 2 整個結構的大小必須是其中最大字段大小的整數倍。 按照上面兩個原則 就大概能算出來常規套路下結構體需要內存對齊后的大小 最后還是自己寫一個例子,通過實操記憶一下: 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct str1{ 5 char a; 6 char b; 7 short c; 8 long d; 9 }; 10 11 struct str2{ 12 char a; 13 }; 14 15 int main(int argc, char *argv[]) 16 { 17 struct str2 s2; 18 struct str1 s1; 19 char *p; 20 char c; 21 short s; 22 long l; 23 24 printf("size of str2 : %ld\n", sizeof(struct str2)); 25 printf("addr of str2.a : %p\n", &s2.a); 26 printf("size of str1 : %ld\n", sizeof(struct str1)); 27 printf("addr of str1.a : %p\n", &s1.a); 28 printf("addr of str1.b : %p\n", &s1.b); 29 printf("addr of str1.c : %p\n", &s1.c); 30 printf("addr of str1.d : %p\n", &s1.d); 31 printf("addr of char pointer p : %p\n", &p); 32 printf("addr of char c : %p\n", &c); 33 printf("addr of long l : %p\n", &l); 34 printf("addr of short s : %p\n", &s); 35 }

運行結果如下:

分析:

(1)結構體內存對齊按照上面說的規律

(2)其余的變量內存分配,并不是完全按照變量定義的順序,我的理解是按照變量的所占字節的大小,字節大的分配在高地址(stack地址分配由高向低生長),這樣有助于節約內存空間,降低內存對齊帶來的memory的浪費。

另,深入看了一下malloc函數,果然malloc也是考慮了內存對齊的問題的。

(1)man malloc可以看到如下的信息:

(2)這個blog專門講malloc考慮內存對齊的內存分配機制的:http://blog.csdn.net/elpmis/article/details/4500917

?

4. 對于char c = 0 和 char c = '\0'問題的解釋

二者本質是一樣的,只是表述上有所區別,ascii碼'\0'的值就是0.

http://stackoverflow.com/questions/16955936/string-termination-char-c-0-vs-char-c-0

?

===================================分割線===================================

?

?

APUE這本書刷到這里也差不多了,后面兩章內容不是很新暫時不刷了。

這本書看過一遍,感覺還是遠遠不夠,以后應該常放在手邊翻閱。

轉載于:https://www.cnblogs.com/xbf9xbf/p/5121748.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的【APUE】Chapter17 Advanced IPC sign extension 结构体内存对齐的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲免费一区二区 | 福利视频91 | 欧美国产成人精品一区二区三区 | 三上悠亚在线观看一区二区 | 调教驯服丰满美艳麻麻在线视频 | jlzzjlzz欧美大全 | 国产在线网站 | 日日爽视频 | 欧美深夜在线 | 午夜在线影院 | 黑人vs亚洲人在线播放 | 黄色免费国产 | 欧美色图第一页 | 亚洲欧美日韩第一页 | 香蕉视频污在线观看 | 91高清免费视频 | 青青草原国产在线 | 色就是色欧美色图 | 国产污在线观看 | av作品在线 | 国产乱国产 | 超碰一区 | 好av在线| 岛国精品在线观看 | 男人的天堂国产 | 张津瑜国内精品www在线 | 在线观看你懂的视频 | 欧美日日操 | a级片免费在线观看 | 特级西西人体 | 国产毛片高清 | 国产精华一区二区三区 | 夜夜夜网| 高清乱码免费看污 | 亚洲专区欧美 | 久草精品在线 | 欧美成人一区二区三区高清 | 午夜精品亚洲 | 日本免费在线观看视频 | 噼里啪啦免费看 | 免费看黄色片网站 | 国产精品区二区三区日本 | 国内久久久 | 香蕉久久夜色精品 | 毛茸茸亚洲孕妇孕交片 | 激情综合网五月天 | 亚洲另类视频 | 天天操天天爱天天干 | 亚洲伦理一区二区 | 亚洲黄a | 国产真实乱 | 中文在线a√在线8 | 国产三区四区视频 | 亚洲a影院| 特级西西人体444www高清大胆 | 九九热精品| 日韩久久一区二区 | 国产成人无遮挡在线视频 | 诱惑の诱惑筱田优在线播放 | 精品黑人一区二区三区观看时间 | 黄色免费网页 | 香蕉毛片 | 欧美日韩一区二区三区69堂 | 在线视频一二区 | 神马久久av| 亚洲一区在线不卡 | 肉丝美脚视频一区二区 | 果冻传媒18禁免费视频 | 日韩在线不卡 | 五十路在线视频 | 国产精品美女www | 国产资源在线视频 | 老司机深夜网站 | 欧美日韩在线影院 | 久久麻豆av| 新婚之夜玷污岳丰满少妇在线观看 | 午夜宅男网 | 日韩免费高清视频 | 国产精品96 | 国产区在线观看 | 免费高清毛片 | 一本色道久久88综合日韩精品 | 亚洲再线 | 亚洲国产精品18久久久久久 | 午夜剧场欧美 | 欧美乱操| 在线精品自拍 | 理论片午午伦夜理片影院99 | 国产人妻精品一区二区三区不卡 | aaaa黄色片 | 黄色三级av| 日日爽爽 | 久久久久久国产精品三级玉女聊斋 | 成人做爰www免费看视频网站 | 亚洲香蕉av | 艳母动漫在线播放 | 狠狠操天天操 | 欧美性猛交乱大交3 | 黄色99|