Professional Documents
Culture Documents
SUBMITTED BY
Peeyush Bhardwaj
SG-17333
5. Shell Programing
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.
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.
DISADVANTAGES
1. Reliability problem.
2. One must have to take care of security and integrity of user programs and data.
3. Data communication problem.
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
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
DISADVANTAGES
Real-time systems are used when there are time requirements are very strict like
missile systems, air traffic control systems, robots etc.
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
DISADVANTAGES
What is Linux?
· 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 Utility:
System Utility programs are responsible for doing specialized, individual level
tasks.
UBUNTU
It’s FEATURES
a) Nano: This command is used to create a new file and it can also use to display a
file.
Creating files:-
Displaying files:-
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:-
OUTPUT
Syntax: rm <filename>
various options can be used with rm command to extend its operations. Some
operations are:-
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.
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
/home/linux/Documents/DS
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.
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.
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
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.
a) Looping statements:-
b) Branching statements:-
code: OUTPUT
echo enter 1st number
read a enter 1st number
echo enter 2nd number
read b 3.4
code:
for item in * OUTPUT
do Documents
if [ -d $item ] Downloads
then Music
echo $item Pictures
fi Public
done Templates
Videos
snap
command:
#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";
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;
OUTPUT
#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
#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
1. process creation:-
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;
}
#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;
}