31046040系统级编程 - A

更新时间:2024-05-24 08:31:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

311046040 系统级编程(A 闭) 一、单项选择题(本大题共 30 小题,每小题 1 分,共 30 分)

2013-2014-1 提示:在每小题列出的四个备选项中只有一个是符合题目要求的,请将其代码填写在下表中。错选、多选或 未选均无分。

1. In Visual C++, a Win32 Console Application is A.the status window of the Visual C++ environment

B.the simplest type of application Visual C++ can generate

C.a program that is able to control the operating system of a windows computer D. built by using sophisticated \

2. When using a debugger to find the cause of a program's incorrect behavior, A.it is often necessary to start the program multiple times under the debugger

B.the program is usually executed to the point at which the behavior occurs and then executed backwards to find the cause

C.the faulty code fragment must first be identified

D.it is fastest to start by stopping the debugger long before the behavior appears 3. how is -11 (decimal) represented in an 8-bit 2's complement binary format? A.10001011 B.11110100 C.11110101 D.11111011

4. What is the output of this C code? #include void main() {

int x = 4; int *p = &x; int *k = p++; int r = p - k; printf(\

} A.4

B.8 C.1 D.Run time error

5. Explain the feature of stack. A.stack cannot reuse its memory C.All operations are at one end

B.All elements are of different data types

D.Any element can be accessed from it directly

6. a jump instruction

A.changes a pointer to point to the next element of an array B.unconditionally sets the program counter to its operand C.increases the program counter

D.changes the program counter only if its operand is equal to zero

7. in c, assuming that an int takes 4 bytes, how many bytes are required to represent the following array? int a[12]; A.44 B.52 C.12 D.48 8. given the following declaration and initialization of s, what is the value of the expression s[6]? char s[] = \分) A.'g' B.'\\0' C.'\\n' D.an unpredictable value

1 / 8

311046040 系统级编程(A 闭)

9. activation records are organized in stacks because?

2013-2014-1

2 / 8

311046040 系统级编程(A 闭) A.stacks allow activation records to be pushed and popped in any order. B.stacks are simple enough for the hardware to manage.

C.functions need to access all the variables of the functions that call them. D.they are seldm needed during program execution.

2013-2014-1

10. when executing a function callee(), which of the following are true regarding the value of the frame pointer? i. it marks the top of the stack frame of the function that invoked callee(). ii.it marks the bottom of the stack frame of callee() iii.it is the top of the stack. A.iii only B.ii only C.i only D.i and ii only

11. consider the malloc() function. which one of the following sentences is correct? A.the malloc() returns the amount of memory allocated

B.the malloc() allocates the desired amount of memory on the stack C.the malloc() allocates the desired amount of memory on the heap D.the allocated memory is only local to the function

12. which statement is true?

A.Using the ?rst-?t algorithm on a free list that is ordered according to decreasing block sizes results in low performance for allocations, but avoids external fragmentation.

B.For the best-?t method, the list of free blocks should be ordered according to increasing memory addresses C.The best-?t method chooses the largest free block into which the requested segment ?ts.

D.Using the ?rst-?t algorithm on a free list that is ordered according to increasing block sizes is equivalent to using the best-?t algorithm.

13. for which of the following applications are threads well suited?

i. background processing such as data compression or spell-checking

ii. displaying a web page on the screen before it has fully arrived from the server iii. fetching a page of a web document while the user scrolls through the previous one A.i and ii only B.i only C.i, ii, and iii D.ii only

14. threads differ from processes in that threads A.cannot make system calls. B.exist only in the operating system kernel. C.cannot be preempted. D.share a single virtual address space.

15. in IA32 or x86, which exception is used to implement demand paging i. interrupt ii.trap iii. fault iv. abort A.iv B.iii. C.ii. D.i .

16. which variable will be put into bss? int printf( const char* format, ... ); int global_init_var = 84;

int global_uninit_var; void func1( int i ) { printf( \

}

3 / 8

311046040 系统级编程(A 闭)

int main(void) {

2013-2014-1

4 / 8

311046040 系统级编程(A 闭) static int static_var = 85; static int static_var2; int a = 1; int b;

func1( static_var + static_var2 + a + b ); return a;

}

i a and b ii static_var

iii global_init_var iv global_uninit_var A.i only B.ii only

2013-2014-1 C.iii only D.iv only

17. what can loader do?

i. translate the c code into machine code ii.resolution

iii.load or map the executable object file from the disk to memory A.i and ii only. B.i and iii only. C.i, ii and iii.

D.iii only.

18. about the cache in a computer system, which is true

(a) every computer system has 3 level cache, that is l1, l2, l3 cache

(b) every computer systems' cache system have data cache and instruction cache (c) every computer systems' cache system has 2 level cache, that is l1, and l2 cache (d) every computer system's cache system has l1 and l2 cache inside cpu chip A.ii and iii only B.i only C.none D.i and iii only

19. which of the following manages the transfer of data between the cpu registers and the cache? A.hardware. B.registry. C.compiler. D.operating system.

20. which of the following levels of a typical memory hierarchy transfers data in chunks of biggest size? A.they all transfer one byte at a time. B.main memory disk. C.cpu registers cache. D.cache main memory.

21. which of the following is likely to offer the best performance improvement for programs that spend 50% of their time comparing strings?

A.be sure to use hardware string-comparison instructions.

B.store strings uniquely so that pointer comparison can be used.

C.write in-line code for string comparison to eliminate a procedure call. D.call a library function for string comparison.

22. which of the following approaches towards optimizing programs is most advisable? A.optimize after all functions are written and debugged. B.optimize main() first.

C.optimize as you go: make sure every function is optimized before writing the next one. D.optimize the more complex functions first.

23. cpu time measures

5 / 8

311046040 系统级编程(A 闭)

A.the percentage utilization of the cpu by the system. B.wall time

2013-2014-1

6 / 8

311046040 系统级编程(A 闭) C.the time spent executing system functions.

2013-2014-1 D.the time spent by a program executing program instructions.

24. to resolve memory leaks in c, one common approach is

A.to ensure that memory blocks are allocated only on word boundaries.

B.to check whether the number of calls to malloc() is greater than the number of calls to free().

C.to add padding before and after allocated memory blocks and to fill that memory with a known value. D.to store the source code line whence each block is allocated.

25. in c, when a struct is freed,

A.only those pointers within the struct that point into the heap are freed automatically. B.any pointers within the struct are also freed automatically. C.a destructor function is called automatically to clean up. D.no pointers within the struct are freed automatically.

26. in this sequence of c statements long a[10];

ptr = a + 5; *ptr++ = x;

the last line could be rewritten as A.a[5] = x; ptr = ptr + 1; B.ptr = x; *ptr++;

C.ptr = ptr + 1; *ptr = x; D.a[6] = x;

27. which of the following features apply to standard heap allocation in c? i.the size of heap objects must be known at compile time. ii. heap memory must be explicitly allocated. iii. heap memory is deallocated when a function returns. A.i and ii only. B.i only. C.ii only. D.i and iii. 28.Consider the program given below.

int callee(void) {

int count = 4;

printf(\return count; }

int main (int argc, char *argv[])

{ int count = 5; count = callee();

printf(\return 0; }

Which of the following describes the output of the program?

A. Two different integers are printed, and the value of neither can be determined from the information given. B. 4 and 5 are printed, in that order on the same line. C. 4 is printed twice on the same line.

D. One integer is printed twice, and its value cannot be determined from the information given. 29.Which of the following are advantages of using statistical sampling to profile programs?

7 / 8

311046040 系统级编程(A 闭) I. Exact run times of all functions can be determined. II.Code can be instrumented automatically.

III.The performance impact due to measurement can be minimal. A. I and II only B. I, II, and III C. I and III only D. II and III only

2013-2014-1

30.Amdahl's law, applied to program optimization, says that A.program measurement is a prerequisite to optimization

B. successive program optimizations tend to produce diminishing returns C. each optimization about doubles a program's performance

D. algorithmic design is more important than code quality for performance

二、简答题(本大题共 4 小题,每小题 5 分,共 20 分)。

1. What is linker ? Describe the process of linking.

一个可以把编译器生成的一个或多个对象组合成一个可执行程序的程序。

连接是搜集和结合程序需要加载到内存和执行的各个代码和数据片段的过程

2. Answer the following questions. a. State Amdahl’s law.

系统优化某部件所获得的系统性能改善程度,取决于该部件被使用的频率,或所占的执行时间的比例

b. It is found that in a typical execution of the Spice circuit simulation program 80% of all instructions do floating point operations. If we add the fastest available floating point co-processor to the system, what is the upper bound on the achievable speed up?1.6

3. What is automatic garbage collection? Describe the reference counting technique. 自动的回收堆分配的内存空间,应用程序不必关心内存空间的释放。 引用计数:记录每一个对象上的指针数,当指针数为零时,判断其为垃圾 4. Describe the computer memory hierarchy.

计算机都把存储器分成若干级,称为Memory Hierarchy,按照离CPU由近到远的顺序依次是CPU寄存器、Cache、内存、硬盘,越靠近CPU的存储器容量越小但访问速度越快

三、问答题(本大题共 5 小题,每小题 10 分,共 50 分)。

1. Use operators ( ! ~ & ^ | + << >> == ) to implement the following two functions.

/*

* shiftsAreArithmetic()

* Function returns 1 when run on a machine that uses arithmetic right shifts for int’s, and 0 otherwise. * Max ops: 8

int shiftsAreArithmetic() {

int shift_val = sizeof(int)<<3; int xright = (-1)>>shift_val; return ((xright & 0x10)==0x10);

}

/*

* isNonNegative - return 1 if x >= 0, return 0 otherwise

8 / 8

311046040 * 系统级编程(Example: isNonNegative(-1) = 0. isNonNegative(0) = 1. A 闭)

* Max ops: 6 */

int isNonNegative(int x) {

int shift_val = sizeof(int)<<3; x>>shift_val;

if((x & 0x8)!=0x8)return 1; else return 0;

}

2. Given the following C function:

int proc(void) {

int x,y;

2013-2014-1

9 / 8

311046040 系统级编程(A 闭) scanf(\, &x); return x-y; }

2013-2014-1

VC compiler generates the following assembly code:

1. push ebp 2. mov ebp,esp 3. sub esp,48h 4. push ebx 5. push esi 6. push edi 7. lea edi,[ebp-48h] 8. mov ecx,12h 9. mov eax,0CCCCCCCCh 10. rep stos dword ptr [edi] 11. lea eax,[ebp-4] 12. push eax 13. lea ecx,[ebp-8] 14. push ecx 15. push offset string \16. call scanf

diagram stack frame at this point 17. add 18. mov 19. sub 20. pop 21. pop 22. pop 23. add 24. mov 25. pop 26. ret

esp,0Ch

eax,dword ptr [ebp-4] eax,dword ptr [ebp-8] edi esi ebx esp,48h esp,ebp ebp

Assume that function proc starts executing with the following register values: Register esp ebp Value 0x800040 0x800060

Suppose proc calls scanf, and that scanf reads values 0x46 and 0x53 from the standard input. Assume that the string \A. What value does ebp get set to on line 2? 0x80003C

B. At what addresses are local variables x and y stored? eax ecx C. What is the value of esp at line 16? 0x7ffff4

D. Draw a diagram of the stack frame for proc right after scanf returns. Include as much information as you can about the addresses and the contents of the stack frame elements.

3. Are there any memory errors in the following program? If so, identify all of the errors and provide a corrected code fragment to alleviate the problem.

#include #include

10 / 8

311046040 系统级编程(A 闭)

int main(void) {

2013-2014-1

11 / 8

311046040 系统级编程(A 闭) char *str[8] = {\char *concat = 0; int total_size = 0; int i = 0;

2013-2014-1

for (i = 0; i < 8; i++)

total_size += strlen(str[i]); concat = (char*) malloc(total_size); for (i = 0; i < 8; i++)

{ strcat(concat, str[i]); free(str[i]); }

printf(\free(concat); return 0;

}

4. We could converte the factorial routine code from recursive to iteration as following: 1 int fact(int n) 2 {

3 int i;

4 int result = 1; 5

6 for (i = n; i > 0; i--) 7 result = result * i; 8 return result; 9 }

By doing so, we have reduced the number of CPE for the function from 63 to 4. Still, we would like to do better. One of the programmers generated the following code: 1 int fact_u2(int n) 2 { 3 int i; 4 int result = 1; 5 for (i = n; i > 0; i-=2) { 6 result = (result * i) * (i-1); 7 } 8 return result; 9 }

Unfortunately, the team discovered that this code returns 0 for some values of argument n . A. For what values of n will fact_u2and fact return different values?单数 B. Show how to fix fact_u2. for (i = n; i > 1; i-=2)

C. What kind of performance improvement technique did we use here?更快的循环

D. Benchmarking fact_u2 shows no improvement in performance. How would you explain that? E. Describe two more code optimization techniques.

12 / 8

311046040 系统级编程(A 闭) 2013-2014-1

5. Consider a computer with a 12-bit address space and a two level cache. Both levels use a LRU replacement policy. The parameters of the caches are as follows: * L1: 32bytes, direct mapped,8-byte cachelines.

* L2: 512 bytes, 4-wayset associative,32-byte cache lines.

The boxes below represent the bit-format of a physical address. In each box, for each cache (L1 and L2), indicate which field that bit represents (it's possible that a field doesn't exist). Here are the fields:

O: Byte offset within the cache line I: The cache(set) index T: The cache tag

The table below shows a trace of memory accesses (loads) made by the processor. For each access specify whether it is a level 1 cache hit (Ll), a level 2 cache hit (L2), or a miss (M). If the access is a hit, specify which previous access (by line number) loaded the value into the cache. Assume that initially all cache lines are invalid.

13 / 8

本文来源:https://www.bwwdw.com/article/iwo7.html

Top