我需要一个共享内存,它应该有2个字段 mh_data标题; 和 任何类型任何长度的可变数据字段
我使用了代码 - >
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
//#include <conio.h>
#define ERROR -1
#define SUCCESS 0
typedef char int8;
typedef unsigned int u_int32;
typedef unsigned short u_int16;
typedef int error_code;
typedef union {
u_int32 group_user; /* group/user numbers */
struct {
u_int16 grp; /* group number */
u_int16 usr; /* user number */
} grp_usr;
} owner_id;
typedef struct mh_com {
u_int16
m_sync, /* sync bytes */
m_sysrev; /* system revision check value */
u_int32
m_size; /* module size */
owner_id
m_owner; /* group/user id */
u_int32
m_name; /* offset to module name */
u_int16
m_access, /* access permissions */
m_tylan, /* type/lang */
m_attrev, /* attributes/revision */
m_edit; /* edition */
u_int32
m_needs, /* module hardware requirements flags (reserved) */
m_share, /* shared data offset */
m_symbol, /* symbol table offset */
m_exec, /* offset to execution entry point */
m_excpt, /* offset to exception entry point */
m_data, /* data storage requirement */
m_stack, /* stack size */
m_idata, /* offset to initialized data */
m_idref, /* offset to data reference lists */
m_init, /* initialization routine offset */
m_term; /* termination routine offset */
u_int32
m_dbias, /* data area bias */
m_cbias; /* code area bias */
u_int16
m_ident; /* ident code for ident program */
char
m_spare[8]; /* reserved bytes */
u_int16
m_parity; /* header parity */
int8 m_shm_obj[256];
} mh_com, *Mh_com;
typedef mh_com mh_data;
error_code _os_datmod(
const char *mod_name,
u_int32 sz,
u_int16 *attr_rev,
u_int16 *type_lang,
u_int32 perm,
void **mod_data,
mh_data **mod_head)
{
struct datmod
{
mh_data header;
int size;
/*union Data
{
char str[sz];
}data;*/
char data[sz];
//void* dataPtr;
};
int fd;
int len;
struct stat shm_mod_stat;
void *addr;
len = strlen(mod_name);
/* Open new POSIX shared memory object */
if ((fd = shm_open(mod_name, O_RDWR |O_CREAT, /* mode_t */ 0666)) == -1)
{
/* TODO: If mod_name path is invalid, then return EOS_PNNF */
return ERROR;
}
if(ftruncate(fd, sizeof(struct datmod))==-1)
{
perror("ftruncate");
shm_unlink(mod_name);
return ERROR;
}
/* do mmap */
addr = (struct datmod *)mmap(NULL, sizeof(struct datmod), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)
{
shm_unlink(mod_name);
return ERROR;
}
((struct datmod *)addr)->size = sz;
/* mmaped address */
*mod_data = &(((struct datmod *)addr)->data);
*mod_head = &(((struct datmod *)addr)->header);
//memset((((union Data*)(*mod_data))->str),0,sz);
memset(((*(char **)mod_data)),0,sz);
//(*mod_head)->m_shm_obj = (int8 *) malloc(len + 1);
memcpy(&((*mod_head)->m_shm_obj), (mod_name), len);
(*mod_head)->m_shm_obj[len] = '\0';
close(fd);
return SUCCESS;
}
int main()
{
void* data;
mh_data * header;
char str[] = "/Sandeep";
_os_datmod(str,50,0,0,0,&data, &header);
struct DATA
{
int i;
float j;
}*dat;
dat = (struct DATA *)(data);
dat->i = 5;
dat->j = 4.2;
//strcpy((char*)data , str);
int res = fork();
if(res==0)
{
_os_datmod(str,50,0,0,0,&data, &header);
dat = (struct DATA *)(data);
printf("i=%d,j=%f\n",dat->i,dat->j);
//printf("%s",(char *)data);
}
printf("hello world\n");
}
但它会打印出来 你好,世界 I = 0,J = 0.000000 你好世界
我还需要改变
struct datmod
{
mh_data header;
int size;
/*union Data
{
char str[sz];
}data;*/
char data[sz];
//void* dataPtr;
};
这样 数据字段可以扩展到任何长度和任何类型。 所以,请建议