惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - 萧佰刚

Mtk Ft6306 touch 驱动 . 深圳市鑫侑触控技术有限公司 电容式触摸屏|电容式触摸屏厂|手机触摸屏厂|偏光片贴合 Qt Creator 常用快捷键 结构体指针的指针使用(转) "互斥锁用于上锁,条件变量则用于等待" Linux下UDP编程 Linux Socket编程学习 linux管道学习资料 linux进程间通信——消息队列 C实现单链表(转) c语言 位操作 C语言枚举类型使用简介 指向结构体指针的例子 字符数组 字符指针 sizeof strlen 的区别 Linux进程间共享临界区“信号量”编程 Linux下Socket编程 Linux下常用函数- 信号处理函数(转) linux信号signal常用信号 Linux 守护进程的编程方法(转)
System V共享内存资料
萧佰刚 · 2011-07-16 · via 博客园 - 萧佰刚

                                        内存映射图 

所用主要函数:shmget(),shmat(),shmctl(),shmdt()

shmget:通过文件描述符(一般为绝对路径名)产生Key,创建新的共享内存区(最大4096KB)

shmat:    将共享内存区映射到调用进程(A,B进程)的地址空间。

shmctl:   设置(IPC_SET),获取(IPC_STAT),删除(IPC_RMID)共享内存区。

 shmdt():进程删除共享内存区映射的内存段

*********************程序相关信息*********************/
#include
<sys/ipc.h>
#include
<sys/shm.h>
#include
<stdlib.h>
#include
<string.h>
#include
<stdio.h>
int main()
{
    
int pid,shmid;//后者为共享内存识别代号
    char *write_address;
    
char *read_address;
    
struct shmid_ds dsbuf;
    
if((shmid=shmget(IPC_PRIVATE,32,0))<0)//分配共享内存
    {
        printf(
"shmid共享内存分配出现错误。\n");
        exit(
1);
    }
    
else
        printf(
"shmid共享内存分配成功,共享内存识别代号为:%d。\n",shmid);
    
if((pid=fork())<0)
    {
        printf(
"fork函数调用出现错误!\n");
        exit(
2);
    }
    
else if(pid>0)//父进程,向共享内存中写入数据
    {
        printf(
"父进程的ID是:%d\n",getpid());
        write_address
=(char *)shmat(shmid,NULL,0);//连接共享内存
        if((int)write_address==-1)
        {
            printf(
"shmat连接共享内存错误。\n");
            exit(
3);
        }
        
else
        {
            printf(
"shmat连接共享内存成功。\n");
            strcpy(write_address,
"我是写入共享内存的测试数据");//将数据写入共享内存
            printf("写入共享内存的信息为“%s”。\n",write_address);
            
if((shmdt((void *)write_address))<0)//断开与共享内存的连接
                printf("shmdt共享内存断开错误。\n");
            
else
                printf(
"shmdt共享内存断开成功。\n");
            sleep(
2);
            
return;
        }
    }
    
else//子进程,从共享内存中读取数据
    {
        sleep(
2);//等待父进程写入共享内存完毕
        printf("子进程ID是:%d\n",getpid());
        
if((shmctl(shmid,IPC_STAT,&dsbuf))<0)
        {
            printf(
"shmctl获取共享内存数据结构出现错误。\n");
            exit(
4);
        }
        
else
        {
            printf(
"shmctl获取共享内存数据结构成功。\n建立这个共享内存的进程ID是:%d\n",dsbuf.shm_cpid);
            printf(
"该共享内存的大小为:%d\n",dsbuf.shm_segsz);
            
if((read_address=(char *)shmat(shmid,0,0))<0)//连接共享内存
            {
                printf(
"shmat连接共享内存出现错误。\n");
                exit(
5);
            }
            
else
            {
                printf(
"自共享内存中读取的信息为:“%s”。\n",read_address);
                printf(
"最后一个操作该共享内存的进程ID是:%d\n",dsbuf.shm_lpid);
                
if((shmdt((void *)read_address))<0)//断开与共享内存的连接
                {
                    printf(
"shmdt共享内存断开错误。\n");
                    exit(
6);
                }
                
else
                    printf(
"shmdt共享内存断开成功。\n");
                
if(shmctl(shmid,IPC_RMID,NULL)<0)//删除共享内存及其数据结构
                {
                    printf(
"shmctl删除共享内存及其数据结构出现错误。\n");
                    exit(
7);
                }
                
else
                    printf(
"shmctl删除共享内存及其数据结构成功。\n");
                exit(
0);
            }
        }    
    }
}
/*********************程序运行结果*********************


其它参考学习:享内存的使用方法