当前位置: 技术问答>linux和unix
关于共享内存的的问题~
来源: 互联网 发布时间:2015-11-24
本文导语: 最近在弄一个程序,要使用共享内存,要求很简单,就是父进程写进信息,子进程读出来 大家给一个例子吧 比如说: struct student { int id; char name[`11]; }STU[MAX]; 父进程把这个结构体信息放入共享内存中,然后...
最近在弄一个程序,要使用共享内存,要求很简单,就是父进程写进信息,子进程读出来
大家给一个例子吧
比如说:
struct student
{
int id;
char name[`11];
}STU[MAX];
父进程把这个结构体信息放入共享内存中,然后子进程读出并修改,
大家给个例子~~~
大家给一个例子吧
比如说:
struct student
{
int id;
char name[`11];
}STU[MAX];
父进程把这个结构体信息放入共享内存中,然后子进程读出并修改,
大家给个例子~~~
|
#include
#include
#define ARRAY_SIZE 40000
#define MALLOC_SIZE 100000
#define SHM_SIZE 100000
#define SHM_MODE 0600 /* user read/write */
char array[ARRAY_SIZE]; /* uninitialized data = bss */
int
main(void)
{
int shmid;
char *ptr, *shmptr;
printf("array[] from %lx to %lxn", (unsigned long)&array[0],
(unsigned long)&array[ARRAY_SIZE]);
printf("stack around %lxn", (unsigned long)&shmid);
if ((ptr = malloc(MALLOC_SIZE)) == NULL)
printf("malloc errorn");
printf("malloced from %lx to %lxn", (unsigned long)ptr,
(unsigned long)ptr+MALLOC_SIZE);
if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE))
#include
#define ARRAY_SIZE 40000
#define MALLOC_SIZE 100000
#define SHM_SIZE 100000
#define SHM_MODE 0600 /* user read/write */
char array[ARRAY_SIZE]; /* uninitialized data = bss */
int
main(void)
{
int shmid;
char *ptr, *shmptr;
printf("array[] from %lx to %lxn", (unsigned long)&array[0],
(unsigned long)&array[ARRAY_SIZE]);
printf("stack around %lxn", (unsigned long)&shmid);
if ((ptr = malloc(MALLOC_SIZE)) == NULL)
printf("malloc errorn");
printf("malloced from %lx to %lxn", (unsigned long)ptr,
(unsigned long)ptr+MALLOC_SIZE);
if ((shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE))