Protection and system calls
This session is dedicated to the study of protection and system calls in Operating Systems.
You don't need to submit source files or reports for this session.
For this session, you will need to log into a PC running Linux. If you are at Eurecom, simply log on a PC of rooms 52 or 53. If you were to use another PC running GNU/Linux, I cannot guarantee that the lab works in the same way (outputs could be different, manual pages could be different, etc.).
I. Basic
- Be sure you have understood all the slides on the Introduction on Operating Systems
- How would you define a system call? Once you are happy with your definition, you can check on wikipedia for instance.
II. Services
- 
        Type the following command. How would you  explain the difference with the write()function of the source code given as example in the slides?$ man write 
- 
        Similarly, try to figure out if the following commands are bashcommands,syscalls, or C library functions:read,print,printf,malloc,exit,getpid,ps
 (Help me!) Useman -s2 fto know if a functionfis a syscall or not
III. Using system calls and data structures
In this exercise you will learn how to compile a C program, how to correct errors and warnings, how to use basic data structures, and finally how to use Linux system calls. You will also discover how to manipulate files from a C program.
-  In this exercise, you will manipulate files and data structures: we want to read from a text file line by line, and we want to store each line content in a linked list. A linked list is a set of cells, each cell containing a value and a pointer to the next cell. It is typically defined in C as follows:
    typedef struct LinkedList { char value[MAX_VALUE_SIZE]; struct LinkedList * next; } LinkedList;
 This linked list size is thus 2 (it contains two cells). The last cell points to "null", indicating that this is the last cell of the list.value = "first cell string" value = "second cell string" next = pointer to second cell next = NULL 
- 
        Save the ll.cfile into your work directory, open it with your favorite text editor. Locate where theopen()function is used. Verify thatopen()is a system call. Find out how many syscalls are used inll.c.
- Study this source code in detail, find out its main purpose. What errors can be raised during execution? Can you explain in which case each of them is raised?
- 
        Compile ll.cto a binary file namedll:$ gcc -Wall -o ll ll.c 
- 
        Execute ll. But first, create a file namedhello.txtthat contains one word per line. For instance,hello\nworld\nhow\nare\nyou\ntoday?.$ ./ll hello.txt The process should openhello.txtand load it into a linked list. Verify that everything works as expected. Do you understand how the linked list is progressively filled with the file content? Be sure to have understood this before switching to the next questions!
-  Start the program without any argument:
$ ./ll What happens? How could you provide a better error message even beforeopen()is called?
 (Help me!) Check that an argument is indeed provided on the command line. You could useargcfor instance.
-  Complete the printListContentfunction ofll.c. Basically, you should go through the different cells of the list, starting from the first one, and print the value of each cell.
 First, think of how to access each cell. Then, for each cell, you just need to perform aprintfon the value of the cell.
 Look at the help that follows only once you have really tried!
 (Help me!) SPOILER!!!
 TRY BEFORE LOOKING AT THIS!while(ll != NULL) { printf("%s ", ll->value); ll = ll->next; } printf("\n");
-  Freeing the list. What exactly makes the call to free(ll);? Remember whatfreemakes: frees the memory allocated at the provided address. What was allocated at this address? Only one cell! So, only one cell is freed with that call.
 Now that you have understood the problem, you are asked to code a functionfreeLinkedList()that frees the whole linked list given as parameter. Code and test.
 
 When starting to program with data structures, this is quite frequent to encounter "segmentation fault" errors, which means that you have tried to access to an invalid address. Usingprintfto debug allocation errors might not be the best solution. A better solution is to use a debugger likegdb. For this, you need to compile your program with debugging information:$ gcc -Wall -g -o ll ll.c And then you can investigate your error. Execute withgdb(to run typerun, to quit typequit, to restart from the beginning type:start):$ gdb ./ll hello.txt ... (gdb) run ... You should reach your error.gdbis a complex debugger: you will learn more commands ofgdbduring the next lab. But at least, during this lab, this should give you the line number at which the error occurs in your code.
-  Implement a function that returns the current size of a linked list: 
int sizeOfList(LinkedList *ll) { ... }Test with different linked lists.
-  Implement a function to remove the nth element of a linked list: 
void removeElementAt(LinkedList *ll, int index) { ... }Test by removing the first element of the list, one in the middle, and the one at the end.
- Implement a new C function that stores a new element at the end of the list. Currently, elements in the provided program are stored at the beginning of the list.
LinkedList* addToTheEndOfTheList(LinkedList *ll, char *content, int startIndex, int lastIndex) { ... }
- Implement a new version of the linked list with each cell containing a String of variable length (so: not an array), that is dynamically allocated (e.g. with malloc/calloc) to the exact size of the String you intend to store. You will have to accordingly update your function to free the whole list.
- BONUS 1: implement a function that saves the linked list to a file, and that can reload this linked list from a file.
- BONUS 2: add an insert function to your linked list: insertion of a given text at a given index. Test a lot.
- BONUS 3: define a doubly linked list, i.e. a linked list with each cell pointing to its next element and previous element. Modify all your functions (adding an element, removing an element at a given index, freeing the list, etc.)

