文档介绍:LINUX C编程
教师:中国地质大学朱静
第四讲系统调用
系统调用简介
错误处理
文件管理
进程管理
信号
进程间通信
线程及其管理
系统调用简介
系统调用概念
为了利用文件操作、进程管理和设备管理这些操作系统提供的服务,应用程序必须和操作系统之间交互。这种交互通过“系统调用”来实现。
系统调用是程序员和linux内核的函数接口。
对程序员来说,系统调用就象库函数(事实上有些就是库函数,由这些库函数再进行真正的系统调用),只是前者直接在linux的核心执行子程序调用。
系统调用简介
系统调用类别
文件管理
进程管理
错误管理
系统调用简介
文件管理系统调用的层次结构
文件
特殊文件
目录文件
open
close
read
write
lseek
unlink
chown dup2 fcntl fstat ftruncate stat
truncate sync dup link
套接字
套接字
mknod ioctl pipe
getdents
accept bind connect listen
socket
gethostbyname gethostname htonl
htons inet_addr inet_ntoa
系统调用简介
进程管理系统调用的层次结构
进程
信号
nice chdir wait fork exec exit
alarm singnal kill pause
系统调用简介
错误处理系统调用的层次结构
错误处理
perror
#include <>
char *strerror(int errnum) 返回错误原因的描述字符串
错误处理
错误处理perror()
#include <>
#include <>
系统调用失败时返回-1,但这个返回值说明不了错误发生的原因,要查询错误原因,必须了解:
errno 保存上次系统调用错误的数字码的全局变量,初值为0
从程序访问errno,预定义的错误码在C程序中通过包含文件“/usr/include/”来定义。
perror程序把errno的当前值转换为文本描述
错误处理
错误处理perror()
例:/usr/include/…/:
#define EPERM 1 /*Operation not permitted*/
#define ENOENT 2 /*No such file or directory*/
#define ESRCH 3 /*No such process*/
#define EINTR 4 /*Interrupted system call*/
错误处理
错误处理perror()
例:perror()应用
//
#include <>
#include <>
#include <>
main()
{int fd;
/*打开一个不存在的文件,强制发生一个错误*/
fd=open(“”,O-RDONLY);
If (fd==-1)) /*an error occurred*/
{
printf(“errno=%%d\n”,errno);
perror(“main”);
}