1、用户登录
登录名称:密文口令:用户ID:组ID:注释信息:HOME:SHELL
示例:billing:!:207:202::/billing:/usr/bin/ksh
2、文件与目录
char *getcwd (char *Buffer,size_t Size) //获取当前工作目录
int chdir (const char *Path) //改变当前工作目录
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if( argc != 2 )
{
printf("usage: %s <directory_name>\n", argv[0]);
exit(0);
}
/* 打开目录 */
if( (dp = opendir(argv[1])) == NULL )
{
perror(argv[1]);
exit(1);
}
/* 逐一读取目录条目 */
while( (dirp = readdir(dp)) != NULL )
{
printf("%s\n", dirp->d_name);
}
/* 关闭目录 */
closedir(dp);
exit(0);
}
rm -- -1
3、输入/输出
操作对象为文件描述符,非负小整数
特殊描述符:STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO
相关调用示例:open()、read()、write()、lseek()、close() …
例子:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int n;
char buf[BUFSIZ];
/* 读取标准输入 */
while( (n = read(STDIN_FILENO, buf, BUFSIZ)) > 0 )
{
/* 写入标准输出 */
if( write(STDOUT_FILENO, buf, n) != n )
{
perror("write");
exit(-1);
}
}
if( n < 0 )
{
perror("read");
}
exit(0);
}
操作对象为文件指针,FILE *
特殊指针:stdin、stdout、stderr
相关调用示例:printf()、fgets()、fputs() …
4、程序与进程
#include <stdio.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int status;
char buf[1024];
/* print prompt (%% to print %) */
printf("%% ");
/* 读取标准输入 */
while( fgets(buf, sizeof(buf), stdin) != NULL )
{
/* replace newline with null */
if( buf[strlen(buf) - 1] == '\n' )
{
buf[strlen(buf) - 1] = 0;
}
/* 派生子进程 */
if( (pid = fork()) < 0 )
{
perror("fork");
exit(-1);
}
else if( pid == 0 ) /* child */
{
/* 执行读取的指令 */
execlp(buf, buf, (char *)0);
perror(buf);
exit(127);
}
/* parent */
if( (pid = waitpid(pid, &status, 0)) < 0 )
{
perror("waitpid");
}
printf("%% ");
}
exit(0);
}
5、错误处理
函数执行成功并不清除上次的errno取值;
函数不会将errno设置为0;
char *strerror (int ErrorNumber) //获取错误编号对应的错误信息
void perror (const char *String) //显示错误信息
uid_t getuid (void) //获取用户ID
gid_t getgid (void) //获取组ID
允许一个用户属于多个组,示例:
uid=207(billing)gid=202(dba) groups=205(billing)
7、信号
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
void sig_int(int signo)
{
printf("interrupt\n%% ");
}
int main(void)
{
pid_t pid;
int status;
char *p, buf[1024];
struct sigaction sa;
/* 初始化 */
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
/* 设置信号处理动作 */
sa.sa_handler = sig_int;
sigaction(SIGINT, &sa, NULL);
/* print prompt (%% to print %) */
printf("%% ");
while( 1 )
{
errno = 0;
/* 读取指令 */
p = fgets(buf, sizeof(buf), stdin);
if( p == NULL )
{
if( errno == EINTR ) continue;
else break;
}
/* replace newline with null */
if( buf[strlen(buf) - 1] == '\n' )
{
buf[strlen(buf) - 1] = 0;
}
/* 派生子进程 */
if( (pid = fork()) < 0 )
{
perror("fork");
exit(-1);
}
else if( pid == 0 ) /* child */
{
/* 执行指令 */
execlp(buf, buf, (char *)0);
perror(buf);
exit(127);
}
/* parent,等待子进程终止 */
if( (pid = waitpid(pid, &status, 0)) < 0 )
{
perror("waitpid");
}
printf("%% ");
}
exit(0);
}
8、时间值
日历时间,表示自1970.1.1零点以来UTC的秒数累计
进程时间,用以度量进程对CPU资源的占用
时钟时间的取值与系统活动有关
time find / -name a.txt
9、系统调用与库函数