0% found this document useful (0 votes)
8 views

OSLabManual 1 3

exp 1 to 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

OSLabManual 1 3

exp 1 to 3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Ex. No.

1A BOOTING PROCESS OF LINUX Date:

Aim:
To boot Linux Operating System.

Algorithm:
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.

Power On

Basic Input/Output System executes


BIOS
MBR

Master Boot Record executes GRUB


MBR

Grand Unified Bootloader executes


GRUB
Kernel
executes /sbin/init

Kernel Init

Init
Kernel executes runlevel programs

Runlevel programs are executed from


Runlevel
/etc/rc.d/rc*.d/

Login 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. 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 initrd images.

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
1 – halt
2 – Single user mode
3 –Multiuser,without NFS
4 – Full multiuser mode
5 – unused
6 – X11
7 – 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.

Step 6. Run level programs


● When the Linux system is booting up, you might see various services getting started. For example, it
might say “starting sendmail …. OK”. Those are the runlevel programs, executed from the run level
directory as defined by your run level.
● Depending on your default init level setting, the system will execute the programs from one of the
following directories.
▪ Run level 0 – /etc/rc.d/rc0.d/
▪ Run level 1 – /etc/rc.d/rc1.d/
▪ Run level 2 – /etc/rc.d/rc2.d/
▪ Run level 3 – /etc/rc.d/rc3.d/
▪ Run level 4 – /etc/rc.d/rc4.d/
▪ Run level 5 – /etc/rc.d/rc5.d/
▪ Run level 6 – /etc/rc.d/rc6.d/
● Please note that there are also symbolic links available for these directory under /etc directly. So,
/etc/rc0.d is linked to /etc/rc.d/rc0.d.
● Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K. ● Programs
starts with S are used during startup. S for startup.
● Programs starts with K are used during shutdown. K for kill.
● There are numbers right next to S and K in the program names. Those are the sequence number in
which the programs should be started or killed.
● For example, S12syslog is to start the syslog deamon, which has the sequence number of 12.
S80sendmail is to start the sendmail daemon, which has the sequence number of 80. So, syslog program
will be started before sendmail.

Login Process
1. Users enter their username and password
2. The operating system confirms your name and password.
3. A "shell" is created for you based on your entry in the "/etc/passwd" file 4. You are
"placed" in your "home"directory.
5. 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. 6. Additional information is read from
the file named ".profile" that is located in your "home" directory. This file is known as your personal login
file.

Result:
Hence the Linux Operating system was booted successfully and the execution of basic
linux commands in JS Linux online compiler was successfully completed
Ex. No. 1B BASIC LINUX COMMANDS Date:

Aim:
To execute basic linux commands in JS Linux online compiler

Algorithm:
Basics
1. echo SRM
→ to display the string SRM
2. clear → to clear the screen
3. date → to display the current date and time
cal 6 2003
4. cal 2003 → to display the calendar for the year 2003 → to display
the calendar for the June-2003 5. passwd → to change password

a) Working with Files


1. ls ls –l
→ list files in the present working directory → list files with detailed
information (long list)
ls –a
→ list all files including the hidden files
2. cat f1 → display the content of the file f1
wc –c f1
3. wc f1 → list no. of characters, words & lines of a file f1 → list only no. of
characters of file f1
wc –w f1
→ list only no. of words of file f1
wc –l f1
→ list only no. of lines of file f1
4. cp f1 f2 → copy file f1 into f2
5. mv f1 f2 → rename file f1 as f2

6. rm f1 remove the file f1
7. head –5 f1 → list first 5 lines of the file f1
tail –5 f1
→ list last 5 lines of the file f1

b) Working with Directories


1. mkdir elias
→ to create the directory elias
2. cd elias
→ to change the directory as elias
3. rmdir elias
→ to remove the directory elias
4. pwd → to display the path of the present working directory 5. cd → to go to the home
directory
cd ..
→ to go to the parent directory
cd -
→ to go to the previous working directory
cd /
→ to go to the root directory

c) File name substitution


1. ls f? →
→ list files start with ‘f’ and followed by any one character 2.ls *.c list files
with extension ‘c’
3.ls [gpy]et → →
list files whose first letter is any one of the characters g, p or y and
followed by the word et
4.ls[a-d,l-m]ring → listfiles whose first letter is any one of the characters from a to d and l
to m and followed by the word ring.
d) I/O Redirection
1. Input redirection
wc –l < ex1 file ‘ex1’
→ To find the number of lines of the
2.Outputredirecti
on
who > f2 cat > f1
→ the output of ‘who’ will be redirected to file f2 → to create a file. That
is standard input data will be redirected to the file f1
3. cat >> f1 → to append more into the file f1

e) Piping
Syntax : Command1 | command2
Output of the command1 is transferred to the command2 as input. Finally output of the command2
will be displayed on the monitor.
ex. cat f1 | more →list the contents of file f1 screen by screen head –6 f1 |tail –2 →prints the 5th & 6th
lines of the file f1. cal 2015 | wc -c → counts the number of characters in the displaying the cal g)
Environment variables
1. echo $HOME
→ display the path of the home directory
2. echo $PS1 → display the prompt string $
3. echo $PS2 → display the second prompt string ( > symbol by default ) 4. echo $LOGNAME
→ login name
5. echo $PATH
→ List of pathname where the OS searches for an executable file

h) File Permission
-- chmod command is used to change the access permission of a file. Method-1
Syntax : chmod [ugo] [+/-] [ rwxa ] filename
u : user, g : group, o : others
+ : Add permission - : Remove the permission
r : read, w : write, x : execute, a : all permissions
ex. chmod ug+rw f1
adding ‘read & write’ permissions of file f1 to both user and group members. Method-2
Syntax : chmod octnum file1
The 3 digit octal number represents as follows
● first digit -- file permissions for the user
● second digit -- file permissions for the group
● third digit -- file permissions for others
Each digit is specified as the sum of following
4 – read permission,2 – write permission, 1 – execute ex. chmod 754 f1 it change the file
permission for the file as follows
● read, write & execute permissions for the user ie; 4+2+1 = 7 ● read, & execute permissions
for the group members ie; 4+0+1 = 5 ● only read permission for others ie; 4+0+0 = 4

Result:
The execution of basic linux commands in JS Linux online compiler was successfully completed
and the output was verified.
Ex. No. 2A LINUX FILE SYSTEM Date:

Aim:
To execute file system commands in JS Linux through Online JS linux compiler.

Algorithm:
Linux File System
Linux File System or any file system generally is a layer which is under the operating system that
handles the positioning of your data on the storage, without it; the system cannot knows which file starts
from where and ends where.
Linux offers many file systems types like:
● Ext: an old one and no longer used due to limitations.
● Ext2: first Linux file system that allows 2 terabytes of data allowed. ● Ext3: came from Ext2, but with
upgrades and backward compatibility. ● Ext4: faster and allow large files with significant speed.(Best Linux
File System) It is a very
good option for SSD disks and you notice when you try to install any Linux distro that this one is the
default file system that Linux suggests.
● JFS: old file system made by IBM. It works very well with small and big files, but it failed and files
corrupted after long time use, reports say.
● XFS: old file system and works slowly with small files.
● Btrfs: made by Oracle. It is not stable as Ext in some distros, but you can say that it is a replacement
for it if you have to. It has a good performance.

File System Structure


The following table provides a short overview of the most important higher-level directories you find
on a Linux system
Director Contents
y

/ Root directory—the starting point of the directory tree.

/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.

/root Home directory for the superuser root.

/sbin Essential system binaries.

/srv Data for services provided by the system.

/tmp Temporary files.

/usr Secondary hierarchy with read-only data.

/var Variable data such as log files


Result :
Hence the linux file system commands were executed using JS Linux and the output was
verified.
Ex. No. 2B EDITORS AND FILTERS Date :

Aim:
To study the various editors and filtering commands of linux OS and execute them in JS Linux

Algorithm:
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

a) 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
o (small o) open new line after the current line to type text
O (capital O) open new line before the current line to type text

b) Cursor movements
h left
j down
k up
l right
(The arrow keys usually work also)
^F forward one screen
^B back one screen
^D down half screen
^U up half screen
(^ indicates control key; case does not matter)
0 (zero) beginning of line
$ end of line

c)) Deleting text


Note : (n) indicates a number, and is optional
dd deletes current line
(n)dd deletes (n) line(s) ex. 5
dd deletes 5 lines
(n)dw deletes (n) word(s)
D deletes from cursor to end of line
x deletes current character (n)
x deletes (n) character(s)
X deletes previous character

d) Saving files
:w to save & resume editing (write & resume)
:wq to save & exit (write & quit)
:q! quit without save
e) Cut, Copy and Paste
yy copies current line
(n) yy copies (n) lines from the current line. ex. 4yy copies 4 lines.
p paste deleted or yanked (copied) lines after the cursor

FILTERS

1. cut : Used to cut characters or fileds from a file/input


Syntax : cut -cchars filename
-f fieldnos filename
By default, tab is the filed separator(delimiter). If the fileds of the files are separated by any other
character, we need to specify explicitly by –d option
cut -ddelimitchar -ffileds filname

2. paste : Paste files vertically. That is nth line of first file and nth line of second file are pasted as
the n line of result
th

Syntax : paste file1 file2


-ddchar option 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 : Used to translate characters from standard input


Syntax : tr char1 char2 < filename
It translates char1 into char2 in file filename

Octal representation characters can also be used


Octal value Character
‘\7’ Bell
‘\10’ Backspace
‘\11’ Tab
‘\12’ Newline
‘\33’ Escape
Ex. tr : ‘\11’ < f1
translates all : into tab of f ile f1
-s Option translate multiple occurrences of a character by single character. -d Option is to
delete a character

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 files 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
-r option sorts the file in descending order
-u eliminates duplicate lines
-o filename writes sorted data into the file fname
-tdchar sorts the file in which fileds are separated by dchar
-n sorts the data as number
+1n skip first filed and sort the file by second filed numerically 6. Uniq : Displays unique lines

of a sorted file

Syntax : uniq filename


-d option displays only the duplicate lines
-c displays unique lines with no. of occurrences.

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

Three columns of output will be displayed.


First column displays the lines that are unique to file1
Second column displays the lines that are unique to file2
Third column displays the lines that are appears in both the files
-1 option suppress first column
-2 option suppress second column
-3 option suppress third column
-12 option display only third column
-13 option display only second column
-23 option display only first column

Result:
Hence the editors and filtering commands were successfully executed and the output was verified
using JS Linux .
Ex. No. 3A COMPILATION OF C PROGRAM Date :

Aim:
To compile a C program by file creation in Linux OS and execute it using appropriate linux commands.

Algorithm:
Compilation of C Program
Step 1 : Open the terminal and edit your program using vi editor/gedit editor and save
withextension “.c”
Ex. vi test.c
(or) gedit text.c

Step 2 : Compile your program using gcc compiler


Ex. gcc test.c 🡪 Output file will be “a.out”
(or)
gcc –o test text.c 🡪Output file will be “test”

Step 3 : Correct the errors if any and run the program Ex. ./a.out
(or)
./test
Optional Step : In order to avoid ./ prefix each time a program is to be executed, insert thefollowing
as the last line in the file .profile export PATH=.:$PATH This Step needs only to be done once.

Debug C Programs using gdb debugger


Step 1 : Compile C program with debugging
option –gEx. gcc –g test.c

Step 2 : Launch gdb. You will get gdb


promptEx. gdb a.out

Step 3 : Step break points inside C program


Ex. (gdb) b 10

Break points set up at line number 10. We can have any number of break

pointsStep 4 : Run the program inside gdb


Ex. (gdb) r

Step 5 : Print variable to get the intermediate values of the variables at break point Ex. (gdb) p i 🡪
Prints the value of the variable ‘i'
Step 6 : Continue or stepping over the program using the following gdb
commandsc 🡪 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 linel 🡪List the program statements
Step 7 : Quit the debugger (gdb) q

Result: Hence the c program was successfully compiled and the output was verified.
Ex. No. 3B PROCESS CREATION Date :

Aim:
To create a process and check how the child and the parent processes work usingvarious
commands in JS linux .

Algorithm:
Process creation
1. fork() causes the UNIX system to create a new process, called the "child process", with a new
process ID.
2. The contents of the child process are identical to the contents of the parentprocess.

3. The prototype for the fork() system


call is:int fork()
Returns 0 in child process and child process ID in parent process.

4. Other Related Functions


int getpid() 🡪 returns the current process ID int getppid() 🡪 returns the parent
process ID wait() 🡪 makes a process wait for other process to complete

Virtual fork vfork() function is similar to fork but both processes shares the same address space.

Q1. Find the output of the following program #include <sys/types.h>


#include <unistd.h>
#include<stdio.h>
main()
{
int pid,ppid;
pid=getpid();
ppid=getppid();
printf("\n Process ID is %d\n",pid);
printf("\n Parent Process ID is %d\n",ppid);
}
Output :
Q2. Find the output of the following program and write your understanding #include <sys/types.h>
#include <unistd.h>
main()
{
printf("\n This is to demonstrate the fork()\n");
fork();
printf("\nAfter fork()\n");
}
Output

Q3. Write the output the following program and brief your understanding #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;
printf("In Parent a=%d b=%d \n",a,b);
} return 0;
}
Output :

Q4. Rewrite the program in Q1 using vfork() and write the output

#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=vfork();
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;
printf("In Parent a=%d b=%d \n",a,b);
} return 0;
}

The address space of child is used by parent, hence have a problem in accessing variable
Q5. Calculate the number of times the text “SRMIST” is printed and write your
inference in this.

#include <stdio.h>
#include<unistd.h>
int main()
{ fork(); fork(); fork();
printf(“SRMIST\n”); return
0;

}
Output :

prints for 2n times

Q6. Complete the following program as described below :-


The child process calculates the sum of odd numbers and the parent process calculate the sum of
even numbers up to the number ‘n’. Ensure the Parent process waits for the child process to finish.
Output

Q7. How many child processes are created for the following code?

Hint : Check with small values of ‘n’.


for (i=0; i<n; i++)
fork();
Output :
If we sum all levels of above tree for i = 0 to n-1, we get 2n - 1. So there will be 2n – 1 child processes.
On the other hand, the total number of process created are (number of child processes)+1.
And this may vary because of fork failures

Q8. Write a program to print the Child process ID and Parent process ID in both Child and
Parent processes
Output:

Q9. How many child processes are created for the following code? Brief your
inference#include <stdio.h>
#include<unistd.h>
int main()
{ fork();
fork()&&fork()||fork();
fork(); printf(“Yes”);
return 0;

Output :

20 times Yes will be printed

Q10. Write the output of the following


#include <unistd.h>
#include <sys/types.h>
#include<stdio.h>
main()
{
execl("/bin/date", "date", NULL);
}

Output

Result :
The programs given are executed successfully.

You might also like