A: printf(\
B: exit(0); 列出所有可能的输出结果:
(2分)4个,对1~3个给1分,全对给2分
CFBF FCBF FBCF FBFC Part II
请阅读以下程序,然后回答问题(假设程序中的函数调用都可以正确执行,且每条语句都是原子动作):
pid_t pid;
int even = 0;
int counter1 = 0; int counter2 = 1;
void handler1(int sig) { if (even % 2 == 0) {
printf(\ counter1 = A ; } else {
printf(\ counter2 = B ; } even = even+ C ; }
void handler2(int sig) { if (_____D_____) { counter1 = even*even; } else { counter2 = even*even; } }
int main() {
signal(SIGUSR1, handler1); signal(SIGUSR2, handler2); if ((pid = fork()) == 0) {
while (1) {};
} while (even < 30) { kill(pid, E ); sleep(1);
16
kill(pid, F ); sleep(1); even = even+ G ; }
kill(pid, SIGKILL); exit(0);
}
(1)完成程序,使得程序在输出的数字为以下Q队列的前30项,Q队列定义如下:
??+1, n%2=0
??0=0, ??1=1, ????+2={?? (??=0,1,2,3,...)
????×2, n%2≠0
(若某个位置中的程序内容,对本次程序执行结果没有影响,请在相应位置填写“无关”)
(7分)7个,对1个给1分 A: counter1+1 B: counter2*2 C: 1
D: 无关 E: SIGUSR1 F: SIGUSR1 G: 2
17
得分 第六题(12分)
程序i16.c如下:
#include
#include
int main(int argc, char **argv) { int fd1, fd2; char *cs; struct stat stat; int sz, i; fd1 = Open(argv[1], O_RDWR, S_IRUSR | S_IWUSR); Fstat(fd1, &stat); sz = stat.st_size;
fd2 = Open(argv[2], O_RDWR | O_TRUNC | O_CREAT, \\
S_IRUSR | S_IWUSR);
cs = (char *)Mmap(NULL, sz, PROT_READ | PROT_WRITE, \\
X , fd1, 0);
if (Fork()) { Wait(&i); Write(fd2, cs, sz); } else { for (i = 0; i < sz; i++) { if (islower(cs[i])) cs[i] = (char) toupper(cs[i]); else if (isupper(cs[i])) cs[i] = (char) tolower(cs[i]); }
Y ;
} Munmap(cs, sz); Close(fd1); Close(fd2);
18
}
1、假定t1内容为ICSexam,根据X/Y处的内容,判断执行./i16 t1 t2之后的结果:(均为单选题)
1.1、X为MAP_PRIVATE,Y为空时,结果为( ) A. t1为ICSexam,t2为ICSexam B. t1为ICSexam,t2为icsEXAM C. t1为icsEXAM,t2为icsEXAM D. t1为icsEXAM,t2为ICSexam
1.2、X为MAP_SHARED时,Y为空时,结果为( ) A. t1为ICSexam,t2为ICSexam B. t1为ICSexam,t2为icsEXAM C. t1为icsEXAM,t2为icsEXAM D. t1为icsEXAM,t2为ICSexam
1.3、X处的内容为MAP_PRIVATE,Y为Write(fd2, cs, sz)时,t2的结果为( )
A. t2为ICSexam B. t2为icsEXAM
C. t2为icsEXAMICSexam D. t2为icsEXAMicsEXAM
1.4、X处的内容为MAP_SHARED,Y为Write(fd2, cs, sz)时,t2的结果为( )
A. t2为ICSexam B. t2为icsEXAM
C. t2为icsEXAMICSexam D. t2为icsEXAMicsEXAM 答案: 1.1:A 1.2:C 1.3:C 1.4:D
19
2、X为MAP_SHARED,当子进程运行到Y处时,内存映射的内容如下:(只保留了/proc/pid/maps中的三部分内容:分别为address/perms/pathname)
00400000-00405000 r-xp /home/tao/i16/i16 00604000-00605000 r--p /home/tao/i16/i16 00605000-00606000 A /home/tao/i16/i16
7f5df1e61000-7f5df2020000 r-xp /lib/x86_64-linux-gnu/libc-2.23.so 7f5df2020000-7f5df2220000 ---p /lib/x86_64-linux-gnu/libc-2.23.so 7f5df2220000-7f5df2224000 r--p /lib/x86_64-linux-gnu/libc-2.23.so 7f5df2224000-7f5df2226000 rw-p /lib/x86_64-linux-gnu/libc-2.23.so 7f5df2226000-7f5df222a000 rw-p
7f5df222a000-7f5df2250000 r-xp /lib/x86_64-linux-gnu/ B 7f5df243f000-7f5df2442000 rw-p
7f5df244c000-7f5df244d000 rw-s C 7f5df244d000-7f5df244f000 rw-p
7f5df244f000-7f5df2450000 r--p /lib/x86_64-linux-gnu/ B 7f5df2450000-7f5df2451000 rw-p /lib/x86_64-linux-gnu/ B 7f5df2451000-7f5df2452000 rw-p
7ffd4e35f000-7ffd4e380000 rw-p [ D ] 7ffd4e3b0000-7ffd4e3b2000 r--p [vvar] 7ffd4e3b2000-7ffd4e3b4000 r-xp [vdso]
ffffffffff600000-ffffffffff601000 r-xp [vsyscall]
根据程序执行情况,填充空白处: A: B: C: D:
答案: A:rw-p
B:ld-2.23.so 或 ld.so C:/home/tao/i16/t1 或 t1
D:stack
3、X为MAP_SHARED,当子进程运行到Y处时:(均为单选题)
3.1、关于页表的描述,正确的是:( )
20