⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lc_shmem.c

📁 open source for car navigation in linux
💻 C
字号:
/* * Copyright (C) 2002 Ricardo Arroyo <ricardo.arroyo@eresmas.net> * * This code may be used under the terms of Version 2 of the GPL, * read the file COPYING for details. * */#include <stdio.h>#include <errno.h>#include <unistd.h>#include <ulimit.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include "lc_syslog.h"#include "lc_debug.h"#include "lc_shmem.h"/*__________________________________________________________________________*//* FUNCTION                pCreateShMem(key_t SHmKey, int iLong)            *//* DESCRIPTION             Creates a shared memory area asociated with      *//*                         given SHmKey and iLong bytes long                *//*			                                                    *//* INPUT PARAMETERS        SHmKey       Shared Memory Key                   *//*                                      (use ftok to get it)                *//*                                                                          *//*                         iLong        Length in bytes of memory area      *//*                                                                          *//* OUTPUT PARAMETERS       pointer to shared memory area                    *//* 			                                                    *//*__________________________________________________________________________*/void* pCreateShMem(key_t SHmKey, int iLong){void* punt;int shm_id;   if (iDebug & DEP_SHMEM)   {      fprintf(stderr,"pCreateShMem: break = %lx, ulimit = %lx\n",(long) sbrk(0),(long)ulimit(3,0L));   }   if ((shm_id = shmget(SHmKey, iLong, 0666 | IPC_CREAT)) == -1)   {      vLC_syslog(LCLOG_ERR_SHMGET, "pCreateShMem", errno);      return (NULL);   }   else if ((int)(punt = shmat(shm_id, 0, 0666)) == -1)   {      vLC_syslog(LCLOG_ERR_SHMAT, "pCreateShMem", errno);      return (NULL);   }   if (iDebug & DEP_SHMEM)   {      fprintf(stderr,"pCreateShMem: Shared Memory attached, Key = %ld, shm_id = %d\n",(long)SHmKey, iLong);      fprintf(stderr,"pCreateShMem: break = %lx, shm = %lx, ulimit = %lx\n",(long) sbrk(0),(long)punt,(long)ulimit(3,0L));   }   return(punt);}/*__________________________________________________________________________*//* FUNCTION                pAssignShMem(key_t SHmKey, int iLong)            *//* DESCRIPTION             Attachs a previous created shared memory area    *//*                         asociated with given SHmKey and iLong bytes long *//*			                                                    *//* INPUT PARAMETERS        SHmKey       Shared Memory Key                   *//*                                      (use ftok to get it)                *//*                                                                          *//*                         iLong        Length in bytes of memory area      *//*                                                                          *//* OUTPUT PARAMETERS       pointer to shared memory area                    *//* 			                                                    *//*__________________________________________________________________________*/void* pAssignShMem(key_t SHmKey, int iLong){void* punt;int shm_id;   if (iDebug & DEP_SHMEM)   {      fprintf(stderr,"pAssignShMem: break = %lx, ulimit = %lx\n",(long)sbrk(0),(long)ulimit(3,0L));   }   if ((shm_id = shmget(SHmKey, iLong, 0666 | IPC_CREAT)) == -1)   {      vLC_syslog(LCLOG_ERR_SHMGET, "pAssignShMem", errno);      return (NULL);   }   else if ((int)(punt = shmat(shm_id, 0, 0666)) == -1)   {      vLC_syslog(LCLOG_ERR_SHMAT, "pAssignShMem", errno);      return (NULL);   }   if (iDebug & DEP_SHMEM)   {      fprintf(stderr,"pAssignShMem: Shared Memory attached, Key = %ld, shm_id = %d\n",(long)SHmKey, iLong);      fprintf(stderr,"pAssignShMem: break = %lx, shm = %lx, ulimit = %lx\n",(long)sbrk(0),(long)punt,(long)ulimit(3,0L));   }   return(punt);}/*__________________________________________________________________________*//* FUNCTION                iDetachShMem (punt)                              *//*                                                                          *//* INPUT PARAMETERS        punt       pointer to shared memory area         *//*                                                                          *//* OUTPUT PARAMETERS       0 = memory dettached                             *//*                        -1 = error (chech errno)                          *//* 			                                                    *//*__________________________________________________________________________*/int iDetachShMem(void *punt){int	res;   if (iDebug & DEP_SHMEM)   {      fprintf(stderr,"iDetachShMem: break = %lx, shm = %lx, ulimit = %lx\n",(long)sbrk(0), (long)punt,(long)ulimit(3,0L));   }	res = shmdt(punt);   if (iDebug & DEP_SHMEM)   {      fprintf(stderr,"iDetachShMem: break = %lx, ulimit = %lx\n",(long) sbrk(0),(long)ulimit(3,0L));   }   return(res);}/*__________________________________________________________________________*//* FUNCTION                iDelShMem (key_t ShmKey)                         *//*                                                                          *//* INPUT PARAMETERS        SHmKey       Shared Memory Key                   *//*                                      (use ftok to get it)                *//*                                                                          *//* OUTPUT PARAMETERS       0 = memory dettached                             *//*                        -1 = error (chech errno)                          *//* 			                                                    *//*__________________________________________________________________________*/int iDelShMem( key_t SHmKey){int shm_id;struct shmid_ds shmstr;int res;   if ((shm_id = shmget(SHmKey,0,0666)) != -1)   {      if ((res = shmctl(shm_id, IPC_RMID, &shmstr)) == -1)      {         vLC_syslog(LCLOG_ERR_SHMRM, "iDelShMem", errno);         return (res);      }      return(0);   }   vLC_syslog(LCLOG_ERR_SHMRM,"iDelShMem", 0);   return(-1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -