From owner-freebsd-hackers Sat Dec 21 14:04:33 1996 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.4/8.8.4) id OAA03773 for hackers-outgoing; Sat, 21 Dec 1996 14:04:33 -0800 (PST) Received: from hub.org (root@hub.org [207.107.138.200]) by freefall.freebsd.org (8.8.4/8.8.4) with ESMTP id OAA03755 for ; Sat, 21 Dec 1996 14:04:29 -0800 (PST) Received: from thelab.hub.org (thelab.hub.org [207.107.138.221]) by hub.org (8.8.2/8.7.5) with SMTP id RAA24234 for ; Sat, 21 Dec 1996 17:04:35 -0500 (EST) Date: Sat, 21 Dec 1996 17:04:23 -0500 (EST) From: "Marc G. Fournier" To: hackers@freebsd.org Subject: Bug using MMAP'd region, or misunderstanding Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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...