include/unistd.h分析
定义标准输入、输出、错误输出
#define stdin_fileno 0
#define stdout_fileno 1
#define stderr_fileno 2
null的定义,注意:null一般是用在指针的。
#ifndef null
#define null ((void *)0)
#endif
文件操作lseek
/* lseek */
#define seek_set 0
#define seek_cur 1
#define seek_end 2
在1.0版本时定义在include/unistd.h中,到后来定义到include/asm/unistd.h中,其中asm为指向具体机型的连接(如:asm-i386,asm-x86_64 ......)
系统调用定义:
#define __nr_setup 0 /* used only by init, to get system going */
#define __nr_exit 1
#define __nr_fork 2
#define __nr_read 3
#define __nr_write 4
#define __nr_open 5
#define __nr_close 6
#define __nr_waitpid 7
#define __nr_creat 8
#define __nr_link 9
#define __nr_unlink 10
#define __nr_execve 11
#define __nr_chdir 12
#define __nr_time 13
#define __nr_mknod 14
#define __nr_chmod 15
#define __nr_chown 16
#define __nr_break 17
#define __nr_stat 18
#define __nr_lseek 19
#define __nr_getpid 20
#define __nr_mount 21
#define __nr_umount 22
#define __nr_setuid 23
#define __nr_getuid 24
#define __nr_stime 25
#define __nr_ptrace 26
#define __nr_alarm 27
#define __nr_fstat 28
#define __nr_pause 29
#define __nr_utime 30
#define __nr_stty 31
#define __nr_gtty 32
#define __nr_access 33
#define __nr_nice 34
#define __nr_ftime 35
#define __nr_sync 36
#define __nr_kill 37
#define __nr_rename 38
#define __nr_mkdir 39
#define __nr_rmdir 40
#define __nr_dup 41
#define __nr_pipe 42
#define __nr_times 43
#define __nr_prof 44
#define __nr_brk 45
#define __nr_setgid 46
#define __nr_getgid 47
#define __nr_signal 48
#define __nr_geteuid 49
#define __nr_getegid 50
#define __nr_acct 51
#define __nr_phys 52
#define __nr_lock 53
#define __nr_ioctl 54
#define __nr_fcntl 55
#define __nr_mpx 56
#define __nr_setpgid 57
#define __nr_ulimit 58
#define __nr_uname 59
#define __nr_umask 60
#define __nr_chroot 61
#define __nr_ustat 62
#define __nr_dup2 63
#define __nr_getppid 64
#define __nr_getpgrp 65
#define __nr_setsid 66
到2.4.32时系统调用已经增加到252个
#define _syscall0(type,name) \
type name(void) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__nr_##name)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
#define _syscall1(type,name,atype,a) \
type name(atype a) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__nr_##name),"b" (a)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
#define _syscall2(type,name,atype,a,btype,b) \
type name(atype a,btype b) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__nr_##name),"b" (a),"c" (b)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
#define _syscall3(type,name,atype,a,btype,b,ctype,c) \
type name(atype a,btype b,ctype c) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__nr_##name),"b" (a),"c" (b),"d" (c)); \
if (__res<0) \
errno=-__res , __res = -1; \
return __res;\
}
这些系统调用是在内核中实现的,再通过一定的方式把系统调用给用户,一般都通过门(gate)陷入(trap)实现。系统调用是用户程序和内核交互的接口。
linux系统在cpu的保护模式下提供了四个特权级别,目前内核都只用到了其中的两个特权级别,分别为“特权级0”和“特权级3”,级别0也就是我们通常所讲的内核模式,级别3也就是我们通常所讲的用户模式。划分这两个级别主要是对系统提供保护。内核模式可以执行一些特权指令和进入用户模式,而用户模式则不能。
系统调用对于内核来说就相当于函数,我们是关键问题是从用户模式到内核模式的转换、堆栈的切换以及参数的传递。
可以说用户程序要进入系统内部完成相应功能就必须使用到系统调用。
错误号定义:
extern int errno;
我们通常用strerror()来得到更详细的错误信息。
阅读(3186) | 评论(1) | 转发(0) |