文件进程间通讯
open函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{
int fd1;
int fd2;
pid_t pid;
char buf[1024];
char *str = "-------test for shared fd in parent child process------\n";
int len;
pid = fork();
if(pid < 0)
{
perror("fork error");
exit(1);
}
else if(pid == 0)
{
fd1 = open("test.txt", O_RDWR);
if(fd1 < 0)
{
perror("open error");
exit(1);
}
write(fd1, str, strlen(str));
printf("child wrote over...\n");
}
else
{
fd2 = open("test.txt", O_RDWR);
if(fd2 < 0)
{
perror("open error");
exit(1);
}
sleep(1);
len = read(fd2, buf, sizeof(buf));
write(STDOUT_FILENO, buf, len);
wait(NULL);
}
return 0;
}
存储映射I/O
mmap创建映射区
mmap函数 和munmap函数 ftruncate函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(void)
{
int len;
int ret;
char *p = NULL;
int fd;
fd = open("mmaptest.txt", O_CREAT|O_RDWR, 0644);
if(fd < 0)
{
perror("open error");
exit(1);
}
len = ftruncate(fd, 5);
if(len == -1)
{
perror("ftruncate error");
exit(1);
}
p = mmap(NULL, 5, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(p == MAP_FAILED)
{
perror("mmap error");
exit(1);
}
strcpy(p, "abc\n");
ret = munmap(p, 5);
if(ret == -1)
{
perror("munmap error");
exit(1);
}
close(fd);
return 0;
}
通过映射区的指针来把数据写入文件
mmap父子进程通讯
目录项的概念
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
int var = 100;
int main(void)
{
int *p;
pid_t pid;
int fd;
int ret;
fd = open("fork_mmap.txt", O_RDWR|O_CREAT|O_TRUNC, 0644);
if(fd < 0)
{
perror("open error");
exit(1);
}
unlink("fork_mmap.txt");
ftruncate(fd, 4);
p = (int *)mmap(NULL, 4, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(p == MAP_FAILED)
{
perror("mmap error");
exit(1);
}
close(fd);
pid = fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
else if(pid == 0)
{
*p = 2000;
var = 1000;
printf("child, *p = %d, var = %d\n", *p, var);
}
else
{
sleep(1);
printf("parent, *p = %d, var = %d\n", *p, var);
wait(NULL);
ret = munmap(p, 4);
if(ret == -1)
{
perror("munmap error");
exit(1);
}
}
return 0;
}
匿名映射
用宏创建匿名映射
这个宏MAP_ANONYMOUS 只能在linux下用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
int var = 100;
int main(void)
{
int *p;
pid_t pid;
int ret;
p = (int *)mmap(NULL, 4, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
if(p == MAP_FAILED)
{
perror("mmap error");
exit(1);
}
pid = fork();
if(pid == 0)
{
*p = 2000;
var = 1000;
printf("child, *p = %d, var = %d\n", *p, var);
}
else
{
sleep(1);
printf("parent, *p = %d, var = %d\n", *p, var);
wait(NULL);
ret = munmap(p, 4);
if(ret == -1)
{
perror("munmap error");
exit(1);
}
}
return 0;
}
用/dev/zero创建匿名映射
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
int var = 100;
int main(void)
{
int *p;
pid_t pid;
int fd;
int ret;
fd = open("/dev/zero", O_RDWR);
p = (int *)mmap(NULL, 4, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(p == MAP_FAILED)
{
perror("mmap error");
exit(1);
}
pid = fork();
if(pid == 0)
{
*p = 2000;
var = 1000;
printf("child, *p = %d, var = %d\n", *p, var);
}
else
{
sleep(1);
printf("parent, *p = %d, var = %d\n", *p, var);
wait(NULL);
ret = munmap(p, 4);
if(ret == -1)
{
perror("munmap error");
exit(1);
}
}
return 0;
}
mmap无血缘关系进程间通讯
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
struct STU
{
int id;
char name[20];
char sex;
};
void sys_err(char *str)
{
perror(str);
exit(1);
}
int main(int argc, char *argv[])
{
int fd;
struct STU student = {18, "beipi", 'm'};
char *mm;
if(argc < 2)
{
printf("./a.out file_shared\n");
exit(-1);
}
fd = open(argv[1], O_RDWR|O_CREAT, 0664);
ftruncate(fd, sizeof(student));
mm = mmap(NULL, sizeof(student), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(mm == MAP_FAILED)
{
sys_err("mmap");
}
close(fd);
while(1)
{
memcpy(mm, &student, sizeof(student));
student.id++;
sleep(1);
}
munmap(mm, sizeof(student));
}
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
struct STU
{
int id;
char name[20];
char sex;
};
void sys_err(char *str)
{
perror(str);
exit(-1);
}
int main(int argc, char *argv[])
{
int fd;
struct STU student;
struct STU *mm;
if(argc < 2)
{
printf("./a.out file_shared\n");
exit(-1);
}
fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
sys_err("open error");
}
mm = mmap(NULL, sizeof(student), PROT_READ, MAP_SHARED, fd, 0);
if(mm == MAP_FAILED)
{
sys_err("mmap error");
}
close(fd);
while(1)
{
printf("id = %d\tname = %s\tsex = %c\n", mm->id, mm->name, mm->sex);
sleep(2);
}
munmap(mm, sizeof(student));
return 0;
}
文件无血缘关系进程间通讯
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#define N 5
int main(void)
{
char buf[1024];
char *str = "------------secesuss------------\n";
int ret;
int fd;
fd = open("file_test.txt", O_RDWR|O_TRUNC|O_CREAT, 0664);
write(fd, str, strlen(str));
printf("file_w write innto file_test.txt finish\n");
sleep(N);
lseek(fd, 0, SEEK_SET);
ret = read(fd, buf, sizeof(buf));
ret = write(STDOUT_FILENO, buf,ret);
if(ret == -1)
{
perror("write second error");
exit(1);
}
close(fd);
return 0;
}
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf[1024];
char *str = "--------------------file_r write secesuss-----------\n";
int ret;
int fd;
sleep(2);
fd = open("file_test.txt", O_RDWR);
ret = read(fd, buf, sizeof(buf));
write(STDOUT_FILENO, buf, ret);
write(fd, str, strlen(str));
printf("file_r read/write finish\n");
close(fd);
return 0;
}
|