"Linux_unix system Programming" CHAPTER6 process

Source: Internet
Author: User

CHAPTER6 process
Focus on the layout and content of the process virtual memory.
6.1 Processes and procedures
A process is an instance of an executable program.
A program is a file that contains a series of information that describes how to create a process at run time with the following content.
(1): Binary format identification: Each program file contains meta information that describes the format of the executable file.
(2): machine language instruction; Encode program algorithm
(3): Program Entry Address: Identifies the starting command location when the program starts executing.
(4): Data: Contains the variable initial value and the literal value used by the program
(5): Symbol Table and Relocation table: Describes the location and name of functions and variables in the program.
(6): Shared Library and dynamic Link information:
(7): Other information

6.2 Process number and parent process number
Each process has a process number (PID), which is a positive number that uniquely identifies a process in the system.
#include <unistd.h>
pid_t getpid (void);
In addition to a few system processes, the INIT process number is 1, and there is no fixed relationship between the program and the process number that runs the change program.
The Linux kernel limit process number is less than or equal to 32767.
Each process has a parent process that creates its own. Use System Transfer Getppid () to retrieve the process number of the parent process
#include <unistd.h>
pid_t getppid (void);
Process--init Process 1th, the ancestor of all processes. Pstree (1) command to view this "family tree". If the child process's parent process terminates, the child process becomes "orphan", and the Init process will then adopt the process, and subsequent calls to Getppid () will return the process number 1.

6.3 Process Memory Layout
The memory allocated by each process is composed of many parts, often referred to as "segments (segment)".
Text snippet: Contains the program machine language instruction that the process is running.
Initialize data segment: Contains the global variables and static variables that display initialization.
Uninitialized data segment: Contains global variables and static variables that do not display initialization.
Stack: is a dynamic growth and contraction of the end, composed of stack frame (stack frames).
Heap: is an area in which memory allocations can be dynamically made at run time (for variables).

In most UNIX implementations, the C language programming environment provides 3 global symbols: Etext,edata and end, which can be used within a program to obtain the corresponding program text segment, initialize the data segment, and the address of the next byte at the end of the non-initialized data segment. With these symbols, the declaration must be displayed as follows:
extern Char EText, edata, end;
6.4 Virtual Memory Management
(1): Spatial locality: Refers to programs that tend to access memory near memory addresses that have recently been accessed (because instructions are executed sequentially, and sometimes the data structure is processed sequentially)
(2): Time locality: means that the program tends to revisit the memory address that was recently accessed (due to loops) in the near future.

6.5 Stack and stack frame rate
The call and return of the function makes the stack grow and shrink linearly.


6.6 Command-line arguments
Each C language program must have a function called main () as the starting point for the program to start.

6.7 Environment List
Each process has an array of strings associated with it that is called the environment list, or simply an environment.
To access the environment from the program:
In a C language program, you can use the global variable char **environ to access the list of environments.
e.g.:
extern char **environ;
int main (int argc, char *argv[])
{
Char **ep;
for (ep = environ; *ep! = NULL; ep++)
{
Puts (*EP);
}
}
In addition, you can access the list of environments by declaring the third parameter in the main () function:
int main (int argc, char *argv[], char *envp[])
The getenv () function can retrieve a single value from the process environment.
#include <stdlib.h>
Char *getenv (const char* name);

Modify the Environment:
The putenv () function adds a new variable to the environment of the calling process, or modifies a variable value that already exists.
#include <stdlib.h>
int putenv (char *string);
A call failure will return a value other than 0 instead of-1.

The setenv () function can be used in place of the putenv () function to add a variable to the environment.
#include <stdlib.h>
int setenv (const char *name, const char *value, int overwrite);

The unsetenv () function removes the variable marked by the name parameter from the environment.
#include <stdlib.h>
int unsetenv (const char *name);

Clearenv ():
#define _bsd_source
#include <stdlib.h>
int clearenv (void);

6.8 Performing non-local jumps: setjmp () and longjmp ()
Use library functions setjmp () and longjmp () to perform nonlocal jumps.
#include <setjmp.h>
int setjmp (jmp_buf env);
void longjmp (jmp_buf env, int val);
The setjmp () call establishes a jump target for subsequent jumps performed by the longjmp () call. The target is where the program initiates the SETJMP () call.

"Linux_unix system Programming" CHAPTER6 process

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.