#include<stdio.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#define SIZE 1024*4
int main( int argc, char **argv )
{
char*addr;
// charbuf[SIZE];
intshmid;
if((shmid=shmget(IPC_PRIVATE, SIZE, 0020 | 0040) < 0))
{
puts( "getshared mem error!" );
exit( 0);
}
if( fork()!= 0 )
{
addr =shmat( shmid, 0, 0 );
memset(addr, '\0', SIZE );
// write( chmid, argv[1], SIZE );
strncpy(addr, argv[1], SIZE );
wait( NULL);
}
else
{
sleep( 1);
addr =shmat( shmid, 0, 0 );
// read( shmid,buf, SIZE );
printf("address %p is %s", addr, addr );
exit( 1);
}
exit( 1);
}
遇到的问题:
为什么用 write( chmid, argv[1], SIZE )写共享内存时,读出的输入数据后会有乱码?
是不是共享内存不能用write写啊?新手求教
|