zoukankan      html  css  js  c++  java
  • 共享内存应用范例

    1、父子进程通信范例 父子进程通信范例,shm.c源代码如下:

    #include <stdio.h>

    #include <unistd.h>

    #include <string.h>

    #include <sys/ipc.h>

    #include <sys/shm.h>

    #include <error.h>

    #define SIZE 1024

    int main()

    {

    int shmid ;

    char *shmaddr ;

    struct shmid_ds buf ;

    int flag = 0 ;

    int pid ;

    shmid = shmget(IPC_PRIVATE, SIZE, IPC_CREAT|0600 ) ;

    if ( shmid < 0 )

    {

    perror("get shm ipc_id error") ;

    return -1 ;

    }

    pid = fork() ;

    if ( pid == 0 )

    {

    shmaddr = (char *)shmat( shmid, NULL, 0 ) ;

    if ( (int)shmaddr == -1 )

    {

    perror("shmat addr error") ;

    return -1 ;

    }

    strcpy( shmaddr, "Hi, I am child process! ") ;

    shmdt( shmaddr ) ;

    return 0;

    } else if ( pid > 0) {

    sleep(3 ) ;

    flag = shmctl( shmid, IPC_STAT, &buf) ;

    if ( flag == -1 )

    {

    perror("shmctl shm error") ;

    return -1 ;

    }

    printf("shm_segsz =%d bytes ", buf.shm_segsz ) ;

    printf("parent pid=%d, shm_cpid = %d ", getpid(), buf.shm_cpid ) ;

    printf("chlid pid=%d, shm_lpid = %d ",pid , buf.shm_lpid ) ;

    shmaddr = (char *) shmat(shmid, NULL, 0 ) ;

    if ( (int)shmaddr == -1 )

    {

    perror("shmat addr error") ;

    return -1 ;

    }

    printf("%s", shmaddr) ;

    shmdt( shmaddr ) ;

    shmctl(shmid, IPC_RMID, NULL) ;

    }else{

    perror("fork error") ;

    shmctl(shmid, IPC_RMID, NULL) ;

    }

    return 0 ;

    }

    编译 gcc shm.c –o shm。

    执行 ./shm,执行结果如下:

    shm_segsz =1024 bytes

    shm_cpid = 9503

    shm_lpid = 9504

    Hi, I am child process!

    2、多进程读写范例

    多进程读写即一个进程写共享内存,一个或多个进程读共享内存。下面的例子实现的是一个进程写共享内存,一个进程读共享内存。

    (1)下面程序实现了创建共享内存,并写入消息。

    shmwrite.c源代码如下:

    #include <stdio.h>

    #include <sys/ipc.h>

    #include <sys/shm.h>

    #include <sys/types.h>

    #include <unistd.h>

    #include <string.h>

    typedef struct{

    char name[8];

    int age;

    } people;

    int main(int argc, char** argv)

    {

    int shm_id,i;

    key_t key;

    char temp[8];

    people *p_map;

    char pathname[30] ;

    strcpy(pathname,"/tmp") ;

    key = ftok(pathname,0x03);

    if(key==-1)

    {

    perror("ftok error");

    return -1;

    }

    printf("key=%d ",key) ;

    shm_id=shmget(key,4096,IPC_CREAT|IPC_EXCL|0600);

    if(shm_id==-1)

    {

    perror("shmget error");

    return -1;

    }

    printf("shm_id=%d ", shm_id) ;

    p_map=(people*)shmat(shm_id,NULL,0);

    memset(temp, 0x00, sizeof(temp)) ;

    strcpy(temp,"test") ;

    temp[4]='0';

    for(i = 0;i<3;i++)

    {

    temp[4]+=1;

    strncpy((p_map+i)->name,temp,5);

    (p_map+i)->age=0+i;

    }

    shmdt(p_map) ;

    return 0 ;

    }

    (2)下面程序实现从共享内存读消息。

    shmread.c源代码如下:

    #include <stdio.h>

    #include <string.h>

    #include <sys/ipc.h>

    #include <sys/shm.h>

    #include <sys/types.h>

    #include <unistd.h>

    typedef struct{

    char name[8];

    int age;

    } people;

    int main(int argc, char** argv)

    {

    int shm_id,i;

    key_t key;

    people *p_map;

    char pathname[30] ;

    strcpy(pathname,"/tmp") ;

    key = ftok(pathname,0x03);

    if(key == -1)

    {

    perror("ftok error");

    return -1;

    }

    printf("key=%d ", key) ;

    shm_id = shmget(key,0, 0);

    if(shm_id == -1)

    {

    perror("shmget error");

    return -1;

    }

    printf("shm_id=%d ", shm_id) ;

    p_map = (people*)shmat(shm_id,NULL,0);

    for(i = 0;i<3;i++)

    {

    printf( "name:%s ",(*(p_map+i)).name );

    printf( "age %d ",(*(p_map+i)).age );

    }

    if(shmdt(p_map) == -1)

    {

    perror("detach error");

    return -1;

    }

    return 0 ;

    }

    (3)编译与执行

    ① 编译gcc shmwrite.c -o shmwrite。

    ② 执行./shmwrite,执行结果如下:

    key=50453281

    shm_id=688137

    ③ 编译gcc shmread.c -o shmread。

    ④ 执行./shmread,执行结果如下:

    key=50453281

    shm_id=688137

    name:test1

    age 0

    name:test2

    age 1

    name:test3

    age 2

    ⑤ 再执行./shmwrite,执行结果如下:

    key=50453281

    shmget error: File exists

    ⑥ 使用ipcrm -m 688137删除此共享内存。

  • 相关阅读:
    [转载]微软4月13日发布Silverlight 4
    关于文件流Seek以及Read操作的一点不满
    团队基础生成自动化流程之最佳实践(IV) 重写团队基础默认生成流程
    谁是你的下一行CODE
    团队基础生成自动化流程之最佳实践总论(II) – 程序集版本信息
    微软Visual Studio 2010 第三集:幸福要敏捷
    团队基础生成自动化流程之最佳实践(VI) 系统模块化条件编译
    团队基础生成自动化流程之最佳实践(V) 使用Desktop Build
    VS2010 "内存不足" 错误补丁
    彩虹天堂
  • 原文地址:https://www.cnblogs.com/wonglou/p/3971159.html
Copyright © 2011-2022 走看看