linux共享内存通信问题

hanyunqi 2012-07-27 10:13:21
我想通过共享内存(shmget/shmat/shmdt/shmctl)实现父子进程间的通信(fork())。
主要内容就是,申请一段共享内存,子进程向这段内存中写数据,父进程读出来。
可是,我在子进程中写完了,在子进程中可以读,在父进程中却读不出来。
这个问题,已经困扰我一天了,求大神帮忙。
源代码如下:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <malloc.h>
#include <sys/stat.h>
#include <string.h>

int main(int argc, char* argv[])
{
int shm_id, shm_key = 12345;
unsigned short errno;
void **shmptr;
shm_id = shmget((key_t)shm_key, (size_t)(sizeof(char) * 100), IPC_CREAT|0666);
if (shm_id < 0)
{
printf("change the shm_key\n.");
return 0;
}
printf("shm_id = %d\n", shm_id);

shmptr = new void *;
*shmptr = NULL;


pid_t pid;
int count=0;
pid = fork();
printf( "This is first time, pid = %d\n", pid );
printf( "This is secONd time, pid = %d\n", pid );
if ( pid>0 )
{

*shmptr = (char *)shmat(shm_id, 0, 0);
if (*shmptr == (void *)-1)
{
printf("shmat fail.\n");
perror("shmat error");
return 0;
}
//sleep(1);
printf( "This is the parent Process.\n");
printf("%s\n",shmptr);

}
else if ( 0 == pid )
{
*shmptr = (char *)shmat(shm_id, 0, 0);
if (*shmptr == (void *)-1)
{
printf("shmat fail.\n");
perror("shmat error");
return 0;
}
printf( "This is the child Process.\n");
strcpy((char *)shmptr, "123qweasd123");
printf("%s\n",shmptr);
}
else
{
printf( "fork failed.\n" );
}
printf( "This is third time, pid = %d\n", pid );
printf( "This is fouth time, pid = %d\n", pid );
//printf("%s\n",shmptr);




//释放共享内存
if (-1 == shmdt(shmptr))
{
printf("shmdt == -1\n");
perror("shmdt error");
return 0;
}
if (-1 == shmctl(shm_id, IPC_RMID, 0))
{
printf("shmctl == -1\n");
return 0;
}
return 0;
}


运行结果如下:
shm_id = 2261058
This is first time, pid = 0
This is secONd time, pid = 0
This is the child Process.
123qweasd123
This is third time, pid = 0
This is fouth time, pid = 0
shmdt == -1
shmdt error: Invalid argument
This is first time, pid = 13324
This is secONd time, pid = 13324
This is the parent Process.

This is third time, pid = 13324
This is fouth time, pid = 13324
shmdt == -1
shmdt error: Invalid argument
...全文
274 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
诚朴勇毅 2012-07-27
  • 打赏
  • 举报
回复
换成C++代码吧

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <malloc.h>
#include <sys/stat.h>
#include <string.h>

int main(int argc, char* argv[])
{
int shm_id, shm_key = 12345;
unsigned short errno;
void **shmptr;
shm_id = shmget((key_t)shm_key, (size_t)(sizeof(char) * 100), IPC_CREAT|0666);
if (shm_id < 0)
{
printf("change the shm_key\n.");
return 0;
}
printf("shm_id = %d\n", shm_id);

shmptr = new void *;
*shmptr = NULL;


pid_t pid;
int count=0;
pid = fork();
printf( "This is first time, pid = %d\n", pid );
printf( "This is secONd time, pid = %d\n", pid );
if ( pid>0 )
{

*shmptr = (char *)shmat(shm_id, 0, 0);
if (*shmptr == (void *)-1)
{
printf("shmat fail.\n");
perror("shmat error");
return 0;
}
//sleep(1);
printf( "This is the parent Process.\n");
printf("%s\n",shmptr);

}
else if ( 0 == pid )
{
*shmptr = (char *)shmat(shm_id, 0, 0);
if (*shmptr == (void *)-1)
{
printf("shmat fail.\n");
perror("shmat error");
return 0;
}
printf( "This is the child Process.\n");
strcpy((char *)shmptr, "123qweasd123");
printf("%s\n",shmptr);
}
else
{
printf( "fork failed.\n" );
}
printf( "This is third time, pid = %d\n", pid );
printf( "This is fouth time, pid = %d\n", pid );
//printf("%s\n",shmptr);




//释放共享内存
if (-1 == shmdt(shmptr))
{
printf("shmdt == -1\n");
perror("shmdt error");
return 0;
}
if (-1 == shmctl(shm_id, IPC_RMID, 0))
{
printf("shmctl == -1\n");
return 0;
}
return 0;
}
追风筝的猪 2012-07-27
  • 打赏
  • 举报
回复
这个shmat函数
成功时,这个函数返回共享内存的起始地址
失败时返回-1

所以你的printf("%s\n",shmptr);
地址就是错的 该是*shmptr
追风筝的猪 2012-07-27
  • 打赏
  • 举报
回复
shm_id = 393226
This is first time, pid = 0
This is secONd time, pid = 0
This is the child Process.
123qweasd123
This is first time, pid = 24690
This is secONd time, pid = 24690
This is the parent Process.
123qweasd123
追风筝的猪 2012-07-27
  • 打赏
  • 举报
回复
[root@localhost test]# ./fork
shm_id = 393226
This is first time, pid = 0
This is secONd time, pid = 0
This is the child Process.
123qweasd123
This is third time, pid = 0
This is fouth time, pid = 0
shmdt == -1
shmdt error: Invalid argument
This is first time, pid = 24116
This is secONd time, pid = 24116
This is the parent Process.

This is third time, pid = 24116
This is fouth time, pid = 24116
shmdt == -1
shmdt error: Invalid argument
[root@localhost test]#

追风筝的猪 2012-07-27
  • 打赏
  • 举报
回复
试试 判断确定为父进程的时候 先加上个sleep(1);因为fork()父子进程的执行顺序是不确定的。
allenbein 2012-07-27
  • 打赏
  • 举报
回复
错了错了,进程的话应该用wait或者waitpid这两个函数来等子进程退出,pthread_join是用于线程通信的。
allenbein 2012-07-27
  • 打赏
  • 举报
回复
有可能子进程运行了一半,父进程已经退了,子进程也被迫退了。fork一个子进程是需要时间的。在父进程中加一个pthread_join函数来等子进程结束,然后父进程再往下运行。
你试一下
本PDF电子书包含上下两册,共1576页,带目录,高清非扫描版本。 作者: 毛德操 胡希明 丛书名: Linux内核源代码情景分析 出版社:浙江大学出版社 目录 第1章 预备知识 1.1 Linux内核简介. 1.2 Intel X86 CPU系列的寻址方式 1.3 i386的页式内存管理机制 1.4 Linux内核源代码中的C语言代码 1.5 Linux内核源代码中的汇编语言代码 第2章 存储管理 2.1 Linux内存管理的基本框架 2.2 地址映射的全过程 2.3 几个重要的数据结构和函数 2.4 越界访问 2.5 用户堆栈的扩展 2.6 物理页面的使用和周转 2.7 物理页面的分配 2.8 页面的定期换出 2.9 页面的换入 2.10 内核缓冲区的管理 2.11 外部设备存储空间的地址映射 2.12 系统调用brk() 2.13 系统调用mmap() 第3章 中断、异常和系统调用 3.1 X86 CPU对中断的硬件支持 3.2 中断向量表IDT的初始化 3.3 中断请求队列的初始化 3.4 中断的响应和服务 3.5 软中断与Bottom Half 3.6 页面异常的进入和返回 3.7 时钟中断 3.8 系统调用 3.9 系统调用号与跳转表 第4章 进程与进程调度 4.1 进程四要素 4.2 进程三部曲:创建、执行与消亡 4.3 系统调用fork()、vfork()与clone() 4.4 系统调用execve() 4.5 系统调用exit()与wait4() 4.6 进程的调度与切换 4.7 强制性调度 4.8 系统调用nanosleep()和pause() 4.9 内核中的互斥操作 第5章 文件系统 5.1 概述 5.2 从路径名到目标节点 5.3 访问权限与文件安全性 5.4 文件系统的安装和拆卸 5.5 文件的打开与关闭 5.6 文件的写与读 5.7 其他文件操作 5.8 特殊文件系统/proc 第6章 传统的Unix进程间通信 6.1 概述 6.2 管道和系统调用pipe() 6.3 命名管道 6.4 信号 6.5 系统调用ptrace()和进程跟踪 6.6 报文传递 6.7 共享内存 6.8 信号量 第7章基于socket的进程间通信 7.1系统调用socket() 7.2函数sys—socket()——创建插口 7.3函数sys—bind()——指定插口地址 7.4函数sys—listen()——设定server插口 7.5函数sys—accept()——接受连接请求 7.6函数sys—connect()——请求连接 7.7报文的接收与发送 7.8插口的关闭 7.9其他 第8章设备驱动 8.1概述 8.2系统调用mknod() 8.3可安装模块 8.4PCI总线 8.5块设备的驱动 8.6字符设备驱动概述 8.7终端设备与汉字信息处理 8.8控制台的驱动 8.9通用串行外部总线USB 8.10系统调用select()以及异步输入/输出 8.11设备文件系统devfs 第9章多处理器SMP系统结构 9.1概述 9.2SMP结构中的互斥问题 9.3高速缓存与内存的一致性 9.4SMP结构中的中断机制 9.5SMP结构中的进程调度 9.6SMP系统的引导 第10章系统引导和初始化 10.1系统引导过程概述 10.2系统初始化(第一阶段) 10.3系统初始化(第二阶段) 10.4系统初始化(第三阶段) 10.5系统的关闭和重引导

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧