apue 第一章 unix基础知识
kernel(内核):一种软件,控制计算机的硬件资源,提供系统的运行环境
system call(系统调用):内核的接口
公用函数库和shell在系统调用之上
应用程序构建在最外层,既可以使用系统调用,也就可以使用公用函数库和shell
输入和输出
文件描述符
通常是一个非负整数,内核用来标识一个特定的进程正在访问的文件,在读写文件都会使用文件描述符,open或者create一个文件会返回文件描述符
standard input standard output standard error
但shell允许一个新程序时,默认打开三个文件描述符,标准输入,标准输出,标准错误,不特殊指明都指向终端,但可以重定向
#标准输出被重定向到文件
ls > file.list
不带缓冲的IO
从标准输入STDIN_FILENO(终端)中读取数据到buf中
从buf中写东西到标准输出STDOUT_FILENO(终端)中
BUFFZISE一次能读取的最多字节
编译以后执行 ./a.out < infile > outfile
,实现文件复制
#include "apue.h"
#define BUFFSIZE 4096
int
main(void)
{
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
if (write(STDOUT_FILENO, buf, n) != n)
err_sys("write error");
if (n < 0)
err_sys("read error");
exit(0);
}
标准IO
使用标准io函数无需选择缓冲区大小
get一次读取一个字符,读完显示EOF
stdin和stdout表示标准输入和输出
#include "apue.h"
int
main (void)
{
int c;
while ((c = getc(stdin)) != EOF)
if (putc(c, stdout) == EOF)
err_sys("output error");
if (ferror(stdin))
err_sys("input error");
exit(0);
}
程序和进程
程序 program:一个可执行文件,内核将程序读入内存,用以执行
进程 process:程序执行的实例,每个进程都要一个唯一的 进程ID
进程控制
启动程序,获取输入后,在父进程中创建出子进程,在子进程中执行命令
父进程等待子进程终止
fork对子进程返回0对父进程返回id
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%%");
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
//创建子进程
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
//pid = 0 创建子进程成功
else if (pid == 0)
{
//执行程序
execlp(buf, buf, (char *)0);
err_ret("Couldn't execute: %s", buf);
exit(127);
}
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
线程thread
一个进程内的所有线程共享进程相关属性
进程也有id,但只在进程内有意义
用户标识
每个用户一个 用户id ,不能更改,id为0的为root用户
组id 允许各个成员之间共享资源
使用username更加方便,在口令文件里包含了这种映射关系 ls -l
可以查看
用户还有 附属组id
信号signal
用于通知进程系统发生了某种错误系统的处理方式:忽略信号,系统默认处理,捕捉自己处理
系统产生了SIGINT (ctrl+D)信号,会在signal中捕捉到,然后调用sin_int函数处理
#include "apue.h"
#include <sys/wait.h>
static void sig_int(int);
int
main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%%");
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
if (signal(SIGINT, sig_int) == SIG_ERR)
err_sys("signal error");
//创建子进程
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
//pid = 0 创建子进程成功
else if (pid == 0)
{
//执行程序
execlp(buf, buf, (char *)0);
err_ret("Couldn't execute: %s", buf);
exit(127);
}
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
void sig_int(int signo)
{
printf("interrupt\n%% ");
}
最后编辑于 :2017.12.10 02:48:48
©著作权归作者所有,转载或内容合作请联系作者 平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。