這里寫自定義目錄標題
- 選擇
- 填空
- 編程題
選擇
1.linux 中把磁區和目錄對應的程序叫做( ), 掛載
2.信號是在軟體層次上對()機制的一種模擬, 是一種異步通信方式,
中斷
3.用 GCC 編譯程序可以被細分為四個階段:( )
> 預處理,編譯,匯編,連接
4.編譯有執行緒的檔案要加( )引數,
-lpthread
5.父行程等待子行程的結束,可以使用的函式是( )和( ),
wait( ) waitpid( )
6.linux 主要有兩個信號安裝函式,分別是
signal( ) , sigaction( )
7.Linux 作業系統內核由( )撰寫完成,
C 和匯編
8.目錄( )下存放 linux 作業系統啟動時所要用到的程式
/boot
9.Linux 中采用“一對一”的執行緒機制,也就是一個用戶執行緒對應一個( ), 內核執行緒
10.vim 三種模式:在命令模式下按下( )就進入了底線命令模式,
:
11.linux 檔案系統由四部分組成,( )用于存放檔案的控制資訊,
超級塊
12.執行緒本身呼叫( )函式可以退出執行緒,
> pthread_exit()
13.向訊息佇列發送訊息的函式是( ),
msgrcv()
14.( )系統呼叫可以根據檔案描述符來操作檔案特性,
stat()
15.Valgrind 包括很多工具,( )是 valgrind 應用最廣泛的工具,一個重量級的記憶體檢查器,能 夠發現開發中絕大多數記憶體錯誤使用情況,( )是主要用來檢查程式中快取使用出現的問題,
Memcheck,Cachegrind
16.信號發送函式中,( )用于設定定時器,當計時時間到達時,向行程發送 SIGALRM 信號,
setitimer()
加粗樣式17.當一個執行緒的屬性設定為( ),該執行緒結束時立即釋放它所占有的系統資源,
分離狀態
18.以下哪種方式屬于例外終止一個行程(D)
D.接到一個信號并終止,
19.下列命令哪個是創建執行緒私有資料命令(A)
A.pthread_key_create()
20.下面哪種通信方式適用于不同機器之間的行程通信,
(D ) 套接字
21.創建或打開訊息佇列的函式為(A )
msgget()
22.linux 中通過呼叫 waitpid()函式得到行程的退出資訊,該函式原型為 pid_t waitpid(pit_t pid, int *statloc, int options);當第一個引數 pid 取值為-1 時,表示(A) A 等待任一子行程退出,相當于
wait(),
23.Linux 環境中使用 kill 函式向行程或行程組發送信號,Kill 函式原型為 int kill(pid_t pid, int signo);當第一個引數 pid>0 時,表示( A )
A 發送信號給行程 ID 為 pid 的行程;
24.共享主存基本操作( A )將共享主存區映射到行程虛擬地址空間,
A shmat()
25.修改訊息佇列狀態資訊的命令是(B)
B msgctl()
26.使用 gdb 除錯程式時,next 和 step 命令的作用?( )
> 統呼叫的函式原型 next:單步運行,不進入函式內部; setp:單步運行,進入函式內部
27.Linux 系統的設備檔案分為三類?( )
> 字符設備檔案、塊設備檔案和網路設備檔案
28.標準 I/O 提供了三種型別的緩沖,分別是?( )
> 全緩沖,行緩沖,不帶緩沖
- 一個完整的信號生命周期包含 4 個重要的事件,這 4 個重要事件分別是?( )
> 信號誕生 信號在行程中注冊 信號在行程中注銷 信號處理函式執行完畢
30.互斥鎖只有兩種狀態,即?( ) 開鎖和上鎖
31.在標準 IO 庫中,rewind 函式作用?( ) 將檔案流指標指向檔案起始位置
32.c 語言中沒有明確給定初值的全域變數和靜態變數存放在哪兒?( )
> 未初始化資料區
33.函式 geteuid()用于得到行程的?( )
> 用戶有效 UID
34.一個行程是 ( )
> C、PCB 結構與程式和資料的組合,
35.一個行程呼叫 wait 或 waitpid 函式,可能產生 3 中情況,下列不屬于這 3 種 情況的是 ( ), 、
> D、如果該行程沒有子行程,立即回傳,回傳值為 0,
36.回傳呼叫行程的行程標識號的系統函式是 ( ),
A、 getpid
37.程式和行程是兩個不同的概念,以下不能描述這個觀點的是 ( ),
B、同一個程式運行 10 次,產生的是同一個行程
38.fork 函式在父行程中的回傳值是 ( ),
A、創建的子行程的行程標識號
39.在 Linux 中,下列不屬于正常結束行程的方法是 ( ),
D、呼叫 abort 函式,
40.以下程式的輸出結果是( )
******B、===================start of file
—parent my child is 4759,my pid is 4758,myparent pid is 3700
—child,my pid is 4759,my parent pid is 4758
===================end of file
===================end of file
41.以下程式輸出結果是( )
C、parent, var = 288 child, var = 100
------------finish---------------
------------finish---------------******
42.以下程式輸出結果是( )
**A、child, p = 7000, var = 1000 parent, p = 0, var = 100
43.哪種行程之間的通信,資料不可以重復讀( ) B、fifo C、管道
44.下述是 Linux 下多執行緒編程常用的 pthread 庫提供的函式名和意義,說法不正確的是?( )
D、pthread_exit 殺死一個執行緒
45.對執行緒函式來說,說法正確的是( ):
> A、pthread_create 中引數 arg 是傳遞給 start_routine 函式的引數
46.對互斥鎖及條件變數說法不正確的是( ):
> D、在使用條件變數時,互斥鎖會失去作用,所以是否有互斥鎖關系不重要
47.以下說明正確的是( ):
> B、執行緒是一個獨立的指令流,是在行程中被創建的,隨行程的關閉而關閉
48.執行緒 A SendMessage 給執行緒 B,執行緒 B 處理該訊息時又 SendMessage 給執行緒 A,會出現 ( ):
> B、繼承執行
48.linux 檔案系統通常由四部分組成:引導塊,超級塊,索引節點和( ), B、資料塊
49.任何行程在運行時默認打開的三個流物件,都有相應的檔案描述符,標準檔案描述符定 義標準輸入設備的值為( ), A、0
50.系統呼叫的函式原型 int open(const char *pathname, int flags),flag 值中( )表示以讀寫的 方式打開檔案
, *C、O_RDWR 51 系統呼叫的函式原型 int open(const char pathname, int flags),flag 值中( )表示若檔案 存在且為只讀或只寫成功打開,則將長度截為 0, B、O_TRUNC 52此行代碼
length=lseek(fd,0,SEEK_END);中的 length 的值表示( ),
A、檔案 lseek.txt 的大小
53.使用下列函式呼叫 int fcntl(int fd, int cmd);來實作檔案描述符的復制,cmd 引數應使用哪 一個?
A、F_DUPFD
54.閱讀程式填空:如果想要獲取檔案的大小,空白處應該填寫以下哪個答案, A、 statBuf.st_size
55.在創建檔案和目錄時候,有默認權限,如果 umask 值為 0022,則檔案的默認權限為( ) B、0644
56.執行程式,代碼輸出結果為( ), A.link num:2
57.獲取目錄的系統呼叫函式為( ) , B、getcwd()
填空
- gcc 編譯程序:預處理、編譯、匯編、連接, Jdb:b:設定斷點;r:執行程式;c:執行下面的程式 Vim:nyy:賦值 n 行;ndd:洗掉 n 行;p 或 P:粘貼在游標所在行的下一行或者上一 行,
- 行程初始化資料區:全域初始化資料區/靜態資料區, 行程的運行環境:代碼區、初始化資料區、未初始化資料區、堆區、 堆疊區
- 行程的堆區:用于動態記憶體分配,一般由程式員分配和釋放,若程式員不釋放,程式結 束時由 OS 回收,
- Linux 檔案系統中的塊有(根據塊使用的不同):引導塊、超級塊、Inode 塊、資料塊, 控制資訊、檔案基本屬性等是 Inode 節點,為檔案系統索引,存放資料的為資料塊,超 級塊中含有檔案系統的基本資訊,如塊大小、指向空間 inode 和資料塊的指標等相關信 息,
- 行程運行時默認打開的標準物件:標準輸入設備 stdin、標準輸出設備 stdout、標準錯誤 輸出設備 stderr,
- 軟連接和硬鏈接的特點:硬鏈接和原來的檔案公用一個 Inode 節點,相當于加了個相同 的檔案,只是改了改名字, 硬鏈接創建函式:link 洗掉硬鏈接函式:unlink 軟連接創建函式:symlink
讀取軟連接中的內容所用的函式:readlink(檔案名,讀取到的空間,讀取的大小),
- 修改文
件權限命令:chmod、chgrp、chown,
- 檔案描述符為整形:對于用戶空間來說,任何打開的檔案都將分配一個唯一非負整數, 用于標識該打開檔案,該值即檔案描述符,為一個大于等于 0 的整數,
- 權限的值,讀:4 寫:2 執行:1 注意:前三位為用戶的權限,再三位為所屬組的權 限,再三位為其他用戶的權限,
- 獲取檔案屬性,是否穿透,stat 函式具有穿透能力,能夠穿透鏈接檔案;而 lstat 函式不 具有穿透能力,不穿透鏈接檔案,檔案型別還為鏈接檔案,
- opendir 函式:打開一個目錄,回傳一個目錄流指標,引數為欲打開目錄的名稱(路徑), mkdir 函式:第一個引數時欲創建的目錄檔案路徑,第二個引數時創建目錄的權限,
- wait 函式:呼叫 wait 函式的父親行程將堵塞式等待改進的任意一個子行程結束后,回收 該子行程的內核行程資源,引數用來接受子行程退出狀態,回傳值為子行程的 PID, waitpid 函式,用來等待指定子行程結束,回傳值為子行程的 PID, 第一個引數,大于 0 時,表示等待行程 PID 為該 PID 值的行程結束; 等于 0 時,表示等待與當前行程的行程組 PGID 一致的行程結束; 等于-1 時,與 wait 函式相同; 小于-1 時,表示等待行程組 PGID 是此值的絕對值的行程結束, 第二個引數用來接受等待行程的結束狀態,第三個引數一般設定為,
- fork 函式:創建行程,
- 行程的行程塊沒有被釋放,稱為行程的僵死狀態,
- Fcntl 函式:修改某個檔案描述符的特殊屬性
- 創建硬鏈接的系統呼叫:link()
- 安裝信號處理函式:signal 和 sigaction,
- 信號的生命周期:在目的行程中安裝信號;行程產生信號;信號在目的行程中被注冊(目的行程收到信號); 信號在行程中被注銷(在執行相應處理函式之前);信號生命終止(執行完回應處理函式后 再恢復到被中斷的位置繼續執行),
- 訊息佇列中發送資訊的函式:msgsnd
- 信號是在軟體層次上對中斷機制的一種模擬,是一種異步通信方式,
- 接受訊息佇列的函式 msgrcv
- alarm()設定定時器
- 信號分為可靠信號和不可靠信號,
- 管道分為有名管道和匿名管道,
- 互斥鎖的兩種狀態:上鎖和解鎖,
- 自己退出執行緒的函式:pthread_exit() 被取消執行緒運行:pthread_cancle()
- 互斥鎖、條件變數、讀寫鎖的初始化 互斥鎖的初始化:pthread_mutex_init() 條件變數的初始化:pthread_cond_init() 讀寫鎖的初始化:pthread_rwlock_init()
- 讀寫鎖:非阻塞的加鎖解鎖和阻塞的加鎖解鎖, 堵塞的方式申請讀鎖:pthread_rwlock_rdlock() 非堵塞的方式申請讀鎖:pthread_rwlock_tryrdlock() 堵塞方式申請寫鎖:pthread_rwlock_wrlock()
非堵塞方式申請寫鎖:pthread_rwlock_trywrlock()
- 執行緒編譯的時候加上-lpthread,
- 回收執行緒的函式:pthread_join()
- 互斥鎖的上鎖函式是 pthread_mutex_lock(),非阻塞加 try; 解鎖函式是 pthread_mutex_unlock(),
- 面向連接的編程:TCP;面向無連接的是:UDP
- 在不同主機間進行通訊的是:scoket 或者是套接字
- TCP 和 UDP 都是傳輸層的協議,
- 三次握手:連接建立、資料傳輸、連接釋放,
- 創建套接字的函式 scoket()
- 網路編程的常見函式
監聽網路函式:listen() 發起連接函式:connect()
接受連接函式:accept() 讀寫 scoket 物件:read()/write()
TCP 發送和接受資料:send()/recv() 關閉 scoket物件:close()
編程題
1.創建檔案 file1,寫入字串“abcdefghijklmn”; 創建檔案 file2,寫入字串“ABCDEFGHIJKLMN”; 讀取file1中的內容,寫入file2,使file2中的字串內容為“abcdefghijklmn ABCDEFGHIJKLMN”
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
int fd1,fd2;
char str[14];
fd1 = open("file1",O_RDWR|O_CREAT,S_IRWXU);
if(fd1 < 0)
perror("open");
write(fd1,"abcdefghijklmn",14);
lseek(fd1,0,SEEK_SET);
fd2 = open("file2",O_RDWR|O_CREAT,S_IRWXU);
if(fd2 < 0)
perror("open");
lseek(fd2,14,SEEK_END);
write(fd2,"ABCDEFGHIJKLMN",14);
read(fd1,str,14);
lseek(fd2,0,SEEK_SET);
write(fd2,str,14);
close(fd1);
close(fd2);
system("cat file2");
printf("\n");
return 0; }
2.創建新檔案,該檔案具有用戶讀寫權限, 采用 dup/dup2/fcntl 復制一個新的檔案描述符,通過新檔案描述符向檔案寫入“class_name” 字串;通過原有的檔案描述符讀取檔案中的內容,并且列印顯示
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int fd,fd1;
char *str = "class_name";
fd = open("file",O_CREAT|O_RDWR);
if(fd < 0)
perror("open");
fd1 = dup(fd);
if(fd1 < 0)
perror("dup");
write(fd1,str,strlen(str));
lseek(fd,0,SEEK_SET);
char buf[12];
read(fd,buf,12);
printf("The buf is:%s\n",buf);
close(fd);
close(fd1);
return 0; }
3.遞回遍歷/home 目錄,列印出所有檔案和子目錄名稱及節點號, 判斷檔案型別,如果是子目錄,繼續進行遞回遍歷,直到遍歷完所有子目錄為止,
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
void show(char *path)
{
DIR *dir;
char str[128];
struct dirent *dirp;
struct stat statbuf;
dir = opendir(path);
if(dir)
{
while((dirp = readdir(dir)) != NULL) {
sprintf(str,"%s/%s",path,dirp->d_name);
if(lstat(str,&statbuf) < 0)
perror("lstat");
if(dirp->d_name[0] == '.')
continue;
if(S_ISDIR(statbuf.st_mode))
{
show(str);
printf("The dirent's name is: %s\n",dirp->d_name);
printf("The dirent's inode is: %d\n",dirp->d_ino);
}
else
{
printf("The file's name is: %s\n",dirp->d_name);
printf("The file's inode is: %d\n",dirp->d_ino);
} } }
else
perror("opendir");
closedir(dir);
}
int main()
{
show("/home");
return 0; }
4.列印字串“hello world!” 在列印字串“hello world!”前呼叫三次 fork,分析列印結果,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello world!!!\n");
return 0 }
5.創建子行程 在子行程中打開檔案 file1,寫入自己的“班級_姓名_學號”, 父行程讀取 file1 中的內容,并且列印顯示, 在父行程中獲取已經結束的子行程的狀態資訊,列印該資訊,并且列印結束的子行程的行程 號,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
int main()
{
int fd,pid;
fd = open("file",O_CREAT|O_RDWR,S_IRWXU);
if(fd< 0)
perror("open");
pid = fork();
if(pid == 0)
{
printf("This is the child!\n");
char str[128] = "移動二班_段振威_1915925158";
if(write(fd,str,128) < 0)
perror("write");
exit(5);
}
else
{
printf("This is the father!\n");
char buf[128];
int n,status;
if(read(fd,buf,128) < 0)
perror("read");
printf("The buf is: %s\n",buf);
if(wait(&status) < 0)
perror("perror");
if(WIFEXITED(status))
n = WEXITSTATUS(status);
else
printf("wait error!\n");
printf("The child's pid is: %d\n",pid);
printf("The child exit status is: %d\n",n);
}
return 0;
}
6.在父行程中定義變數 n,在子行程中對變數 n 進行++操作;并且列印變數 n 的值,列印子 行程 pid; 在父行程中列印變數 n 的值,并且列印父行程 pid, 要求分別用 fork 和 vfork 創建子行程,
//------fork
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int n = 1;
if(fork() == 0)
{
printf("This is child,the pid is%d\n",getpid());
printf("The n is: %d\n",++n);
}
else
{
printf("This is father,the pid is%d\n",getpid());
printf("The n is: %d\n",n);
}
return 0;
}//-----vfork
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int n = 1;
pid_t pid;
pid = vfork();
if(pid < 0)
perror("vfork");
else if(pid == 0)
{
printf("This is child,the child's pid is: %d\n",getpid());
printf("The n is: %d\n",++n);
exit(0);
}
else
{
printf("This is father,the father's pid is: %d\n",getpid());
printf("The n is: %d\n",n);
}
return 0;
}
7.利用匿名管道實作父子行程間通信,要求 父行程發送字串“hello child”給子行程; 子行程收到父行程發送的資料后,給父行程回復“hello farther”; 父子行程通信完畢,父行程依次列印子行程的退出狀態以及子行程的
pid,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int fd1[2],fd2[2];
pipe(fd1);
pipe(fd2);
int pid;
pid = fork();
if(pid < 0)
perror("fork");
else if(pid == 0)
{
close(fd1[0]);
close(fd2[1]);
char str[12];
printf("This is the child!\n");
if(read(fd2[0],str,12) > 0)
{
printf("Received the news: %s\n",str);
if(write(fd1[1],"hello father",12) < 0)
perror("write");
}
else
perror("read");
exit(5);
}
else
{
int status;
printf("This is the father!\n");
close(fd1[1]);
close(fd2[0]);
char buf[24] = "hello child";
if(write(fd2[1],buf,12) < 0)
perror("write");
else
{
printf("Send news successful!\n");
}
wait(&status);
if(WIFEXITED(status))
{
printf("The child's pid is: %d\n",pid);
printf("The child's exited status is: %d\n",WEXITSTATUS(status));
} }
return 0;
}
- 利用匿名管道實作兄弟行程間通信,要求 兄行程發送字串“This is elder brother ,pid is (兄行程行程號)”給第行程; 第行程收到兄行程發送的資料后,給兄行程回復“This is younger brother ,pid
is(第行程進 程號)”;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main()
{
int fd1[2],fd2[2];
pipe(fd1);
pipe(fd2);
int pid;
pid = fork();
if(pid == 0)
{
printf("This is the elder brother!\n");
printf("The elder's father's pid is: %d\n",getppid());
close(fd1[1]);
close(fd2[0]);
char str1[64],str2[64];
sprintf(str1,"This is the elder brother,pid is %d",getpid());
if(write(fd2[1],str1,64) < 0)
perror("write");
if(read(fd1[0],str2,64) < 0)
perror("read");
else
printf("The news from younger is: %s\n",str2);
}
else
{
if(fork() == 0)
{
printf("This is the younger brother!\n");
printf("The younger's father's pid is: %d\n",getppid());
close(fd1[0]);
close(fd2[1]);
char buf1[64],buf2[64];
if(read(fd2[0],buf1,64) > 0)
{
printf("The news form elder is: %s\n",buf1);
sprintf(buf2,"This is the younger brother,pid is %d",getpid());
if(write(fd1[1],buf2,64) < 0)
perror("write");
}
else
perror("read");
} } }
- 利用有名管道檔案實作行程間通信,要求 寫行程向有名管道檔案寫入 10 次“hello world”; 讀行程讀取有名管道檔案中的內容,并依次列印
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int main()
{
int pid,fd;
if(mkfifo("fifotest",0666) < 0)
perror("mkfifo");
pid = fork();
if(pid < 0)
perror("fork");
else if(pid == 0)
{
printf("This is the write process!\n");
int fd = open("fifotest",0666);
for(int i = 0; i < 10;i++)
{
if(write(fd,"hello world",12) < 0)
perror("write");
sleep(1);
}
close(fd);
}
else
{
char str[128];
printf("This is the read process!\n");
int fd1 = open("fifotest",0666);
for(int i = 0;i < 10;i++)
{
if(read(fd1,str,128) < 0)
perror("read");
else
printf("%s\n",str);
}
system("rm -f fifotest");
} }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/389152.html
標籤:其他
