CS29206 Systems Programming Laboratory, Spring 2022-2023
CS29206 Systems Programming Laboratory, Spring 2022-2023
Class Test 1
15–March–2023 07:00pm–08:00pm Maximum marks: 60
1. Suppose that you compile the program badstart.c using gcc. What warning message you would get? How do you
repair the code to avoid the warning message? Write your answer in the box given below. (3 + 3)
$ cat badstart.c
int main ()
{
printf("Hello world!\n");
return 0;
}
$ gcc -Wall badstart.c
The compiler complains about the implicit definition of the function printf.
The problem can be repaired by inserting
#include <stdio.h>
at the beginning of the code.
2. Consider the following C program argwork.c. The stdlib function atoi() converts a numeric string to an int.
#include <stdio.h>
#include <stdlib.h>
#define NUM(a,b) ((a) > (b) ? a : b)
int main(int argc, char* argv[])
{
int x = atoi(argv[1]), y = atoi(argv[argc-1]);
#ifdef PRINT1
printf("%d\n", NUM(x,y));
#endif
#ifdef PRINT2
printf("%d\n", x + y - NUM(x,y));
#endif
}
(a) What would happen if you run this program without any command-line argument? (2)
The program will encounter a segmentation fault (argv[] is a NULL-terminated array, and argv[1] is accessed).
(b) How should you compile the code so that:
(i) the program prints the larger of the first and the last arguments, (2)
— Page 1 of 4 —
foobar/
3. You have a project with root directory foobar (see the adjacent figure) for preparing a shared
Makefile
(that is, dynamic) library libfoobar.so. The source codes are in two subdirectories:
foo/
foo contains the foomatic functions, and bar contains the bargodic functions. All the foo1.c
required header files reside in the subdirectory include. Both the foomatic and the bargodic foo2.c
functions require the header file common.h. The foomatic functions additionally require the foo3.c
header file foo.h, and the bargodic functions the header file bar.h. A makefile in the foo Makefile
directory is meant for generating the object files from the foomatic source files, and a makefile bar/
in the bar directory is meant for generating the object files from the bargodic source files. bar1.c
bar2.c
No libraries are to be prepared in the foo and the bar directories. A top-level makefile in the Makefile
foobar directory is meant for recursively invoking the makefiles of the foo and the bar
include/
subdirectories and finally combining all the object files into the dynamic library file (in the common.h
foobar directory). The source files use the #include <...> format. Write the three foo.h
makefiles in the boxes below. There is no need to write install and clean targets. bar.h
(4 × 3)
Makefile in foobar/
Makefile in foobar/foo/
Makefile in foobar/bar/
— Page 2 of 4 —
4. Suppose that a C source file contains only the following functions.
void f ( int n ) { printf("%d\n", n); }
void g ( int n ) { while (n > 0) { --n; f(n); } }
int main () { for (int n=1; n<=8; ++n) { f(n); g(n); } }
Running the code with gprof gives the following output (contiguous) in the call graph. Fill in the blanks to complete
the gprof output for the given fragment. Show your calculations in the box given below. (5 + 5)
-------------------------------
Definitely lost 1 16 71
Still in use 8 192 71, 35, 47, 40, 22, 46, 58, 11
— Page 3 of 4 —
6. You compile the adjacent program (called myprog.c) using gcc with 1: #include <stdio.h> (6 + 6)
the -g option, and run the resulting executable under gdb. The line 2: void f (int);
numbers in the program are as shown. You set a breakpoint at Line 19. 3: void g (int);
You then make two runs of the program. In the first run, enter 4 as x, 4: int main ()
and in the second run, enter 5 as x. If a run hits the breakpoint, you 5: {
6: int x;
enter the gdb command bt. After this, you allow the program to finish.
7: printf("Enter x: ");
Show the transcripts of your gdb sessions for the two runs in the boxes 8: scanf("%d",&x);
provided below. Also explain the outputs produced by gdb. 9: f(x);
Assume that in both the cases, main() gets loaded at the memory 10: }
location 0x20010, f() gets loaded at 0x123A4 and g() gets loaded 11: void f(int x)
12: {
at 0x234B5. The transcripts you write need not match the real ones
13: if (x>0) g(x-1);
available from actual experiments, but you should mention the essential 14: else return;
points, and furnish proper explanations. 15: }
16: void g(int y)
17: {
18: if (y>0) f(y-1);
19: else return;
20: }
gdb> run gdb> run
Enter x: 4 Enter x: 5
— Page 4 of 4 —