linux共享内存通信问题
我想通过共享内存(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
主要内容就是,申请一段共享内存,子进程向这段内存中写数据,父进程读出来。
可是,我在子进程中写完了,在子进程中可以读,在父进程中却读不出来。
这个问题,已经困扰我一天了,求大神帮忙。
源代码如下:
#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
...全文
相关推荐
操作系统的共享内存实验代码
通过实验,我们可以知道如何在Linux环境下实现共享内存机制,并解决生产者——消费者问题。
使用共享内存及信号量实现进程间通信例子
本示例代码着重于使用共享内存和信号量来解决进程间的通信和同步问题,这是一种高效且灵活的方法,特别是在多处理器和多线程环境中。下面我们将详细探讨这些概念以及它们在Linux系统中的实现。
基于Linux的同步共享内存的研究与实现
通过这种方式,不仅提高了进程间通信的效率,还解决了消息堵塞的问题,提升了用户体验。#### 五、结论本文通过对共享内存与信号量结合使用的研究,提出了一种有效提高Linux环境下进程间通信效率的方法。
linux进程间通信编程之共享内存
"本文介绍了Linux系统下进程间通信的共享内存机制,包括共享内存的主要功能、关键函数以及一个简单的实例程序。共享内存允许不同进程访问同一块内存区域,实现高效的数据交换,但需要程序员自行处理同步问题。
linux讲解通过共享内存实现进程间的通信
然而,在使用过程中还需要注意同步问题,以确保数据的一致性和完整性。希望本文能帮助读者更好地理解和掌握Linux下的共享内存通信机制。


