#include<stdio.h> #include<unistd.h> int main() { pid_t pid; int pfd[2]; //定義兩個文件描述符,放在數組中 int ret=pipe(pfd); //定義管道 if(ret==-1) { perror("pipe error:"); exit(1); } pid=fork(); if(pid==-1) { perror("fork error:"); exit(1); } else if(pid==0) { close(pfd[1]); //關閉子進程寫端口 char buf[1024]={0}; int ret=read(pfd[0],buf,sizeof(buf)); //從管道讀數據 write(STDOUT_FILENO,buf,ret); }else { close(pfd[0]); //關閉父進程讀端口 write(pfd[1],"hello world\n",sizeof("hello world")); //向管道寫數據 } return 0; }
//寫端程序 #include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<string.h> int main() { //當前目錄有一個myfifo文件 //打開fifo文件 int fd=open("myfifo",O_WRONLY); //寫 char buf[256]={0}; int num=1; while(1) { memset(buf,0x00,sizeof(buf)); sprintf(buf,"newdata%d",num++); write(fd,buf,sizeof(buf)); sleep(1); //循環寫 } //關閉描述符 close(fd); return 0; }
//讀端程序
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<unistd.h> #include<fcntl.h> int main() { int fd=open("myfifo",O_RDONLY); char buf[256]={0}; int ret; while(1) { ret=read(fd,buf,sizeof(buf)); if(ret>0) { printf("%s\n",buf); } } close(fd); return 0; }
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/mman.h> #include<string.h> int main() { int fd=open("mem.txt",O_RDWR); //建立映射 char *mem=mmap(NULL,8,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); //PRIVATE 不修改文件 //char *mem=mmap(NULL,8,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0); if(mem==MAP_FAILED) { perror("mmap error"); return 0; } //拷貝數據 strcpy(mem,"i am you"); //釋放mmap munmap(mem,8); close(fd); return 0; }
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/mman.h> #include<sys/wait.h> int main() { //建立映射區 int fd=open("mem.txt",O_RDWR); int *mem=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); if(mem==MAP_FAILED) { perror("mmap error:"); return 0; } //fork子進程 pid_t pid=fork(); //父進程和子進程交替修改數據 if(pid==0) { //子進程 *mem=100; printf("child,*mem=%d\n",*mem); sleep(3); printf("child,*mem=%d\n",*mem); } else if(pid>0) { //父進程 sleep(1); printf("parent,*mem=%d\n",*mem); *mem=1001; printf("parent,*mem=%d\n",*mem); wait(NULL); } munmap(mem,4); close(fd); return 0; }
經過MAP_ANON和MAP_ANONYMOUS
數組
int *mem=mmap(NULL,4,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANON,-1,0);
//讀端 #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/mman.h> typedef struct Student{ int sid; char sname[20]; }Student; int main(int argc,char *argv[]) {
if(argc!=2)
{
printf("./a.out filename");
return 0;
}
//open file int fd=open(argv[1],O_RDWR); //mmap int length=sizeof(Student); Student * stu=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); if(stu==MAP_FAILED) { printf("map err:"); return 0; } //read data while(1) { printf("sid = %d, sname = %s \n",stu->sid,stu->sname); sleep(1); } munmap(stu,length); close(fd); return 0; }
//寫端 #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/mman.h> typedef struct Student{ int sid; char sname[20]; }Student; int main(int argc,char* argv[]) { if(argc!=2) { printf("./a.out filename\n"); return 0; } //1.open file int fd=open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0666); int length=sizeof(Student); int num=1; ftruncate(fd,sizeof(length)); //2.mmap Student * stu=mmap(NULL,length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); if(stu==MAP_FAILED) { printf("mmap err:"); return 0; } //3.修改內存數據 while(1) { stu->sid=num; sprintf(stu->sname,"xiaoming-%03d",num++); sleep(1); } //4.釋放映射區 關閉文件 munmap(stu,length); close(fd); return 0; }