Date: Sat, 21 Dec 1996 17:04:23 -0500 (EST) From: "Marc G. Fournier" <scrappy@hub.org> To: hackers@freebsd.org Subject: Bug using MMAP'd region, or misunderstanding Message-ID: <Pine.BSF.3.95.961221165457.786B-100000@thelab.hub.org>
next in thread | raw e-mail | index | archive | help
Hi... I've finally gotten it to the point where I *think* I understand what I'm doing with mmap'd regions, but have now hit a bug that I don't understand. I've got a 'server' that is setting up a mmap region as follows: ----------- if((fd = open("/tmp/video", O_RDWR|O_CREAT, 0666)) < 0) { fprintf(stderr, "cannot open /tmp/video\n"); exit(-1); } vspace = sizeof(int) + ((sizeof(int) + FRAMESIZE) * FRAMES); ftruncate(fd, vspace); if((video = mmap(0, vspace, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) == (caddr_t) -1) { fprintf(stderr, "cannot mmap video buffer\n"); exit(-1); } close(fd); currframe = video; video += sizeof(int); for(ii = 0; ii < FRAMES; ii++) { framesize[ii] = video; video += sizeof(int); } for(ii = 0; ii < FRAMES; ii++) { frame[ii] = video; video += FRAMESIZE; } --------- Simple enough. The client end looks similar, with: --------- if((fd = open("/tmp/video", O_RDONLY)) < 0) { fprintf(stderr, "cannot open /tmp/video\n"); exit(-1); } fstat(fd, &statbuf); if((video = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (caddr_t) -1) { fprintf(stderr, "cannot mmap video buffer\n"); exit(-1); } close(fd); currframe = video; video += sizeof(int); for(ii = 0; ii < FRAMES; ii++) { framesize[ii] = video; video += sizeof(int); } for(ii = 0; ii < FRAMES; ii++) { frame[ii] = video; video += FRAMESIZE; } ------- And that's just about where the similarities end right now. currframe and frame[ii] are getting what I believe to be correct values, but framesize[ii] isn't... I'm writting to framesize[ii], in the server, as: ----- sprintf(framesize[ii], "%d", datasize); printf("wrote framesize of %d\n", atoi(framesize[ii])); ----- And the value of atoi(framesize[ii]) is as expected (around 1100bytes), but when the client reads that same location, with the same atoi(): ----- fsize = atoi(framesize[ii]); fprintf(stderr, "frame == %d, currpos == %d, size == %d\n", mmap_pos, currpos, fsize); ----- I'm getting results that look like: ----- frame == 58, currpos == 58, size == 28272847 ----- So, I figure that is something I'm missing(misunderstanding) about mmap, because I would have thought my 'atoi()' test in the server would yeild the same results as the atoi() test in the client, since they are reading the same memory buffer...no? Any comments on what I should be looking at/for? Is there a more correct way of doing the 'sprintf()' above that I should be using? Again, if it was totally incorrect, I would have assumed that the atoi() test in the server would fail too :( Still fighting with the problem, but any suggestions/ideas are most welcome... Thanks in advance...
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.95.961221165457.786B-100000>