In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Posix shared memory involves the following two steps:
1. Specify a name parameter to call shm_open to create a new shared memory object or open an existing shared memory object.
2. Call mmap to map the shared memory area to the address space of the calling process.
Header file:
# include
# include
# include
Int shm_open (const char * name, int oflag, mode_t mode)
Int shm_unlink (const char * name)
Link with-lrt (be careful to connect to this library)
Oflag parameter is O_CRTEA | O_RDWR and other flags
The mode parameter specifies the permission bit. If the O_CREAT flag is not specified, the parameter can be specified as 0. (if it is created, it should be given enough permissions, usually 0644 permissions, otherwise other processes may not have permissions.)
Files that are generally created are automatically placed in the / dev/shm/ directory.
The size of the newly created file is generally 0, so the mmap function will make an error when calling the file identifier, so use the ftruncate () function to expand the file.
# include
Int ftruncate (int fd,off_t length); 0 for success and-1 for error
For a normal file: if the size of the file is larger than the length parameter, the extra data will be discarded.
For a shared memory area object: set the size of the object to length bytes.
When we open an existing shared memory object, we can call fstat to get information about the object.
# include
# include
Int fstat (int fd, struct stat * buf)
The stat structure has 12 or more members, but when fd refers to a shared memory object, only four members contain information.
Struct stat {
Mode_t st_mode
Uid_t st_uid
Gid_t st_gid
Off_t st_size
}
You don't need to define this structure when calling, just instantiate it directly, such as: struct stat sta
Here are two examples, one to write to shared memory and the other to read
Read:
# include#include#include#include#include#includeint main (int argc,char* argv []) {int fd = shm_open (argv [1], Olympiad RDONLY NULL,BUFSIZ,PROT_READ,MAP_SHARED,fd,0 0); void* buf = NULL; if ((buf = mmap (NULL,BUFSIZ,PROT_READ,MAP_SHARED,fd,0)) = = MAP_FAILED) {perror ("mmap error\ n"); return 1;} sleep (1) While (1) {printf ("read:%s\ n", buf); sleep (3);} munmap (buf,BUFSIZ); close (fd);}
Execute:. / read mmap.txt
Write:
# include#include#include#include#include#includeint main (int argc,char* argv []) {int fd = shm_open (argv [1], O_CREAT | Olympian RDWR fd,100 0644); if (fd = =-1) {perror ("shm_open error\ n"); return 1;} ftruncate (fd,100); void* buf = NULL If ((buf = mmap (NULL,BUFSIZ,PROT_WRITE,MAP_SHARED,fd,0)) = = MAP_FAILED) {perror ("mmap error\ n"); return 1;} int i; for
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.