Chinaunix首页 | 论坛 | 博客
  • 博客访问: 153346
  • 博文数量: 34
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-03 09:44
文章分类

全部博文(34)

文章存档

2011年(1)

2008年(33)

我的朋友

分类: LINUX

2008-07-16 09:57:23

#include
 long        mem_id;
 key_t       mem_key;
 void*       mem_addr;
 long  semid;
 key_t semkey;
 struct sembuf sbuf;
void CreateShareMemory()
{
    if ( ( mem_key = ftok("/bin/udhcpd", 1) ) == (key_t)(-1) )  {
            printf("Failed to generate shared memory access key, ERRNO=%d\n",errno);
    return;
    }
    if ( ( mem_id = shmget(mem_key, SHARE_MEMORY_SIZE*sizeof(IP_POOL_SHARE), IPC_CREAT) ) == (-1) )  {
            printf("Failed to obtain shared memory ID, ERRNO=%d\n", errno);
            return;
    }
   if ( ( mem_addr= (void *)shmat(mem_id, 0, 0) ) == (void *)(-1) )
  printf("Failed to attach shared memory,  errno:%d\n",errno);
  else
  printf("Successfully attached shared memory to %p\n",mem_addr);
     semkey=mem_key;
    if ((semid = semget(semkey, 0, 0)) == -1) { 
 
                   /* Semaphore does not exist - Create. */
                   if ((semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL )) != -1)
                   {
                       /* Initialize the semaphore. */
                       sbuf.sem_num = 0;
                       sbuf.sem_op = 1;  /* This is the number of runs  without queuing. */
                       sbuf.sem_flg = 0;
                       if (semop(semid, &sbuf, 1) == -1) {
                           perror("IPC error: semop"); exit(1);
                       }
                   }
                   else if (errno == EEXIST) {
                       if ((semid = semget(semkey, 0, 0)) == -1) {
                           perror("IPC error 1: semget"); exit(1);
                       }
                   }
                   else {
                       perror("IPC error 2: semget"); exit(1);
                   }
 }
}
void askForSem()
{
      sbuf.sem_num = 0;
                       sbuf.sem_op = -1;  /* This is the number of runs  without queuing. */
                       sbuf.sem_flg = 0;
                       if (semop(semid, &sbuf, 1) == -1) {
                           perror("IPC error: semop");
                       }
}
void restoreSem()
{
       sbuf.sem_num = 0;
                       sbuf.sem_op = 1;  /* This is the number of runs  without queuing. */
                       sbuf.sem_flg = 0;
                       if (semop(semid, &sbuf, 1) == -1) {
                           perror("IPC error: semop");
                       }
}
void DeleteShareMemory()
{
 shmctl(mem_id, IPC_RMID, NULL);
 semctl(semid, 0,IPC_RMID, NULL);
}
void unlinkShareMemory()
{
 shmdt(mem_addr); 
}
void ReadShareMemory()
{
}
void ShowShareMemory()
{
 askForSem();
 IP_POOL_SHARE *ips=(IP_POOL_SHARE*)mem_addr;
 int i=0;
 for(i=0;i  {
   if(ips[i].ip!=0)    
    printf("[%d] ip=0x%08x  flag=%d\n",i,ips[i].ip,ips[i].flag);
    
  }
 restoreSem(); 
}
//let the ip which is equal to ip be value
void WriteShareMemory(unsigned int ip,unsigned int value,unsigned char flag)
{
 askForSem();
 IP_POOL_SHARE* ips=(IP_POOL_SHARE*)mem_addr;
 int i=0;
 for(i=0;i  {
   if(ips[i].ip==ip)
    {
    ips[i].ip=value;
    ips[i].flag=flag;
    break;
    }
  }
 restoreSem(); 
 ShowShareMemory();
}
//check whether one ip is in the sharememory.
//1--in use 0--no use
int checkShareMemory(unsigned int ip)
{
 askForSem(); 
 IP_POOL_SHARE *ips=(IP_POOL_SHARE*)mem_addr;
 int i=0;
 for(i=0;i  {
   if(ips[i].ip==ip)    
    {
    printf("The ip 0x%08x has been used by %d\n",ip,ips[i].flag);
    restoreSem();
    return 1;
    }
    
  }
 restoreSem();
 return 0;
}
阅读(1540) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~