Linux 系统编程 · 试卷题目分类汇总
按「题型 → 考点」分类,每题含 英文原题 + 中文翻译 + 参考答案 。
用法建议:可先盖住「答案」自测,再对照订正(先做后对)。共 20 题。
| 分类 | 考点 | 题数 |
|---|---|---|
| A 填空题 | A1 ls 输出解读 / A2 文件类型 / A3 chmod 权限 | 5 |
| B 单选题 | B1 fork 进程 / B2 pipe 管道 / B3 dup·dup2 | 5 |
| C 读程序题 | C1 POSIX 信号量 / C2 IPC 信号量 / C3 Socket / C4 文件操作 / C5 进程线程 | 8 |
| D 写代码题 | D1 生产者---消费者模型 | 2 |
A 类 填空题(Linux 基础命令)
A1 ls 输出解读
A1-1
- 原题:Given
-rwxr-x--- 1 root admin 2048 May 1 12:00 backup.sh, the file type is ____, the owner is ____, the size is ____ bytes, and the octal permission is ____. - 翻译:给定
-rwxr-x--- 1 root admin 2048 May 1 12:00 backup.sh,文件类型是 ____,属主是 ____,大小是 ____ 字节,八进制权限是 ____。 - 答案:普通文件(-);root;2048;rwx r-x --- = 750。
A1-2
- 原题:In the line
drwxr-xr-x 2 alice staff 4096 Jun 9 18:00 mydir, what is the link count, and what does the leading "d" indicate? - 翻译:在
drwxr-xr-x 2 alice staff 4096 Jun 9 18:00 mydir这一行中,硬链接数是多少?开头的 "d" 表示什么? - 答案:硬链接数 = 2 ;开头 "d" 表示这是一个目录(directory)。
A2 文件类型识别
A2-1
- 原题:What file type does the first character represent in each case:
-,d,l,c,b,p,s? - 翻译:第一个字符分别代表什么文件类型:
-、d、l、c、b、p、s? - 答案:- 普通文件;d 目录;l 符号链接;c 字符设备;b 块设备;p 命名管道(FIFO);s 套接字。
A3 chmod 权限
A3-1
- 原题:Write a command to set file
report: owner read/write/execute, group read/execute, others none. - 翻译:写出命令,将文件
report设为:属主读/写/执行,属组读/执行,其他无权限。 - 答案:chmod 750 report(或 chmod u=rwx,g=rx,o= report)。
A3-2
- 原题:The command
chmod 644 a.txtproduces what permission string, and what is the meaning? - 翻译:命令
chmod 644 a.txt产生什么权限字符串,含义是什么? - 答案:6=rw-,4=r--,4=r--,即 -rw-r--r--:属主可读写,属组和其他只读。
B 类 单选题(程序逻辑)
B1 fork 进程
B1-1
- 原题:After
fork(); fork(); fork(); printf("A");, how many times is "A" printed? - 翻译:执行
fork(); fork(); fork(); printf("A");后,"A" 打印多少次? - 答案:3 个 fork → 2^3 = 8 个进程,故输出 8 次。
B1-2
- 原题:What value does fork() return in the parent process and in the child process respectively? A) both 0 B) parent: child PID, child: 0 C) parent: 0, child: PID D) both -1
- 翻译:fork() 在父进程和子进程中分别返回什么值?A) 都为0 B) 父:子进程PID,子:0 C) 父:0,子:PID D) 都为-1
- 答案:B:父进程返回子进程 PID(>0),子进程返回 0,失败返回 -1。
B2 pipe 管道
B2-1
- 原题:In
int fd[2]; pipe(fd);, which is read and which is write? A) fd0 write, fd1 read B) fd0 read, fd1 write C) both read D) both write - 翻译:在
int fd[2]; pipe(fd);中,哪个读哪个写?A) fd0写,fd1读 B) fd0读,fd1写 C)都读 D)都写 - 答案:B:fd0 读端,fd1 写端。
B2-2
- 原题:After all write-ends of a pipe are closed, what does read() on the read-end return?
- 翻译:当管道所有写端都关闭后,对读端调用 read() 返回什么?
- 答案:返回 0(表示读到文件结束 EOF)。
B3 dup / dup2
B3-1
- 原题:What does
dup2(fd, STDOUT_FILENO)accomplish? A) closes fd B) redirects standard output to fd C) reads from fd D) creates a pipe - 翻译:
dup2(fd, STDOUT_FILENO)的作用?A)关闭fd B)把标准输出重定向到fd C)从fd读 D)创建管道 - 答案:B:将标准输出(1号描述符)重定向到 fd 所指文件/管道。
C 类 读程序题
C1 POSIX 信号量
C1-1
- 原题:A POSIX semaphore is initialized with
sem_init(&s, 0, 2). Three threads each callsem_wait(&s)simultaneously. How many proceed and how many block? - 翻译:POSIX 信号量用
sem_init(&s,0,2)初始化。三个线程同时调用sem_wait(&s),几个继续、几个阻塞? - 答案:初值 2 → 前 2 个继续 ,第 3 个阻塞,直到有线程 sem_post。
C1-2
- 原题:In POSIX semaphores, which function performs the P operation and which performs the V operation?
- 翻译:在 POSIX 信号量中,哪个函数执行 P 操作、哪个执行 V 操作?
- 答案:sem_wait = P 操作(值减1,可能阻塞);sem_post = V 操作(值加1,唤醒等待者)。
C2 System V IPC 信号量
C2-1
- 原题:In System V semaphores, what does a sembuf with
sem_op = -1perform, and what happens if the current value is 0? - 翻译:在 System V 信号量中,
sem_op = -1的 sembuf 执行什么?若当前值为 0 会怎样? - 答案:执行 P 操作 (值减1)。若当前值为 0 则阻塞,直到有进程做 V 操作(sem_op=+1)使值变正。
C2-2
- 原题:Which semctl command sets the initial value of a semaphore, and which removes the semaphore set?
- 翻译:哪个 semctl 命令用于设置信号量初值,哪个用于删除信号量集?
- 答案:设初值用 SETVAL ;删除信号量集用 IPC_RMID。
C3 Socket 网络编程
C3-1
- 原题:Put the TCP server calls in correct order: accept, bind, listen, socket.
- 翻译:将 TCP 服务器调用按正确顺序排列:accept、bind、listen、socket。
- 答案:socket → bind → listen → accept。
C3-2
- 原题:For SOCK_STREAM, is the protocol TCP or UDP? Does the client need bind() before connect()?
- 翻译:对 SOCK_STREAM,协议是 TCP 还是 UDP?客户端 connect() 前需要 bind() 吗?
- 答案:SOCK_STREAM = TCP ;客户端通常无需显式 bind,系统自动分配本地端口。
C4 文件操作
C4-1
- 原题:Which returns an int file descriptor and which returns a FILE pointer: open() or fopen()? Which descriptor number is standard error?
- 翻译:哪个返回 int 文件描述符、哪个返回 FILE 指针:open() 还是 fopen()?标准错误是哪个描述符号?
- 答案:open() 返回 int 描述符;fopen() 返回 FILE 指针。标准错误 stderr = 2(stdin=0,stdout=1)。
C5 进程与线程
C5-1
- 原题:True or False: threads in the same process share global variables but each has its own stack. Which call waits for a created thread to finish?
- 翻译:判断:同进程线程共享全局变量、各自独立栈。用哪个调用等待已创建线程结束?
- 答案:正确 ;用 pthread_join() 等待线程结束并回收资源。
D 类 写代码题(生产者---消费者模型)
D1 生产者---消费者
D1-1
- 原题:In the producer-consumer problem with buffer size N, what are the initial values of empty, full, and mutex? Why must P(mutex) come AFTER P(empty) in the producer?
- 翻译:缓冲区大小为 N 的生产者---消费者问题中,empty、full、mutex 初值各是多少?为什么生产者中 P(mutex) 必须放在 P(empty) 之后?
- 答案:empty=N,full=0,mutex=1。若 P(mutex) 在 P(empty) 前,缓冲区满时生产者先占锁再阻塞,消费者拿不到锁无法取走产品,造成死锁;故互斥信号量须在资源信号量内层。
D1-2
- 原题:Write the P/V sequence for the producer and the consumer (using empty, full, mutex).
- 翻译:写出生产者和消费者的 P/V 操作顺序(使用 empty、full、mutex)。
- 答案:生产者:P(empty) → P(mutex) → 放产品 → V(mutex) → V(full);消费者:P(full) → P(mutex) → 取产品 → V(mutex) → V(empty)。
本汇总按题型与考点分类整理,便于针对性练习;实际考试请以课堂讲义和真题为准。