進程終止的方式:
1,正常終止:
從main返回;
調用exit;
調用_exit;
2,異常終止:
調用abort;
由一個訊號終止。
exit和_exit的差異:
#include <stdlib.h>
void exit(int status);
#include <unistd.h>
void _exit(int status);
其中,exit會先執行一些清理工作,而_exit則直接進入核心。
終止處理函數:exit handler
ANSI C規定,一個進程可以登記多至32個函數。
登記方法:
#include <stdlib.h>
int atexit(void (*func)(void));
exit會以登記順序相反的順序調用這些函數。
環境表
extern char** environ;
存取方法:
#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);
設定環境變數的痛點在於環境變數處於記憶體的頂端,需要增加空間的時候處理邏輯比較重要。如果需要記憶體的話就會把環境表移到堆中去。
c程式的儲存空間布局:
可以用size命令查看text,data,bss
共用庫:
減少了每個可執行檔的長度,但增加了一些已耗用時間開銷。
儲存空間分配:
#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);
大多數實現所分配的儲存空間比所要求的要稍大一些,額外的空間用來記錄管理資訊----分配塊的長度,指向下一個分配塊的指標等等。如果寫過一個已指派區的文段,則會改寫後一塊的管理資訊,這種錯誤是災難性的。
另一個致命錯誤是釋放一個已經釋放的塊。
alloca函數在棧上分配空間,並自動釋放:
#include <alloca.h>
void* alloca(size_t size);
非局部跳轉----setjmp和longjmp函數。
非局部跳轉表示在棧上跳過若干調用幀。
#include <setjmp.h>
int setjmp(jmp_buf env);
int longjmp(jmp_buf env, int val);
存放在儲存空間中的變數將具有longjmp時的值,而在CPU和浮點寄存器中的變數則恢複為調用setjmp時的值。
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);
}
結果:
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
資源限制:
規則:
1.任何一個進程都可將一個軟式節流更改為小於或等於其硬限制。
2.任何一個進程都可降低其硬限制值,但它必須大於或等於其軟式節流值。無法復原。
3.只有超級使用者可以提高硬限制。
資源限制影響到調用進程並由其子進程繼承。
#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;
};
例子:
#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("%-14s ",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);
}
}
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) (infinity)
RLIMIT_DATA (infinity) (infinity)
RLIMIT_FSIZE (infinity) (infinity)
RLIMIT_STACK 8388608 (infinity)
RLIMIT_MEMLOCK 65536 65536
RLIMIT_NOFILE 1024 1024
RLIMIT_OFILE 1024 1024
RLIMIT_NPROC (infinity) (infinity)
RLIMIT_RSS (infinity) (infinity)
hoperun@rh-ub063:/local/unix_environment$