Linux/UNIX之進程環境

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   使用   for   strong   sp   

進程環境進程終止

有8種方式使進程終止,當中5中為正常終止,它們是

1)      從main返回

2)      調用exit

3)      調用_exit或_Exit

4)      最後一個線程從其啟動常式返回

5)      最後一個線程調用pthread_exit

異常終止有3中方式

6)      調用abort

7)      接到一個訊號並終止

8)      最後一個線程對取消請求做出對應

exit函數

#include <stdlib.h>

void exit(int status);

void _Exit(int status);

#include <unistd.h>

void _exit(int status);

         三個函數都帶有一個整形參數,成為終止狀態。

exit函數總是先運行一個標準I/O庫的清理關閉操作:為所以開啟流調用fclose函數,這會造成所以緩衝區的輸出資料都被沖洗。

exit(0)等價於return(0)。

atexit函數

依照ISO C的規定,一個進程能夠登記多達32個函數,這些函數將由exit自己主動調用。我們稱這些函數為終止處理常式,並調用atexit函數來登記這些函數。

#include <stdlib.h>int atexit(void (*function)(void));當中,atexit的參數是一個函數地址,當調用此函數時無需向它傳送不論什麼參數,也不期望它返回一個值。exit調用這些函數的順序與他們被atexit登記的順序相反。同一函數如登記多次,則也會被調用多次。例如以下是使用atexit的程式。#include "stdlib.h"#include "stdio.h"static void my_exit1(void);static void my_exit2(void); int main(void){   if (atexit(my_exit2) != 0)       perror("can‘t register my_exit2");    if (atexit(my_exit1) != 0)       perror("can‘t register my_exit1");   if (atexit(my_exit1) != 0)       perror("can‘t register my_exit1");    printf("main is done\n");    return(0);}static void my_exit1(void){   printf("first exit handler\n");} static void my_exit2(void){   printf("second exit handler\n");}


輸出結果例如以下:

main is done

first exit handler

first exit handler

second exit handler

命令列參數

核心是程式啟動並執行唯一方法是調用exec函數。當運行一個程式時,exec的進程可將命令列參數傳遞給該新程式。

C程式的儲存空間布局

本文段:這是由CPU啟動並執行機器指令部分。通常本文段是能夠共用的。

初始化資料區段:通常將此段成為資料區段。包括程式中明白賦初值的全域變數或靜態變數。

非初始化資料區段:通常稱此段為BBS段(block startedby ymbol),未賦初值的全域變數或靜態變數。在程式運行之前,核心將此段的資料初始化為0或null 指標。

棧:自己主動變數以及每次函數調用時所需儲存的資訊都存放在此段中。

堆:通常在堆中進行動態儲存裝置分配。

C程式典型的儲存安排例如以下所看到的:


用size命令報告本文段、資料區段和bbs段的長度。

環境表和環境變數環境表

每個程度都會接收到一張環境表。環境表是一個字元指標數組,當中每個指標包括一個以null結束的C字串的地址。全域變數environ則包括了該指標數組的地址:

extern char**environ

         比如,假設該環境包括5個字串,則其例如以下


當中每一個字串結尾都顯示的有一個null字元。我們稱environ為環境指標,指標數組為環境表,當中各個指標所指字串為環境字串。

環境變數

環境字串的形式通常例如以下:

name = value

ISO C定義了一個函數getenv用於取環境變數值:

#include <stdlib.h>

char *getenv(const char *name);

此函數返回一個指標,指向name = value字串中的value。未找到返回NULL。

#include <stdlib.h>

int putenv(char *string);

int setenv(const char *name, const char*value, int overwrite);

int unsetenv(const char *name);

putenv取形式為name = value的字串,將其放到環境表中。假設name已近存在,則先刪除原定義。

setenv將name設定為value。

unsetenv刪除name的定義。

環境表和環境字串通常儲存在空間的頂部(棧之上)

setjmp和longjmp

C語言中goto是不能跨越函數的,運行這類跳轉功能的函數是setjmp和longjmp。

#include <setjmp.h>

int setjmp(jmp_buf env);

void longjmp(jmp_buf env, int val);

通過程式來看看自己主動變數、全域變數、寄存器變數、靜態變數和易失變數的不同情況:

#include <stdio.h>#include <setjmp.h> static void f1(int, int, int, int);static void f2(void); static jmp_buf  jmpbuffer;static int      globval; int main(void){   int             autoval;   register int    regival;   volatile int    volaval;   static int      statval;    globval = 1; autoval = 2; regival = 3; volaval = 4; statval = 5;    if (setjmp(jmpbuffer) != 0) {       printf("after longjmp:\n");       printf("globval = %d, autoval = %d, regival = %d,"" volaval = %d, statval = %d\n",           globval, autoval, regival, volaval, statval);       exit(0);    }    /*    * Change variables after setjmp, but before longjmp.    */   globval = 95; autoval = 96; regival = 97; volaval = 98;   statval = 99;    f1(autoval, regival, volaval, statval); /* never returns */   exit(0);} static voidf1(int i, int j, int k, int l){   printf("in f1():\n");   printf("globval = %d, autoval = %d, regival = %d,"       " volaval = %d, statval = %d\n", globval, i, j, k, l);   f2();} static voidf2(void){   longjmp(jmpbuffer, 1);}


其運行結果例如以下:

in f1():

globval = 95, autoval = 96, regival = 97,volaval = 98, statval = 99

after longjmp:

globval = 95, autoval = 2, regival = 3,volaval = 98, statval = 99

可見全域變數、靜態變數和易失變數不受影響,自己主動變數和寄存器變數是否變化是不確定的(雖然這裡顯示恢複setjmp的值)。

getrlimit和setrlimit函數

#include <sys/time.h>

#include <sys/resource.h>

int getrlimit(int resource, struct rlimit*rlim);

int setrlimit(int resource, const structrlimit *rlim);

每一個進程都有一組資源限制,當中一些能夠用getrlimit和setrlimit函數查詢和更改。進程的資源限制一般是在系統初始化時由進程0建立的,然後由每一個後序進程繼承。

Linux/UNIX之進程環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.