fixed logic bug
[tridge/junkcode.git] / memdump.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <sys/mman.h>
5 #include <fcntl.h>
6
7 #define LEN 0x1000
8 #define ADDR 0x0
9 #define SIZE 8*1024
10
11 int main()
12 {
13         int fdpair[2];
14         int fd, i=0;
15         int mapfd, status;
16         char *map;
17         char buf[LEN];
18         char lastbuf[LEN];
19
20         fd = open("mem.dat", O_WRONLY|O_CREAT|O_TRUNC, 0600);
21         if (fd == -1) {
22                 perror("open");
23                 exit(1);
24         }
25
26         mapfd = open("/dev/zero", O_RDWR);
27         if (mapfd == -1) {
28                 perror("open");
29                 exit(1);
30         }
31
32         map = mmap(0, SIZE*LEN, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE,
33                    mapfd, 0);
34
35         while (i < SIZE && pipe(fdpair) == 0) {
36                 if (fork()) {
37                         memset(buf,'Z', LEN);
38                         if (read(fdpair[0], buf, LEN) != LEN) {
39                                 perror("read");
40                                 exit(1);
41                         }
42                         if (memcmp(lastbuf,buf,LEN)) {
43                                 if (write(fd,buf,LEN) != LEN) {
44                                         perror("write");
45                                         exit(1);
46                                 } else {
47                                         printf(".");
48                                         fflush(stdout);
49                                 }
50                         }
51                         memcpy(lastbuf, buf, LEN);
52                         waitpid(-1, &status, 0);
53                 } else {
54                         if (write(fdpair[1],ADDR,LEN) != LEN) {
55                                 perror("write");
56                                 exit(1);
57                         }
58                         _exit(0);
59                 }
60                 close(fdpair[0]);
61                 close(fdpair[1]);
62
63                 map[i*LEN] = i++;
64         }
65
66         return 0;
67 }