|
|
1.1 ! root 1: .\" Copyright (c) 1983 Regents of the University of California. ! 2: .\" All rights reserved. The Berkeley software License Agreement ! 3: .\" specifies the terms and conditions for redistribution. ! 4: .\" ! 5: .\" @(#)1.2.t 6.10 (Berkeley) 8/25/86 ! 6: .\" ! 7: .sh "Memory management\(dg ! 8: .NH 3 ! 9: Text, data and stack ! 10: .PP ! 11: .FS ! 12: \(dg This section represents the interface planned for later ! 13: releases of the system. Of the calls described in this section, ! 14: only \fIsbrk\fP and \fIgetpagesize\fP are included in 4.3BSD. ! 15: .FE ! 16: Each process begins execution with three logical areas of memory ! 17: called text, data and stack. ! 18: The text area is read-only and shared, while the data and stack ! 19: areas are private to the process. Both the data and stack areas may ! 20: be extended and contracted on program request. The call ! 21: .DS ! 22: addr = sbrk(incr); ! 23: result caddr_t addr; int incr; ! 24: .DE ! 25: changes the size of the data area by \fIincr\fP bytes and ! 26: returns the new end of the data area, while ! 27: .DS ! 28: addr = sstk(incr); ! 29: result caddr_t addr; int incr; ! 30: .DE ! 31: changes the size of the stack area. ! 32: The stack area is also automatically extended as needed. ! 33: On the VAX the text and data areas are adjacent in the P0 region, ! 34: while the stack section is in the P1 region, and grows downward. ! 35: .NH 3 ! 36: Mapping pages ! 37: .PP ! 38: The system supports sharing of data between processes ! 39: by allowing pages to be mapped into memory. These mapped ! 40: pages may be \fIshared\fP with other processes or \fIprivate\fP ! 41: to the process. ! 42: Protection and sharing options are defined in \fI<sys/mman.h>\fP as: ! 43: .DS ! 44: .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u ! 45: /* protections are chosen from these bits, or-ed together */ ! 46: #define PROT_READ 0x04 /* pages can be read */ ! 47: #define PROT_WRITE 0x02 /* pages can be written */ ! 48: #define PROT_EXEC 0x01 /* pages can be executed */ ! 49: .DE ! 50: .DS ! 51: .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u ! 52: /* flags contain mapping type, sharing type and options */ ! 53: /* mapping type; choose one */ ! 54: #define MAP_FILE 0x0001 /* mapped from a file or device */ ! 55: #define MAP_ANON 0x0002 /* allocated from memory, swap space */ ! 56: #define MAP_TYPE 0x000f /* mask for type field */ ! 57: .DE ! 58: .DS ! 59: .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u ! 60: /* sharing types; choose one */ ! 61: #define MAP_SHARED 0x0010 /* share changes */ ! 62: #define MAP_PRIVATE 0x0000 /* changes are private */ ! 63: .DE ! 64: .DS ! 65: .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u ! 66: /* other flags */ ! 67: #define MAP_FIXED 0x0020 /* map addr must be exactly as requested */ ! 68: #define MAP_INHERIT 0x0040 /* region is retained after exec */ ! 69: #define MAP_HASSEMAPHORE 0x0080 /* region may contain semaphores */ ! 70: #define MAP_NOPREALLOC 0x0100 /* do not preallocate space */ ! 71: .DE ! 72: The cpu-dependent size of a page is returned by the ! 73: \fIgetpagesize\fP system call: ! 74: .DS ! 75: pagesize = getpagesize(); ! 76: result int pagesize; ! 77: .DE ! 78: .LP ! 79: The call: ! 80: .DS ! 81: maddr = mmap(addr, len, prot, flags, fd, pos); ! 82: result caddr_t maddr; caddr_t addr; int *len, prot, flags, fd; off_t pos; ! 83: .DE ! 84: causes the pages starting at \fIaddr\fP and continuing ! 85: for at most \fIlen\fP bytes to be mapped from the object represented by ! 86: descriptor \fIfd\fP, starting at byte offset \fIpos\fP. ! 87: The starting address of the region is returned; ! 88: for the convenience of the system, ! 89: it may differ from that supplied ! 90: unless the MAP_FIXED flag is given, ! 91: in which case the exact address will be used or the call will fail. ! 92: The actual amount mapped is returned in \fIlen\fP. ! 93: The \fIaddr\fP, \fIlen\fP, and \fIpos\fP parameters ! 94: must all be multiples of the pagesize. ! 95: A successful \fImmap\fP will delete any previous mapping ! 96: in the allocated address range. ! 97: The parameter \fIprot\fP specifies the accessibility ! 98: of the mapped pages. ! 99: The parameter \fIflags\fP specifies ! 100: the type of object to be mapped, ! 101: mapping options, and ! 102: whether modifications made to ! 103: this mapped copy of the page ! 104: are to be kept \fIprivate\fP, or are to be \fIshared\fP with ! 105: other references. ! 106: Possible types include MAP_FILE, ! 107: mapping a regular file or character-special device memory, ! 108: and MAP_ANON, which maps memory not associated with any specific file. ! 109: The file descriptor used for creating MAP_ANON regions is used only ! 110: for naming, and may be given as \-1 if no name ! 111: is associated with the region.\(dd ! 112: .FS ! 113: \(dd The current design does not allow a process ! 114: to specify the location of swap space. ! 115: In the future we may define an additional mapping type, MAP_SWAP, ! 116: in which the file descriptor argument specifies a file ! 117: or device to which swapping should be done. ! 118: .FE ! 119: The MAP_INHERIT flag allows a region to be inherited after an \fIexec\fP. ! 120: The MAP_HASSEMAPHORE flag allows special handling for ! 121: regions that may contain semaphores. ! 122: The MAP_NOPREALLOC flag allows processes to allocate regions whose ! 123: virtual address space, if fully allocated, ! 124: would exceed the available memory plus swap resources. ! 125: Such regions may get a SIGSEGV signal if they page fault and resources ! 126: are not available to service their request; ! 127: typically they would free up some resources via \fIunmap\fP so that ! 128: when they return from the signal the page ! 129: fault could be successfully completed. ! 130: .PP ! 131: A facility is provided to synchronize a mapped region with the file ! 132: it maps; the call ! 133: .DS ! 134: msync(addr, len); ! 135: caddr_t addr; int len; ! 136: .DE ! 137: writes any modified pages back to the filesystem and updates ! 138: the file modification time. ! 139: If \fIlen\fP is 0, all modified pages within the region containing \fIaddr\fP ! 140: will be flushed; ! 141: if \fIlen\fP is non-zero, only the pages containing \fIaddr\fP and \fIlen\fP ! 142: succeeding locations will be examined. ! 143: Any required synchronization of memory caches ! 144: will also take place at this time. ! 145: Filesystem operations on a file that is mapped for shared modifications ! 146: are unpredictable except after an \fImsync\fP. ! 147: .PP ! 148: A mapping can be removed by the call ! 149: .DS ! 150: munmap(addr, len); ! 151: caddr_t addr; int len; ! 152: .DE ! 153: This call deletes the mappings for the specified address range, ! 154: and causes further references to addresses within the range ! 155: to generate invalid memory references. ! 156: .NH 3 ! 157: Page protection control ! 158: .PP ! 159: A process can control the protection of pages using the call ! 160: .DS ! 161: mprotect(addr, len, prot); ! 162: caddr_t addr; int len, prot; ! 163: .DE ! 164: This call changes the specified pages to have protection \fIprot\fP\|. ! 165: Not all implementations will guarantee protection on a page basis; ! 166: the granularity of protection changes may be as large as an entire region. ! 167: .NH 3 ! 168: Giving and getting advice ! 169: .PP ! 170: A process that has knowledge of its memory behavior may ! 171: use the \fImadvise\fP call: ! 172: .DS ! 173: madvise(addr, len, behav); ! 174: caddr_t addr; int len, behav; ! 175: .DE ! 176: \fIBehav\fP describes expected behavior, as given ! 177: in \fI<sys/mman.h>\fP: ! 178: .DS ! 179: .ta \w'#define\ \ 'u +\w'MADV_SEQUENTIAL\ \ 'u +\w'00\ \ \ \ 'u ! 180: #define MADV_NORMAL 0 /* no further special treatment */ ! 181: #define MADV_RANDOM 1 /* expect random page references */ ! 182: #define MADV_SEQUENTIAL 2 /* expect sequential references */ ! 183: #define MADV_WILLNEED 3 /* will need these pages */ ! 184: #define MADV_DONTNEED 4 /* don't need these pages */ ! 185: #define MADV_SPACEAVAIL 5 /* insure that resources are reserved */ ! 186: .DE ! 187: Finally, a process may obtain information about whether pages are ! 188: core resident by using the call ! 189: .DS ! 190: mincore(addr, len, vec) ! 191: caddr_t addr; int len; result char *vec; ! 192: .DE ! 193: Here the current core residency of the pages is returned ! 194: in the character array \fIvec\fP, with a value of 1 meaning ! 195: that the page is in-core. ! 196: .NH 3 ! 197: Synchronization primitives ! 198: .PP ! 199: Primitives are provided for synchronization using semaphores in shared memory. ! 200: Semaphores must lie within a MAP_SHARED region with at least modes ! 201: PROT_READ and PROT_WRITE. ! 202: The MAP_HASSEMAPHORE flag must have been specified when the region was created. ! 203: To acquire a lock a process calls: ! 204: .DS ! 205: value = mset(sem, wait) ! 206: result int value; semaphore *sem; int wait; ! 207: .DE ! 208: \fIMset\fP indivisibly tests and sets the semaphore \fIsem\fP. ! 209: If the the previous value is zero, the process has acquired the lock ! 210: and \fImset\fP returns true immediately. ! 211: Otherwise, if the \fIwait\fP flag is zero, ! 212: failure is returned. ! 213: If \fIwait\fP is true and the previous value is non-zero, ! 214: \fImset\fP relinquishes the processor until notified that it should retry. ! 215: .LP ! 216: To release a lock a process calls: ! 217: .DS ! 218: mclear(sem) ! 219: semaphore *sem; ! 220: .DE ! 221: \fIMclear\fP indivisibly tests and clears the semaphore \fIsem\fP. ! 222: If the ``WANT'' flag is zero in the previous value, ! 223: \fImclear\fP returns immediately. ! 224: If the ``WANT'' flag is non-zero in the previous value, ! 225: \fImclear\fP arranges for waiting processes to retry before returning. ! 226: .PP ! 227: Two routines provide services analogous to the kernel ! 228: \fIsleep\fP and \fIwakeup\fP functions interpreted in the domain of ! 229: shared memory. ! 230: A process may relinquish the processor by calling \fImsleep\fP ! 231: with a set semaphore: ! 232: .DS ! 233: msleep(sem) ! 234: semaphore *sem; ! 235: .DE ! 236: If the semaphore is still set when it is checked by the kernel, ! 237: the process will be put in a sleeping state ! 238: until some other process issues an \fImwakeup\fP for the same semaphore ! 239: within the region using the call: ! 240: .DS ! 241: mwakeup(sem) ! 242: semaphore *sem; ! 243: .DE ! 244: An \fImwakeup\fP may awaken all sleepers on the semaphore, ! 245: or may awaken only the next sleeper on a queue.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.