在学习Linux进程间通信时,仿照书上的思路写了一个用共享内存通信的例子,其中父进程向分配的共享内存内发送信息,子进程接收信息,源码如下:
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2021/1/4
* 描述:共享内存demo
************************************************* */
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>
#include<unistd.h>
int main()
{
int pid,shmid;
char* read_add;
char* write_add;
printf("the address of write_add:%x\n",&write_add);
struct shmid_ds dsbuf;
// Allocate shared memory
if((shmid=shmget(IPC_PRIVATE,32,0))<0)
{
printf("Allocate shared memory error\n");
exit(1);
}
else
{
printf