Linux\Unix IPC进程通信实例分析(一):共享内存通信---文件映射mmap方式
2012-06-13 10:03
886 查看
说明:这个例子是采用共享文件映射形式,写端写入数据,读端读取数据。
写端:
读端:
写端:
/*-------------map_normalfile1.c-----------*/ #include <sys/mman.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> typedef struct{ char name[4]; int age; }people; main(int argc, char** argv) // map a normal file as shared mem: { int fd,i; people *p_map; char temp; //open file fd=open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777); printf("file opened\n"); /* man open NAME open, openat - open a file SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *path, int oflag, mode_t mode ...); DESCRIPTION The open() function establishes the connection between a file and a file descriptor. It creates an open file descrip- tion that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file. The file offset used to mark the current position within the file is set to the beginning of the file. The file status flags and file access modes of the open file description are set according to the value of oflag. The mode argument is used only when O_CREAT is specified (see below.) Values for oflag are constructed by a bitwise-inclusive-OR of flags from the following list, defined in <fcntl.h>. Applications must specify exactly one of the first three values (file access modes) below in the value of oflag: O_RDONLY Open for reading only. O_WRONLY Open for writing only. O_RDWR Open for reading and writing. The result is undefined if this flag is applied to a FIFO. Any combination of the following may be used: O_APPEND If set, the file offset is set to the end of the file prior to each write. O_CREAT Create the file if it does not exist. This flag requires that the mode argument be specified. ..... */ lseek(fd,sizeof(people)*5-1,SEEK_SET); write(fd,"",1); /* NAME lseek - move read/write file pointer SYNOPSIS #include <sys/types.h> #include <unistd.h> off_t lseek(int fildes, off_t offset, int whence); DESCRIPTION The lseek() function sets the file pointer associated with the open file descriptor specified by fildes as follows: o If whence is SEEK_SET, the pointer is set to offset bytes. o If whence is SEEK_CUR, the pointer is set to its current location plus offset. o If whence is SEEK_END, the pointer is set to the size of the file plus offset. The symbolic constants SEEK_SET, SEEK_CUR, and SEEK_END are defined in the header <unistd.h>. */ //create mmap p_map = (people*) mmap(NULL, sizeof(people)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); close( fd ); /* NAME mmap - map pages of memory SYNOPSIS #include <sys/mman.h> void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off); DESCRIPTION The mmap() function establishes a mapping between a process's address space and a file or shared memory object. The format of the call is as follows: pa = mmap(addr, len, prot, flags, fildes, off); The mmap() function establishes a mapping between the address space of the process at an address pa for len bytes to the memory object represented by the file descriptor fildes at offset off for len bytes. The value of pa is a function of the addr argument and values of flags, further described below. A successful mmap() call returns pa as its result. The address range starting at pa and continuing for len bytes will be legitimate for the possible (not neces- sarily current) address space of the process. The range of bytes starting at off and continuing for len bytes will be legitimate for the possible (not necessarily current) offsets in the file or shared memory object represented by fildes. */ //write mmap temp = 'a'; for(i=0; i<100; i++) { temp += 1; memcpy(( *(p_map+i)).name, &temp, 2); (*(p_map+i)).age = 20+i; } printf("initialize over\n"); //unmap the mmap sleep(10); munmap((char*)p_map, sizeof(people)*100); printf("umap ok\n"); /* NAME munmap - unmap pages of memory SYNOPSIS #include <sys/mman.h> int munmap(void *addr, size_t len); DESCRIPTION The munmap() function removes the mappings for pages in the range [addr, addr + len), rounding the len argument up to the next multiple of the page size as returned by sysconf(3C). If addr is not the address of a mapping esta- blished by a prior call to mmap(2), the behavior is unde- fined. After a successful call to munmap() and before any subsequent mapping of the unmapped pages, further references to these pages will result in the delivery of a SIGBUS or SIGSEGV signal to the process. */ }
读端:
/*-------------map_normalfile2.c-----------*/ #include <sys/mman.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> typedef struct{ char name[4]; int age; }people; main(int argc, char** argv) // map a normal file as shared mem: { int fd,i; people *p_map; //open file fd=open( argv[1],O_CREAT|O_RDWR,00777 ); //create mmap and read p_map = (people*)mmap(NULL,sizeof(people)*100, PROT_READ|PROT_WRITE, MAP_SHARED,fd,0); for(i = 0;i<100;i++) { printf( "name: %s age %d;\n",(*(p_map+i)).name, (*(p_map+i)).age ); } //unmap munmap((char*)p_map,sizeof(people)*100); }
相关文章推荐
- Linux\Unix IPC进程通信实例分析(一):共享内存通信---系统V
- UNIX环境高级编程学习之第十五章进程间通信 - 两个进程通过映射普通文件实现共享内存通信
- 【C语言】【unix c】使用mmap将文件映射到进程的虚拟地址空间,然后对内存的操作直接反应到文件中
- linux c mmap()映射共享内存实例
- C# .Net 多进程同步 通信 共享内存 内存映射文件
- [Linux管道和IPC]使用信号量和共享内存进行父子进程通信
- linux下进程通信方式--共享内存
- 进程间的八种通信方式----共享内存是最快的 IPC 方式
- Linux内核和用户空间通信的方式(一)— proc文件和mmap共享内存
- 【网络编程基础】Linux下进程通信方式(共享内存,管道,消息队列,Socket)
- 进程间的八种通信方式----共享内存是最快的 IPC 方式
- linux下的多进程通信(IPC)原理及实现方案(管道、队列、信号量、共享内存)
- Unix高级编程:malloc内存管理、缓冲机制、mmap内存映射到进程、系统调用文件操作
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- Linux内核和用户空间通信的方式(一)— proc文件和mmap共享内存
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转
- linux基础编程:进程通信之System V IPC:消息队列,信号量,共享内存