Basic UNIX Commands Overview
Basic UNIX Commands Overview
AIM:
To study the purpose and usage of some basic commands
2. DIRECTORY COMMANDS
i) To create a directory
Syntax: $ mkdir dirname
Example: $ mkdir peacock
ii) To change the name of the directory
Syntax: $ cd dirname
Example: $ cd flower
iii) To remove the directory
Syntax: $ rmdir dirname
Example: $ rmdir flower
3. FILE COMMANDS
i) To create a file.
Syntax: $ cat>filename
Example: $ cat>ex1
ii) To view the content of the file.
Syntax: $ cat filename
Example: $ cat ex1
iii) To append some details with the existing details in the file
Syntax: $ cat>>filename
Example: $ cat>>ex1
iv) To concatenate multiple files
Syntax: $ cat file1 file2 > file3
Example: $ cat computer compiler>world
Output: The contents of computer and compiler are merged into a single file
called world.
v) To know the list of all files in directory
Syntax: $ ls
vi) To copy the file to another file
Syntax: $ cp source destination
Example: $ cp ex1 ex2
vii) To rename the file
Syntax: $ mv oldfile newfile
Example: $ mv ex1 ex3
viii) To delete a file
Syntax: $ rm filename
Example: $ rm ex1
ix) To delete all files
Syntax: $ rm *
x) To display the filename starting with single letter
Syntax: $ echo?
xi) To display the filename starting with two letters
Syntax: $ echo??
xii) To display the filename starting with the letter f
Syntax: $ echo f*
xiii) To display the filename ending with letter f.
Syntax: $ echo *f
xiv) To display first 10 lines
Syntax: $ head [count][filename]
Example: $ head 10 ex1
xv) To display first 6 characters
Syntax: $ head -6c filename
Example: $ head -6c ex1
xvi) To display 5 lines from 2 files
Syntax: $ head -5 file1 file2
Example: $ head -5 ex1 ex2
xvii) To display last 10 lines
Syntax: $ tail [count][filename]
Example: $ tail 10 ex3
xviii) To display the number of words in a file
Syntax: $ wc filename
Example: $ wc ex1
xix) To display the number of characters in a file
Syntax: $ wc –c filename
Example: $ wc –c ex1
xx) To display the number of lines
Syntax: $ wc –l filename
Example: $ wc –l ex3
xxi) To display number of lines with numbers
Syntax: $ nl filename
Example: $ nl ex1
xxii) To provide the line number starting from s
Syntax: $ nl –v3 filename
Example: $ nl –v3 ex3
xxiii) To increment the line number by 5
Syntax: $ nl –i5 filename
Example: $ nl –i5 ex3
xxiv) To reverse and sort the content of file
Syntax: $ sort –r filename
Example: $ sort –r ex1
xxv) To sort the content of the file
Syntax: $ sort filename
Example: $ sort ex1
xxvi) To sort and remove the duplicate
Syntax: $ sort –u filename
Example: $ sort –u ex1
xxvii) To display file contents page by page
Syntax: $ more filename
APPLICATION I
Create a directory called stud, change to the stud directory. Verify whether you have
changed to stud directory. Return to your original directory
APPLICATION II
Create a file called top display the first three and last three lines of a file top in the
directory
2. SHELL PROGRAMMING
OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the two numbers
10 40
40 is greater
Ex. No.: 2b FIBONACCI SERIES
PROGRAM:
OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the value of n
5
0
1
1
2
3
PROGRAM:
echo "Enter the number"
read num
fact=1
i=1
while [ $i -le $num ]
do
fact=`expr $fact \* $i`
i=`expr $i + 1`
done
echo "The factorial of $num=$fact"
OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the number
5
The factorial of 5=120
Ex. No.: 2d SUM OF n NUMBERS
PROGRAM:
echo "Enter the number"
read num
i=1
sum=0
while [ $i -le $num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum is $sum"
OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the number
10
The sum is 55
PROGRAM:
echo "Sorting of n numbers"
echo "Enter the numbers"
cat>p
echo "ascending order"
sort -n p
echo "Descending Order"
sort -nr p
OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Sorting of n numbers
Enter the numbers
12 23 25 24 26 15 14 16 13 (Ctrl+D)
ascending order
12 13 14 15 16 23 24 25 26
Descending Order
26 25 24 23 16 15 14 13 12
PROGRAM:
echo “Enter the number”
read num
k=2
i=0
c=num/2
while[$k –lt $c]
do
r=$[n%k]
if[$r –eq $i]
then
echo “Composite”
exit
else
k=$[k+1]
fi
done
echo “Prime no”
OUTPUT:
[netlab@system-2 oslab]$ sh [Link]
Enter the number
11
Prime no
25
Composite
PROGRAM:
echo "Menu"
echo "[Link] a long list of file"
echo "[Link] files from the directory"
echo "[Link] a file"
read choice
case $choice in
1) ls -l;;
2) echo "Enter the file to be deleted"
read file
rm $file
echo "The file has been deleted";;
3) echo "Enter the name of the file to be inserted"
read new
echo "Enter the text"
cat>>new
echo "The file has been appended";;
esac
OUTPUT:
[netlab@system-2 oslab]$ cat>new
Good Morning
[netlab@system-2 oslab]$ sh [Link]
Menu
[Link] a long list of file
[Link] files from the directory
[Link] a file
1
total 100
-rw-rw-r--. 1 netlab netlab 236 2013-01-05 19:54 [Link]
-rw-rw-r--. 1 netlab netlab 149 2013-01-05 19:52 [Link]~
-rw-rw-r--. 1 netlab netlab 215 2013-01-05 19:49 [Link]
-rw-rw-r--. 1 netlab netlab 137 2013-01-05 19:48 [Link]~
-rw-rw-r--. 1 netlab netlab 193 2013-01-05 19:40 [Link]
-rw-rw-r--. 1 netlab netlab 110 2013-01-05 19:39 [Link]~
-rw-rw-r--. 1 netlab netlab 378 2013-01-05 20:06 [Link]
-rw-rw-r--. 1 netlab netlab 13 2013-01-05 20:07 new
-rw-rw-r--. 1 netlab netlab 32801 2013-01-05 19:20 OPERATING SYSTEM LAB
[Link]
-rw-rw-r--. 1 netlab netlab 13979 2013-01-05 19:55 OPERATING SYSTEM LAB [Link]
-rw-rw-r--. 1 netlab netlab 0 2013-01-05 20:06 p
-rw-rw-r--. 1 netlab netlab 326 2013-01-05 20:01 [Link]
-rw-rw-r--. 1 netlab netlab 127 2013-01-05 20:00 [Link]~
-rw-rw-r--. 1 netlab netlab 212 2013-01-05 19:58 [Link]
-rw-rw-r--. 1 netlab netlab 133 2013-01-05 19:56 [Link]~
OUTPUT:
[cse2a@sys-d02~] $ vi fcfs.c
[cse2a@sys-d02~] $ cc fcfs.c
[cse2a@sys-d02~] $ ./[Link]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & execution time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}
OUTPUT:
Input:
Output:
#include<stdio.h>
#include<math.h>
main()
{
int i,j,n,t;
int ari[10],run[10],ft[10],wt[10],ta[10],st[10],pri[10],name[10];
int avgwt,avgta,totwt,totta;
totwt=totta=0;
printf("\n enter the [Link] jobs");
scanf("%d",&n);
printf("\n enter the priority");
for(i=0;i<n;i++)
scanf("%d",&pri[i]);
printf("\n enter the arrival time");
for(i=0;i<n;i++)
{
scanf("%d",&ari[i]);
}
printf("\n enter the run time");
for(i=0;i<n;i++)
{
scanf("%d",&run[i]);
}
for(i=0;i<n;i++)
name[i]=i+1;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pri[i]>pri[j])
{
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
}
else
if(pri[i]==pri[j])
{
if(run[i]>run[j])
{
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
}
if(run[i]==run[j])
{
if(ari[i]>ari[j])
{
t=pri[i];
pri[i]=pri[j];
pri[j]=t;
t=name[i];
name[i]=name[j];
name[j]=t;
t=ari[i];
ari[i]=ari[j];
ari[j]=t;
t=run[i];
run[i]=run[j];
run[j]=t;
}
}
}
}
}
for(i=0;i<n;i++)
{if(i==0)
{
wt[i]=0;
st[i]=ari[i];
ft[i]=st[i]+run[i];
ta[i]=ft[i]+wt[i]-st[i];
}
else
{
st[i]=ft[i-1];
wt[i]=abs(st[i]-ari[i]);
ft[i]=st[i]+run[i];
ta[i]=ft[i]+wt[i]-st[i];
totwt=totwt+wt[i];
totta=totta+ta[i];
}
avgwt=totwt; "\n";
avgta=totta; "\n";
}
printf("\n \n job priority arrival run start waiting final turnaround");
for(i=0;i<n;i++)
{
printf("\n \n
%d\t%d\t%d\t%d\t%d\t%d\t%d\t",name[i],pri[i],ari[i],run[i],st[i],wt[i],ft[i],ta[i]);
}
printf("\n average waiting time:");
printf("%d",avgwt);
printf("\n average turnaround time");
printf("%d",avgta);
}
OUTPUT:
[cse2a@sys-d02~] $ vi priority.c
[cse2a@sys-d02~] $ cc priority.c
[cse2a@sys-d02~] $ ./[Link]
OUTPUT:
[cse2a@sys-do2~] $ viround.c
[cse2a@sys-do2~] $ cc round.c
[cse2a@sys-do2~] $ ./[Link]
Enter no. of jobs:4
Enter the arrival time:
1
2
3
5
Enter the run time:
4
5
3
2
Enter quantum number:2
Job arrival run time start waiting final turnaround
4 1 4 1 6 11 10
1 2 5 3 8 15 13
2 3 3 5 8 14 11
3 5 2 7 6 9 4
PROGRAM:
#include<string.h>
struct block
{
int b_id;
int b_allocated;
};
struct block b[50];
int main()
{
int no,i,j,n,sblock,eblock,count=0;
int psize,flag=1,pname,bname;
printf("\nEnter the no. of block: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
b[i].b_id=i;
b[i].b_allocated=0;
}
printf("\nEnter the no. of block already exists: ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nEnter the block: ");
scanf("%d",&bname);
b[bname].b_allocated=100;
}
printf("\nEnter the process name: ");
scanf("%d",&pname);
printf("\nEnter the process size: ");
scanf("%d",&psize);
for(i=0;i<n;i++)
{
if(b[i].b_allocated==0)
{
count=count+1;
if(count==1)
sblock=i;
if(count==psize)
{
eblock=i;
for(j=sblock;j<=eblock;j++)
b[j].b_allocated=pname;
printf("\n\nProcess %d is allocated %d blocks from %d to
%d\n\n",pname,sblock,eblock,sblock);
i=n+1;
flag=1;
}
}
else
{
count=0;
flag=0;
}
}
if(flag==0)
printf("\nProcess not allocated");
count=0;
for(i=0;i<n;i++)
{
count=count+1;
if(count<no)
printf("b[%d]->%d\t\t",b[i].b_id,b[i].b_allocated);
else
{
count=0;
printf("\n\n\nb[%d]-->%d\t\t",b[i].b_id,b[i].b_allocated);
count=count+1;
}
}
return 0;
}
OUTPUT:
[netlab@system-2 oslab]$ cc contiguous.c
[netlab@system-2 oslab]$ ./[Link]
PROGRAM:
#include<stdio.h>
void display();
struct block
{
int p_id;
int b_allocated;
struct block *next;
}*sblock=NULL;
int n;
struct block b[50];
int main()
{
int i,pname,psize,no,bname,freespace=0,count,previous;
printf("\nEnter the no. of blocks: ");
scanf("%d",&n);
freespace=n;
for(i=0;i<n;i++)
{
b[i].p_id=0;
b[i].b_allocated=0;
}
printf("\nEnter the no. of blocks already allocated process name: ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nEnter the block: ");
scanf("%d",&bname);
b[bname].p_id=100;
b[bname].b_allocated=1;
freespace=freespace-1;
}
display();
printf("\nEnter process name: ");
scanf("%d",&pname);
printf("\nEnter process size: ");
scanf("%d",&psize);
count=0;
if(psize<=freespace)
{
for(i=0;i<n;i++)
{
if(b[i].b_allocated==0)
{
b[i].b_allocated=1;
b[i].p_id=pname;
count++;
if(count==1)
sblock=&b[i];
else
if(count>psize)
break;
else
b[previous].next=&b[i];
previous=i;
}
}
display();
printf("\nProcess name %d\nStarting address %u\nEnding address
%u",pname,sblock,&b[i]);
}
else
printf("\nProcess cannot be allocated");
}
void display()
{
int i,count=0;
printf("\nBlocks\n");
for(i=0;i<n;i++)
{
if(count<2)
printf("%u-->\tb[%2d]->\t%2d->\t%2d->\t
%4u\t\t",&b[i],i,b[i].p_id,b[i].b_allocated,b[i].next);
else
{
count=0;
printf("\n%u-->\tb[%2d]->\t%2d->\t%2d->\t
%4u\t\t",&b[i],i,b[i].p_id,b[i].b_allocated,b[i].next);
}
count=count+1;
}
}
OUTPUT:
[netlab@system-2 oslab]$ cc linked.c
[netlab@system-2 oslab]$ ./[Link]
PROGRAM:
#include<stdio.h>
int b_allocated[30];
void display(int);
void main()
{
int j=0,index[50],indexloc,i,n,no,psize,pname,block,freespace=0,count=0;
printf("\nEnter the number of blocks: ");
scanf("%d",&n);
freespace=n;
printf("\nEnter the number of blocks that are already allocated: ");
scanf("%d",&no);
for(i=0;i<no;i++)
b_allocated[i]=0;
printf("\nEnter the blocks allocated: ");
for(i=0;i<no;i++)
{
scanf("%d",&block);
b_allocated[block]=1;
freespace=freespace-1;
}
printf("\nFreespace=%d\n",freespace);
display(n);
printf("\nEnter the name of the process: ");
scanf("%d",&pname);
printf("\nEnter the size of the process: ");
scanf("%d",&psize);
if(psize<freespace)
{
for(i=0;i<n;i++)
{
if(b_allocated[i]==0&&count<=psize)
{
if(count==psize)
{
b_allocated[i]=pname;
indexloc=i;
break;
}
else
{
b_allocated[i]=1;
index[j]=0;
index[j]=i;
j=j+1;
count=count+1;
}
}
}
display(n);
printf("\nBlocks stored in index are: \n\n");
for(i=0;i<j;i++)
printf("%d",index[i]);
printf("\nDirectory Structure\n\n");
printf("\nProcess\t\tIndex\n");
printf("\n%d\t\t%d",pname,indexloc);
}
}
void display(int n)
{
int i,count=0;
printf("\n\n");
for(i=0;i<n;i++)
if(count<3)
{
printf("\t\tb[%2d]-->%d",i,b_allocated[i]);
count=count+1;
}
else
{
printf("\n\n\t\tb[%2d]--%d",i,b_allocated[i]);
count=1;
}
}
OUTPUT:
[netlab@system-2 oslab]$ cc indexed.c
[netlab@system-2 oslab]$ ./[Link]
b[ 6]--0 b[ 7]-->0
Enter the name of the process: 3
Enter the size of the process: 2
b[ 0]-->1 b[ 1]-->1 b[ 2]-->1
b[ 6]--3 b[ 7]-->0
Blocks stored in index are:
04
Directory Structure
Process Index
3 6
5. IMPLEMENT SEMAPHORES
PROGRAM:
#include<stdio.h>
#include<string.h>
# define SIZE 10
struct process
{
char a[10];
}buffer[10];
int mutex=1, full=0, empty=SIZE, flag=0;
int wait(int);
int signal(int);
main()
{
int ch,i;
printf("\nPRODUCER - CONSUMER PROBLEM\n");
while(1)
{
printf("\nChoices are\n");
printf("1. Producer Routine\n");
printf("2. Consumer Routine\n");
printf("3. Display the contents of the buffer\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
empty=wait(empty);
mutex=wait(mutex);
if(flag==0)
{
printf("\nEnter the item to be added: ");
scanf("%s",&buffer[full]);
printf("Item inserted successfully\n\n");
full=signal(full);
}
else
{
printf("Buffer Full\n");
flag=0;
}
mutex=signal(mutex);
break;
case 2:
full=wait(full);
mutex=wait(mutex);
if(flag==0)
{
printf("\n\nItem %s is successfully consumed\n",
buffer[0].a);
for(i=0;i<SIZE;i++)
strcpy(buffer[i].a,buffer[i+1].a);
empty=signal(empty);
}
else
{
printf("No item is in buffer\n\n");
flag=0;
}
mutex=signal(mutex);
break;
case 3:
if(full!=0)
{
for(i=0;i<full;i++)
printf("%s\n", buffer[i].a);
}
else
printf("Buffer is empty\n\n");
break;
case 4:
exit(0);
default:
printf("Please enter a valid option\n");
}
}
}
int wait(int s)
{
if(s==0)
flag=1;
else
s--;
return s;
}
int signal(int s)
{
s++;
return s;
}
OUTPUT:
[netlab@system-2 oslab]$ cc semaphores.c
[netlab@system-2 oslab]$ ./[Link]
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1
Enter the item to be added: p1
Item inserted successfully
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1
Enter the item to be added: p2
Item inserted successfully
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 1
Enter the item to be added: p3
Item inserted successfully
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 2
Item p1 is successfully consumed
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 3
p2
p3
Choices are
1. Producer Routine
2. Consumer Routine
3. Display the contents of the buffer
4. Exit
Enter your choice: 4
CODING:
#include<stdio.h>
void main()
int master,s[20];
char f[20][20][20];
char d[20][20];
int i,j;
scanf("%d",&master);
for(i=0;i<master;i++)
scanf("%s",&d[i]);
for(i=0;i<master;i++)
scanf("%d",&s[i]);
for(i=0;i<master;i++)
{
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
}
printf("\n");
printf("___________________________________________________\n");
for(i=0;i<master;i++)
{
for(j=0;j<s[i];j++)
printf("%s\n\t\t ",f[i][j]);
printf("\n");
}
printf("\t\n");
OUTPUT:
[cse2c@sys-d05 ~]$ cc single.c
root
godzilla
eragon
VIP
_______________________________
bin 2 tarzan
godzilla
root 2 eragon
VIP
CODING:
#include<stdio.h>
struct st
char dname[10];
char sdname[10][10];
char fname[10][10][10];
int ds,sds[10];
}dir[10];
void main()
int i,j,k,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",&dir[i].dname);
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
scanf("%s",&dir[i].fname[j][k]);
} } }
printf("\n");
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
}
OUTPUT:
[cse2c@sys-d05 ~]$ cc two1.c
******************************************************
branch1 1 claw
OUTPUT:
/*include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10];
int p, r, i, j, process, count;
count = 0;
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
process = -1;
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
Output:
[root@comps 111a1059]# gcc bankerssafesequence.c
[root@comps 111a1059]# ./[Link]
Enter the no of processes : 5
For process 2 : 3
2
2
For process 3 : 7
0
2
For process 4 : 2
2
2
For process 5 : 4
3
3
For process 2 : 2
0
0
For process 3 : 3
0
2
For process 4 : 2
1
1
For process 5 : 0
0
2
INPUT:
enter total no. of processes : 4
enter claim matrix :
0 1 0 0 1
0 0 1 0 1
0 0 0 0 1
1 0 1 0 1
enter allocation matrix :
1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0
enter resource vector :
2 1 1 2 1
enter the availability vector :
00001
OUTPUT :
deadlock causing processes are : 1 2
OUTPUT:
[netlab@system-2 oslab]$ cc fifopaging.c
[netlab@system-2 oslab]$ ./[Link]
Page fault id 16
OUTPUT:
[netlab@system-2 oslab]$ cc lrupaging.c
[netlab@system-2 oslab]$ ./[Link]
for(i=0;i<f;i++)
{
frame[i]=-1;
}
for(i=0;i<50;i++)
{
count[i]=0;
}
printf("Enter page no : \n");
for(i=0;i<p;i++)
{
scanf("%d",&pages[i]);
}
printf("\n");
for(i=0;i<p;i++)
{
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
{
if(frame[j]!=-1)
{
hit++;
}
flag=0;
frame[j]=pages[i];
break;
}
if(count[least]>count[frame[j]])
{
least=frame[j];
}
}
if(flag)
{
minTime=50;
for(j=0;j<f;j++)
{
if(count[frame[j]]==count[least] && time[frame[j]]<minTime)
{
temp=j;
minTime=time[frame[j]];
}
}
count[frame[temp]]=0;
frame[temp]=pages[i];
}
for(j=0;j<f;j++)
{
printf("%d ",frame[j]);
}
printf("\n");
}
printf("Page hit = %d",hit);
return 0;
}
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
char name[50];
int main()
{
int pid,len,qid;
struct
{
long mtype;
char mtext[50];
}message;
system("clear");
printf("\n Inter Process communication using message queue \n");
qid = msgget((key_t)13,IPC_CREAT|0666);
printf("\n\nMessage queue is created \n");
if(qid==-1)
{
printf("\nMessage queue is not created \n");
exit(1);
}
printf("\nThe value of qid is %d",qid);
printf("\nEnter the message for communication: ");
scanf("%s",name);
strcpy([Link],name);
[Link]=1;
len=strlen([Link]);
pid=fork();
printf("\n The value of pid is %d \n",pid);
if(pid==0)
{
msgsnd(qid,&message,len,IPC_NOWAIT);
printf("\n\n MESSAGE SEND BY CHILD PROCESS\n");
}
if(pid>0)
{
wait();
msgrcv(qid,&message,strlen([Link]),0,IPC_NOWAIT|
MSG_NOERROR);
printf("\n MESSAGE RECEIVED BY PARENT PROCESS\n");
printf("\n Received message is %s \n",[Link]);
}
if(pid==-1)
{
printf("\n Error in creating a child \n");
exit(1);
}
}
OUTPUT:
[netlab@system-2 oslab]$ cc ipcmsg.c
[netlab@system-2 oslab]$ ./[Link]
PROGRAM:
#include<stdio.h>
main()
{
int pid,ppid,pdes[2],n,a;
printf("\nInterprocess communication using pipes");
if(pipe(pdes)==-1)
{
printf("\n\tThere is no error");
exit(1);
}
else
{
pid=fork();
if(pid==-1)
printf("\nThere is an error in the process");
else if(pid)
{
close(pdes[0]);
printf("\nParent is %d\n",ppid);
printf("\nProcess is %d\n",pid);
printf("\nEnter [Link] process");
scanf("%d",&a);
write(pdes[1],&a,sizeof(a));
}
else
{
close(pdes[1]);
open(pdes[0]);
read(pdes[0],&n,sizeof(a));
if(n%2==0)
printf("\nThe number %d is even",n);
}
}
}
OUTPUT:
[netlab@system-2 oslab]$ cc ipc3.c
[netlab@system-2 oslab]$ ./[Link]
OUTPUT:
[netlab@system-2 oslab]$ cc ipcshared.c
[netlab@system-2 oslab]$ ./[Link]
OUTPUT:
[cse2a@sys-do2~] $ visend.c
[cse2a@sys-do2~] $ cc send.c
[cse2a@sys-do2~] $ ./[Link]
Interprocess communication using message queues
Enter the message:Hello
Message is sent
The sent message is Hello
#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<sys/ipc.h>
#include<unistd.h>
main()
{
int mid;
charmsg[200];
printf(“\n Interprocess communication using message queues”);
mid=msgget(27,IPC_CREAT|0777);
if(mid==1)
{
printf(“Invalid message”);
exit;
}
msgrcv(mid,msg,strlen(msg),0);
printf(“Message is received”,msg);
printf(“The received message %s”,msg);
}
OUTPUT:
[cse2a@sys-do2~] $ vireceive.c
[cse2a@sys-do2~] $ cc receive.c
[cse2a@sys-do2~] $ ./[Link]
Interprocess communication using message queues
Message is received
The received message is Hello
PROGRAM:
#include<stdio.h>
void main()
{
int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
printf("\nMEMORY MANAGEMENT SCHEME - BEST FIT \n");
printf("Enter No. of Blocks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst block size: ",i);
scanf("%d",&a[i]);
}
printf("Enter No. of Process: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst process: ",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
if(p[i]>p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nProcess\tBlock Size\n");
for(i=0;i<n;i++)
printf("%d\t%d\n",p[i],a[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("\nThe process %d [size %d] allocated to block
%d\n", j,p[j],a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
if(p[j]!=10000)
printf("\nThe process %d is not allocated\n",p[j]);
}
OUTPUT:
[netlab@system-2 oslab]$ cc bestfit.c
[netlab@system-2 oslab]$ ./[Link]
PROGRAM:
#include<stdio.h>
void main()
{
int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
printf("\nMEMORY MANAGEMENT SCHEME - FIRST FIT \n");
printf("Enter No. of Blocks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst block size: ",i);
scanf("%d",&a[i]);
}
printf("Enter No. of Process: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst process: ",i);
scanf("%d",&p[i]);
}
printf("\nProcess\tBlock Size\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\n",p[i],a[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The process %d [size %d]allocated to block
%d\n",j,p[j],a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
if(p[j]!=10000)
printf("The process %d is not allocated\n",j);
}
OUTPUT:
[netlab@system-2 oslab]$ cc firstfit.c
[netlab@system-2 oslab]$ ./[Link]
PROGRAM:
#include<stdio.h>
void main()
{
int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
printf("\nMEMORY MANAGEMENT SCHEME - WORST FIT \n");
printf("Enter No. of Blocks: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst block size: ",i);
scanf("%d",&a[i]);
}
printf("Enter No. of Process: ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst process: ",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nProcess\tBlock Size\n");
for(i=0;i<n;i++)
printf("%d\t%d\n",p[i],a[i]);
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("\nThe process %d [size %d] allocated to block
%d\n",j,p[j],a[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
if(p[j]!=10000)
printf("\nThe process %d is not allocated\n",p[j]);
}
OUTPUT
[netlab@system-2 oslab]$ cc worstfit.c
[netlab@system-2 oslab]$ ./[Link]