天天看點

程序的挂起 程序的等待 程序的終止 特殊程序 孤兒程序 守護程序 sleep函數  exit 函數  _exit函數 wait 函數 waitpid 函數程序的挂起程序的終止

程序的挂起

   程序在一定的時間内沒有任何動作, 稱為程序的挂起

#include <unistd.h>

/*
 * 功能:
 *    程序挂起指定的秒數,直到指定的時間用完或 收到信号才解除挂起
 * return:
 *    程序挂起到sec指定的時間 傳回0
 *    有信号中斷 傳回剩餘秒數
 * 注意:
 *    程序挂起指定的秒數後程式并不會立即執行,系統隻是将此程序切換到就緒态
 */
unsigned int sleep(unsigned int sec);
           

    程序的等待

          父子程序有時需要簡單的程序間同步, 如父程序等待子程序的結束

       wait 函數

#include <sys/types.h>
#include <sys/wait.h>

/*
 * 功能:
 *   等待子程序終止,如果子程序終止,該函數會回收子程序的資源
 *   調用wait函數的程序會挂起,直到它的一個子程序退出或 收到一個不能被忽視的信号才被喚醒
 *   調用程序沒有子程序或 它的子程序已結束,該函數立即傳回
 * 參數:
 *    函數傳回時,參數 status 中包含子程序退出時的狀态資訊 
 *    子程序的退出資訊在一個 int 中包含了多個字段,用宏定義可以取出其中的每個字段
 * return:
 *   成功:子程序的程序号
 *   失敗:-1
 * 取出子程序的退出資訊:
 *   WIFEXITED(status)
 *     子程序是正常終止的,取出的字段值非零
 *   WEXITSTATUS(status)
 *     傳回子程序的退出狀态,退出狀态儲存在 status 變量的 8~16 位
 *     在用此宏前應先用宏 WIFEXITED 判斷子程序是否正常退出, 正常退出才可以使用此宏
 * 注意:
 *    status:wait 的參數指向的整型變量
 */
pid_t wait(int *status);
           
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t pid;
   
    pid = fork();
    if(pid < 0)
    {
        perror("fork");
    }

    if(0 == pid)
    {
        int i = 0;
        for(i = 0; i < 5; i++)
        {
            printf("this is son process\n");
            sleep(1);
        }
        _exit(2);
    }
    else
    {
        int status = 0;
        wait(&status);
        if(WIFEXITED(status) != 0)
        {
            printf("son process return %d\n", WEXITSTATUS(status));
        }
        printf("this is father process\n");
    }
    return 0;
}
           
程式的挂起 程式的等待 程式的終止 特殊程式 孤兒程式 守護程式 sleep函數  exit 函數  _exit函數 wait 函數 waitpid 函數程式的挂起程式的終止

      waitpid 函數

/*
 * 功能:
 *   等待子程序終止,如果子程序終止,此函數會回收子程序的資源
 * return:
 *   成功:子程序 ID
 *   出錯:-1
 * pid 的值有以下幾種類型:
 *   pid>0:
 *     等待程序 ID 等于 pid 的子程序。
 *   pid=0:
 *      等待同一個程序組中的任何子程序,如果子程序已經加入了别的程序組,waitpid 不會等待它
 *   pid=-1:
 *     等待任一子程序,此時 waitpid 和 wait 作用一樣
 *   pid<-1:
 *      等待指定程序組中的任何子程序,這個程序組的 ID 等于 pid 的絕對值
 * 
 *   status 參數中包含子程序退出時的狀态資訊
 * 
 * options 參數能控制 waitpid 的操作:
 *    0:
 *      同 wait, 阻塞父程序,等待子程序退出
 *   WNOHANG:
 *      沒有任何已經結束的子程序,則立即傳回
 *   WUNTRACED
 *       如子程序暫停了,此函數馬上傳回,并且不予以理會子程序的結束狀态(跟蹤調試,很少用到)
 * 傳回值:
 *   成功:
 *     傳回狀态改變了的子程序的程序号,如 設定了選項 WNOHANG 并且 pid 指定的程序存在則傳回 0
 *   出錯:
 *     傳回-1 當 pid 所訓示的子程序不存在,或此程序存在,但不是調用程序的子程序,waitpid 就 
 *  會出錯傳回,這時 errno 被設定為 ECHILD
 */
pid_t waitpid(pid_t pid, int *status, int options);
           
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t pid;
    pid = fork();

    if(pid < 0)
    {
        perror("fork");
    }

    if(pid == 0)
    {
        int i = 0;
        for(i = 0; i < 5; i++)
        {
            printf("this is son process\n");
            sleep(1);
        }
        _exit(2);
    }
    else
    {
        waitpid(pid, NULL, 0);
        printf("this is father process\n");
    }
    return 0;
}
           
程式的挂起 程式的等待 程式的終止 特殊程式 孤兒程式 守護程式 sleep函數  exit 函數  _exit函數 wait 函數 waitpid 函數程式的挂起程式的終止

    特殊程序

       僵屍程序(Zombie Process)

           程序已運作結束, 但程序的占用的資源未被回收

           原因: 子程序已運作結束, 父程序未調用 wait 或 waitpid 函數回收子程序的資源

      孤兒程序(Orphan Process)

           父程序運作結束, 但子程序未運作結束的子程序

      守護程序(精靈程序)(Daemon process)

          特殊的孤兒程序, 這種程序脫離終端, 在背景運作

程序的終止

    exit 函數   

#include <stdlib.h>

/*
 * 結束程序執行
 * 參數:
 *  status 傳回給父程序的參數(低 8 位有效)
 */
void exit(int value)
           

  _exit 函數

#include <unistd.h>

/*
 * 結束程序執行
 * 參數:
 *   status:傳回給父程序的參數(低8位有效)
 */
void _exit(int value)
           

     exit 為庫函數, 而_exit 為系統調用

程式的挂起 程式的等待 程式的終止 特殊程式 孤兒程式 守護程式 sleep函數  exit 函數  _exit函數 wait 函數 waitpid 函數程式的挂起程式的終止

繼續閱讀