linux共享内存初体验

一,通信代码

1.进程1代码:

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include<sys/types.h>

intmain()

{

        void* shm = NULL;

        int shmid;

        shmid = shmget(1314,10,0666|IPC_CREAT);

        if(-1 == shmid)

        {

                printf("shmgeterror\n");

                return -1;

        }

        shm = shmat(shmid,0,0);

        if((void*)-1 == shm)

        {

                printf("shmaterror\n");

                return -1;

        }

        sprintf((char*)shm,"%s","hello");

        if(-1 == shmdt(shm))

        {

                printf("shmdterror\n");

                return -1;

        }

        return 0;

 

}

2.进程2代码:

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/shm.h>

#include<sys/types.h>

intmain()

{

        void* shm = NULL;

        int shmid;

        shmid = shmget(1314,10,0666|IPC_CREAT);

        if(-1 == shmid)

        {

                printf("shmgeterror\n");

                return -1;

        }

        shm = shmat(shmid,0,0);

        if((void*)-1 == shm)

        {

                printf("shmaterror\n");

                return -1;

        }

        printf("%s\n",(char*)shm);

        if(-1 == shmctl(shmid,IPC_RMID,NULL))

        {

                printf("shmctlerror\n");

                return -1;

        }

        return 0;

 

}

二,相关函数说明

1.  相关包含头文件:

#include <sys/ipc.h>

#include <sys/types.h>

#include<sys/shm.h>

2.shmget

函数作用:得到一个共享内存标识符或创建一个共享内存对象。

原型:intshmget(key_t key, size_t size, int shmflg)

返回值:成功返回共享内存的标识符,出错返回-1.

3. shmat

函数作用:把共享内存区对象映射到调用进程的地址空间。

原型:void *shmat(intshmid, const void *shmaddr, int shmflg)

返回值:成功返回附加好的共享内存地址,出错返回-1.

4. shmdt

函数作用:断开共享内存连接。

原型:int shmdt(constvoid *shmaddr)

返回值:成功返回0,出错返回-1.

5. shmctl

函数作用:共享内存管理

原型:int shmctl(intshmid, int cmd, struct shmid_ds *buf)

返回值:成功返回0,出错返回-1.

三,共享内存释放

1.   shell中可以查看和释放已分配的共享内存。

2.   ipcs命令

不加参数,会显示出共享内存,信号量,消息队列;

-m参数,只显示共享内存;

例如:

root@ubuntu:/usr/study# ipcs

 

------ Shared Memory Segments --------

key        shmid      owner     perms      bytes      nattch    status     

0x00000522 65536     root       666        10         0                      

 

------ Semaphore Arrays --------

key        semid      owner     perms      nsems    

 

------ Message Queues --------

key        msqid      owner     perms      used-bytes   messages   

 

root@ubuntu:/usr/study# ipcs -m

 

------ Shared Memory Segments --------

key        shmid      owner     perms      bytes      nattch    status     

0x00000522 65536     root       666        10         0                      

3.ipcrm命令

删除共享内存。

root@ubuntu:/usr/study#ipcs -m

 

------ SharedMemory Segments --------

key        shmid      owner     perms      bytes      nattch    status     

0x0000052265536      root       666        10         0                      

 

root@ubuntu:/usr/study#ipcrm -m 65536

root@ubuntu:/usr/study#ipcs -m

 

------ SharedMemory Segments --------

key        shmid      owner     perms      bytes      nattch    status     

4.释放所有已分配的共享内存

ipcs -m | awk '$2 ~ /[0-9]+/ {print $2}' | while read s; dosudo ipcrm -m $s; done

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值