unix 环境高级编程3


一、出错处理

    函数出错通常是返回一个负数,而且errno通常代表一定的出错信息,对于返回值是指针类型的通常返回null。

    char *strerror(int errno) ;

   void perror(const char *m s g) ;

二、信号

    通知进程发生某种事情的技术

//从标准输入读命令并执行
#include "sys/types.h"
#include "sys/wait.h"
#include <iostream>
#include "stdio.h"
#include "string.h"
#include <signal.h>

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分钟


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值