Regarding putting the structure in the shared memory, the value of the string in the structure "disappears out of thin air"

Record the small client / server jobs with shared memory shm and multi-process fork under Linux

I put such a structure in shared memory

struct buffer_area{
	char ID[MAXLINE];
	char MSG[MAXLINE];
};

among them

#define MAXLINE 8192

This is how shared memory is created

shmid=shmget((key_t)MEM_KEY,sizeof(struct buffer_area*),IPC_CREAT|0666);

Then there is a problem, that is, after my MSG is assigned in one process, the value in another process is inexplicably gone. I have been distressed for a long time, and then there is a flash of light. Like me, I think it may be that the array in the structure is too large , and the shared memory is not enough. Then I changed MAXLINE to 1024 ,,,, and it was solved like this, I vomited

Published 19 original articles · praised 0 · visits 416

Guess you like

Origin blog.csdn.net/weixin_42986599/article/details/105319521