一、出错处理
函数出错通常是返回一个负数,而且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 )