Systems Programming
1 Linux environment and programming
Linux Environment File management
◦ Unix like ◦ Create, read, or delete files
Filesystem ◦ List content of directories
Multi-user
Multi-process
◦ File attributes and security
Multi-threaded ◦ Virtual files and directories
◦ POSIX (Portable Operating System Interface) compli-
ant
Information management
◦ Linux is only the kernel
◦ Time/date
◦ Kernel
Core of the operating system
◦ Users and access control
Contains device drivers ◦ System and software settings
Allows software to access hardware
Mediates communication between software
Communication
Schedules shared resources
Handles filesystem, virtual devices ◦ Manage connections between applications
◦ User space vs. kernel space ◦ In computer and inter computer communication sys-
tems
User space is restricted, cannot access hardware
directly ◦ Networking
User space software can only access their own
memory regions
Modern CPU can help enforcing these rules Compiling on Linux
◦ The administrator of the system is called root ◦ C code files are simple text files, just use file extension
.c
System calls ◦ GCC is commonly used in Linux for compiling C pro-
grams
◦ Allows applications to reach kernel functionality
◦ You can compile program.c to output using the follow-
◦ This includes even the most basic functionality, includ- ing command
ing reading keys from the keyboard
gcc -o output program.c
◦ Separate from library functions, which themselves also
use system calls. System calls resides in kernel space ◦ It is possible to specify multiple c files. Executable files
in Linux do not have extensions
◦ Categories
Process control ◦ If an external library is needed, you may include it dur-
ing compilation using -l switch:
File management
Device management gcc -o output -lpthread program.c
Information management
Communication
Terminal commands
◦ Even though Linux has user interface, sometimes it is
Process control faster (for user) and more convenient to use terminal
◦ Create new process and threads ◦ ls: list files, use ls -la to list all files
◦ Start different applications ◦ cd dir : change directory to dir , use .. for parent
directory, start with / to start from the root of the
◦ Kill, control, wait, or signal other processes
filesystem
◦ Resource sharing between processes
◦ rm file : to remove file use rm -rf dir to erase a
◦ More on this next week directory
1/2
◦ mv source dest : to rename source to dest . You
may use this command to move a file to somewhere else
◦ cp source dest : to copy a file
◦ pwd: prints the current directory
Program arguments
◦ Allows programs to receive input from command line
◦ Using program arguments instead of regular input
should be preferred as program arguments can be spec-
ified easily from another application
◦ Instead of int main(void), use int main(int argc ,
char *argv [])
◦ argc is the number of arguments. First argument is
the name of the program, so this value should at least
be one
◦ argv is the argument vector. It contains arguments
starting from the name of the program. This array has
one NULL element at the end.
◦ The following program prints out all the given argu-
ments given to the program
int main(int argc, char *argv[]) {
char **arg = argv;
while(*arg) {
printf("%s\n", *arg);
arg++;
}
return 0;
}
2/2