#include
#include
#include
#include
#include
#include
#include
int main()
{
char buf[5]={'0'};
int fd=open("hello.c",O_RDWR|O_CREAT,0666);
if(fd<0)
{
printf("open is erroe\r\n");
return -1;
}
write(fd,buf,5);
pid_t pid = fork();//建立子进程
if(pid < 0)
{
printf("fork is error\r\n");
return -1;
}else if(pid ==0)//child
{
sleep(1);
char * p = mmap(NULL,5, PROT_READ| PROT_WRITE , MAP_SHARED,fd,0);
if(p == (char *)-1)
{
printf(" child mmap is error\r\n");
return -1;
}
printf("This is child \r\n");
puts(p);
//取消映射
munmap(p,5);
}else{
char * p = mmap(NULL,5,PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED,fd,0);
if(p == (char *)-1);
{
printf(" father mmap is error\r\n");
return -1;
}
mencpy(p,"hello",5);
}
return 0;
}