apue 第一章 unix基礎(chǔ)知識
kernel(內(nèi)核):一種軟件粥庄,控制計算機(jī)的硬件資源狮杨,提供系統(tǒng)的運行環(huán)境
system call(系統(tǒng)調(diào)用):內(nèi)核的接口
公用函數(shù)庫和shell在系統(tǒng)調(diào)用之上
應(yīng)用程序構(gòu)建在最外層半沽,既可以使用系統(tǒng)調(diào)用,也就可以使用公用函數(shù)庫和shell
輸入和輸出
文件描述符
通常是一個非負(fù)整數(shù)吴菠,內(nèi)核用來標(biāo)識一個特定的進(jìn)程正在訪問的文件者填,在讀寫文件都會使用文件描述符,open或者create一個文件會返回文件描述符
standard input standard output standard error
但shell允許一個新程序時橄务,默認(rèn)打開三個文件描述符幔托,標(biāo)準(zhǔn)輸入穴亏,標(biāo)準(zhǔn)輸出蜂挪,標(biāo)準(zhǔn)錯誤,不特殊指明都指向終端嗓化,但可以重定向
#標(biāo)準(zhǔn)輸出被重定向到文件
ls > file.list
不帶緩沖的IO
從標(biāo)準(zhǔn)輸入STDIN_FILENO(終端)中讀取數(shù)據(jù)到buf中
從buf中寫東西到標(biāo)準(zhǔn)輸出STDOUT_FILENO(終端)中
BUFFZISE一次能讀取的最多字節(jié)
編譯以后執(zhí)行 ./a.out < infile > outfile
棠涮,實現(xiàn)文件復(fù)制
#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);
}
標(biāo)準(zhǔn)IO
使用標(biāo)準(zhǔn)io函數(shù)無需選擇緩沖區(qū)大小
get一次讀取一個字符,讀完顯示EOF
stdin和stdout表示標(biāo)準(zhǔn)輸入和輸出
#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);
}
程序和進(jìn)程
程序 program:一個可執(zhí)行文件刺覆,內(nèi)核將程序讀入內(nèi)存严肪,用以執(zhí)行
進(jìn)程 process:程序執(zhí)行的實例,每個進(jìn)程都要一個唯一的 進(jìn)程ID
進(jìn)程控制
啟動程序谦屑,獲取輸入后驳糯,在父進(jìn)程中創(chuàng)建出子進(jìn)程,在子進(jìn)程中執(zhí)行命令
父進(jìn)程等待子進(jìn)程終止
fork對子進(jìn)程返回0對父進(jìn)程返回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;
//創(chuàng)建子進(jìn)程
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
//pid = 0 創(chuàng)建子進(jìn)程成功
else if (pid == 0)
{
//執(zhí)行程序
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
一個進(jìn)程內(nèi)的所有線程共享進(jìn)程相關(guān)屬性
進(jìn)程也有id氢橙,但只在進(jìn)程內(nèi)有意義
用戶標(biāo)識
每個用戶一個 用戶id 酝枢,不能更改,id為0的為root用戶
組id 允許各個成員之間共享資源
使用username更加方便悍手,在口令文件里包含了這種映射關(guān)系 ls -l
可以查看
用戶還有 附屬組id
信號signal
用于通知進(jìn)程系統(tǒng)發(fā)生了某種錯誤系統(tǒng)的處理方式:忽略信號帘睦,系統(tǒng)默認(rèn)處理,捕捉自己處理
系統(tǒng)產(chǎn)生了SIGINT (ctrl+D)信號坦康,會在signal中捕捉到竣付,然后調(diào)用sin_int函數(shù)處理
#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");
//創(chuàng)建子進(jìn)程
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
//pid = 0 創(chuàng)建子進(jìn)程成功
else if (pid == 0)
{
//執(zhí)行程序
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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者