- Course 00 - How to save your code with git and GitHub
- Course 01 - Learning Linux basics
- Course 02 - System calls for process management
- Course 03 - communication via sockets
- Course 04 - system calls for file management
To save your code in this internship we recommend to use git and a git server such as GitHub. However you can save your code as you wish.
Please make sure you have either a virtual machine or your host running with Ubuntu Desktop!
Tutorials:
- How to create an Ubuntu USB stick on Windows
- How to create an Ubuntu USB stick on MacOS
- How to install Ubuntu
Press CTRL + ALT + T to open a new terminal in Ubuntu.
Run the following command to install git
sudo apt install git
If you plan to use only the command line to perform git commands, use this step to create a repository in your GitHub space. If you plan to use the source control panel from within vscode, skip this step:
Press CTRL + ALT + T to open a new terminal in Ubuntu.
Run the following command to create a new directory named bs
in the current directoy
mkdir bs
Enter your GitHub git username and email into the terminal with
git config --global user.name YOUR_USERNAME
git config --global user.email YOUR_USER_EMAIL
so every time you make a git commit, your username and email will be written into this commit.
Initialize your new local git repository with the git init command:
cd bs
git init
Connect your GitHub repository to your local git repository with:
git remote add origin https://github.com/YOUR_GITHUB_ACCOUNT_NAME/YOUR_GITHUB_REPOSITORY_NAME.git
Press CTRL + ALT + T to open a new terminal in Ubuntu.
Run the following command to install git
sudo apt install git
If you do not wish to use the text editors vi
or nano
inside the terminal, you can chose to install vscode, which brings features like synthax highlighting, intellisense autocomplete and more for editing C-Files.
To do so, run the following command in an Ubuntu terminal (press CTRL + ALT + T) :
sudo snap install code --classic
Start vscode from your application grid and install the C/C++ extension from Microsoft from the extension menu:
Enter your GitHub git username and email into the terminal with
git config --global user.name YOUR_USERNAME
git config --global user.email YOUR_USER_EMAIL
so everytime you make a git commit, your username and email will be written into this commit.
Click the "Publish to GitHub" button, to create a private repository on GitHub, add the GitHub repository as a remote to your local repository and publish your local repository for the first time - all in one click. You will be asked to authorize VsCode to be connected to GitHub, so you do not need to enter your GitHub credentials every time you push a commit:
Everytime you want to "upload" changes to your GitHub git repository, you need to follow these steps.
Before you can bundle changes into a git commit you need to add files to your staging area, from which the commit is created.
With this command you add a file to the git staging area
git add NAME_OF_YOUR_FILE
or add all files you've changed to your staging area
git add *
Now you can finalize a git commit (think of a bundled package of changes). Enter the following command to create a git commit with your staged files and a commit message:
git commit -m "your commit message"
Now your local repository has new changes bundled as (a) commit(s). To update your remote repository on GitHub you need to use the push command. You can either just use
git push
to "upload" the current branch you are in (if you did not change, it should be named "master") to the origin remote, or decide which branch to push to which remote:
git push REMOTE_NAME BRANCH_NAME
Alternatively, you can use VsCode (or any other git client) to stage, commit, switch branch, pull, push, fetch or to use any other git command.
Dislaimer: even if we talk about Linux here, most if not all commands should also work on other unix systems such as MacOs, however it has only been tested on Linux.
For all of the following commands you need a command line interface such as GNOME terminal in Ubuntu (CTRL + ALT + T) or the built-in terminal in vscode.
Information about how to use the commands can be gained with calling the terminal program man
.
For example:
man pwd
Info: some very important shell commands are not called from a different binary but are built in in the shell. You can read the manual here with
man builtins
man
should be your primary source of information about a terminal program in the following internships. It comes preinstalled with most Linux distributions and does not need any internet connection.
With mkdir
you can create/make directories in Linux/Unix.
Create the directory test
:
mkdir test
With cd
you can change the directory. Change to your newly created directory:
cd test
With pwd
you can print the current working directory to your terminal:
pwd
With cp
you can copy files and directories (-r
).
Copy the file passwd
(which was used by the program passwd
) into your current working directory:
cp /etc/passwd .
cp passwd passwdcopy01
cp passwd passwdcopy02
With ls
you can print a list of files and (sub)directories in a given directory.
Use:
ls
ls -lt
ls -l passwdcopy01
ls -l p*
With more
and tail
you can print the content of files, view the manual for further information and differences:
man more
man tail
more passwdcopy02
more *
tail passwdcopy01
With mv
you can move or rename files and directories.
Rename your passwdcopy01 to passwdcopy01_renamed with:
mv passwdcopy01 passwdcopy01_renamed
remove your renamed copy01 of passwd with:
rm passwdcopy01_renamed
Create a file and write into it from within the terminal, when you are done quit with CTRL + D:
cat > my_new_file
my name is Hans # quit with CTRL + C
Use either terminal editors like vi
or nano
or a more comfortable solution like vscode & C/C++ extension to create a C program my_arg.c
that returns the arguments that you've entered.
To use the C-Compiler you need to install it first with:
sudo apt install gcc
Compile your created C program with the following shell command, where my_arg
is the name of the output file (thus the argument -o
) which you can chose and my_arg.c
is the C file you've previously created and the source of the compilation:
gcc -o my_arg my_arg.c
To run your program in the terminal run it with a trailing ./
before its file name:
./my_arg
Add any strings you can chose to have arguments as your output:
./my_arg 2021 is the year of the linux desktop
Write a C-program my_shell.c
that includes the following structure and test your program with the command date
and with /bin/date
:
...
while(1) {
print_command_promt();
read_and_save_command();
if(fork() > 0) {
/* parent process */
wait(status);
}
else {
/* child process */
execve(command, ...);
}
}
...
Tips:
- use the well known functions
scanf()
andprintf()
for I/O - design your program to ignore arguments attached to
./my_shell.c
- print the return value and the value of the
errno
variable of each syscall (seterrno
to0
before calling it)
Write a C-program my_fork.c
:
- create ten child processes with a break of one second between each fork()
- every child process should sleep() for 30 seconds and should terminate after
- after the parent process has created the child process, it should wait() for the termination of the son process
Tips:
- print out what is happening in your program with proper printf()'s at each step
- run your program
my_fork.c
in the background (/my_fork.c &
) and show the processes withps
Create a client/server application to communicate via sockets.
Create the C program server.c
that receives a port as an argument and runs the following system calls:
socket
- create a socketbind
- bind the port you've given the server via the argument to the socket you've createdlisten
: marks the socket as passive and ready for incoming connections- endless loop:
accept
- wait and accept a client requestrecv
- process the client messagesend
- send the string "SERVER" in returnclose
- close the connection
Create the C program client.c
that connects to the given name and port of the target system and runs the following system calls:
socket
- creation of a socketconnect
- connect to the serversend
- send "CLIENT" to the serverrecv
- receive and print the responseclose
- close the connection
- The server provides the following services:
- add - addition of two integers
- sub - subtraction of two integers
- div - division of two integers
- mul - multiplication of two integers
- Create a struct
message
inmessage.h
to define the structure of the provided services - expand the client:
- after the connection is set, read a math task in the form of
7 + 5
withscanf()
- package the task into a
message
and send it to the server - after the client has received the answer from the server the server message will be "unziped" and printed to the terminal
- after the connection is set, read a math task in the form of
- expand the server
- the client math task will be extracted from the client message
- the task will be calculated
- the result will be put into an answer message
General must-haves for your programs:
- Implement an argument check at the start of your programs - if the amount of arguments or the arguments are wrong the programs should terminate with a
printf()
tip for the correct arguments - You must check the return values of the sys calls
- If an error occurs you must check the
errno
variable andprintf()
the corresponding error message
To-Do:
my_creat [filename] [text]
: creates a file with a givenfileame
and writes the giventext
into itmy_open [filename]
: opens a file and prints the contentmy_lseek [filename]
: opens a file, moves withlseek
to end end of file, increases the file by 20 chars withlseek
, writes text at the end of the file and returns the content of the file withread
my_stat [filename]
: prints the attributes of the file withfstat
. Tip: thetime_t
attributes can be converted into a date format withlocaltime
Bonus:
my_chmod [filename] [mode]
: sets the mode of a file withfchmod
my_access [filename]
: prints the permissions of a filemy_mkdir [dirname] [mode]
: creates a directory with a given name and permissionsmy_read_dir [dirname]
: prints the contents of the given directory