IPC

系统创建进程会为进程分配虚拟地址空间,虚拟地址空间包括一组虚拟内存地址,该地址空间是私有的,从而将不同进程进行隔离,防止相互影响(比如一个进程指针问题带来的其他进程的崩溃)。虚拟地址代表的不是内存中的物理地址,系统为每一个进程维护一个 page table,实现从虚拟地址到物理地址的映射。因此,多进程不能通过全局变量或函数传参的方式直接通讯,需要采用

 

共享内存

多进程共享同一块内存。对于大块的数据(如图像),为了防止拷贝,可以使用共享内存。

 shm_open在/dev/shm/下建立一个文件。

//@param name: points to a string naming a shared memory object, processes calling shm_open() with the same value of name refer to the same shared memory object
//@return : a file descriptor for the shared memory object that is used by other functions to refer to that shared memory object. 
int shm_open(const char *name, int oflag, mode_t mode);

例如:

int fd;
fd = shm_open("/myregion", O_CREAT | O_RDWR, 0777);  //O_CREAT: the shared memory object is created; O_RDWR: Open for read or write access.

为共享内存设定属性(in qnx):

//@param fd : The file descriptor that's associated with the shared memory object
//@param falgs: SHMCTL_PHYS use physical address
//@param paddr: A physical address to assign to the object, if you set SHMCTL_PHYS in flags.
//@param size: The new size of the object, in bytes, regardless of ANON/PHYS flag.
int shm_ctl( int fd, int flags, _uint64 paddr, _uint64 size );

使用mmap将shm_open创建的文件映射到内存中,从而,使得server端和client端可以用操作内存指针的方式来操作文件。

//@brief: The mmap() function establishes a mapping between the address space of the process at an address (returned value) for len bytes to the memory object represented by the file descriptor (fd) at offset (offset) for (len) bytes. 
//@param addr: Is the desired starting address of the memory mapped region. If addr is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping. If addr is not NULL, then the kernel takes it as a hint about where to place the mapping; on Linux, the mapping will be created at a nearby page boundary. The address of the new mapping is returned as the result of the call.
//@param flags: MAP_SHARED Modifications are shared between all processes mapping the same range of the same file, and changes to the memory region are reflected in the mapped file.
//@return void * : the address at which the mapping was placed
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
void *mmap64(void *addr, size_t len, int prot, int flags, int fd, off64_t offset); //The mmap64() function is identical to the mmap() function except that it can be used to map memory from files that are larger than 2 gigabytes into the process memory. The mmap64() function is a part of the large file extensions.

munmap删除mapping.

删除共享内存: 

int shm_unlink(const char *name)

 

参考资料:https://blog.csdn.net/ababab12345/article/details/102931841

 

 

socket : https://blog.csdn.net/pashanhu6402/article/details/96428887

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuyuelongfly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值