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

推荐订阅源

S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
量子位
Security Latest
Security Latest
P
Proofpoint News Feed
P
Privacy International News Feed
P
Palo Alto Networks Blog
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
Vercel News
Vercel News
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
Kaspersky official blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Stack Overflow Blog
Stack Overflow Blog
AWS News Blog
AWS News Blog
博客园 - Franky
爱范儿
爱范儿
T
Tor Project blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
小众软件
小众软件
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
W
WeLiveSecurity
SecWiki News
SecWiki News
L
LangChain Blog
I
InfoQ

博客园 - 萧佰刚

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);
            }
        }    
    }
}
/*********************程序运行结果*********************


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