不同进程处于不同用户地址空间,相互相对独立,进程间实现通信需要通过内核缓冲区,通过这个区域实现不同进程间数据的写入与读取,称之为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;
}