Chapter 7 UNIX process environment

Source: Internet
Author: User

Method of Process Termination:

1. Normal termination:

Return from main;

Call exit;

Call _ exit;

2. exceptional termination:

Call abort;

Terminated by a signal.

Difference between exit and _ Exit:

# Include <stdlib. h>

Void exit (INT status );

# Include <unistd. h>

Void _ exit (INT status );

Here, exit will first execute some cleanup work, while _ exit will directly enter the kernel.

Termination handler: Exit Handler

Ansi c requires that a process can register up to 32 functions.

Registration Method:

# Include <stdlib. h>

Int atexit (void (* func) (void ));

Exit calls these functions in the reverse order of registration.

Environment table

Extern char ** environ;

Access Method:

# Include <stdlib. h>

Char * getenv (const char * Name );

Int putenv (char * string );

Int setenv (const char * Name, const char * value, int overwrite );

Int unsetenv (const char * Name );

Int clearenv (void );

The difficulty in setting environment variables is that the environment variables are at the top of the memory, and the processing logic is important when you need to increase the space. If you need memory, the Environment table will be moved to the heap.

C program storage space layout:

You can use the SIZE command to view text, data, and BSS.

Shared Library:

Reduces the length of each executable file, but increases the running time overhead.

Memory Allocation:

# Include <stdlib. h>

Void * malloc (size_t size );

Void * calloc (size_t nobj, size_t size );

Void * realloc (void * PTR, size_t size );

Void * free (void * PTR );

In most implementations, the allocated storage space is slightly larger than the required one. The additional space is used to record management information-the length of the allocation block, and the pointer to the next allocation block. If you have written a text segment in the allocated area, the management information of the last segment will be rewritten. This error is catastrophic.

Another fatal error is releasing a released block.

The alloca function allocates space on the stack and automatically releases it:

# Include <alloca. h>

Void * alloca (size_t size );

Non-local jump ---- setjmp and longjmp functions.

Non-local jump indicates that several call frames are skipped on the stack.

# Include <setjmp. h>

Int setjmp (jmp_buf env );

Int longjmp (jmp_buf ENV, int Val );

Variables stored in the memory will have longjmp values, while those in the CPU and floating-point registers will be restored to the values when setjmp is called.

Ex:

# Include <setjmp. h>
# Include <stdio. h>
# Include <stdlib. h>

Static void F1 (INT, Int, INT );
Static void F2 (void );

Static jmp_buf jmpbuffer;

Int main (void ){
Int count;
Register int val;
Volatile int sum;

Count = 2;
Val = 3;
Sum = 4;
If (setjmp (jmpbuffer )! = 0 ){
Printf ("after longjmp: Count = % d, val = % d, sum = % d \ n", Count, Val, sum );
Exit (0 );
}
Count = 97;
Val = 98;
Sum = 99;

F1 (count, Val, sum );
}

Static void F1 (int I, Int J, int K ){
Printf ("in F1 (): Count = % d, val = % d, sum = % d \ n", I, j, k );
F2 ();
}

Static void F2 (void ){
Longjmp (jmpbuffer, 1 );
}

Result:

Hoperun @ rh-ub063:/local/unix_environment $ gcc-O testjmp. C-o testjmp
Hoperun @ rh-ub063:/local/unix_environment $ testjmp
In F1 (): Count = 97, val = 98, sum = 99
After longjmp: Count = 2, val = 3, sum = 99
Hoperun @ rh-ub063:/local/unix_environment $ GCC testjmp. C-o testjmp
Hoperun @ rh-ub063:/local/unix_environment $ testjmp
In F1 (): Count = 97, val = 98, sum = 99
After longjmp: Count = 97, val = 98, sum = 99

Resource restrictions:

Rules:

1. Any process can change a soft limit to a value smaller than or equal to its hard limit.

2. Any process can reduce its hard limit value, but it must be greater than or equal to its soft limit value. Irreversible.

3. Only Super Users can raise the hard limit.

Resource restrictions affect the calling process and are inherited by its child processes.

# Include <sys/time. h>

# Include <sys/resource. h>

Int getrlimit (INT resource, struct rlimit * rlim );

Int setrlimit (INT resource, const struct rlimit * rlim );

Struct rlimit
{
/* The current (soft) limit .*/
Rlim_t rlim_cur;
/* The Hard limit .*/
Rlim_t rlim_max;
};

Example:

# Include <sys/types. h>
# Include <sys/time. h>
# Include <sys/resource. h>
# Include <stdio. h>
# Include <stdlib. h>

# Define doit (name) pr_limits (# Name, name );

Static void pr_limits (char *, INT );

Int main (void ){
Doit (rlimit_core );
Doit (rlimit_cpu );
Doit (rlimit_data );
Doit (rlimit_fsize );
Doit (rlimit_stack );

# Ifdef rlimit_memlock
Doit (rlimit_memlock );
# Endif
# Ifdef rlimit_nofile
Doit (rlimit_nofile );
# Endif
# Ifdef rlimit_ofile
Doit (rlimit_ofile );
# Endif
# Ifdef rlimit_nproc
Doit (rlimit_nproc );
# Endif
# Ifdef rlimit_rss
Doit (rlimit_rss );
# Endif
# Ifdef rlimit_vmem
Doit (rlimit_vmem );
# Endif
Exit (0 );
}

Static void pr_limits (char * Name, int Resource ){
Struct rlimit limit;

If (getrlimit (resource, & limit) <0 ){
Printf ("getrlimit error for % s", name );
}
Printf ("%-14 s", name );
If (limit. rlim_cur = rlim_infinity ){
Printf ("(infinity )");
} Else {
Printf ("% 10ld", limit. rlim_cur );
}
If (limit. rlim_max = rlim_infinity ){
Printf ("(infinity) \ n ");
} Else {
Printf ("% 10ld \ n", limit. rlim_max );
}
}

Execution results under unbuntu:

Hoperun @ rh-ub063:/local/unix_environment $ GCC rlimittest. C-o rlimittest
Hoperun @ rh-ub063:/local/unix_environment $ rlimittest
Rlimit_core 0 (infinity)
Rlimit_cpu (infinity)
Rlimit_data (infinity)
Rlimit_fsize (infinity)
Rlimit_stack 8388608 (infinity)
Rlimit_memlock 65536 65536
Rlimit_nofile 1024 1024
Rlimit_ofile 1024 1024
Rlimit_nproc (infinity)
Rlimit_rss (infinity)
Hoperun @ rh-ub063:/local/unix_environment $

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.