Basics of Operating Systems and introduction to C
This session is dedicated to the study of basic concepts of OS and C: pointers and basic memory management.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.).
Trick. In Linux, do you know that you can do a copy/paste very easily with your mouse only? Simply select some text with your mouse, then move your mouse pointer to the place where you want to copy this text, and then click on the middle mouse button ... the text is copied!
This session refers to the sections "Introduction to OS" and "The Basics of C" of these slides. Do use these slides whenever necessary.
I. First experiments with GNU/Linux
- You are currently logged on a GNU/Linux PC. Run the following command to verify this:
- Thanks to uname, you now know which kernel is used, and what is the name of the release that has been used to install the GNU/Linux OS. Let us learn more about this release:
$ cat /etc/os-release
- Finally, use uptime and find out when your computer was rebooted for the last time
$ uname -aWhat other information can you gather by running this command? Use the man uname command if necessary, and explain the information that you can learn about your Linux kernel. In particular, which option makes it possible to print only the Kernel's version?
II. A first C program
This exercise might be difficult for students not at ease with C programming. A good understanding of basic concepts of C is needed to do well in this course. You will need it for lectures, labs, project and exam. So, if you have trouble doing the following exercise, spend 30 minutes reading any good C tutorial you can find, e.g. this one or this one. Also, do not hesitate to ask for help!Depending on your background, a tutorial on bash linux commands -- especially the part on navigation commands -- may also be useful.
During this exercise, you will learn how to compile a C program, how to correct errors&warnings.
- Open a terminal and create an new directory for this lab
- Create a file named hello.c and put the following C code in this file:
- Now compile this file from a terminal:
int main() { printf("Hello World!\n") return 0; }
$ gcc -o hello hello.cYou should obtain one warning, and one error. The two messages related to these warning/error should be quite explicit, so, correct the warning and the error, and recompile until no warnings nor errors are given. Then, execute your program:
$ ./hello
III. Enhancing your program
- Make a program that prints "hello" for each first name given as parameter. Your program should be able to take as many names as possible in argument. You can use a similar program provided in the slides. Your program should works as follows, for two names:
- Now, instead of giving as argument the different names, ask the user to enter a list of names. For this, you suggest you to use function "fgets". Understand how this function works by using "man". Then, modify the previous program. It shoud, for instance, work like this:
- Finally, based on the previous program, the user can enter either just a first name, or lastName_firstName: in that case, you have to extract the first name from the string. The program should work like this:
./hello Entre a list of first names: Alice Doe_Bob Hello Alice! Hello Bob!
./hello Alice Bob Hello Alice! Hello Bob!
./hello Entre a list of first names: Alice Bob Hello Alice! Hello Bob!
IV. Bonus
- Instead of taking as input a list of first names, the program now takes as argument a reference to a file. This file contains a list of last_first names separated by ";". Program this new hello world. Be sure that your program works even when the input is not correctly formatted. For instance, if you were to inject a totally different file, e.g., an executable file, your program should still work.