LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1121|回复: 7

share memory?

[复制链接]
发表于 2007-5-23 17:13:39 | 显示全部楼层 |阅读模式
我想share 一個int 數字給client 使用和修改...

怎樣可以實現??
 楼主| 发表于 2007-5-23 20:55:24 | 显示全部楼层
這個改自網絡例子
大部分的例子都是用 char * ...
我想問改成 int * 之後, 怎樣 share int 出來...

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#define SHMSZ (27)

int main(){
        char c;
        int shmid;
        key_t key;
        char *shm;
       
        key = 5678;
       
        if((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0){
                perror("shmget");
                exit(1);
        }else{
                printf("shraed memory for key %d: %d\n", key, shmid);
        }

        if((shm = shmat(shmid, NULL, 0)) == (char *) -1){
                perror("shmat");
                exit(1);
        }

        sprintf(shm, "hello world\n");
        printf("shared memory content: %s\n", shm);
        sleep(1000);
        return 0;
}
回复 支持 反对

使用道具 举报

发表于 2007-5-24 11:16:47 | 显示全部楼层
我的一个例子
  1. void *group_count_notify()
  2. {
  3.         int *count;
  4.         int fd;
  5.         int shm_id;
  6.         key_t key;
  7.         char *name="/dev/shm/groupcount";
  8.         if((fd = open(name,O_CREAT|O_EXCL,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1)
  9.         {
  10.                 if(errno != EEXIST)
  11.                         return NULL;
  12.         }
  13.         else
  14.                 close(fd);
  15.         key = ftok(name,0);
  16.         if(key == -1)
  17.                 log(LOG_ERROR, "ftok error\n");
  18.         shm_id = shmget(key,sizeof(int),IPC_CREAT);
  19.         if(shm_id == -1)
  20.         {
  21.                 log(LOG_ERROR, "shmget error\n");
  22.                 return NULL;
  23.         }
  24.         count = (int *)shmat(shm_id,0,0);
  25.         signal(SIGPIPE,SIG_IGN);
  26.         while(1){
  27.                 *count = group_get_count("calling", "GROUP");
  28.                 sleep(2);
  29.         }
  30.         return NULL;
  31. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-24 12:31:39 | 显示全部楼层
請問錯在哪裡...
不能將 紅色部分 share 出去...
shm_read..讀取不到 數字 (999)


shm_allocate.c

  1. #include <stdio.h>
  2. #include <sys/shm.h>
  3. #include <sys/stat.h>

  4. int main(int argc, char *argv[])
  5. {
  6.    int seg_id;
  7.    int seg_size = 0x4000;
  8.    int *shared_memory;

  9.    /* shared memory: allocate */
  10.    seg_id = shmget(IPC_PRIVATE, seg_size,
  11.                    IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
  12.    printf("Shared Memory Segment ID: %d\n", seg_id);

  13.    /* shared memory: attach */
  14.    shared_memory = (int *)shmat(seg_id, 0, 0);
  15.    
  16.    /* write to shared memory */
  17.    //sprintf(shared_memory, argv[1]);

  18.    [color=red]shared_memory = (int *)999;[/color]
  19.    printf("Address: %p  and Message:: %d\n",shared_memory, shared_memory);
  20.    /* shared memory: detach */
  21.    shmdt(shared_memory);

  22.    exit(0);
  23. }
复制代码



shm_read.c

  1. #include <stdio.h>
  2. #include <sys/shm.h>
  3. #include <sys/stat.h>

  4. int main(int argc, char *argv[])
  5. {
  6.    int seg_id;
  7.    int *shared_memory;

  8.    /* get shmid */
  9.    seg_id = atoi(argv[1]);

  10.    /* shared memory: attach */
  11.    shared_memory = (int *)shmat(seg_id, 0, 0);
  12.   

  13.    /* read shared memory */
  14.    for (;;) {
  15.       printf("Address: %p  and  Message: %d\n", shared_memory, shared_memory);
  16.       sleep(1);
  17.    }

  18.    /* shared memory: detach */
  19.    shmdt(shared_memory);

  20.    exit(0);
  21. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-5-24 13:12:43 | 显示全部楼层
你那是在将999这个数转换成一个地址再将这个地址赋给shared_memory,而不是将999这个数写道shared_memory所指的内存中,怎么可以那样强制转换呢,可以的话就是我无知了。
  1.    shared_memory = (int *)shmat(seg_id, 0, 0);
  2.    
  3.    /* write to shared memory */
  4.    //sprintf(shared_memory, argv[1]);
  5.    *shared_memory = 999;
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-24 13:22:53 | 显示全部楼层
Post by Arthur.Echo
你那是在将999这个数转换成一个地址再将这个地址赋给shared_memory,而不是将999这个数写道shared_memory所指的内存中,怎么可以那样强制转换呢,可以的话就是我无知了。
  1.    shared_memory = (int *)shmat(seg_id, 0, 0);
  2.    
  3.    /* write to shared memory */
  4.    //sprintf(shared_memory, argv[1]);

  5.    *shared_memory = 999;
复制代码



可以了..
為什麼 shared_memory 在shm_allocate.c 印出來的地址   及shm_read.c 的地址不一樣??
回复 支持 反对

使用道具 举报

发表于 2007-5-25 09:00:59 | 显示全部楼层
因为每个进程都有自己的地址空间,而且也不是真实的物理地址空间,32位机的话每个进程的地址空间都是2^32-1,所以说,两个进程的地址是不相关的,即便他们的某个指针指向了同一个物理内存单元,在不同的进程中那个指针值也不一定相同,我是这样理解的,错了就请纠正
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-25 20:19:06 | 显示全部楼层

semaphore 是否比 share memory 更有效管理read/ write??

semaphore 是否比 share memory 更有效管理read/ write??
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表