操作系统导论期末复习题
进程
1、When a process makes a system call to transmit a TCP packet over the network, which of the following steps do NOT occur always?
A、The process moves to kernel mode.
B、The program counter of the CPU shifts to the kernel part of the address space.
C、The process is context-switched out and a separate kernel process starts execution.
D、The OS code that deals with handling TCP/IP packets is invoked.
正确答案: C
2、Which of the following C library functions do NOT directly correspond to (similarly named) system calls? That is, the implentations of which of these C library functions are NOT straightforward invocations of the underlying system call?
A、system, which executes a bash shell command.
B、fork, which creates a new child process.
C、exit, which terminates the current process.
D、strlen, which returns the length of a string.
正确答案: AD
3、Consider a parent process that has forked a child in the code snippet below.
int count = 0;
ret = fork();
if(ret == 0) {
printf(“count in child=%d\n”, count);
} else {
count = 1;
}
The parent executes the statement ”count = 1” before the child executes for the first time. Now, what is the value of count printed by the code above? Assume that the OS implements a regular fork (not a copy-on-write fork).
正确答案:
第一空: 0
fork一次调用,两次返回,在子进程中,返回;在父进程中返回新创建的子进程ID;如果出现错误,返回一个负值。这两个进程的变量都是独立的,存在不同的地址中,不是共用的。
4、Consider a parent process P that has forked a child process C in the program below.
int a = 5;
int fd = open(…) //opening a file
int ret = fork();
if(ret > 0) {
close(fd);
a = 6;
…
} else if (ret == 0) {
printf(“a=%d\n”, a);
read(fd, something);
}
After the new process is forked, suppose that the parent process is scheduled first, before the child process. Once the parent resumes after fork, it closes the file descriptor and changes the value of a variable as shown above. Assume that the child process is scheduled for the first time only after the parent completes these two changes.
(a)____ is the value of the variable a as printed in the child process, when it is scheduled next.
(b) Will the attempt to read from the file descriptor succeed in the child? Yes or No
正确答案:
第一空: 5
第二空: Yes
5、Can two processes be concurrently executing the same program executable?
正确答案: √
6、Can two running processes share the complete process image in physical memory (not just parts of it)?
正确答案: ×
7、A process in user mode cannot execute certain privileged hardware instructions.
正确答案: √
8、A context switch can occur only after processing a timer interrupt, but not after any other system call or interrupt.
正确答案: ×
上下文切换(有时也称做进程切换或任务切换)是指 CPU 从一个进程或线程切换到另一个进程或线程。上下文切换只能发生在内核态中。内核态是CPU的一种有特权的模式,在这种模式下只有内核运行并且可以访问所有内存和其他系统资源。其他的程序,如应用程序,在最开始都是运行在用户态,但是他们能通过系统调用来运行部分内核的代码。
上下文切换在多任务操作系统中是一个必须的特性。多任务操作系统是指多个进程运行在一个 CPU 中互不打扰,看起来像同时运行一样。这个并行的错觉是由于上下文在高速的切换(每秒几十上百次)。当某一进程自愿放弃它的 CPU 时间或者系统分配的时间片用完时,就会发生上下文切换。
上下文切换有时也因硬件中断而触发。硬件中断是指硬件设备(如键盘、鼠标、调试解调器、系统时钟)给内核发送的一个信号,该信号表示一个事件(如按键、鼠标移动、从网络连接接收到数据)发生了。
9、A process undergoes a context switch every time it enters kernel mode from user mode.
正确答案: ×
当系统调