天嵌 ARM开发社区

 找回密码
 注册
查看: 915|回复: 0

关于共享内存的问题

[复制链接]
溪孟 发表于 2013-11-12 16:16:30 | 显示全部楼层 |阅读模式
#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写啊?新手求教

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-9-28 11:25 , Processed in 1.042032 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表