linux 學習第二週小結

2021-04-27 04:43:51 字數 3976 閱讀 8169

本週學習概況

● 系統呼叫

● 標準i/o庫

● 程序控制

● 程序間通訊:管道、命名管道、訊息佇列

● 訊號的基本操作

● 執行緒的基本操作

具體內容如下:

● 系統呼叫

基本命令

#who

作用: 顯示登陸使用者名稱,終端名,登陸時間

#open

作用 : 開啟乙個檔案

標頭檔案 #include

原型 int open(char *name, int how)

#close

作用:關閉乙個檔案

標頭檔案: #include

原型: int close(int fd)

#read

把指定數目的資料讀到緩衝區

#include

ssize_t read(int fd, void *buf, size_t count)

● 標準i/o庫

以下的庫函式均為#include

開啟和關閉流

file* fopen(const char *path, const char *mode);

int fclose(file *stream);

每次乙個字元的i/o

int getc(file *fp)

int fgetc(file *fp)

int getchar(void)

int putc(int c, file *fp)

int fputc(int c, file *fp)

int putchar(int c)

每次一行的i/o

char *fgets(char *buf, int n, file *fp)

char *gets(char *buf)

int fputs(const char *str, file *fp)

int puts(const char *str)

資料塊i/o

size_t fread(void *ptr, size_t size, size_t nobj, file *fp)

size_t fwrite(const void *ptr, size_t size, size_t nobj, file *fp)

定位流long ftell(file *fp)

int fseek(file *fp, long offset, int whence)

void rewind(file *fp)

int fgetpos(file *fp, fpos_t *pos)

int fsetpos(file *fp, fpos_t *pos)

格式化輸出

int printf(const char *format, …)

int fprintf(file *fp, const char *format, …)

int sprintf(char *buf, const char *format, …)

int snprintf(char *buf, size_t n, const char *format,…)

格式化輸入

int scanf(const char *format,…)

int fscanf(file *fp, const char *format,…)

int sscanf(const char *buf, const char *format,…)

檔案流的檢測

int feof(file *fp)

int ferror(file *fp)

●程序控制

fork函式:建立乙個新程序

#include

pid_t fork(void)

vfork函式:建立乙個新程序

#include

pid_t vfork(void);

exec函式

int execl(const char *path, const char *arg0, … ,(char *)0 )

int execv(const char *path, char *const argv)

int execle(const char *path, const char *arg0, … ,(char *)0, char *const envp )

int execve(const char *path, char *const argv, char *const envp)

int execlp(const char *filename, const char *arg0, …, (char *)0 )

int execvp(const char *filename, char *const argv)

wait和waitpid函式

#include

pid_t wait(int *status)

pid_t waitpid(pid_t pid, int *status, int options)

exit與_exit函式

#include

void exit(int status)

void _exit(int status)

sleep函式

unsigned int sleep(unsigned int sec)

●程序間通訊

pipe管道的建立

#include

int pipe(int filedes[2]);

fifo有名管道的建立

#include

#include

int mkfifo(const char *pathname, mode_t mode)

訊息佇列的建立、傳送、接收、控制訊息

int msgget(key_t key, int flag);

key要通過函式ftok來生成唯一的值.

key_t ftok(char *pathname, int projid);

int msgsnd(int msqid, const void *ptr, size_t nbytes, int flag);

struct msgbuf;

execv("/bin/ls",argv);

4. 寫乙個簡單的shell指令碼,並用execl呼叫。

5. 練習使用wait和waitpid函式,確定主程序在子程序之後退出;

6. 在主程序中建立2個子程序,第乙個子程序等待2秒,第二個子程序等待3秒,主程序中用waitpid分別等待兩個子程序退出,輸出一些提示資訊觀察結果。

7. 區別exit(0)和_exit(0)觀察下面兩段程式的執行結果:

int main()

{ printf("this is a test/n")

printf("test exit fun");

exit(0);

int main()

{ printf("this is a test/n")

printf("test exit fun");

_exit(0);

程序間通訊

1.用pipe實現「ls –al | more」。涉及的知識點:pipe、dup2、fork。程式名dup.c

2 .fifo 實現本機上的兩終端的聊天通訊。涉及的知識點:mkfiof、 fork 、open、 close、 read 、write、fgets。程式名 chat1.c

chat2.c

3.訊息佇列實現本機上的兩終端的聊天通訊。涉及的知識點:msgget msgsnd msgrcv msgctl fork fgets.程式名msg_chat.c

訊號的練習:

練習:fork建立2個程序,父程序用系統呼叫kill()向2個子程序分別傳送16和17軟中斷訊號,在子程序中用signal函式設定訊號處理函式,pause等待到訊號後dosomething結束。程式名:signal_ex.c。

執行緒的練習:

倉庫,生產者,消費者的練習.涉及的知識點:互斥鎖、pv操作。

程式名:semmphore.c。

Linux 學習第二週

第二週的學習就有些進展了,因為在這段時間裡,我開始接觸並使用centos了!剛剛開啟文字介面的時候,我的內心可是異常的興奮啊!那個時候我才感覺到 書上的東西是有用的 原諒我以前認為看書沒什麼太大意義 當時我馬上翻開書,照葫蘆畫瓢 就開始寫命令,玩的不亦樂乎。現在我已經基本了解了檔案的一些基本屬性和許...

原生JS學習第二週(小結)

立即執行函式 function 形參 實參 閉包 函式在子作用域被return 到外作用域導致的記憶體洩漏問題 物件 表示式 可直接新增屬性,如text.name等 包裝類就是在給變數 無屬性 賦屬性的時候會new乙個新變數對其賦值,然後在delete,無法對原值進行操作 原型 物件.prototy...

第二週工作小結

本週點評作業鏈結 本週點評作業數量 18 本週作業提交數量 57.本週未提交數量 12.作業平均分 11.3 作業情況 大多數同學完成質量不錯,部分同學作業問題回答不全面,有糊弄的情況。4.本週主要工作內容 1 給同學的作業評分,點評並給出相對的建議。2 邀請未進入班級部落格的同學進入班級部落格 任...