不同進程處於不同用戶地址空間,相互相對獨立,進程間實現通信需要通過內核緩沖區,通過這個區域實現不同進程間數據的寫入與讀取,稱之為IPC,InterProcess Communication。
以下內容皆基於Linux / Unix系統。
其實現機制有管道(Pipe)、共享內存(Shared Memory),消息隊列(Message Queue)等,目前最為常用的為UNIX Domain Socket。
UNIX Domain Socket類似於網絡通信中的Socket API,不過無需網絡協議、打包拆包等,更有效率。其結構體使用sockaddr_un,地址為文件系統中建立的一個socket類型的文件的路徑。
以下給出一個范例,基於Linux,GCC編譯通過。
接收端(類似Server):
#include <stdlib.h> #include <stdio.h> #include <stddef.h> #include <sys/socket.h> #include <sys/un.h> #include <sys/stat.h> #define QLEN 10 int main(void) { int n, clifd, fd, size; struct sockaddr_un un; struct stat statbuf; char buf[20]; memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; strcpy(un.sun_path, "foo.socket"); if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("socket error"); exit(1); } size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); if (bind(fd, (struct sockaddr*)&un, size) < 0) { perror("bind error"); exit(1); } printf("UNIX domain socket bound\n"); memset(&buf, 0, sizeof(buf)); if (listen(fd, QLEN) < 0) { perror("listen error"); exit(1); } if ((clifd = accept(fd, (struct sockaddr*)&un, &size)) < 0) { perror("accept error"); exit(1); } while(1) { n = read(clifd, buf, 20); if (n ==0) break; printf("%s\n", buf); } un.sun_path[strlen(un.sun_path)] = 0; if (stat(un.sun_path, &statbuf) < 0) { perror("stat error"); exit(1); } if (S_ISSOCK(statbuf.st_mode) == 0) { perror("S_ISSOCK error"); exit(1); } unlink(un.sun_path); return 0; }
發送端(類似Client):
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <sys/un.h> #include <errno.h> #include <sys/stat.h> #include <sys/socket.h> int main() { int fd, len, err; struct sockaddr_un un; char str[20]; if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("socket error"); exit(1); } memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; sprintf(un.sun_path, "%05d", getpid()); len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); unlink(un.sun_path); if (bind(fd, (struct sockaddr *)&un, len) < 0) { perror("bind error"); exit(1); } memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; strcpy(un.sun_path, (char*)"foo.socket"); len = offsetof(struct sockaddr_un, sun_path) + strlen((char*)"foo.socket"); if (connect(fd, (struct sockaddr *)&un, len) < 0) { perror("connect error"); exit(1); } while(1) { memset(str, 0, sizeof(str)); printf("Wait inputing....\n"); scanf("%s", str); if (write(fd, str, strlen(str)) < 0) { perror("write error"); exit(1); } else { printf("str: %s, len: %d\n", str, strlen(str)); } } close(fd); return 0; }