int main(int argc, char **argv) { int fd1, fd2, *ptr1, *ptr2; pid_t childpid; struct stat stat; if (argc != 2) err_quit("Usage: test2 <name>"); Shm_unlink(Px_ipc_name(argv[1])); fd1 = Shm_open(Px_ipc_name(argv[1]), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); Ftruncate(fd1, sizeof(int)); fd2 = Open("./shm", O_RDONLY); Fstat(fd2, &stat); if ((childpid = Fork()) == 0) { ptr2 = Mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd2, 0); ptr1 = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0); printf("child: shm ptr = %p, motd ptr = %p\n", ptr1, ptr2); sleep(5); printf("shared memory integer = %d\n", *ptr1); exit(0); } //parent: map in reverse order from child ptr1 = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0); ptr2 = Mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd2, 0); *ptr1 = 777; Waitpid(childpid, NULL, 0); exit(0); }
/* *@description:返回一段共享内存空间 */ long *meter(int nchildren) { int fd; long *ptr; #ifdef MAP_ANON ptr = Mmap(0,nchildren*sizeof(long),PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,-1,0); #else fd = Open("/dev/zero",O_RDWR,0); ptr = Mmap(0,nchildren*sizeof(long),PROT_READ | PROT_WRITE, MAP_SHARED,fd,0); #endif return (ptr); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); if(ishttps) { SSL_write(ssl, buf, strlen(buf)); SSL_write(ssl, srcp, filesize); } else { Rio_writen(fd, buf, strlen(buf)); Rio_writen(fd, srcp, filesize); } Munmap(srcp, filesize); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize, int is_head) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); // line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); // line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); // char c; // c = getchar(); // printf("%c\n", c);/*to ignore the EPIPE error!!! */ sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // line:netp:servestatic:endserve if (is_head) { return; // the request is head } /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); // line:netp:servestatic:open srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); // Close(srcfd); // Rio_writen(fd, srcp, filesize); // Munmap(srcp, filesize); // }
int main(int argc, char *argv[]) { int fd, i, nloop, zero = 0; int *ptr; sem_t *mutex; if(argc != 3) err_quit("usage: incr2 <pathname> <#loops>"); fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE);/*打开文件用于读写,不存在则创建它*/ Write(fd, &zero, sizeof(int));/*写一个值为0的值保存到文件*/ /*调用mmap把刚打开的文件映射到本进程的内存空间中*/ ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); mutex = Sem_open(SEM_NAME, O_CREAT | O_EXCL, FILE_MODE, 1); Sem_unlink(SEM_NAME); setbuf(stdout, NULL);/*把标准输出设置为非缓冲区*/ if(Fork() == 0){ for(i = 0; i < nloop; ++i){ Sem_wait(mutex); printf ("child:%d\n",(*ptr)++); Sem_post(mutex); } exit(0); } for(i = 0; i < nloop; ++i){ Sem_wait(mutex); printf ("parent:%d\n",(*ptr)++); Sem_post(mutex); } return 0; }
void encode_mmap() { int fd; int mmaplen; // handle share memory if(shm_unlink(Px_ipc_name(MMAP_SHM_NAME)) == -1) { CAP_DBG("shm_unlink error: %s", strerror(errno)); } mmaplen = sizeof(mmap_ring_t) + MMAP_NODE_NUM * sizeof(mmap_node_t); fd = Shm_open(Px_ipc_name(MMAP_SHM_NAME), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); CAP_DBG("shm_open create successfully\n"); Ftruncate(fd, mmaplen); h264_buf = Mmap(NULL, mmaplen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // initialize mmap memory after create memset(h264_buf, 0, mmaplen); x264_ring = (mmap_ring_t *)h264_buf; shm_init_mutex(); shm_init_cond(); x264_node = (mmap_node_t *)(h264_buf + sizeof(mmap_ring_t)); Close(fd); }
int main(int argc, char *argv[]) { int fd, i, nloop; pid_t pid; struct shmstruct *ptr; if(argc != 4) err_quit("usage: client1 <shmname> <semname> <#loop>"); nloop = atoi(argv[3]); fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR, FILE_MODE); ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); mutex = Sem_open(Px_ipc_name(argv[2]), 0); pid = getpid(); for(i = 0; i < nloop; ++i) { Sem_wait(mutex); printf("pid %ld:%d\n", (long)pid, ptr->count++); Sem_post(mutex); } return 0; }
int main(int argc, char **argv) { int fd, i; char *ptr; size_t filesize, mmapsize, pagesize; if (argc != 4) err_quit("usage: test1 <pathname> <filesize> <mmapsize>"); filesize = atoi(argv[2]); mmapsize = atoi(argv[3]); /* 4open file: create or truncate; set file size */ fd = Open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE); Lseek(fd, filesize-1, SEEK_SET); Write(fd, "", 1); ptr = Mmap(NULL, mmapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); pagesize = Sysconf(_SC_PAGESIZE); printf("PAGESIZE = %ld\n", (long) pagesize); for (i = 0; i < max(filesize, mmapsize); i += pagesize) { printf("ptr[%d] = %d\n", i, ptr[i]); ptr[i] = 1; printf("ptr[%d] = %d\n", i + pagesize - 1, ptr[i + pagesize - 1]); ptr[i + pagesize - 1] = 1; } printf("ptr[%d] = %d\n", i, ptr[i]); exit(0); }
int main(int argc, char *argv[]) { int fd, i, nloop; struct shared *ptr; if(argc != 3) err_quit("usage: incr3 <pathname> <#loop>"); nloop = atoi(argv[2]); /*映射到内存*/ fd = Open(argv[1], O_RDWR | O_CREAT, FILE_MODE); Write(fd, &shared, sizeof(struct shared)); ptr = Mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); Sem_init(&ptr->mutex, 1, 1);/*初始化信号量*/ setbuf(stdout, NULL); if(Fork() == 0){ for(i = 0; i < nloop; ++i){ Sem_wait(&ptr->mutex); printf("child:%d\n", ptr->count++); Sem_post(&ptr->mutex); } exit(0); } for(i = 0; i < nloop; ++i){ Sem_wait(&ptr->mutex); printf("parent: %d\n", ptr->count++); Sem_post(&ptr->mutex); } return 0; }
void requestServeStatic(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); // Rather than call read() to read the file into memory, // which would require that we allocate a buffer, we memory-map the file srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); // put together response sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: CS537 Web Server\r\n", buf); sprintf(buf, "%sContent-Length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // Writes out to the client socket the memory-mapped file Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
/* * serve_static - serve static content */ void serve_static(int connfd, char* filename, struct stat filestat) { int srcfd; char *srcp; char filetype[MAXLINE], body[MAXBUF]; /* get file type */ if (strstr(filename, ".html")) strcpy(filetype, "text/html"); else if (strstr(filename, ".gif")) strcpy(filetype, "text/gif"); else if (strstr(filename, ".jpg")) strcpy(filetype, "text/jpg"); else strcpy(filetype, "text/plain"); /* send response headers to client */ sprintf(body, "HTTP/1.0 200 OK\r\n"); sprintf(body, "%sServer: Tiny Web Server\r\n", body); sprintf(body, "%sContent-length: %d\r\n", body, (int)filestat.st_size); sprintf(body, "%sContent-type: %s\r\n\r\n", body, filetype); Rio_writen(connfd, body, strlen(body)); /* send response body to client */ srcfd = Open(filename, O_RDONLY, 0); /* map the file into a chunk of virtual memory */ srcp = Mmap(NULL, filestat.st_size, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(connfd, srcp, filestat.st_size); Munmap(srcp, filestat.st_size); }
int main(int argc, char **argv) { int c, fd, flags; char *ptr; off_t length; flags = O_RDWR | O_CREAT; while ( (c = getopt(argc, argv, "e")) != -1) { switch (c) { case 'e': flags |= O_EXCL; break; } } if (optind != argc - 2) err_quit("usage: shmcreate [ -e ] <name> <length>"); length = atoi(argv[optind + 1]); fd = Shm_open(argv[optind], flags, FILE_MODE); Ftruncate(fd, length); ptr = Mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); exit(0); }
int main() { int fd; void* ptr = NULL; const size_t memLen = sizeof(SharedBuffer) + SHARED_BUFSIZ; //!! 注意flag和producer不同 fd = Shm_open( SHARED_NAME, O_RDWR, FILE_MODE); ptr = Mmap(NULL, memLen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Ftruncate(fd, memLen); Close(fd); pBaseAddr = (char*)ptr + sizeof(SharedBuffer); SharedBuffer *pSharedBuf = (SharedBuffer*)ptr; unsigned char ch; while( true ) { pSharedBuf->Read( &ch, 1 ); DBG("Consumed %02u", ch); // sleep(5); } // while return 0; }
MemMappedFile::MemMappedFile(cf_const std::string & name, size_t size, cf_int prot, bool lock,bool autoUnmap) :_pShm(NULL), _size(size), _isLocked(lock), _autoUnmap(autoUnmap) { if (size<=0) _THROW(ValueError, "size !"); cf_int fd =cf_open(name.c_str(), ipcdefs::FLAG_CREATE, ipcdefs::MODE_DEFAULT); if(0!=fd) _THROW(SyscallExecuteError, "Failed to execute cf_open !"); struct stat st_buf = {0}; if(0!=cf_fstat(fd,&st_buf)) _THROW(SyscallExecuteError, "Failed to execute cf_fstat !"); if (st_buf.st_size < _size) { if(0!=cf_ftruncate(fd,size)) _THROW(SyscallExecuteError, "Failed to execute cf_ftruncate !"); } if (size == 0) _size = st_buf.st_size; _pShm = Mmap(fd, prot); if (lock && 0!=cf_mlock(_pShm, _size)) _THROW(SyscallExecuteError, "Failed to execute cf_mlock !"); }
void requestServeStatic(request_t request, char *filename, int filesize, thread_t * thread) { int fd = request.connfd; int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; char tmp = 0; int i; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); double time_start_read = get_time(); // Rather than call read() to read the file into memory, // which would require that we allocate a buffer, we memory-map the file srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); // The following code is only needed to help you time the "read" given // that the file is memory-mapped. // This code ensures that the memory-mapped file is brought into memory // from disk. // When you time this, you will see that the first time a client //requests a file, the read is much slower than subsequent requests. for (i = 0; i < filesize; i++) { tmp += *(srcp + i); } double time_end_read = get_time(); request.Stat_req_read = time_end_read - time_start_read; double time_start_write = get_time(); request.Stat_req_complete = time_start_write - request.Stat_req_arrival; sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%s Server: CS537 Web Server\r\n", buf); // CS537: Your statistics go here -- fill in the 0's with something useful! sprintf(buf, "%s Stat-req-arrival: %d\r\n", buf, request.Stat_req_arrival); sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, request.Stat_req_dispatch); sprintf(buf, "%s Stat-req-read: %d\r\n", buf, request.Stat_req_read); sprintf(buf, "%s Stat-req-complete: %d\r\n", buf, request.Stat_req_complete); sprintf(buf, "%s Stat-req-age: %d\r\n", buf, request.Stat_req_age); sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, thread->Stat_thread_id); sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, thread->Stat_thread_count); sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, thread->Stat_thread_static); sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, thread->Stat_thread_dynamic); sprintf(buf, "%s Content-Length: %d\r\n", buf, filesize); sprintf(buf, "%s Content-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // Writes out to the client socket the memory-mapped file Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
void IoWrite::writeFile(const std::string&fileName,int filesSize) { int srcfd; char *srcp; srcfd = Open(const_cast<char*>(fileName.c_str()),O_RDONLY,0); srcp= reinterpret_cast<char *>(Mmap(0,filesSize,PROT_READ,MAP_PRIVATE,srcfd,0)); Close(srcfd); Rio_write(fileDescriptor,srcp,filesSize); Munmap(srcp,filesSize); }
// Initialize the windowed map, using the specified protection and size int initialize_mmap_window(mmap_window *mw,int fd,int prot,size_t len){ mw->mapbase = Mmap(NULL,len,prot,mmap_flags(fd),fd,0); if(mw->mapbase == MAP_FAILED){ track_failloc(); return -1; } track_allocation("mmap_window"); // FIXME take as argument? mw->maplen = len; mw->mapoff = 0; return 0; }
/* * mmapcopy - uses mmap to copy * file fd to stdout */ void mmapcopy(int fd, int size) { /* Ptr to memory mapped VM area */ char *bufp; bufp = Mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); Write(1, bufp, size); return; }
int main(int argc, char **argv) { int fd, index, lastnoverflow, temp; long offset; struct shmstruct *ptr; if (argc != 2) err_quit("usage: server2 <name>"); /* 4create shm, set its size, map it, close descriptor */ shm_unlink(Px_ipc_name(argv[1])); /* OK if this fails */ fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Ftruncate(fd, sizeof(struct shmstruct)); Close(fd); /* 4initialize the array of offsets */ for (index = 0; index < NMESG; index++) ptr->msgoff[index] = index * MESGSIZE; /* 4initialize the semaphores in shared memory */ Sem_init(&ptr->mutex, 1, 1); Sem_init(&ptr->nempty, 1, NMESG); Sem_init(&ptr->nstored, 1, 0); Sem_init(&ptr->noverflowmutex, 1, 1); /* 4this program is the consumer */ index = 0; lastnoverflow = 0; for ( ; ; ) { Sem_wait(&ptr->nstored); Sem_wait(&ptr->mutex); offset = ptr->msgoff[index]; printf("index = %d: %s\n", index, &ptr->msgdata[offset]); if (++index >= NMESG) index = 0; /* circular buffer */ Sem_post(&ptr->mutex); Sem_post(&ptr->nempty); Sem_wait(&ptr->noverflowmutex); temp = ptr->noverflow; /* don't printf while mutex held */ Sem_post(&ptr->noverflowmutex); if (temp != lastnoverflow) { printf("noverflow = %d\n", temp); lastnoverflow = temp; } } exit(0); }
void misc() { char *bufp; int size=0; /* $begin execve */ Execve("a.out", NULL, NULL); /* $end execve */ /* $begin mmap */ bufp = Mmap(-1, size, PROT_READ, MAP_PRIVATE|MAP_ANON, 0, 0); /* $end mmap */ }
PosixShM::PosixShM(cf_cpstr name, size_t size, cf_int oflag, cf_int prot , mode_t mode, bool autoUnlink,bool autoClose): _name(name), _size(size), _autoUnlink(autoUnlink), _autoClose(autoClose) { if (size<=0) _THROW(ValueError, "size !") if( -1==(_fd=cf_shm_open(_name.c_str(), oflag, mode)) ) _THROW(SyscallExecuteError, "Failed to execute cf_shm_open !"); Ftruncate(_size); _pShm =Mmap(_fd, prot); }
int main(int argc, char *argv[]) { int fd,i,nloop,zero = 0; int *ptr; sem_t *mutex; if (argc != 3) { err_quit("usage: incr2 <pathname> <#loops> \n"); return -1; } nloop = atoi(argv[2]); fd = Open(argv[1],O_RDWR | O_CREAT,00666); Write(fd, &zero,sizeof(int)); ptr = (int *)Mmap( NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 ); close(fd); mutex =Sem_open(SEM_NAME,O_CREAT | O_EXCL,00666,1); Sem_unlink(SEM_NAME); setbuf(stdout,NULL); if (Fork() == 0) { for (i=0; i<nloop; i++) { Sem_wait(mutex); printf("child: %d \n",(*ptr)++); Sem_post(mutex); } } for (i=0; i<nloop; i++) { Sem_wait(mutex); printf("parent: %d \n",(*ptr)++); Sem_post(mutex); } return 0; }
int main(int argc, char **argv) { int fd, i; char *ptr; //open create or truncate, then mmap file fd = Open(FILE, O_RDWR | O_CREAT | O_TRUNC, FILE_MODE); ptr = Mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); for (i = 4096; i <= SIZE; i += 4096) { printf("setting file size to %d\n", i); Ftruncate(fd, i); printf("ptr[%d] = %d\n", i - 1, ptr[i-1]); } exit(0); }
void my_lock_init(char *pathname) { int fd; pthread_mutexattr_t mattr; fd = Open("/dev/zero", O_RDWR, 0); mptr = Mmap(0, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); Pthread_mutexattr_init(&mattr); Pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); Pthread_mutex_init(mptr, &mattr); }
int main(int argc, char **argv) { int fd, i, nloop; int *ptr; sem_t *mutex; if (argc != 2) { err_quit("usage: incr_dev_zero <#loops>"); } nloop = atoi(argv[1]); /* open /dev/zero, map into memory */ fd = Open("/dev/zero", O_RDWR); ptr = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); /* create, initialize, and unlink semaphore */ mutex = Sem_open(SEM_NAME, O_CREAT | O_EXCL, FILE_MODE, 1); Sem_unlink(SEM_NAME); // stdout is unbuffered setbuf(stdout, NULL); if (Fork() == 0) { // child process for (i = 0;i < nloop; ++i) { Sem_wait(mutex); printf("child:%d\n", (*ptr)++); Sem_post(mutex); sleep(1); } exit(0); } for (i = 0;i < nloop; ++i) { Sem_wait(mutex); printf("parent:%d\n", (*ptr)++); Sem_post(mutex); sleep(1); } exit(0); }
void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; get_filetype(filename, filetype); sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
int main(int argc, char **argv) { int fd, i, nloop, nusec; pid_t pid; char mesg[MESGSIZE]; long offset; struct shmstruct *ptr; if (argc != 4) err_quit("usage: client2 <name> <#loops> <#usec>"); nloop = atoi(argv[2]); nusec = atoi(argv[3]); /* 4open and map shared memory that server must create */ fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR, FILE_MODE); ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); Close(fd); pid = getpid(); for (i = 0; i < nloop; i++) { Sleep_us(nusec); snprintf(mesg, MESGSIZE, "pid %ld: message %d", (long) pid, i); if (sem_trywait(&ptr->nempty) == -1) { if (errno == EAGAIN) { Sem_wait(&ptr->noverflowmutex); ptr->noverflow++; Sem_post(&ptr->noverflowmutex); continue; } else err_sys("sem_trywait error"); } Sem_wait(&ptr->mutex); offset = ptr->msgoff[ptr->nput]; if (++(ptr->nput) >= NMESG) ptr->nput = 0; /* circular buffer */ Sem_post(&ptr->mutex); strcpy(&ptr->msgdata[offset], mesg); Sem_post(&ptr->nstored); } exit(0); }
int main(int argc,char **argv) { int i,fd; unsigned char c,*ptr; struct stat stat; if(argc!=2) err_quit("usgae: shmread <name>"); fd=Shm_open(argv[1],O_RDONLY,FILE_MODE); Fstat(fd,&stat); ptr=Mmap(NULL,stat.st_size,PROT_READ,MAP_SHARED,fd,0); Close(fd); for(i=0;i<stat.st_size;i++) if((c=*ptr++)!=(i%256)) err_ret("ptr[%d] =%d",i,c); exit(0); }
// Move the map back to the beginning of the underlying object (slide it left) int reset_mmap_window(mmap_window *mw,int fd,int prot){ void *tmp; if(mw->maplen <= 0){ bitch("Invalid arguments (%zu)\n",mw->maplen); return -1; } if(fd >= 0){ tmp = Mmap(mw->mapbase,mw->maplen,prot,mmap_flags(fd) | MAP_FIXED,fd,0); if(tmp != mw->mapbase){ bitch("Invalid MAP_FIXED result (%p, %p)\n",tmp,mw->mapbase); if(tmp != MAP_FAILED){ // FIXME verify it doesn't overlap! Munmap(tmp,mw->maplen); } return -1; } } mw->mapoff = 0; return 0; }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); //line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); //line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); //line:netp:servestatic:endserve /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); //line:netp:servestatic:open srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);//line:netp:servestatic:mmap Close(srcfd); //line:netp:servestatic:close Rio_writen(fd, srcp, filesize); //line:netp:servestatic:write Munmap(srcp, filesize); //line:netp:servestatic:munmap }