OS Practical File
OS Practical File
OS Practical File
1
Booting Process of Linux
Press the power button on your system, and after few moments you see the Linux login
prompt. From the time you press the power button until the Linux login prompt appears,
the following sequence occurs. The following are the 6 high level stages of a typical
Linux boot process.
Step-1. BIOS
BIOS stands for Basic Input/Output System.
Performs some system integrity checks.
Searches, loads, and executes the boot loader program.
It looks for boot loader in floppy, CD-ROMs, or hard drive. You can press a key
(typically F12 or F2, but it depends on your system) during the BIOS startup to
change the boot sequence.
Once the boot loader program is detected and loaded into the memory, BIOS
gives the control to it.
So, in simple terms BIOS loads and executes the MBR boot loader.
Step-2. MBR
MBR stands for Master Boot Record.
It is located in the 1st sector of the bootable disk. Typically /dev/hda, or
/dev/sda.
MBR is less than 512 bytes in size. This has three components 1) primary boot
loader info in 1st 446 bytes 2) partition table info in next 64 bytes 3) mbr
validation check in last 2 bytes.
It contains information about GRUB (or LILO in old systems).
So, in simple terms MBR loads and executes the GRUB boot loader.
1
Step-3. GRUB
GRUB stands for Grand Unified Bootloader.
If you have multiple kernel images installed on your system, you can choose
which one to be executed.
GRUB displays a splash screen, waits for few seconds, if you don’t enter
anything, it loads the default kernel image as specified in the grub configuration
file.
GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t
understand filesystem).
Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this).
The following is sample grub.conf of CentOS.
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/boot/grub/splash.xpm.gz hiddenmenu
title CentOS(2.6.18-194.el5PAE)
root(hd0,0)
kernel/boot/vmlinuz-2.6.18-194.el5PAE ro root=LABEL=/
initrd /boot/initrd-2.6.18-194.el5PAE.img
As you notice from the above info, it contains kernel and initrd image.
So, in simple terms GRUB just loads and executes Kernel and initrdimages.
Step-4. Kernel
Mounts the root file system as specified in the “root=” in grub.conf
Kernel executes the /sbin/init program
Since init was the 1st program to be executed by Linux Kernel, it has the process
id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
initrd stands for Initial RAM Disk.
initrd is used by kernel as temporary root file system until kernel is booted and
the real root file system is mounted. It also contains necessary drivers compiled
inside, which helps it to access the hard drive partitions, and other hardware.
Step-5. Init
Looks at the /etc/inittab file to decide the Linux run level.
Following are the available run levels
0 – halt
1 – Single user mode
2 – Multiuser, without NFS
3 – Full multiuser mode
2
4 – unused
5 – X11
6 – reboot
Init identifies the default initlevel from /etc/inittab and uses that to load all appropriate
program.
Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level
If you want to get into trouble, you can set the default run level to 0 or 6. Since you
know what 0 and 6 means, probably you might not do that.
Typically you would set the default run level to either 3 or 5.
Login Process
i. Users enter their username and password
ii. The operating system confirms your name and password.
iii. A "shell" is created for you based on your entry in the "/etc/passwd" file
iv. You are "placed" in your "home"directory.
v. Start-up information is read from the file named "/etc/profile". This file is known as
the system login file. When every user logs in, they read the information in this file.
vi. Additional information is read from the file named ".profile" that is located in your
3
"home" directory. This file is known as your personal login file.
1. Basics
i. echo SRM: to display the string SRM
ii. clear: to clear the screen
iii. date: to display the current date and time
iv. cal 2022: to display the calendar for the year 2022
v. cal 6 2022: to display the calendar for the June-2022
vi. passwd: to change password
4
4. File name substitution
i. ls f?: list files start with ‘f’ and followed by any one character
ii. ls *.c: list files with extension ‘c’
iii. ls [gpy]et: list files whose first letter is any one of the character g, p or y and followed
by the word et
iv. ls [a-d,l-m]ring: list files whose first letter is any one of the character from a to d and l
to m and followed by the word ring.
5. I/O Redirection
Input redirection
wc –l < ex1: To find the number of lines of the file ‘ex1’
Output redirection
6. Piping
7. Environment variables
echo $PS2 display the second prompt string ( > symbol by default )
Method-1
Syntax : chmod [ugo] [+/-] [ rwxa ] filename
5
u : user, g : group, o : others
+ : Add permission - : Remove the permission r : read, w : write, x : execute, a : all
permissions
ex. chmodug+rw f1
adding ‘read & write’ permissions of file f1 to both user and group members.
Method-2
Syntax : chmodoctnum file1
6
Program No. 3
Linux File System
7
Directory Contents
/ Root directory—the starting point of the directory tree
/bin Essential binary files. Binary Executable files
/boot Static files of the boot loader.
/dev Files needed to access host-specific devices.
/etc Host-specific system configuration files.
/lib Essential shared libraries and kernel modules.
/media Mount points for removable media.
/mnt Mount point for temporarily mounting a file system.
/opt Add-on application software packages.
Result- /root Home directory for the superuser root. The given
program
/sbin Essential system binaries.
has been
/srv Data for services provided by the system. performed
/tmp Temporary files.
/usr Secondary hierarchy with read-only data.
/var Variable data such as log files
successfully.
8
Program No. 4
Editors and Filters
VI EDITOR
vi fname: to open the file fname
There are two types of mode in vi editor
Escape mode – used to give commands – to switch to escape mode, press <Esc> key
Command mode – used to edit the text – to switch to command mode, press any one the
following inserting text command
1. Inserting Text
i insert text before the cursor
a append text after the cursor
I insert text at the beginning of the line
A append text to the end of the line
r replace character under the cursor with the next character typed
R Overwrite characters until the end of the line
(small o) open new line after the current line to type text
(capital O) open new line before the current line to type text
2. Cursor Movements
h left
j down
k up
l right
(The arrow keys usually work also)
9
0 (zero) beginning of line
$ end of line
3. Deleting Text
Note: (n) indicates a number, and is optional
4. Saving Files
:w- to save & resume editing (write & resume)
:wq- to save & exit (write & quit)
:q!- quit without save
FILTERS
1. cut
Used to cut characters or fields from a file/input
Syntax: cut -cchars filename
-ffieldnos filename
By default, tab is the field separator (delimiter). If the fields of the files are separated by any
other character, we need to specify explicitly by –d option
cut -ddelimitchar
-ffiledsfilname
2. paste
Paste files vertically. That is n th line of first file and n th line of second file are pasted as the n th
line of result
Syntax: paste file1 file2
-ddcharoption is used to paste the lines using the delimiting character dchar
-s option is used paste the lines of the file in a single line
3. tr
10
Used to translate characters from standard input
Syntax: tr char1 char2 < filename
It translates char1 into char2 in file filename
4. grep
Used to search one or more files for a particular pattern.
Syntax: grep pattern filename(s)
Lines that contain the pattern in the file(s) get displayed pattern can be any regular
expressions
More than one file can be searched for a pattern
-v option displays the lines that do not contain the pattern
-l list only name of the files that contain the pattern
-n displays also the line number along with the lines that matches the pattern
5. sort
Used to sort the file in order
Syntax : sort filename
Sorts the data as text by default
Sorts by the first filed by default
11
7. cmp
Used to compare two files
Syntax: cmp f1 f2 compare two files f1 & f2 and prints the line of first difference
8. diff
Used to differentiate two files
Syntax: diff f1 f2 compare two files f1 & f2 and prints all the lines that are differed between
f1 & f2
9. comm
Used to compare two sorted files
Syntax: comm file1 file2
12
Program No. 5
Compilation of C Program
Compilation of C Program
Step-1: Open the terminal and edit your program using vi editor/gedit editor and save with
extension “.c”
Ex. vi test.c
(or) gedittext.c
13
c continue till the next break
n Execute the next line. Treats function as single statement
s Similar to ‘n’ but executes function statements line by line
l List the program statements
Step-7: Quit the debugger
(gdb) q
Virtual fork
vfork() function is similar to fork but both processes shares the same address space.
#include <stdio.h>
#include<unistd.h>
int main()
{
int a=5,b=10,pid;
printf("Before fork a=%d b=%d \n",a,b); pid=fork();
if(pid==0)
{
a=a+1; b=b+1;
printf("In child a=%d b=%d \n",a,b);
}
else
{
sleep(1); a=a-1; b=b-1;
14
printf("In Parent a=%d b=%d \n",a,b);
}
return 0;
}
Output:
#include
<stdio.h> #include<unistd.h>
int main()
{
fork(); fork();
fork();
printf(“SRMIST\n”);
return 0;
}
Output:
15
Q3. How many child processes are created for the following code?
#include <stdio.h>
#include<unistd.h>
intmain()
{ fork();
fork()&&fork()||fork();
fork();
printf(“Yes ”); return 0;
}
Output:
16
Result- The given programs have been executed successfully.
Program No. 7
System Admin Commands
INSTALLING SOFTWARE
To install a package/software
sudo apt-get install <package-name>
To reinstall a package
sudo apt-get install <package-name> --reinstall
17
Q2. Install the package “simplescreenrecorder”
18
MANAGING USERS
Managing users is a critical aspect of server management.
In Ubuntu, the root user is disabled for safety.
Root access can be completed by using the sudo command by a user who is in the
“admin” group.
When you create a user during installation, that user is added automatically to the
admin group.
To add a user:
sudoadduser username
To disable a user:
sudopasswd -l username
To enable a user:
sudopasswd -u username
To delete a user:
sudouserdel –r username
To create a group:
sudoaddgroupgroupname
To delete a group:
sudodelgroupgroupname
To create a user with group:
sudoadduser username groupname
To see the password expiry value for a user,
sudochage -l username
To make changes:
sudochage username
19
GUI Tool for User Management
If you do not want to run the commands in terminal to manage users and groups, then you can
install a GUI add-on.
sudo apt install gnome-system-tools
Once done, type
users-admin
Q4. Create a user ‘elias’. Login to the newly created user and exit.
Q5. Create a group ‘cse’ and add the user ‘elias’ in that group .
Q6. Delete the user ‘elias’ and then delete the group ‘cse’.
20
FILE SYSTEM
A file system is a permanent storage for containing data. Any non-volatile storage device like
hard disk, usbetc has a file system in place, on top of which data is stored. While installing
Linux, you may opt for either EXT4 or EXT3 file system.
Disk Partitions can be viewed by the command sudofdisk -l File system information are
available in the file /etc/fstab
21
NETWORKING
Most networking is configured by editing two files:
/etc/network/interfaces
Ethernet, TCP/IP, bridging
/etc/resolv.confo DNS
Other networking files:
/etc/hosts
/etc/dhcp3/dhcpd.conf
22
Q9. Find the IP address of the localhost.
23
INSTALLING INTERNET SERVICES
24
Q13. Create a new database for mysql server
25
Program No. 8
Simple Task Automation
Linux Cron utility is an effective way to schedule a routine background job at a specific time
and/or day on an on-going basis. You can use this to schedule activities, either as one- time
events or as recurring tasks.
Crontab Syntax m h dom mon dow command
m – The minute when the cron job will run (0-59)
h - a numeric value determining the hour when the tasks will run (0-23)
dom – Day of the Month when the cron job will run (1-31)
mon - The month when the cron job will run (1-12)
dow – Day Of the Week from 0-6 with Sunday being 0
command- The linux command you wish to execute
Q1. Schedule a task to display the following message on the monitor for every 2 minutes.
26
Q2. Schedule a task to take backup of your important file (say file f1) for every 30 minutes
27
Result- The given program has been performed successfully.
28
Program No. 9
Shell Programs
Important Hints
No space before and after the assignment operator Ex. sum=0
Single quote ignores all special characters. Dollar sign, Back quote and Back slash are
not ignored inside Double quote. Back quote is used as command substitution. Back
slash is used to remove the special meaning of a character.
Arithmetic expression can be written as follows : i=$((i+1) or i=$(expr $i + 1)
Command line arguments are referred inside the programme as $1, $2, ..and so on
$* represents all arguments, $# specifies the number of arguments
read statement is used to get input from input device. Ex. read a b
if [ condition ] then
...
elif [ condition ] then
... else
...
fi
29
Syntax for While loop
while commandtdo
...
...
done
30
Program No. 10
Pipes
Pipe is a communication medium between two or more processes. The system call for
creating pipe is
int pipe(int p[2]);
This system call would create a pipe for one-way communication i.e., it creates two
descriptors, first one is connected to read from the pipe and other one is connected to write
into the pipe.
Descriptor p[0] is for reading and p[1] is for writing. Whatever is written into p[1] can be read
from p[0].
Output:
31
Implementing command line pipe using exec() family of functions00
32