APUE notes: details of the process environment

  • 2020-04-01 23:38:05
  • OfStack

Calling exit (1) in main is equivalent to calling reutrn 1.
The only way the kernel can start a process is by calling exec, and the only way a user program can terminate a process is by displaying or implicitly calling _exit or EXIT.
Each process has a process table. The process table is a global pointer: environ. Extern char **environ can view this environment table. In addition, the modification environment table can be obtained through getenv and putenv. Write a program and try it.
Segment of Linux: the Linux body segment starts at cell 0X08048000, and the bottom of the stack starts at 0xC0000000

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/201305231528159.png ">

The stack is growing down and up. This is the logical address.
At compile time, gcc-static prevents programs from using dynamic libraries and instead USES static libraries.
Malloc: allocates a specified byte store with an uncertain value.
Calloc: prototype as void *calloc(size_t nobj, size_t size), allocate size*nobj bytes.
Realloc: changes the length of the previous storage area. Void *realloc(void * PTR, size_t new_size); The return value may still be the original address (if there is enough storage behind it) and may have changed (there is not enough storage behind it, so you need to apply for a large one and copy the original contents). New_size is the size of the new buffer, not the difference. If PTR is empty, it equals malloc, and if size is 0, it equals free. The new application is not initialized.
The returned address must be aligned so that it can store any object type. For example, some systems require that the starting address of double-type data be a multiple of 8.
Three functions, putenv,setenv, and unsetenv, can change the environment variables of a process.
Setenv :int setenv(const char *name, const char *value, int rewrite)//rewrite: if existing or not.
The putenv function puts the input directly into the environment table without allocating storage. In this way, if it is a stack, an error will occur.
Setjmp and longjmp are global goto, but try not to use them. There are many pitfalls and errors.
Getrlimit and setrlimit can modify the resource limits of a process. Use the command to modify all processes. This function can be used for the current process. The corresponding imperative ulimit.

Write test program to verify:
1.   Program dead loop, with printf, foreground start and background start the CPU is the same?
2,   Open the file, then close the file, and say if it took too long.
Ok. Average at 50us (0.05 milliseconds)
3,   Does opening a large file take the same time as opening a small file? Is there a big difference?
There is a difference between open mode and open time (the following code: open the file, write the same thing) :
A + : 20 us
W + / w: 70 us
R: 20 us
R + : 20 us
There is still a difference between opening a large file and opening a small file.
One 8M and one 3k are also suitable for a+ opening without writing:
8 m: 37 us
3 k: 12 us
One 8M, one 3k, also applicable to a+ open, write content (1K) :
8 m: 60 us
3 k: 23 us
I'm writing a 250M file, which is about 60us. It's not much different from 8M.
After switching to flush, the average write-flush is about 9us.
4,   Write file: write the same content, write to a blank file and write to a large file, time is the same?
About the same.
5,   A file, open, write. Delete the entire file out of process and then write the file. See what happens: whether the write succeeds, whether the file is regenerated, whether it is generated when closed, whether it returns an error to know that the file does not exist, and whether you can get this information through errno.
There is no good method at present, the return value of the written function is successful, but the file does not exist. My current practice is to determine whether the file exists when I write it. It takes about 9us. This is much faster than opening and closing.
6,   Does it take the same amount of time for one thread to write multiple files sequentially and for multiple threads to write multiple files in parallel?
7,   When is the file IO's native cache written to the file? Is it calling the last write function? Or is it inside the system? If it is the previous one, the return time of the write call is different.


Related articles: