Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS 3214 Sample Midterm

Solutions are shown in this style. This exam was given Spring 2018.

1. Compiling and Linking (17 pts)

a)  (4 pts) Separate Compilation. Consider the following module.c file:

// module.c

int iiii;

static double dddd;

extern float eeee;

short ssss = 42;

static float

ffff(float gggg) {

return gggg + eeee;

}

When running gcc c m32 module.c; nm module.o which of the following is output? (Hint: -m32 compiles in 32-bit mode in which integers are 32-bit long.) Check one!

A

B

C

D

00000000 b dddd

00000000 t ffff

00000004 C iiii

00000000 D ssss

00000000 b dddd U eeee

00000000 t ffff

00000004 D iiii

00000000 s ssss

00000004 d gggg

00000000 b dddd U eeee

00000000 t ffff

00000004 C iiii

00000000 D ssss

00000000 d dddd U eeee

00000000 T ffff

00000004 C iiii

00000000 D ssss

Explanation:

A is missing the external reference to eeee which shows up as U         B includes gggg, which is local variable that’s resolved by the compiler D has ffff as a global symbol, but it’s static.  Ditto for ssss.

b)  (4 pts) As you may remember from exercise 2, the size command outputs the size (in bytes) taken up by the text, data, and bss sections of an           executable or object module. If we then ran size module.o, what output would we see?

Hint: since weakly defined symbols have not been resolved (we have not  invoked the linker), size will not include any memory needed for them yet.

A

text

70

data

12

bss

4

dec

86

hex filename

56 module o

B

text

70

data

2

bss

8

dec

80

hex filename

50 module.o

C

text

70

data

2

bss

12

dec

84

hex filename

54 module.o

D

text

0

data

2

bss

8

dec

10

hex filename

A module.o

Explanation:

The short ssss takes up 2 bytes in the data section, and the double dddd takes   up 8 bytes in the BSS section. As per hint, the weak global iiii is not counted.      There are no other definitions. The module contains a function, so the size of the program text cannot be 0.

c)  (9 pts) Let’s add a second file, main.c, as follows:

// main.c

float ffff(float

int main()

{

ffff(0.5); }

gggg);

If you compiled and linked the two files like so, you’d see:

$ gcc main.c module.c

/tmp/cc2sWLpp.o: In function `main':

main.c:(.text+0xd): undefined reference to `ffff'

/tmp/ccxL3Lms.o: In function `ffff':

module.c:(.text+0xd): undefined reference to `eeee'

collect2: error: ld returned 1 exit status

Which strategies will work to fix these errors?

Check all that apply (i.e., strategies that would, when applied in isolation, correct the error)!

To correct/avoid the undefined reference to `ffff’ error, we can

A

Move the declaration float ffff(float gggg) from the main.c file to a .h header file (and include it in both module.c and main.c)

B

Add the keyword extern to the declaration in main.c, e.g. extern float ffff(gggg)

C

Add the keyword static to the declaration in main.c

D

Remove the keyword static from the definition in module.c

To correct/avoid the undefined reference to `eeee' error, we can

A

Replace the keyword extern with the keyword static in the declaration of eeee in module.c

B

Add float eeee; to main.c

C

Remove the keyword extern from the declaration of eeee in module.c

D

Add extern float eeee; to main.c

E

Add float eeee; to a header file that is included at the top of both main.c and module.c

You can try all of these out yourselves and look at the resulting symbol tables.

This was graded like 9 true/false questions (1 pts for correct answer each.)

2. Understanding Unix Processes (30 pts)

a)  (5 pts) How many processes are created when the user types in ./fork3 on the command line, where fork3 is the executable corresponding to the       following C program:

// fork3.c

int

main()

{

fork();

if (fork())

fork();

sleep(1000000);

}

Check one!

A

B

C

D

E

3

4

5

6

7