You are on page 1of 27

PRACTICAL FILE

SUBMITTED BY
Peeyush Bhardwaj

SG-17333

C.S.E 4th SEM


INDEX

1. Types of Operating Systems

2. Familiarization with Linux

3. Linux Basic commands

4. Introduction to Vim Editor

5. Shell Programing

6. CPU Scheduling Programs

7. Process creation, wait and Semaphores


EXPERIMENT 1
Types of Operating Systems

OPERATING SYSTEM
An Operating System acts as an interface between user and the machine. Operating
system generally manages the hardware and performs all the basic tasks like
managing file, process, and memory. Thus operating system acts as manager of all
the resources, i.e. resource manager.

Various types of operating systems


1. Batch Operating System
This types of OS do not interact with the computer directly. There is an operator which
takes similar jobs having same requirements and group them into batches.

ADVANTAGES
1. Multiple users can share the batch systems.
2. The idle time batch system is very less.

DISADVANTAGES
1. Batch systems are hard to debug.
2. It is sometime costly.
3. The other jobs will have to wait for an unknown time if any job fails.

2. Time-Sharing Operating System


In this OS each task has given some time to execute, so that all the tasks work
smoothly, i.e. multiple tasks are executed by shifting the CPU among them so that
user can interact with a task while it is running.
ADVANTAGES
1. Each task gets an equal opportunity.
2. CPU idle time can be reduced.

DISADVANTAGES

1. Reliability problem.
2. One must have to take care of security and integrity of user programs and data.
3. Data communication problem.

3. Distributed Operating System

Distributed Operating System is a model where distributed applications are running on


multiple computers linked by communications. This system looks to its users like an
ordinary centralized operating system but runs on multiple, independent central
processing units (CPUs). These systems are referred as loosely coupled systems
where each processor has its own local memory and processors communicate with
one another through various communication lines, such as high speed buses or
telephone lines.

ADVANTAGES

1. Failure of one will not affect the other network communication, as all systems are
independent from each other.
2. Since resources are being shared, computation is highly fast and durable.
3. Load on host computer reduces.

DISADVANTAGES

1. Failure of the main network will stop the entire communication


2. To establish distributed systems the language which are used are not well defined
yet

4. Network Operating System

These systems runs on a server and provides the capability to manage data, users,
groups, security, applications, and other networking functions. These type of operating
systems allows shared access of files, printers, security, applications, and other
networking functions over a small private network.
ADVANTAGES

1. Highly stable centralized servers.


2. Security concerns are handled through servers.

DISADVANTAGES

1. Servers are costly.


2. User has to depend on central location for most operations.
3. Maintenance and updates are required regularly.

5. Real-Time Operating System

Real-time systems are used when there are time requirements are very strict like
missile systems, air traffic control systems, robots etc.

Two types of Real-Time Operating System which are as follows:

1. Hard Real-Time Systems: These OSs are meant for the applications where time
constraints are very strict and even the shortest possible delay is not acceptable.

2. Soft Real-Time Systems: These OSs are for applications where for time-
constraint is less strict.

ADVANTAGES

1. Maximum consumption of resources.


2. Focus on single application at one time.
3. Error free.
4. These are embedded systems.

DISADVANTAGES

1. It can perform limited number of tasks at the same time.


2. The algorithms for these systems is very complex.
3. These systems uses heavy system resources.
EXPERIMENT 2
Familiarization with Linux

What is Linux?

Linux is an operating system or a kernel, which is based on UNIX Operating System.


It is open source as its source code is freely available. It is free to use. Linux was
designed considering UNIX compatibility. Its functionality list is quite similar to that of
UNIX.

Linux is typically packaged in a Linux distribution or distro. Distributions include Linux


kernel and supporting system software and libraries, many of which are provided by
the GNU project.

Popular Linux ditros are DEBIAN, FEDORA and UBUNTU.

Components of Linux System


Linux Operating System has three components:

· Kernel:

Kernel is the core part of Linux. It is responsible for all major activities of this
operating system. It is consists of various modules and it interacts directly with
the underlying hardware. Kernel provides the required abstraction to hide low
level hardware details to system or application programs.

· System Library:

System libraries are special functions or programs using which application


programs or system utilities accesses Kernel's features. These libraries
implement most of the functionalities of the operating system and do not
require kernel module's code access rights.

· System Utility:

System Utility programs are responsible for doing specialized, individual level
tasks.
UBUNTU

Ubuntu is a free and open-source Linux distribution based on Debian. It is developed


by Canonical and the community under a meritocratic governance model Canonical
provides security updates and support for each Ubuntu release, starting from the
release date and until the release reaches its designated end-of-life.

It’s FEATURES

1. Latest Ubuntu uses GNOME as its default


Desktop interface.

2. Most of the work on Ubuntu or any


other distribution can be done using
its command line.
e.

3. Ubuntu offers Thousands of app, Free to use.


EXPERIMENT 3
Linux Basic Commands

1. File related commands

a) Nano: This command is used to create a new file and it can also use to display a
file.

Creating files:-

syntax: nano <filename>

Eg: nano abc.txt // abc is a text file

After creating a file, blank screen appears


in which user can type the contents of a file.

Displaying files:-

syntax: nano <filename>

Eg: nano abc.txt // displaying “abc” text file

b) ls (listing directory):-

This command is used to display the list of files and directories in the current directory
that do not begin with (.) i.e. hidden files and directories

various options can be used with ls command to extend its operations. Some
operations are:-

i) ls -a: It list all the files of the current directory.


ii) ls -l: It list the files and diretories of current directory in long format,including file
permissions size and data information.
iii) ls -s: List files and directories of current directory with its size in KB.
iv) ls -R: List the enteries recursively,including current directory & its subdirectories.

Eg: executing ls -s command

OUTPUT

4 abc.txt 4 Downloads 4 Pictures 4 Templates


4 Desktop 12 examples.desktop 4 Public 4 Videos
4 Documents 4 Music 4 snap
c) cp command: This command is used to copy the content of one into another.

Syntax: cp <source filename> <destination filename>

Eg: cp abc.txt ab.txt

In given example, abc.txt is a source file and ab.txt is a destination file .


When cp command is executed , content of abc file is copied to ab file.

d) mv command: This command is generally used to move the file or content of


one file into another file or in another directory. When this command is executed,
the source file or the original one is removed.

Syntax: mv <filename/directory> <filename/directory>

Eg: mv abc.txt ab.txt // moving the content of abc file into ab

mv ab.txt Documents // moving ab file into Document directory.

e) rm command: This command is generally used to delete files.

Syntax: rm <filename>

Eg: rm abc.txt // removing abc file from system

various options can be used with rm command to extend its operations. Some
operations are:-

1. rm -r: Recursively remove directories and subdirectories.


2. rm -f: Remove all files forcefully without prompting the user.
3. rm -i: This is used to ask the user to whether to remove file or not before
actually removing it.

f) chmod command: This command is generally used to change the accessing


mode of a file i.e. change the permissions of a file. Two ways of using chmod

1. symbolic method
2. octal method

1. Symbolic method: Every file or directing in a Linux file system has 3 types of
permissions that define whether certain actions can be carried out. These
permissions are:-

a) read(r):- If file is set for ‘read’ then the user can read its contents or make a copy
of it.
b) write(w):- The user can alter or reove the contents of that file. For directory, user
can delete and create files in that directory.
c) execute(x):- If the user has execute permissions then the user can execute the
file.

Eg: chmod u+x abc.txt OUTPUT

chmod g+w abc.txt -rwxrw-r-x 1 linux linux 16 Apr 19 15:33 abc.txt

chmod o+x abc.txt

2. Octal method: In this method the permissions are given using a squence of 3
octal digits.

0= none
1= Execute
2= Write
4= Read

Eg: chmod 765 abc.txt OUTPUT

-rwxrw-r-x 1 linux linux 16 Apr 19 15:33 abc.txt

g) pwd: Thid command gives the path of present working directory.

Eg: executing pwd command OUTPUT

/home/linux/Documents/DS

h) mkdir command: It is used to create new directories or subdirectories.The


name of the directory must be less than 14 characters.

Syntax: mkdir <directory name>

Eg: mkdir abc // creating a directory abc

i) rmdir command: It is used to delete directories or subdirectories.

Syntax: rmdir <path> <directory name>

Eg: rmdir Documents/DS/g // deleting ‘g’ directory


EXPERIMENT 4

Introduction to Vim editor

Vim Editor

Vim is an editor to create or edit a text file.There are two modes in vim, one is the
command mode and another is the insert mode.

In the command mode, user can move around the file,delete,text etc.

In the insert mode user can insert text.

Changing mode from one to another


From command mode to insert mode type a/A/i/I/o/O
From insert mode to command mode type Esc

Some useful commands for VIM


Text Entry Commands (Used to start text entry)
a Append text following current cursor position
A Append text to the end of current line
i Insert text before the current cursor position
I Insert text at the beginning of the cursor line
o Open up a new line following the current line and add text there
O Open up a new line in front of the current line and add text there

Some Cursor Movement Commands (for command mode only)


h Moves the cursor one character to the left
l Moves the cursor one character to the right
k Moves the cursor up one line
j Moves the cursor down one line
$ Move cursor to the end of current line
0 (zero) Move cursor to the beginning of current lin
w Forward one word
b Backward one word
Exit Commands
:wq Write file to disk and quit the editor
:q! Quit (no warning)
:q Quit (a warning is printed if a modified file has not been saved)
ZZ Save workspace and quit the editor (same as :wq)

Text Deletion Commands


x Delete character
dw Delete word from cursor on
db Delete word backward
dd Delete line
d$ Delete to end of line
d^ (d caret, not CTRL d) Delete to beginning of line

Yank (VI's copy command):-


yy yank current line
y$ yank to end of current line from cursor
yw yank from cursor to end of current word
5yy yank, for example, 5 lines

Paste (used after delete or yank to recover lines.)


p paste below cursor
P paste above cursor
u Undo last change
U Restore line
J Join next line down to the end of the current line

File Manipulation Commands


:w Write workspace to original file
:e file Start editing a new file
:r file Read contents of a file to the workspace
EXPERIMENT 5
Shell Programing

what is shell?
A shell is special user program which provide an interface to user to use operating
system services. Shell accept human readable commands from user and convert them
into something which kernel can understand. It is a command language interpreter
that execute commands read from input devices such as keyboards or from files.

There are several shells are available for Linux systems like –
1. BASH (Bourne Again SHell)– It is most widely used shell in Linux systems. It is
used as default login shell in Linux systems and in macOS. It can also be installed
on Windows OS.

2. CSH (C SHell)– The C shell’s syntax and usage are very similar to the C
programming language.

3. KSH (Korn SHell)– The Korn Shell also was the base for the POSIX Shell standar
specifications etc.

Shell Scripting
Usually shells are interactive that mean, they accept command as input from users
and execute them. However some time we want to execute a bunch of commands
routinely, so we have type in all commands each time in terminal. As shell can also
take commands as input from file we can write these commands in a file and can
execute them in shell to avoid this repetitive work. These files are called Shell Scripts
or shell Programs.

A shell script comprises following elements –


1. Shell Keywords – if, else, break etc.
2. Shell commands – cd, ls, echo, pwd, touch etc.
3. Functions
4. Control flow – if..then..else, case and shell loops etc.

Shell Variables
Shell variables provide ability to store and manipulate information withen a shell script.
The shell enables us to create, assign and delete variables.
a) Creating a variable: To create a variable, simply write the its name followed by ‘=’
and again followed by the value.

Eg: a=30

b) Accessing a variable: To access the value stored in a variable, prefix its name
with the dollar sign.

Eg: echo $a

c) Deleting a variable: Use ‘unset’ command to delete a variable.

Eg: unset a

Echo command
echo command in linux is used to display line of text/string that are passed as an
argument . This is a built in command that is mostly used in shell scripts and batch
files to output status text to the screen or a file.

Syntax: echo [option] [string]

Flow control statements

a) Looping statements:-

i) while loop ii) for loop

Syntax: while condition Syntax: for var in arg1,arg2.....argn


do do
commands command
done done

b) Branching statements:-

i) if statement \ii) if-else statement

Syntax: if condition Syntax: if condition


then then
statement statement
fi else
statement
fi
Shell Scripts:

1. Write a script to compare two simple numbers


OUTPUT
code:
echo enter 1st number enter 1st number
read a 4
echo enter 2nd number enter 2nd number
read b
7
if [ $a -gt $b ] b is greater than a
then
echo a is greater than b
else
echo b is greater than a
fi

2. Write a script to compare two real numbers

code: OUTPUT
echo enter 1st number
read a enter 1st number
echo enter 2nd number
read b 3.4

var=$(awk 'BEGIN{ print "'$a'"<"'$b'" }') enter 2nd number


4.4
if [ "$var" -eq 1 ];
then a and b are not same
echo "a and b are not same"
else
echo "a and b are same"
fi
3. Write a script to print the directories in a home folder

code:
for item in * OUTPUT
do Documents
if [ -d $item ] Downloads
then Music
echo $item Pictures
fi Public
done Templates
Videos
snap

4. Write a script to search/count a particular character in a text file

command:

grep -c “This” abc.txt OUTPUT


2 “is the count of ‘This’ in the text file”
EXPERIMENT 6

CPU Scheduling programs

1. FCFS CPU SCHEDULING

#include<iostream>
using namespace std;
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,int bt[], int wt[])
{ // waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
void findTurnAroundTime( int processes[], int n,int bt[], int wt[], int tat[])
{ // calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void findavgTime( int processes[], int n, int bt[])
{ int wt[n], tat[n], total_wt = 0, total_tat = 0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
cout << "Processes "<< " Burst time " << " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn


// around time
for (int i=0; i<n; i++)
{ total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
out << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];

//Burst time of all processes


int burst_time[] = {10, 5, 8};

findavgTime(processes, n, burst_time);
return 0;
}

OUTPUT
Processes Burst time Waiting time Turn around time
1 10 0 10
2 5 10 15
3 8 15 23
Average waiting time = 8.33333
Average turn around time = 16
2. SJF CPU SCHEDULING

#include<stdio.h>
void main()
{ int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{ printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{ pos=i;
for(j=i+1;j<n;j++)
{ if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

OUTPUT

Enter number of process:4


Enter Burst Time:
p1:4
p2:3
p3:7
p4:2
Process Burst Time Waiting Time Turnaround Time
p4 2 0 2
p2 3 2 5
p1 4 5 9
p3 7 9 16
3. PRIORITY SCHEDULING

#include<iostream>
using namespace std;
int main()
{ int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total Number of Process:";
cin>>n;
cout<<"\nEnter Burst Time and Priority\n";
for(i=0;i<n;i++)
{cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; //contains process number
}
for(i=0;i<n;i++)
{pos=i;
for(j=i+1;j<n;j++)
{if(pr[j]<pr[pos])
pos=j;}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
}
avg_tat=total/n; //average turnaround time
cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;
return 0;}

OUTPUT
Enter Total Number of Process:4
Enter Burst Time and Priority

P[1] P[2] P[3] P[4]


Burst Time:3 Priority:2 Burst Time:8 Priority:1 Burst Time:7 Priority:5 Burst Time:1
Priority:0

Process Burst Time Waiting Time Turnaround Time


P[4] 1 0 1
P[2] 8 1 9
P[1] 3 9 12
P[3] 7 12 19
Average Waiting Time=5
Average Turnaround Time=10
4. ROUND ROBIN SCHEDULING

#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum
}
if(rt[count]==0 && flag==1)
{remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}

OUTPUT
Enter Total Process: 4
Enter Arrival Time and Burst Time for Process Process Number 1 :0
9
Enter Arrival Time and Burst Time for Process Process Number 2 :1
5
Enter Arrival Time and Burst Time for Process Process Number 3 :2
3
Enter Arrival Time and Burst Time for Process Process Number 4 :3
4
Enter Time Quantum: 5

Process | Turnaround Time | Waiting Time


P[2] | 9 | 4
P[3] | 11 | 8
P[4] | 14 | 10
P[1] | 21 | 12
Average Waiting Time= 8.500000
Avg Turnaround Time = 13.750000
EXPERIMENT 7

Process creation, wait and Semaphores

1. process creation:-

a) #include <stdio.h> OUTPUT


#include <sys/types.h> Hello world!
#include <unistd.h> Hello world!
int main()
{ fork();
printf("Hello world!\n");
return 0;
}

b) #include <stdio.h>
#include <sys/types.h>
#include <unistd.h> OUTPUT
void forkexample() Hello from Parent!
{ Hello from Child!
if (fork() == 0)
printf("Hello from Child!\n");
else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}
2. Make a process wait:-

#include <stdio.h>
#include <sys/wait.h> OUTPUT
#include <unistd.h> HP: Hello from Parent!
int main() HC: Hello from Child!
{ if (fork() == 0) Bye
printf("HC: Hello from Child!\n"); CT: child has terminated
else Bye
{ printf("HP: Hello from Parent!\n");
wait(NULL);
printf("CT: child has terminated\n");
}
printf("Bye \n");
return 0;
}

3. Program to demonstrate writing of semaphores

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h> OUTPUT
#include <unistd.h> Entered..
sem_t mutex; Just Exiting...
void* thread(void* arg) Entered..
{ Just Exiting...
//wait
sem_wait(&mutex);
printf("\nEntered..\n");

//critical section
sleep(4);
//signal
printf("\nJust Exiting...\n");
sem_post(&mutex);
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL);
sleep(2);
pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
sem_destroy(&mutex);
return 0;
}

You might also like