inf322 – operating systemsozancaglayan.com/wp-content/uploads/2013/04/tp01s.pdfmanaging a...

62
INF322 – Operating Systems TP01: Introduction to Linux Ozan Çağlayan [email protected] ozancaglayan.com Galatasaray University Computer Engineering Department

Upload: tranxuyen

Post on 28-Apr-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

INF322 – Operating SystemsTP01: Introduction to Linux

Ozan Çağ[email protected]

Galatasaray UniversityComputer Engineering Department

Fundamental Concepts

Definition of Operating System

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.

This is the definition that concerns us in this lab

+

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,

● Memory Management,

● Device Access,

● Networking,

● System Call Interface,

● ...

User space

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 1st disk partition

– D:\ for the 2nd disk partition

– E:\ for DVD-ROM

– ...

Single Directory Hierarchy/

bin run etc home usr

firefox group passwd ozan bob include

TP1.pdf stdio.h

Regular File

Directory

Root (/) directory

media

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

[ozan@DellFedora ~]$ lsCyberClient.conf Downloads git Pictures svnDesktop Dropbox Music Public TemplatesDocuments fontconfig OctaveToolboxes Setups Videos

What you will find in a typical home directory:

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/

bin run etc home usr

firefox group passwd ozan bob include

TP1.pdf stdio.h

Regular File

Directory

Root (/) directory

media

If there is a single directory hierarchy, whereare all other disks, USB sticks, DVDs, CDs?

Single Directory Hierarchy/

bin run etc

firefox group passwd

Root (/) directory

media

Single Directory Hierarchy/

bin run etc

firefox group passwd

Root (/) directory

media

User ozan plugged aUSB stick

Single Directory Hierarchy/

bin run etc

firefox group passwd

Root (/) directory

media

ozan

OZAN

guide.pdf boardingpass_1360935081443.pdf

User name

Disk label

Single Directory Hierarchy/

bin run etc

firefox group passwd

Root (/) directory

media

ozan

OZAN

guide.pdf boardingpass_1360935081443.pdf

User name

Disk labelEvery storage becomes a

Sub-tree of this hierarchy!

Single Directory Hierarchy/

bin run etc

firefox group passwd

Root (/) directory

media

ozan

OZAN

guide.pdf boardingpass_1360935081443.pdf

User name

Disk label

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)

Input using punch cards

Output using printer

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 ~]$ whoamiozan[ozan@DellFedora ~]$ datePrş Ş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/bashUSER=ozanPATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbinHOME=/home/ozan......

Environment Variables[ozan@DellFedora ~]$ printenv...SHELL=/bin/bashUSER=ozanPATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbinHOME=/home/ozan......[ozan@DellFedora ~]$ echo $USERozan[ozan@DellFedora ~]$ echo USERUSER

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 ~]$ whoamiozan

Exercice: Let's write whoami!

● Now, how to compile it? Done.

● How to run it?

– Type whoami and press Enter:

[ozan@DellFedora ~]$ whoamiozan

Really?

Exercice: Let's write whoami!

● Now, how to compile it? Done.

● How to run it?

– Type whoami and press Enter:

● But how does it differ from the system provided whoami?

[ozan@DellFedora ~]$ whoamiozan

Really?

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

/bin

/usr/local/sbin

/usr/sbin

$PATH

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

/bin

/usr/local/sbin

/usr/sbin

$PATH

Does /usr/local/bin/whoamiexist?

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:

/usr/local/bin

/usr/bin

/bin

/usr/local/sbin

/usr/sbin

$PATH

Does /usr/bin/whoamiexist?

Yes, execute it!

Back to whoami

● Now, how to compile it? Done.

● How to run it?

– Type whoami and press Enter:

[ozan@DellFedora ~]$ whoamiozan

Really?

Back to whoami

● Now, how to compile it? Done.

● How to run it?

– Type whoami and press Enter:

● Shell will run the system-provided whoami found under /usr/bin.

[ozan@DellFedora ~]$ whoamiozan

Really? No.

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:

● '.' (dot) specifies current directory,● '..' specifies upper directory,● '/' is path separator.

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 ~]$ ./whoamiozan

Finally...

● That's not how the real whoami utility is implemented,

● Run the following command:

– USER=”bill” ./whoami

● What happened and why? Discuss.

Any questions?

Ozan Çağ[email protected]

Galatasaray UniversityComputer Engineering Department

Reference bookThe Linux Programming Interface Michael Kerrisk