01-introduction-to-linux-system-os-2024
01-introduction-to-linux-system-os-2024
Linux commands
and
Basics of System Calls
Abhijit A. M.
[email protected]
Why GNU/Linux ?
Few Key Concepts
What is a file?
File is a “dumb” sequence of bytes lying on the hard
disk (or SSD or CD or PD, etc)
It has a name, owner, size, etc.
Does not do anything on its own ! Just stays there !
Few Key Concepts
What is an application?
Application is a program that runs in an
“environment” created by the operating system,
under the control of the operating system
Hence also called “User Applications”
Words: Program, Application, Code.
Code: Any piece of complete/incomplete
apps
programming language
Programs
Program: Any piece of “complete” code (OS,
Code
device drivers, applications, ...)
Few Key Concepts
Files don't open themselves
Always some application/program open()s a file.
Files don't display themselves
A file is displayed by the program which opens it.
Each program has it's own way of handling files
It’s possible NOT TO HAVE an application to
display/show a file
Few Key Concepts
Programs don't run themselves
You click on a program, or run a command -->
equivalent to request to Operating System to run it.
The OS runs your program
Users (humans) request OS to run programs, using
Graphical or Command line interface
and programs open files
Path names
Tree like directory structure
Root directory called /
Programs need to identify files using path names
A absolute/complete path name for a file.
/home/student/a.c
Relative path names, . and .. notation
concept: every running program has a current
working directory
. current directory
A command
Name of an executable file
For example: 'ls' is actually “/bin/ls”
Command takes arguments
E.g. ls /tmp/
Command takes options
E.g. ls -a
A command
Command can take both arguments and options
E.g. ls -a /tmp/
Options and arguments are basically argv[] of the
main() of that program
int main(int argc, char *argv[]) {
int i;
for(i = 0; i < argc; i++)
Printf(“%s\n”, argv[i]);
}
$ ./a.out hi hello 123 /a/b/c.c ./m/a.c
Basic Navigation Commands
pwd
ls
cd /tmp/
cd
cd /home/student/Desktop
notation: ~
cd ~
Before the command line, the concept
of Shell and System calls
System Call
Applications often need to tasks involving hardware
Reading input, printing on screen, reading from
network, etc.
They are not permitted to do it directly and
compelled to do it using functionality given by OS
How is this done? We’ll learn in later few lectures.
This functionality is called “system calls”
Before the command line, the concept
of Shell and System calls
System Call
A function from OS code
Does specific operations with hardware (e.g. reading
from keyboard, writing to screen, opening a file from
disk, etc.)
Applications can't access hardware directly, they
have to request the OS using system calls
Examples
open(“/x/y”, ..)
The Shell
Shell = Cover
Covers some of the
Operating System's
“System Calls” (mainly
fork+exec) for the
Applications
Talks with Users and
Applications and does
some talk with OS
Not a very accurate diagram !
The Shell
Shell waits for user's input
GUI is a Shell !
Let's Understand fork() and exec()
#include <unistd.h> #include <unistd.h>
int main() { int main() {
fork(); printf("hi\n");
printf("hi\n"); execl("/bin/ls", "ls",
NULL);
return 0;
printf("bye\n");
}
return 0;
}
A simple shell
#include <stdio.h>
#include <unistd.h>
int main() {
char string[128];
int pid;
while(1) {
printf("prompt>");
scanf("%s", string);
pid = fork();
if(pid == 0) {
execl(string, string, NULL);
} else {
wait(0);
}
}
}
Users on Linux
Root and others
root
superuser, can do (almost) everything
Uid = 0
Other users
Uid != 0
UID, GID understood by kernel
Groups
Set of users is a group
File Permissions on Linux
3 sets of 3 permission
Octal notation: Read = 4, Write = 2, Execute = 1
644 means
Read-Write for owner, Read for Group, Read for
others
chmod command, used to change permissions, uses
these notations
It calls the chmod() system call
Permissions are for processes started by the user,
File Permissions on Linux
http//free-
Permissions: more !
Setuid/setgid bit
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 68208 Nov 29 17:23 /usr/bin/passwd
How to set the s bit?
chmod u+s <filename>
What does this mean?
Any user can run this process, but the process itself runs as if run by the
the owner of the file
passwd runs as if run by “root” even if you run it
Man Pages (self study)
http://www.cs.ucr.edu/~weesan/cs183/
GNU / Linux filesystem structure
Not imposed by the system. Can vary from one system to the other,
even between two GNU/Linux installations!
/ Root directory
/bin/ Basic, essential system commands
/boot/ Kernel images, initrd and configuration
files
/dev/ Files representing devices
/dev/hda: first IDE hard disk
/etc/ System and application configuration files
/home/ User directories
/lib/ Basic system shared libraries
http//free-
GNU / Linux filesystem structure (self study)
/lost+found Corrupt files the system tried to recover
/media Mount points for removable media:
/media/usbdisk,
/media/cdrom
/mnt/ Mount points for temporarily mounted
filesystems
/opt/ Specific tools installed by the sysadmin
/usr/local/ often used
instead
/proc/ Access to system information
/proc/cpuinfo,
/proc/version ...
/root/ root user home directory
/sbin/ Administrator-only commands
/sys/ System and device controls
(cpu frequency, device
power, etc.) http//free-
GNU / Linux filesystem structure (self
study)
/tmp/ Temporary files
/usr/ Regular user tools (not essential to the
system)
/usr/bin/, /usr/lib/,
/usr/sbin...
/usr/local/ Specific software installed by the sysadmin
(often preferred to /opt/)
/var/ Data used by the system or system servers
/var/log/, /var/spool/mail
(incoming mail), /var/spool/lpd
(print jobs)...
http//free-
Files: cut, copy, paste, remove,
(self study)
cat <filenames> mv <source> <target>
mv a.c b.c
cat /etc/passwd
mv a.c /tmp/
cat fork.c
mv a.c /tmp/b.c
cat <filename1> rm <filename>
<filename2> rm a.c
cp <source> <target> rm a.c b.c c.c
cp a.c b.c rm -r /tmp/a
mkdir
cp a.c /tmp/
mkdir /tmp/a /tmp/b
cp a.c /tmp/b.c
rmdir
Useful Commands
(self study)
echo grep
http//free-
Mounting
Partition
temp\
windows\
x.dll
xyz.mp3 abc.mp3 write.exe notepad.exe
Windows Namespace
C:\ D:\ Are partitions of the disk drive
One D:
Typical convention: C: contains programs, “tree” per partition
contains data
Together they make a “forest”
C:\ D:\
prog.c
x.dll file.jpg game.exe
xyz.mp3 abc.mp3 write.exe notepad.exe xyz.mp3 abc.mp3
Linux Namespace: On a partition
/usr/songs/xyz.mp3 On every partition:
/ Root is “/”
Separator is also “/”
usr/ home/
abhijit/
songs/ note.txt guest/
myfile
xyz.mp3 abc.mp3 midspark.odp a.out
Linux namespace: Mount
Linux namespace is a single “tree” and not a “forest” like
Windows
Combining of multiple
/ trees is done through “mount”
Linux Hard disk
usr/ home/
abhijit/
songs/ note.txt guest/
mydir/
xyz.mp3 abc.mp3 midspark.od a.out
p x/ y/
http//free-
Files that are not regular/directory
Special devices (1)
Device files with a special behavior or contents
/dev/null
The data sink! Discards all data written to this file.
Useful to get rid of unwanted output, typically log information:
mplayer black_adder_4th.avi &> /dev/null
/dev/zero
Reads from this file always return \0 characters
Useful to create a file filled with zeros:
dd if=/dev/zero of=disk.img bs=1k count=2048
http//free-
Special devices (2)
/dev/random
Returns random bytes when read. Mainly used by cryptographic
programs. Uses interrupts from some device drivers as sources of
true randomness (“entropy”).
Reads can be blocked until enough entropy is gathered.
/dev/urandom
For programs for which pseudo random numbers are fine.
Always generates random bytes, even if not enough entropy is
available (in which case it is possible, though still difficult, to predict
future byte sequences from past ones).
See man random for details.
http//free-
Files names and inodes
http//free-
Creating “links”
Hard link
$ touch m
$ ls -l m
-rw-rw-r-- 1 abhijit abhijit 0 Jan 5 16:18 m
$ ln m mm
$ ls -l m mm
-rw-rw-r-- 2 abhijit abhijit 0 Jan 5 16:18 m
-rw-rw-r-- 2 abhijit abhijit 0 Jan 5 16:18 mm
$ ln mm mmm