1. Beforetime, almost all unix used a apt method to manage its memory which using the hardware policy to make sure every program have its own memory segment.
2. When we use the function ‘malloc()’ to distribute memory, the return is always a pointer to the beginning of some linetype memory segment. So the pointer can be mapped to any type pointer. But that’s why? It is easy that the pointer of unix is 32bit long and the length is enough for us to find the memory with the max size as
3. By the following example, we can see the fact:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define A_MEGABYTE (1024 * 1024)
int main() {
char *some_memory;
size_t size_to_allocate = A_MEGABYTE; 
int megs_obtained = 0;
while (megs_obtained < 16) {
some_memory = (char *)malloc(size_to_allocate);
if (some_memory != NULL) {
megs_obtained++;
sprintf(some_memory, "Hello World");
printf("%s - now allocated %d Megabytes\n", some_memory, megs_obtained);
}
else {
exit(EXIT_FAILURE);
}
}
exit(EXIT_SUCCESS);
} 

We can see the result that we got the memory overstep our physical memory space. Actually, unix carry out the ‘request page virtual memory system’. The memory which program gets hold of is the virtual memory. when the program try to call memory, system would always be a exchange between the virtual and the physical page.
4. It’s so nice to our programmer, because of the good manage to memory, we can use more memory as we need.
5. Even if it seem as the memory size will be no top, we should not abuse memory. It is dangerous at times that ‘malloc’ usually try to write the memory which is out of distributive space.
6. Unix have a thin skin to the null pointer, it can be read by returning the string “(null)”, but to write to a null will be return error.
7. Function ‘free(…)’ to dispose the memory to malloc manage; ‘calloc(…)’ is used to distribute memory to structure; ‘relloc(…)’ is used to change the size of memory which was distributed before, and when you try to change something, you’d like to backup the former because the former pointer would not be used any more whether the result is success or error.
8. The lock of the file works as the signal, it is a good idea to create lock file to control the serial device for its monopolization. But if there is a large file shared, you'd better not to do like this. Because at the same time, many programs are trying to modify the file, we can make use of blanking off some area of the file to achieve.
9. The process can get the lock information (structure flock which include the lock type, size and so on) about some area of file by the function F_GETLK.
10. F_SETLK is used to set the lock of some file.
11. When the file is locked, you’d better to use the base read()/write() to R/W and not to use the upper function fread()/fwrite(). Because fread()/fwrite will take the data which will be operated to the buffer first, in this way, it is clear that we will take the data as the size of buffer, then we likely to get the dirty data when the next reading.
12. According with the X/Open technology criterion, the accordant edition of Unix have a database named dbm. This database is adapt to store the data which be modified rarely.


浙公网安备 33010602011771号