本文整理汇总了C++中shm_open函数的典型用法代码示例。如果您正苦于以下问题:C++ shm_open函数的具体用法?C++ shm_open怎么用?C++ shm_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了shm_open函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: cserve2_shm_map
void *
cserve2_shm_map(Shm_Handle *shm)
{
int fd;
const char *name;
if (shm->refcount++)
return shm->data;
name = cserve2_shm_name_get(shm);
fd = shm_open(name, O_RDWR, S_IWUSR);
if (fd == -1)
return MAP_FAILED;
shm->data = mmap(NULL, shm->image_size, PROT_WRITE, MAP_SHARED,
fd, shm->image_offset);
close(fd);
return shm->data;
}
开发者ID:jigpu,项目名称:efl,代码行数:22,代码来源:evas_cserve2_shm.c
示例2: shm_open
void Cleaner::start() {
/* Open hash table */
shmFile = shm_open(shmFilename.c_str(), O_RDWR);
if (shmFile == -1) {
std::cout << "[shm_open]:\t" << strerror(errno) << std::endl;
return;
}
hTable = new CHashTable();
if (hTable->allocate(shmFile) == -1)
return;
/* Open semaphore */
semaphore = sem_open(semFile.c_str(), 0);
if (semaphore == SEM_FAILED) {
std::cout << "[sem_open]:\t" << strerror(errno) << std::endl;
return;
}
/* Run cleaner */
while (true) {
/* Lock semaphore */
if (sem_wait(semaphore) == -1) {
std::cout << "[sem_wait]:\t" << strerror(errno) << std::endl;
return;
}
hTable->checkTTL();
/* Unlock semaphore */
if (sem_post(semaphore) == -1) {
std::cout << "[sem_post]:\t" << strerror(errno) << std::endl;
return;
}
sleep(1);
}
}
开发者ID:marinae,项目名称:cache-server,代码行数:39,代码来源:cleaner.cpp
示例3: test_fstat_shmfd
uintmax_t
test_fstat_shmfd(uintmax_t num, uintmax_t int_arg, const char *path)
{
struct stat sb;
uintmax_t i;
int shmfd;
shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
if (shmfd < 0)
err(-1, "test_fstat_shmfd: shm_open");
if (fstat(shmfd, &sb) < 0)
err(-1, "test_fstat_shmfd: fstat");
benchmark_start();
for (i = 0; i < num; i++) {
if (alarm_fired)
break;
(void)fstat(shmfd, &sb);
}
benchmark_stop();
close(shmfd);
return (i);
}
开发者ID:2trill2spill,项目名称:freebsd,代码行数:22,代码来源:syscall_timing.c
示例4: create_shm_file
//
// shm_open() を使って新しい共有ファイルを作りそれをオープンする
// ただし unlink して共有ファイルは削除し、fd だけを返す。
//
static int
create_shm_file(void)
{
int fd;
char tempfile[NAME_MAX];
snprintf(tempfile, sizeof(tempfile), "/fd-passing-shm-%d", getpid());
if ((fd = shm_open(tempfile, O_RDWR|O_CREAT, 0644)) == -1) {
perror("shm_open");
exit(EXIT_FAILURE);
}
if (ftruncate(fd, (off_t)TEMPFILE_SIZE)) {
perror("ftruncate");
exit(EXIT_FAILURE);
}
shm_unlink(tempfile);
return fd;
}
开发者ID:nminoru,项目名称:misc,代码行数:26,代码来源:fd-passing.c
示例5: CreateFileMapping
void MemArena::GrabLowMemSpace(size_t size)
{
#ifdef _WIN32
#ifndef _XBOX
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL);
GetSystemInfo(&sysInfo);
#endif
#elif defined(ANDROID)
// Use ashmem so we don't have to allocate a file on disk!
fd = ashmem_create_region("PPSSPP_RAM", size);
// Note that it appears that ashmem is pinned by default, so no need to pin.
if (fd < 0)
{
ERROR_LOG(MEMMAP, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno));
return;
}
#else
// Try to find a non-existing filename for our shared memory.
// In most cases the first one will be available, but it's nicer to search
// a bit more.
for (int i = 0; i < 10000; i++)
{
std::string file_name = StringFromFormat("/citramem.%d", i);
fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd != -1)
{
shm_unlink(file_name.c_str());
break;
}
else if (errno != EEXIST)
{
ERROR_LOG(MEMMAP, "shm_open failed: %s", strerror(errno));
return;
}
}
if (ftruncate(fd, size) < 0)
ERROR_LOG(MEMMAP, "Failed to allocate low memory space");
#endif
}
开发者ID:Dreadgamer9,项目名称:citra,代码行数:39,代码来源:mem_arena.cpp
示例6: main
int main()
{
int fd = shm_open(SHM_NAME, O_RDWR, 0777);
if (fd < 0)
{
perror("shm_open");
}
if (shm_unlink(SHM_NAME) < 0)
{
perror("shm_unlink");
}
if (sem_unlink(SEM_AFFICHAGE) < 0)
{
perror("sem_unlink");
}
if (sem_unlink(SEM_MUTEX) < 0)
{
perror("sem_unlink");
}
Tmorpion* pmorpion = (Tmorpion*) mmap(NULL, sizeof(Tmorpion), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd); // on n'a plus besoin du descripteur de fichier
if (pmorpion == (void*)-1)
{
perror("mmap");
}
int i;
char str_sem[17];
for (i=0; i < NB_MAX_JOUEURS; i++)
{
sprintf(str_sem, SEM_JOUEUR"%c", 'a'+i);
if (sem_unlink(str_sem) < 0)
perror(str_sem);
}
if (munmap((void*)pmorpion, sizeof(Tmorpion)) < 0)
{
perror("munmap");
}
exit(0);
}
开发者ID:limoges,项目名称:school,代码行数:39,代码来源:detruit.c
示例7: shm_mutex_init
int shm_mutex_init(key_t key, shm_mutex_t *pshmmutex)
{
size_t size = sizeof(pthread_mutex_t);
if( pshmmutex == NULL )
{
LOGERROR("shm_mutex_t structure is NULL.");
errno = EINVAL;
return RC_ERROR;
}
// create/attach to shared memory mutex
if( shm_open(key, size, shm_mutex_create, pshmmutex) < 0 )
{
LOGSYSERROR("shm_mutex_init(%d=0x%x, ...)", key, key);
return RC_ERROR;
}
LOGDIAG3("Shared memory mutex initialized (key=%d=0x%x, ...)", key, key);
return OK;
}
开发者ID:roadnarrows-robotics,项目名称:librnr,代码行数:22,代码来源:shm.c
示例8: open_shared_memory
static void* open_shared_memory(char* shm_name, int shm_size, int* shm_fd)
{
*shm_fd = shm_open(shm_name, O_RDWR, 0666);
if (*shm_fd == -1) {
perror("shm_open");
return NULL;
}
void* mem = mmap(0, shm_size, PROT_WRITE | PROT_READ,
MAP_SHARED, *shm_fd, 0);
if (mem == MAP_FAILED) {
int rc = close(*shm_fd);
if (rc == -1) {
perror("close");
return NULL;
}
perror("mmap");
return NULL;
}
return mem;
}
开发者ID:silviu,项目名称:so.2.linux,代码行数:22,代码来源:server.c
示例9: shm_open
static struct stats *init_stats_obj(void)
{
int32_t fd = 0;
/* Create named shared memory. */
fd = shm_open(name, O_RDWR, 0666);
if(fd < 0)
{
output(ERROR, "Can't create named shared memory\n");
return (NULL);
}
/* Map named shared object. */
test_stat = mmap(NULL, sizeof(struct stats), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(test_stat == MAP_FAILED)
{
output(ERROR, "Can't mmap shared object: %s\n", strerror(errno));
return (NULL);
}
return (test_stat);
}
开发者ID:LucaBongiorni,项目名称:nextgen,代码行数:22,代码来源:test_utils.c
示例10: main
int main(int argc, char *argv[]){
int shmfd, error;
printf("create.c\n\n");
if(argc != 2){
printf("usage: ./create [SHM-Name]\n");
return -1;
}
shmfd = shm_open(argv[1], O_CREAT | O_RDWR | O_EXCL, 0666);
if(shmfd < 1){
printf("error creating Shared Memory Segment\n");
return -1;
}
error = ftruncate(shmfd, sizeof(shared));
if(error < 0){
printf("error setting size of SHM \"%s\"\n",argv[1]);
return -1;
}
shared* myShared = (shared*)mmap(NULL, sizeof(shared), PROT_WRITE, MAP_SHARED, shmfd, 0);
if(myShared == MAP_FAILED){
printf("error mapping Shared Memory Segment\n");
}
error = sem_init(&(myShared->sem), 1, 1);
if(error != 0){
printf("\nError initializing semaphore. Error: %d\n", error);
}
error = munmap(myShared, sizeof(shared));
if(error < 0){
printf("error closing Shared Memory Segment\n");
return -1;
}
return 0;
}
开发者ID:rawgi,项目名称:BsRaVs,代码行数:38,代码来源:create.c
示例11: VLOG
void RDPListener::processDisplaySwitch(std::vector<uint32_t> msg)
{
// note that under current calling conditions, this will run in the thread of the RDPServerWorker associated with
// the VM.
VLOG(2) << "LISTENER " << this << ": Now processing display switch event";
uint32_t displayWidth = msg.at(2);
uint32_t displayHeight = msg.at(3);
pixman_format_code_t displayFormat = (pixman_format_code_t) msg.at(1);
int shim_fd;
size_t shm_size = 4096 * 2048 * sizeof(uint32_t);
// TODO: clear all queues if necessary
// map in new shmem region if it's the first time
if (!shm_buffer) {
std::stringstream ss;
ss << "/" << vm_id << ".rdpmux";
VLOG(2) << "LISTENER " << this << ": Creating new shmem buffer from path " << ss.str();
shim_fd = shm_open(ss.str().data(), O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
VLOG(3) << "LISTENER " << this << ": shim_fd is " << shim_fd;
void *shm_buffer = mmap(NULL, shm_size, PROT_READ, MAP_SHARED, shim_fd, 0);
if (shm_buffer == MAP_FAILED) {
LOG(WARNING) << "mmap() failed: " << strerror(errno);
// todo: send this information to the backend service so it can trigger a retry
return;
}
VLOG(2) << "LISTENER " << this << ": mmap() completed successfully! Yayyyyyy";
this->shm_buffer = shm_buffer;
}
this->width = displayWidth;
this->height = displayHeight;
this->format = displayFormat;
VLOG(2) << "LISTENER " << this << ": Display switch processed successfully!";
}
开发者ID:datto,项目名称:RDPMux,代码行数:38,代码来源:RDPListener.cpp
示例12: main
int main(int argc, char *argv[])
{
int pid;
int i=0;
struct shm_cnt *counter;
pid=fork();
sleep(1);
//shm_open: first process will create the page, the second will just attach to the same page
//we get the virtual address of the page returned into counter
//which we can now use but will be shared between the two processes
shm_open(1,(char **)&counter);
// printf(1,"%s returned successfully from shm_open with counter %x\n", pid? "Child": "Parent", counter);
for(i = 0; i < 10000; i++)
{
uacquire(&(counter->lock));
counter->cnt++;
urelease(&(counter->lock));
//print something because we are curious and to give a chance to switch process
if(i%1000 == 0)
printf(1,"Counter in %s is %d at address %x\n",pid? "Parent" : "Child", counter->cnt, counter);
}
if(pid)
{
printf(1,"Counter in parent is %d\n",counter->cnt);
wait();
} else
printf(1,"Counter in child is %d\n\n",counter->cnt);
//shm_close: first process will just detach, next one will free up the shm_table entry (but for now not the page)
shm_close(1);
exit();
return 0;
}
开发者ID:kglgsp,项目名称:lab2-f17,代码行数:38,代码来源:lab4.c
示例13: main
int main()
{
ring1_int *ringque = NULL;
int c,fd,flags;
char *ptr;
off_t length;
flags = O_RDWR | O_CREAT;
length = sizeof(*ringque);
fd = shm_open("kenny",flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(fd < 0){
printf("error\n");
exit(0);
}
ftruncate(fd,length);
ringque = (ring1_int*)mmap(NULL,length,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
printf("%x\n",ringque);
ring1_int_init(ringque);
while(1){
ring1_int_push(ringque,1);
}
return 0;
}
开发者ID:Danewalker,项目名称:distri.lua,代码行数:23,代码来源:shmserver.c
示例14: shm_open
bool SharedObjectsFactory::Initialize()
{
/*
* initialize using POSIX shm
*/
int shm_fd = shm_open(SHM_NAME, O_RDWR, 0666);
if(shm_fd < 0)
{
LOG_WARN_WITH_NO("shm_open");
return false;
}
void* addr = mmap(NULL, sizeof(struct SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if(addr == MAP_FAILED)
{
LOG_CRITICAL_WITH_NO("mmap");
return false;
}
this->m_addr = (struct SharedMemory*)addr;
return true;
}
开发者ID:ardeola,项目名称:RasterMap,代码行数:23,代码来源:factory.cpp
示例15: create_shmem
static void create_shmem(void)
{
int shm_fd;
if (asprintf(&shmem_name, "/%s%d", PROGNAME, getpid()) == -1)
fatal("asprintf (%s)", strerror(errno));
shm_fd = shm_open(shmem_name, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if (shm_fd == -1)
fatal("shm_open");
if (ftruncate(shm_fd, sizeof(*shm)) == -1)
fatal("ftruncate");
shm = mmap(NULL, sizeof(*shm), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
close(shm_fd);
if (shm == MAP_FAILED)
fatal("mmap");
memset(shm, 0, sizeof(*shm));
}
开发者ID:sstefani,项目名称:mtrace-old,代码行数:23,代码来源:server.c
示例16: InitSendRecvMem
void* InitSendRecvMem()
{
int rc;
send_recv_fd = shm_open(RECV_SEND_MEM, O_CREAT | O_RDWR, 0644);
rc = ftruncate(send_recv_fd, 2 * info->proc_nr * getpagesize());
DIE(rc == -1, "ftruncate");
send_recv_mem = mmap(0, 2 * info->proc_nr * getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, send_recv_fd, 0);
DIE(send_recv_mem == MAP_FAILED, "mmap");
int i;
memset(send_recv_mem, 0x0, 2 * info->proc_nr * getpagesize());
big_mem_access = malloc(sizeof(sem_t*) * info->proc_nr);
for(i = 0; i < info->proc_nr; ++i){
char name[40];
sprintf(name, "sem_name_%d", i);
big_mem_access[i] = sem_open(name, O_CREAT, 0644, 1);
DIE(big_mem_access[i] == SEM_FAILED, "sem_open failed");
}
return send_recv_mem;
}
开发者ID:yonutix,项目名称:MPI-lib,代码行数:23,代码来源:mpirun.c
示例17: m_startOffset
MmapFile::MmapFile(const char *fileName, const int isShm,
const Mode mode, const int pageSize)
: m_startOffset(0), m_endOffset(0), m_isShm(isShm),
m_ptr(NULL), m_fileName(NULL)
{
int sysPageSize = getpagesize();
m_pageSize = pageSize > 0 ? pageSize : sysPageSize;
m_fd = m_isShm ? shm_open(fileName, F_MODE[mode], S_IRUSR | S_IWUSR)
: open(fileName, F_MODE[mode], S_IRUSR | S_IWUSR);
if (-1 == m_fd)
{
ERRORLOG2("open file %s failed, err %s",
fileName, strerror(errno));
return;
}
StrUtil::copy(m_fileName, fileName);
m_prot = M_MODE[mode];
m_fileSize = File::getSize(m_fd);
mapByOffset(0);
}
开发者ID:ivanchen36,项目名称:eagle,代码行数:23,代码来源:MmapFile.cpp
示例18: openPosixSharedMemory
void* openPosixSharedMemory()
{
int shm_fd = shm_open("/MyMemoryMap",O_CREAT | O_RDWR, /*S_IRWXU | S_IRWXG*/0777);
if(shm_fd==-1)
{
perror("shm_open");
return NULL;
}
if ( ftruncate(shm_fd, 1000) == -1 ) {
perror("ftruncate");
return NULL;
}
void *memory = mmap(NULL, 1000, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (memory == NULL) {
perror("mmap");
return NULL;
}
return memory;
}
开发者ID:vovaprog,项目名称:learn,代码行数:23,代码来源:posix_memory_map.cpp
示例19: test_dup
uintmax_t
test_dup(uintmax_t num, uintmax_t int_arg, const char *path)
{
int fd, i, shmfd;
shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
if (shmfd < 0)
err(-1, "test_dup: shm_open");
fd = dup(shmfd);
if (fd >= 0)
close(fd);
benchmark_start();
for (i = 0; i < num; i++) {
if (alarm_fired)
break;
fd = dup(shmfd);
if (fd >= 0)
close(fd);
}
benchmark_stop();
close(shmfd);
return (i);
}
开发者ID:edgar-pek,项目名称:PerspicuOS,代码行数:23,代码来源:syscall_timing.c
示例20: shm_open
bool shared_memory::open(std::string const& _name, size_t _sz)
{
shmem_internal *si = shmem_internal::get(mInternal);
if(!si || si->handle != -1)
return false;
int fd = shm_open(_name.c_str(), O_RDWR, 0664);
if(fd == -1)
return false;
void *ptr = mmap(NULL, _sz, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if(!ptr)
{
::close(fd);
return false;
}
si->handle = fd;
si->pointer = ptr;
si->size = _sz;
return true;
}
开发者ID:ricky26,项目名称:netlib,代码行数:23,代码来源:shared_memory_linux.cpp
注:本文中的shm_open函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论