APUE notes: process environment details

Source: Internet
Author: User

In main, calling exit (1) is equivalent to reutrn 1.
The only way for the kernel to start a process is to call exec. The only way for the user program to terminate the process is to display or implicitly call _ exit or EXIT.
Each process has an input table. The progress table is a global pointer: environ. Extern char ** environ allows you to view the environment table. In addition, you can use getenv and putenv to obtain and modify the environment table. Write a program and try it out.
Linux segment: the linux Text Segment starts from Unit 0X08048000, And the stack base starts from 0xC0000000.



Stack is growth down, and growth up. The logical address is described here.
During compilation, gcc-static can prevent programs from using dynamic libraries instead of static libraries.
Malloc: allocate a specified byte storage area. The value is uncertain.
Calloc: The prototype is void * calloc (size_t nobj, size_t size), and the size * nobj is allocated.
Realloc: change the length of the previous bucket. Prototype: void * realloc (void * ptr, size_t new_size); The returned value may still be the original address (if there is enough storage space behind it ), there may be changes (there is not enough storage area in the end, you need to apply for a large one, and copy the original content ). New_size is the size of the new buffer, not the difference value. If ptr is null, it is equivalent to malloc. If size is 0, it is equivalent to 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 data must be a multiple of 8.
Putenv, setenv, and unsetenv functions can be used to change the environment variables of a process.
Setenv: int setenv (const char * name, const char * value, int rewrite) // rewrite: If overwrite exists or not.
The putenv function directly puts the input parameters in the Environment table without assigning a storage area. In this way, an error occurs if the stack is used.
Setjmp and longjmp are global goto, but they are hidden in many traps and prone to errors.
Getrlimit and setrlimit can modify the resource limits of processes. You can use commands to modify all processes. This function can be used for the current process. The corresponding imperative ulimit.

Write Test Program Verification:
1. Is the CPU occupied by the program's endless loop of printf, foreground startup and background startup the same?
2. open the file and close the file, saying whether it takes too much time.
Okay. Average in 50 US (0.05 ms)
3. Does it take the same time to open a large file or a small file? Is the difference big?
The open mode differs from the open time (the following code: open a file and write the same content ):
A +: 20us
W +/w: 70us
R: 20us
R +: 20us
Opening a large file is different from opening a small file.
An 8 M, a 3 k, also applies to a + open, do not write content:
8 M: 37us
3 k: 12us
An 8 M, a 3 k, also applies to a + open, write content (1 K ):
8 M: 60us
3 k: 23us
Write a M file, which is almost 60us. It is not much different from 8 m.
After using flush, the average flush value after writing is about 9us.
4. Write a file: write the same content, write it to a blank file, and write it to a large file. Is the time consistent?
Similar.
5. Open a file and write the content. Delete the entire file outside the process, and then write the file. Effect: whether the file is successfully written, whether the file is generated again, whether the file is generated when it is closed, whether an error is returned, and whether the file does not exist. Whether this information can be obtained through errno.
Currently, there is no good method. The return value of the written function is successful, but the file does not exist. My current practice is to determine whether a file exists during writing. It takes about 9us time. This is much faster than opening and closing.
6. Write multiple files in the same thread sequence and write multiple files in parallel in multiple threads. Is the same amount of time consumed?
7. When is the built-in cache of file I/O written to the file? Is the last write function called? Or inside the system? If it is the previous one, the return time of the write call is different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.