本文整理汇总了C++中shm_open函数的典型用法代码示例。如果您正苦于以下问题:C++ shm_open函数的具体用法?C++ shm_open怎么用?C++ shm_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了shm_open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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);
}
}
示例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);
}
示例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;
}
示例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
}
示例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);
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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!";
}
示例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;
}
示例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;
}
示例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;
}
示例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));
}