Unix環境高度プログラミング3


一、エラー処理
関数エラーは通常負の数を返し、errnoは通常一定のエラー情報を表し、戻り値がポインタタイプの場合はnullを返します.
    char *strerror(int errno) ;   void perror(const char *m s g) ;二、信号
プロセスに何かが起こったことを知らせる技術
//           
#include "sys/types.h"
#include "sys/wait.h"
#include 
#include "stdio.h"
#include "string.h"
#include 

using namespace std;

static void sig_int(int);

int main(void)
{
    char buf[1024];
    pid_t pid;
    int status;

    if(signal(SIGINT, sig_int)==SIG_ERR)
        cout << "signal error" << endl;

    while( fgets(buf, 1024, stdin) != NULL  )
    {   
        buf[strlen(buf)-1]=0;
        if( (pid = fork())<0 )
            cout << "fork error" << endl;
        else if(pid == 0)
        {
            execlp(buf, buf, (char*)0);
        }
        if( (pid = waitpid(pid, &status, 0))<0)
        {
            cout << "wait pid error" << endl;
        }
    }
    return 0;
}

void sig_int( int signo )
{
    cout << signo << endl;
}
                         

三、システム呼び出しとライブラリ関数
アプリケーションコード->ストレージ割当関数(malloc)->カーネル(sbrk)
->残り9998時間30分