Lab Task
Lab Task
Lab Task
Which of the gcc tools is responsible for producing a map le? 5. What is the content of each of the sections in a map le. Explain briey. 6. Rewrite hello.c to produce entries in the map le for .data, .bss, and .rodata. Hint: This can be done by adding one variable for each type to the le. 7. Add the following function to hello.c: double multiply(double x1, double x2), which returns x1*x2. Use gcc to generate an assembly code listing for the program, and examine the assembly code. What assembly instructions are used to do this? Repeat this task, but now replace double with float. Explain!
On Unix systems make is an important build tool. In particular, many programs written in C/C++ depends on make1 to compile, build, and install software. For this part of the lab, run through the example found at http://mrbook.org/tutorials/make/ Questions: 8. How does make know if a le must be recompiled? 9. Provide a make command to use a le named mymakefile instead of the default makefile.
3.1
In this section of the lab, we are going to develop a small C application that makes use of two libraries. The two libraries are called: lib1 and lib2. The application only knows about lib1, however lib1 will make calls into lib2, as illustrated here: app lib1 lib2 A skeleton of the code is given in the following listings:
void main( int argc, char *argv[] )
{ //Make a table of size give by an argument on the command line. //Fill the table with random numbers between MIN and MAX //MIN = 0.0 if not specified with another value on command line, i.e. optional argument //MAX = 100.0 if not specified with another value on command line, i.e. optional argument //Call tab_sort_sum() in lib1 //Print out table and sum }
Page 3
BOOL tab_sort( double *tab, int tab_size ); { //Sort the table and return true for success }
Task: Write the three C les (main.c, l1.c, l2.c) and prepare the corresponding makele to compile an executable. You must also make corresponding .h les (l1.h, l2.h): Make sure that dependencies are handled correctly. The makele should also contain a TEST target, where all three C les are compiled and tested. You can choose if you want to use specic C les for testing, or if you add test code to the existing les. When executing the TEST target necessary apps and libs must be built and a test executed. A failing test should fail make. Questions: 10. Observe that lib2 makes use of BOOL, but lib1 does not pass this on to main. Rewrite lib1 and main to handle the BOOL. Where and how should BOOL be dened? 11. How do you implement an include guard, and why is it needed?
3.2
Tips
The following should be done to check that your makefile is correct: If you delete a single .o le. Does make rebuild only the necessary les? If you delete a lib le, does the rebuild work as it should? If you change a .h le (e.g. save it to refresh its timestamp), does make rebuild the correct les?
A small report where all questions are answered. In assignments where there are several programming steps, only the last need step need to be included. For example, only the last version of the Hello, world! program needs to be delivered.
In this part of the lab, we will look at how threads and protection mechanisms are used in dierent languages and dierent operating system. You will solve the same problem in two dierent languages (see Table 1), one language shall be C, the other is yours to choose. The objective of the assignment is to make a program intended to fail due to lack of protection of a global resource (variable). Then you will implement the program with a protection mechanism and show that the protection is working.
6
6.1
The Program
Global resource:
Create a global unprotected resource. Think simple; for example a variable that more than one thread read-modify-write should suce.
Page 4
6.2
The main() function of your program should create a congurable number of threads (e.g. use command line arguments to congure the number of threads). Each thread should access the global resource a given number of times (could also be congurable through a command line argument). When all of the threads have nished, the number of protection violations should be printed on the console.
6.3
Next, extend your two programs by adding protection on the global resource. When the program completes, the result printed to the console should indicate that no protection violations were discovered. That is, all accesses to the global resource executed appropriately.
6.4
Implement low-level protection by using inline assembly with the atomic test-and-set instruction. Before you start on this task, spend some time to gure out: How to add inline assembly2 i C. What an atomic test-and-set instruction is, and why we need it.
6.5
Combine the C language part of Sections 6.2, 6.3, and 6.4 into on single application where the behavior of the application can be chosen using command line arguments or conditional compilation. Use the method in Listing 2 or Listing 3.
Algorithm
Below we outline the pseudo code for the main program. You may use either the pthread or sthread library for your implementation in C. 1. Create a global resource G 2. Create N threads 3. Start the N threads 4. Wait for all N threads to complete their execution 5. Compare the expected result of G with the actual value, and print out OK/FAIL
Enviroment
Choose two of the languages from Table 1. One of the languages must be C.
8.1
Low-level Protection in C
From the C language we can use standard protection primitives like mutex locks and semaphores. However, we now want you to examine the underlying mechanisms that the operating system uses to ensure that shared data structures are protected from concurrent accesses.
2
Page 5
Table 1: Environment Note Mandatory. Use the gcc compiler Visual Studio Express on own laptop
Windows, Linux or Mac Windows, Linux or Mac Windows, Linux or Mac Only if conrmed with TA
12. What kind of scheduling algorithm is used in your two applications? 13. For C, discuss the pros and cons of using OS locks() vs. inline assembly. 14. Is it also possible to protect your global resource by using the intLock() function, which will disable interrupts? Elaborate.
A small report where all question are answered. In assignment where there are several programmingsteps, only the last step should be delivered.
10
To help point you in right direction for the dierent languages, please take a look at Table 2 for a list of keywords that can give you some ideas or that you can use when searching. Table 2: Keywords Language C Linux Keywords posix, PTHREAD MUTEX INITIALIZER, pthread mutex lock, pthread mutex unlock, pthread create, pthread join InitializeCriticalSection, EnterCriticalSection, LeaveCriticalSection, CreateThread, asm, WaitForMultipleObjects System.Threading, Mutex, WaitOne, ReleaseMutex, ThreadStart The synchronized keyword can be used. But also packages: java.util.concurrent and java.util.concurrent.locks. import threading, global, threading, acquire, release, start, join
C Windows
C# Java Python
Page 6