Galatasaray University Computer Engineering Department INF322 Operating Systems TP01: Introduction to Linux Ozan Çağlayan ocaglayan@gsu.edu.tr ozancaglayan.com
Fundamental Concepts Definition of Operating System This is the definition that concerns us in this lab A central software managing a computer's resources + Accompanying softwares like command-line interpreters, GUIs, editors, browsers, etc. Central software that manages and allocates computer resources like CPU, RAM, devices, etc.
Fundamental Concepts Definition of Operating System Central software that manages and allocates computer resources like CPU, RAM, devices, etc. Kernel
Fundamental Concepts Definition of Operating System Central software that manages and allocates computer resources like CPU, RAM, devices, etc. Kernel [noun] the inner and usually edible part of a seed or grain or nut or fruit stone; "black walnut kernels are difficult to get out of the shell"
Kernel Provides a software layer to manage the limited resources of a computer, Process Management, User space Memory Management, Device Access, Networking, System Call Interface,... Kernel Space
Directory Layout
Single Directory Hierarchy Linux maintains a single hierarchical directory structure to organize all files in the system, This contrasts with Windows where each storage has its own directory hierarchy: C:\ for the 1 st disk partition D:\ for the 2 nd disk partition E:\ for DVD-ROM...
Single Directory Hierarchy / Root (/) directory bin run etc home usr firefox media group passwd ozan bob include TP1.pdf stdio.h Regular File Directory
Pathnames A pathname is a string consisting of an optional initial slash (/) followed by a series of filenames separated by slashes: /home/ozan/documents/tp1.pdf Path separator Filename
Pathnames An absolute pathname begins with a (/) and specifies the location of a file with respect to the root directory: /home/ozan/documents/tp1.pdf
Pathnames An absolute pathname begins with a (/) and specifies the location of a file with respect to the root directory: /home/ozan/documents/tp1.pdf A relative pathname specifies the location of a file with respect to the current working directory: Documents/TP1.pdf (When the current directory is /home/ozan)
Single Directory Hierarchy /bin: Essential commands /dev: Devices /home: User's home directories /etc: System-wide configuration files /root: Home directory for root user /var: Variable files like logs.
Home Directories Every user in the system has an associated home directory: C:\Users\ozan for Win Vista & 7 C:\Documents and Settings\ozan for Win XP /home/ozan under Linux Separating user data from system-wide data is always a good thing.
Home Directories What you will find in a typical home directory: [ozan@dellfedora ~]$ ls CyberClient.conf Downloads git Pictures svn Desktop Dropbox Music Public Templates Documents fontconfig OctaveToolboxes Setups Videos A typical user should only use its home directory for everything!
Users and Groups Each user is uniquely identified, and users may belong to groups, Every user has: A numeric ID called User ID (UID), A login name (username), A home directory under /home/username, A password, etc.
Superuser One special user known as superuser has special privileges in the system, User ID == 0 Login name (username) == root Superuser has access to everything, can install new software, can remove any directories, etc. Home directory for superuser is /root.
Superuser One special user known as superuser has special privileges in the system, User ID == 0 Login name (username) == root Superuser has access to everything, can install new software, can remove any directories, etc. Home directory for superuser is /root.
Permissions Each file has an associated User ID (UID) and Group ID (GID) which define the owner of the file and the group to which it belongs, Every user has full read/write access to all files and directories in their /home directory, Superuser has all access to everything!
Permissions Every user in the system is isolated from the rest in terms of their home directories, User Bob cannot touch anything within: /home/ozan /home/anna /home/ahmet...
Mounting Other Storage Devices
Single Directory Hierarchy / Root (/) directory bin run etc home usr If there is a single directory hierarchy, where are all other disks, USB sticks, DVDs, CDs? firefox media group passwd ozan bob include TP1.pdf stdio.h Regular File Directory
Single Directory Hierarchy / Root (/) directory bin run etc media firefox group passwd
Single Directory Hierarchy User ozan plugged a USB stick / Root (/) directory bin run etc media firefox group passwd
Single Directory Hierarchy / Root (/) directory bin run etc media firefox group passwd User name ozan Disk label OZAN guide.pdf boardingpass_1360935081443.pdf
Single Directory Hierarchy / Root (/) directory bin run etc media firefox group passwd User name Disk label ozan OZAN Every storage becomes a Sub-tree of this hierarchy! guide.pdf boardingpass_1360935081443.pdf
Single Directory Hierarchy / Root (/) directory bin run etc media firefox group passwd User name ozan Disk label OZAN guide.pdf boardingpass_1360935081443.pdf
Single Directory Hierarchy All other storage devices are tied/mounted into this single directory hierarchy, This is done automatically by the system.
Interacting With The System
? Question: How come people interacted with computers at times when graphic cards were not invented?
Terminals Question: How come people interacted with computers at times when graphic cards were not invented? Answer: Using primitive terminals connected through serial port (RS-232).
Terminals (TeleTYpewriters, TTY) Output using printer Input using punch cards
Terminals (Early Video Type) Still connected through RS-232 port, Designed using microprocessors.
Terminals (Today) As many terminals as tabs in a terminal emulator, Each one runs its own shell.
Shell Special-purpose program to read commands typed by a user and execute programs in response to that commands. Kernel
Shell Special-purpose program to read commands typed by a user and execute programs in response to that commands. Shell Kernel
Shell Special-purpose program to read commands typed by a user and execute programs in response to that commands. Shell is just a program which waits for your input in a loop, Different shells available: sh, bash, csh, tcsh, zsh, etc.
Practice Press ALT+F2 and type gnome-terminal to launch a terminal emulator, Now you have a shell prompt at your order: [ozan@dellfedora ~]$ whoami ozan [ozan@dellfedora ~]$ date Prş Şub 21 13:27:56 EET 2013 [ozan@dellfedora ~]$ echo "GSU" GSU
Environment Variables Each process has an array of strings in key=value form called Environment List, A common use of environment variables is in the shell. Shell holds a list of these variables for various things, Now type printenv to see a list of these environment variables.
Environment Variables [ozan@dellfedora ~]$ printenv... SHELL=/bin/bash USER=ozan PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin HOME=/home/ozan......
Environment Variables [ozan@dellfedora ~]$ printenv... SHELL=/bin/bash USER=ozan PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin HOME=/home/ozan...... [ozan@dellfedora ~]$ echo $USER ozan [ozan@dellfedora ~]$ echo USER USER You have to put '$' before the variable so that the shell interprets is as a variable instead of a plain string!
Exercice: Let's write whoami! You've seen that shell has an environment variable called $USER,
Exercice: Let's write whoami! You've seen that shell has an environment variable called $USER, Standard C library provides: char *getenv(const char *name) Use man utility to see documentation about practically anything if a documentation exists and is installed: [ozan@dellfedora ~]$ man getenv
Exercice: Let's write whoami! GETENV(3) Linux Programmer's Manual NAME getenv, secure_getenv - get an environment variable SYNOPSIS #include <stdlib.h> char *getenv(const char *name); DESCRIPTION The getenv() function searches the environment list to find the environment variable name, and returns a pointer to the corresponding value string. RETURN VALUE The getenv() function returns a pointer to the value in the environment or NULL if there is no match.
Exercice: Let's write whoami! Type gedit and press Enter to launch a simple text editor, Final source code is ~12 lines and looks something like this:
Exercice: Let's write whoami! Type gedit and press Enter to launch a simple text editor, Final source code is ~12 lines and looks something like this:
Exercice: Let's write whoami! Now, how to compile it? We use gcc (GNU project C and C++ compiler) to compile it: gcc whoami.c -o whoami List of.c sources Name of the output executable When -o is not given, the resulting executable is called a.out
Exercice: Let's write whoami! Now, how to compile it? Done. How to run it?
Exercice: Let's write whoami! Now, how to compile it? Done. How to run it? Type whoami and press Enter: [ozan@dellfedora ~]$ whoami ozan
Exercice: Let's write whoami! Now, how to compile it? Done. How to run it? Type whoami and press Enter: [ozan@dellfedora ~]$ whoami ozan Really?
Exercice: Let's write whoami! Now, how to compile it? Done. How to run it? Type whoami and press Enter: [ozan@dellfedora ~]$ whoami ozan Really? But how does it differ from the system provided whoami?
Steps of program execution When you type whoami and press Enter, shell executes the whoami program and whoami prints your username. Questions Where is whoami? How is it located by the shell?
Steps of program execution whoami utility comes from a package called coreutils: http://www.gnu.org/software/coreutils/ coreutils also provides cat, chmod, chown, ls, cp, dd, df, du, head, mkdir, mv, printenv, rm, etc. practically every basic tool that we daily use on a Linux system.
Steps of program execution coreutils is installed in every Linux distribution be it Fedora, Ubuntu or OpenSuSE, etc. The installation directory may differ for each distribution but usually you will find whoami under /usr/bin, [ozan@dellfedora OS-Course]$ ls -lh /usr/bin/whoami -rwxr-xr-x 1 root root 32K Oca 24 18:43 /usr/bin/whoami Permissions Ownership Modification Time Filename
Steps of program execution Shell looks in the environment variable $PATH and adds whoami at the end of each path to see if the resulting path actually exists: /usr/local/bin /usr/bin $PATH /bin /usr/local/sbin /usr/sbin
Steps of program execution Shell looks in the environment variable $PATH and adds whoami at the end of each path to see if the resulting path actually exists: $PATH /usr/local/bin /usr/bin /bin /usr/local/sbin /usr/sbin Does /usr/local/bin/whoami exist? No, continue
Steps of program execution Shell looks in the environment variable $PATH and adds whoami at the end of each path to see if the resulting path actually exists: $PATH /usr/local/bin /usr/bin /bin /usr/local/sbin /usr/sbin Does /usr/bin/whoami exist? Yes, execute it!
Back to whoami Now, how to compile it? Done. How to run it? Type whoami and press Enter: [ozan@dellfedora ~]$ whoami ozan Really?
Back to whoami Now, how to compile it? Done. How to run it? Type whoami and press Enter: [ozan@dellfedora ~]$ whoami ozan Really? No. Shell will run the system-provided whoami found under /usr/bin.
Back to whoami Now, how to compile it? Done. How to run it? You have to explicitly tell your shell to run the whoami found in the current directory:
Back to whoami Now, how to compile it? Done. How to run it? You have to explicitly tell your shell to run the whoami found in the current directory: [ozan@dellfedora ~]$./whoami ozan '.' (dot) specifies current directory, '..' specifies upper directory, '/' is path separator.
Finally... That's not how the real whoami utility is implemented, Run the following command: USER= bill./whoami What happened and why? Discuss.
Galatasaray University Computer Engineering Department Any questions? Reference book The Linux Programming Interface Michael Kerrisk Ozan Çağlayan ocaglayan@gsu.edu.tr ozancaglayan.com