You are on page 1of 9

CS3500: Operating Systems, Lab 1

CS17B015: K.V.Nikhil

Date: 7th Aug 2019

Declaration: I declare that I have not indulged in any malpractice in completing the following
assignment. I agree to be penalized by 5 times the marks of this assignment, if any malpractice is
provably identified.
Add name

1. Compiling
Consider the following simple C program:
#include <stdio.h>
int main(int argc, char*argv[]) {
printf("Hello world\n");
return(0);
}

(a) The printf() command requires including stdio.h. Where does Linux look for this file?

Solution: The compiler looks for the header files in the current directory (present
working directory or pwd) and other global system directories such as /usr/include
/ and /usr/local/include/.

It also looks in the locations passed as an argument to the compiler using the -I flag.

(b) What is the command to compile the above program?

Solution: The command to compile above program is(assuming that the file name is
helloworld) gcc helloworld.c

(c) The gcc command runs multiple programs in compiling the c file. These include ccp,cc1
,as and ld. Find out what each of these programs do individually.

1
Solution: 1)cpp (c preprocessor) expands macros, strips comments and inserts the
content of header files into the program text.
2)ccl (compiler) converts preprocessed code to assembly language.
3)as (assembler) translates assembly language to machine language.
4)ld (linker) links object files to produce the final executable file.

(d) Execute the generated a.out file

Solution: Executing the a.out file gives Hello World as output. We can pass some
arguments to the program at this stage, but since the program is not using them, it
doesn’t matter.

2. Flags
The gcc compiler provides multiple flags which we will routinely use in the course. You can
check the man page of gcc to learn about the flags and to answer the following.

(a) What is the flag to rename the generated a.out executable?

Solution: The -o flag is used to rename the a.out executable.

(b) What is the flag to enable debugging?

Solution: The -g flag enables debugging.

(c) What are the flags for running individually the different stages of gcc: preprocess only,
compile only, compile and assemble, and compile, assemble and link?

Solution: The flags for running 1)preprocess only is -E


2)compile only is -S
3)compile and assemble -c
4)compile, assemble, and link (no flag needed)

(d) What is the flag to generate all reasonable warnings?

Solution: The -Wall flag generates all reasonable warnings.

2
(e) What is the flag to turn on compilation optimization?

Solution: The -O flag is used to turn on optimization.

(f) What is the flag to look for headers in a different folder?

Solution: The -I tells the compiler to look for headers in a different folder.

3. Linking

(a) In the course, we will spend some time with processes. Each process has a unique process
identifier which can be accessed through getpid(). Find out the file to be included for
invoking getpid(). Hint: See the man page for getpid

Solution:
The file to be included to invoke getpit() is unistd.h

(b) How would you know if the getpid() or any other function is part of the standard C
library or not?

Solution: check if it is present in man function.

(c) Write a program foo.c which prints the pid of the current process. Compile and execute
it.

Solution:
include <iostream>
include <unistd.h>
using namespace std;

// foo.c
int main()
{
int pid;
pid = fork();
if (pid == 0)
{

3
cout < "Process id : " < getpid() « endl;
}
return 0;

(d) How would you evaluate the correctness of your output? Hint: run the executable as a
background process.

Solution: Run the executable as a background process and compare the process id.

1
(e) Now suppose you wanted to compute x 4 given the function sqrt(). Which library would
you include? Is this part of the standard C library? If not this would require additional
linking during compilation. Does the man page for sqrt() provide information on this?

Solution:
math.h is the library that is included to use sqrt function
This library is not a part of c standard library so it is linked using the flag -lm

1
(f) Write a simple program bar.c which has a function to compute x 4 for a given x as ar-
1
gument. Use the main function to print x 4 of a number provided as a command line
argument. Compile this file with appropriate flag for linking.

Solution:
include<stdio.h>
float func(float x)
{
return sqrt(sqrt(x));
}
int main(int argc, char** argv[])
{
printf("%f",func(atof(argv[1])));
return 0;
}
compilation: gcc bar.c -lm

4. Static and dynamic libraries

4
(a) When compiling a program bar.c with "gcc bar.c -lxxx", the linker looks for which
library file?

Solution:
Searches for the library file xxx.

(b) Libraries come with two extensions .a and .so. What is the difference?

Solution:
A .a file is a static library, while a .so file is a shared object (dynamic) library similar
to a DLL on Windows.

(c) Why would you prefer a statically linked library over a dynamically linked library? And
why a dynamically linked library over a statically linked library?

Solution:
Programs that use statically-linked libraries are usually faster than those that use
shared libraries and because all the code is contained in a single executable mod-
ule , they never run into compatibility issues.

In dynamic linking load time might be reduced if the shared library code is already
present in memory an in static linking takes constant load time

(d) If you wanted to specifically use the statically linked library, what flag would you provide
to the compiler?

Solution:
gcc -static is the flag to specifically use the statically linked library

(e) If you had libraries in a custom folder, what compiler flag would you use?

Solution:
gcc -L is the flag used to add libraries in a custom folder

5. Compiling in stages

5
(a) Consider the foo.c and bar.c files you have written. Modify the bar.c to not have a main
1
function, but only the function to compute x 4 . You want to compile them individually the
two c files individually and then link them together. Write the sequence of gcc commands
for this. Hint: First generate object files for both programs, and then link them (with any
additional libraries).

Solution:
gcc -c foo.c
gcc -c bar.c
gcc -o foobar foo.o bar.o -lm

(b) If you wanted to perform optimisations with the -O flag, which of the gcc commands
would you change?

Solution: replace gcc -c foo.c and gcc -c bar.c with gcc -c -O foo.c and gcc -c -0 bar.c

(c) Now write a single gcc command to compile the two c files into an executable in one go.

Solution:
gcc foo.c bar.c -o foobar -lm

(d) When would a staged compilation be better than a single gcc command? [Hint: what
happens if we only change one of the source files]

Solution:
In a staged compilation,If one of the source files is changed then we don’t require re-
compilation of other files whereas in single gcc command we have to compile all the
files

(e) With the staged approach, compiling foo.c should show some warnings. Fix these with
appropriate function declarations in foo.c.

Solution: add the function prototype in foo.c to fix the warnings.

(f) Adding function declarations for functions in other files is not a good idea. Why? Modify
this with including a header file bar.h in foo.c. Compile again and see if the warnings
go away.

6
Solution: Adding function declarations for functions in other files is not a good idea
because any changes done to the function will not be reflected in other file.

(g) what if the function declaration in bar.h does not match with bar.c. How can you enforce
this?

Solution:

6. Makefile
The process of compiling multiple programs can be automated with a makefile. A typical unit
of a makefile is a "target" with the syntax:

target: requirement1 requirement2 ...


command1
command2

(a) Target is the file to be generated, requirements are the files necessary for generating the
target, and commands are executed to generate target. Understand the above syntax and
write a makefile for the simple program from problem 1. (Mind the gap, the indentation
is a tab and not spaces)

Solution:
default:Hello
Hello: Hello.c
gcc Hello.c -o Hello

(b) Write a makefile for the program from problem 5 with staged compilation.

Solution:
default:foobar
foo.i: foo.c
gcc -E foo.c > foo.i
foo.s: foo.i
gcc -S foo.i
foo.o: foo.s
as foo.s -o foo

7
foo: foo.o
gcc foo.o

(c) The "clean" target is used in the makefile to remove any intermediate files. Implement
this for the makefile in B.

Solution:
default:foobar
foo.i: foo.c
gcc -E foo.c > foo.i
foo.s: foo.i
gcc -S foo.i
foo.o: foo.s
as foo.s -o foo
foo: foo.o
gcc foo.o
clean:
-rm -f program.o
-rm -f program

(d) Instead of using explicit commands for each target, makefiles are shortened by using vari-
ables such as SRC for source files, TARG for target file, CC for compiler, FLAGS for compi-
lation flag, and LIBS for library flags. Further generic rules are written such as: %.o:%.c

$(CC) $(OPTS) -c $< -o $@

What does the above do?

Solution: 1)%.o: %.c means to make ‘.o’ files from ‘.c’ files, it makes all ‘.o’ files using
this pattern rule.
2)CC is a variable. It tells the makefile to chose a compiler(like gcc or g++).
3)OPTS refers to the user specific options.
4)-c command refers to statical linking
5)‘$@’ and ‘$<’ are automatic variables.It is used to substitute the names of the target
file and the source file in each case where the rule applies
6)All in all, these rules help us compile multiple .c files at once using variables. One

8
can also inlcude flags as variables like $(FLAGS) and should declare it before.
7)’-o’ is used for renaming

(e) The TARG may be generated using the following commands. What do they do?

OBJS = $(SRCS:.c=.o)
$(TARG): $(OBJS)
$(CC) -o $(TARG) $(OBJS) $(LIBS)

Solution: 1)$(OBJS) - dependency specification - OBJS = $(SRCS:.c=.o) this helps


to fulfill dependencies, which takes the srcs files of .c filetype and makes OBJS to
srcs.o(in short, it is for naming of object files).
2)CC specifies the compiler.
3)LIBS inlcudes the libraries.
4)OBJS will have all the object files after execution ofthis part of make.

(f) Rewrite the Makefile from 5 with the use of variables described above.

Solution:

You might also like