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

Final Os Record New

Uploaded by

delulusolulu1111
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)
11 views

Final Os Record New

Uploaded by

delulusolulu1111
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/ 87

EX.

NO:1
DATE: BASIC COMMANDS IN UNIX

AIM:

To write a basic commands in UNIX

COMMANDS:

a)date
–used to check the date and time Syn: $date
Format Purpose Example Result

+%m To display only month $date+%m 06


+%h To display month name $date+%h June
+%d To display day of month $date+%d O1
+%y To display last two digits of $date+%y 09
years
+%H To display hours $date+%H 10
+%M To display minutes $date+%M 45
+%S To display seconds $date+%S 55

b) cal
–used to display the calendar
Syn: $cal2 2009
c) echo
–used to print the message on the screen.
Syn: $echo “text”
d) ls
–used to list the files. Your files are kept in a directory.
Syn: $ls ls–s
All files (include files with prefix) ls–l Lodetai (provide file statistics) ls–t Order by
creation time
ls–u = Sort by access time (or show when last accessed together with–l)ls–s Order by
size
ls–r = Reverse order
ls–f Mark directories with /,executable with* , symbolic links with @, local sockets
with =, named pipes (FIFOs) with
ls–s = Show file size
ls–h = “Human Readable”, show file size in Kilo Bytes &Mega Bytes
(h can be used together with –l or)
ls[a-m]* = List all the files whose name begin with alphabets From „a‟ to „m‟
ls[a]*List all the files whose name begins with „a‟ or „A‟
Eg:$ls>my list Output of „ls‟ command is stored to disk file named „my list‟
e) lp
–used to take printouts
Syn: $lp filename
f) man
–used to provide manual help one very UNIX commands.
Syn: $man unix command
$man cat
g) who & whoami
–it displays data about all users who have logged into the system currently. The
next command displays about current user only.
Syn:$who$whoami
h) uptime
–tells you how long the computer has been running sincerity last reboot or
power-off.
Syn: $uptime
i) uname
–it displays the system information such as hardware platform, system name
and processor, OS type.
Syn: $uname–a
j) hostname
–displays and set system hostname Syn: $hostname
FILEMANIPULATIONCOMMANDS
a)cat
–this create, view and concatenate files.
Creation:
Syn: $cat>filename
Viewing:
Syn: $catfilename
Add text to an existing file:
Syn: $cat>>filename
Concatenate:
Syn: $cat file1file2 > file3
$catfile1file2>>file3 (no over writing of file3)
b)grep
– used to search a particular word or pattern related to that word from the file.
Syn: $grep search word file name
Eg: $grepanu student
c)rm
– deletes a file from the file system Syn: $rm filename
d)touch
– used to create a blank file.
Syn: $touch file names
e)cp
–copies the files or directories
Syn: $cp source file destination file
Eg:$cp student stud
f)mv
–to rename the file or directory
syn:$mv old file new file
Eg: $mv–I student student list(-I prompt when overwrite)
g)cut
–it cursor pick up a given number of character or fields of the file.
Syn:$cut<option><filename>
Eg: $cut– cfilename
$cut–c1-10emp
$cut–f3,6emp
$cut –f3-6 emp
-ccutting columns
-fcutting fields
h)head
–displays10 lines from the head (top)of a given file Syn:$headfilename
Eg: $head student Syn: $head-2student
i)tail
–displays last 10 lines of the file Syn:$tail filename
Eg: $tail student
To display the bottom two lines;
Syn: $tail -2student
j)chmod
–used to change the permissions of a file or directory.
Syn: $ch mod category operation permission file
Where, Category–is the user type
Operation–is used to assign or remove permission Permission–is the type of
permission
File–are used to assign or remove permission all
Examples:
$chmod u-wx student
Removes write and execute permission for users
$chmod u+rw, g+rw student
Assigns read and write permission for users and groups
$chmodg=rwx student
Assigns absolute permission for groups of all read, write and execute permissions
k) wc
–it counts the number of lines, words, character in a specified file(s) with the
options as –l,-w,-c
Category Operation Permission
u– +assign R-
usersg– -Remove Readw-
groupo– =Assign Absolutely Write-
Others Execute
Syn:
$wc–lfilename $wc–wfilename $wc–c filename

RESULT

Thus the program has been executed successfully.


EX.NO: 2
DATE: SHELL PROGRAMMING

AIM:
To write a shell program for factorial

ALGORITHM:
Step-1:Start the program
Step-2:Ask the user to enter an integer to find the factorial
Step-3:Read the integer and assign it to a variable
Step-4:From the value of the integer up to 1,
multiply each digit and update the final value
Step-5:The final value at the end of all the multiplication till 1 is the factorial
Step-6:Stop the

PROGRAM:
echo "Enter a number to find Factorial"
read num
fact=1
while [ $num -gt 1 ]
do
fact=$((fact*num))
num=$((num-1))
done
echo "Factorial of given number is $fact"
Output:

RESULT:

Thus the Program was Executed and Output Verified Successfully


EX.NO : 3 IMPLEMENT THE FOLLOWING CPU SCHEDULING
DATE: ALGORITHMS

(a) ROUND ROBIN SCHEDULING

AIM:

Write a C program to implement the Round Robin Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue and time
quantum (or)time slice.
Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
i. Waiting time for process(n) = waiting time of process(n-1)+
burst timeof process(n-1 ) + the time difference in getting the CPU
from process(n-1)
ii. Turnaround time for process(n) = waiting time of process(n)
+ bursttime of process(n)+ the time difference in getting CPU from
process(n).
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number
ofprocess
Step 8: Stop the process.
PROGRAM

#include<stdio.h>

void main()

int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;

int bt[10],flag[10],ttat=0,twt=0;

float awt,atat;

printf("\t\tROUND ROBIN SCHEDULING\n");

printf("Enter the number of Processors:\n");

scanf("%d",&n);

n1=n;

printf("\nEnter the Time Slice:\n");

scanf("%d",&ts);

for(i=1;i<=n;i++)

printf("\nEnter the process Id %d:",i);

scanf("%d",&pid[i]);

printf("Enter the Burst Time for the process:");

scanf("%d",&bt[i]);

need[i]=bt[i];

}
for(i=1;i<=n;i++)

flag[i]=1;

wt[i]=0;

while(n!=0)

for(i=1;i<=n;i++)

if(need[i]>=ts)

for(j=1;j<=n;j++)

if((i!=j)&&(flag[i]==1)&&(need[j]!=0))

wt[j]=+ts;

need[i]-=ts;

if(need[i]==0)

flag[i]=0;

n--;

}
else

for(j=1;j<=n;j++)

if((i!=j)&&(flag[i]==1)&&(need[j]!=0))

wt[j]+=need[i];

need[i]=0;

n--;

flag[i]=0;

for(i=1;i<=n1;i++)

tat[i]=wt[i]+bt[i];

twt=twt+wt[i];

ttat=ttat+tat[i];

awt=(float)twt/n1;

atat=(float)ttat/n1;
printf("\n\nROUND ROBIN SCHEDULING ALGORITHM\n\n");

printf("\n\nProcess\tProcess ID\tBurst Time\tWaiting Time\tTurnaround


Time\n");

for(i=1;i<=n1;i++)

printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n",i,pid[i],bt[i],wt[i],tat[i]);

printf("\n The Average Waiting Time=%4.2f",awt);

printf("\n The Average Turnaround Time=%4.2f",atat);

}
Output:

RESULT:

Thus the Program was Executed and Output Verified Successfully


(b) SHORTEST JOB FIRST SCHEDULING(SJF)

AIM:

To Write a C program to implement SJF Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.

Step 4: Start the Ready Q according the shortest Burst time by sorting
according tolowest to highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time
as itsburst time.

Step 6: For each process in the ready queue,

Calculate

(a) Waiting time for process(n)= waiting time of process (n-1) +


Burst timeof process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+
Burst timefor process(n)
Step 6: Calculate

(A) Average waiting time = Total waiting Time / Number of process


(B) Average Turnaround time = Total Turnaround Time / Number of
processStep 7: Stop the process.
PROGRAM:

#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
printf("Enter the number of process :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the Burst Time of process:%d ",i+1);
scanf("\n%d",&t[i]);
}
for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}
}
printf("\n\t\t\t****************************************");
printf("\n\t\t\tSHORTEST JOB FIRST SCHEDULILNG ALGORITHM");
printf("\n\t\t\t****************************************");
printf("\n\nPROCESS ID\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
printf("\n----------\t---------\t------------\t----------------\n");
wt[0];
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d\t\t%5d\t\t%5d\t\t%5d\n\n",p[i]+1,t[i],wt[i],tt[i]);
}
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\nAVERAGE WAITAING TIME %4.2F",awt);
printf("\nAVERAGE TURNAROUND TIME %4.2f",atat);
}
Output:

RESULT

Thus the program has been executed successfully.


(C) FIRST COME FIRST SERVED SCHEDULING (FCFS)

AIM :

To Write a C program to implement FCFS Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.

Step 4: Set the waiting of the first process as ‘0’ and its burst time as its
turnaround time.
Step 5: For each process in the Ready Q calculate

(a) Waiting time for process(n)= waiting time of process (n-1) +


Burst timeof process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+
Burst timefor process(n)
Step 6: Calculate

(a) Average waiting time = Total waiting Time / Number of process


(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process.
PROGRAM:
#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
printf("Enter number of process:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the Burst Time of the process %d:",i+1);
scanf("\n%d",&t[i]);
}
printf("\n\nFIRST COME FIRST SERVE SCHEDULING ALGORITHM\n");
printf("\nProcess ID\tWaiting Time\tTurn Around Time\n");
printf("1\t\t0\t\t%d\n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n%d\t\t%d\t\t%d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\nAverage Waiting Time %4.2f",awt);
printf("\nAverage Turn Around Time %4.2f",atat);
}

OUTPUT:

RESULT

Thus the program has been executed successfully.


(d) PRIORITY SCHEDULING ALGORITHM

AIM:

To Write a C program to implement Priority Scheduling.

ALGORITHM:

Step 1: Start the process


Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the
CPUburst time.

Step 4: Sort the ready queue according to the priority number.


Step 5: Set the waiting of the first process as ‘0’ and its burst time as its
turnaround time.

Step 6: For each process in the Ready Q calculate

(a) Waiting time for process(n)= waiting time of process (n-1) +


Burst timeof process(n-1)
(b) Turn around time for Process(n)= waiting time of Process(n)+
Burst timefor process(n)
Step 7: Calculate

(a) Average waiting time = Total waiting Time / Number of process


(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process.
PROGRAM:
#include<stdio.h>
void main()
{
int i,j,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0,n;
float awt,atat;
printf("\n----------PRIORITY SCHEDULING----------\n");
printf("Enter the No of process: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
pid[i]=i;
printf("Enter the Burst time of Pid %d:",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d:",i);
scanf("%d",&pr[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=pid[i];
pid[i]=pid[j];
pid[j]=t;
}
}
tat[0]=bt[0];
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1] + bt[i-1];
tat[i]=wt[i] + bt[i];
}
printf("\n------------------------------------------------------------");
printf("\nPid\t Priority\tBurst time\t Waiting Time\tTurnArroundTime\n");
printf("\n------------------------------------------------------------");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat=ttat+tat[i];
twt=twt+wt[i];
} awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n\nAvg.Waiting Time: %f\nAvg.Turn Around Time:%f\n",awt,atat);
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO: 4
DATE: IMPLEMENT ALL FILE
ALLOCATION STRATEGIES

(a) SEQUENTIAL FILE ALLOCATION

AIM :

To Write a C Program to implement Sequential File Allocation method.

ALGORITHM:

Step 1: Start the program.


Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each
process. Step 4: First fit algorithm searches all the entire memory block
until a hole which
Is big enough is encountered. It allocates that memory block for
therequesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole
whichCan be allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest
hole andallocates it to the process.
Step 7: Analysis all the three memory management techniques and
display theBest algorithm which utilizes the memory resources
effectively and efficiently.
Step 8: Stop the program.
PROGRAM

#include<stdio.h>
int main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no.of blocks occupied by files %d :",i+1);
scanf("%d",&b[i]);
printf("Enter the starting blockd of file %d :",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j] =sb[i]++;
}
printf("Filename\tstart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\n",i+1,t[i],b[i]);
printf("Enter the file name:");
scanf("%d",&x);
printf("File nameis:%d",x);
printf("\nLength is%d",b[x-1]);
printf("\nblocks occpaied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
}

OUTPUT:
Enter no.of files: 2

Enter no. of blocks occupied by file 1 : 4


Enter the starting block of file1 : 2
Enter no. of blocks occupied by file2 : 10

Enter the starting block of file2 : 5


File name Start block length

1 2 4
2 5 10

Enter file name: 1

File name is: 1

length is:4

blocks occupied:2 3 4 5

RESULT

Thus the program has been executed successfully.


(b) INDEXED FILE ALLOCATION

AIM:

To Write a C Program to implement Indexed File Allocation method.

ALGORITHM:

Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer.
If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.
PROGRRAM

#include<stdio.h>
int main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter starting block and sizeof file %d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("\nEnter blocks occupied by file %d:",i+1);
scanf("%d",&m[i]);
printf("\nEnter blocks of file %d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
}
printf("\nFile\tindexed\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}
printf("\nEnter File name:");
scanf("%d",&x);
printf("File name is %d\n",x);
i=x-1;
printf("Indexed is %d",sb[i]);
printf("Block Occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


(c) LINKED FILE ALLOCATION
AIM:

To Write a C Program to implement Linked File Allocation method.

ALGORITHM:

Step 1: Create a queue to hold all pages in memory


Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.

PROGRAM
#include<stdio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
void main()
{
int i,j,n;
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname[i]);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tStart\tSize\tBlock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO : 5
IMPLEMENT SEMAPHORES
DATE:

AIM:
To write a C-program to implement the producer – consumer problem using
semaphores.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the required variables.

Step 3: Initialize the buffer size and get maximum item you want to
produce. Step 4: Get the option, which you want to do either producer,
consumer or exitfrom the operation.

Step 5: If you select the producer, check the buffer size if it is full the
producer should not produce the item or otherwise produce the item and
increase the valuebuffer size.

Step 6: If you select the consumer, check the buffer size if it is empty the
consumer should not consume the item or otherwise consume the item and
decrease the valueof buffer size.

Step 7: If you select exit come out of the program.Step 8: Stop the program.
PROGRAM:

#include<stdio.h>
void main()
{
int buffer[10],bufsize,in,out,produce,consume,choice=0;
in=0;
out=0;
bufsize=10;
while(choice!=3)
{
printf("\n 1.produce\t2.consume\t3.exist");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if((in+1)%bufsize==out)
printf("\n buffer is full");
else
{
printf("\n Enter the value:");
scanf("%d",&produce);
buffer[in]=produce;
in=(in+1)%bufsize;
}
break;
case 2:
if(in==out)
printf("\n buffer is empty");
else
{
consume=buffer[out];
printf("\n The consumed value is %d",consume);
}
break;
} }}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO : 6
IMPLEMENT ALL FILE
DATE: ORGANIZATION TECHNIQUES

(a) SINGLE LEVEL DIRCTORY

AIM:

To write program to stimulate single level directory file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the

directory.Step 3: get the

number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of filled

circle Step 6: Every file is connected with the

given directory

Step 7: Display the connected gragh along with name using

graphicsStep 8: Stop the program.


PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
printf("Do you want to enter another files(yes -1 or no - 0):");
scanf("%d",&ch);
}
while(ch==1);
printf("Directory name is:%s\n",mdname);
printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
}

Output:

RESULT

Thus the program has been executed successfully.


B)TWO LEVEL DIRECTORY

AIM:

To write a program to stimulate two level directory file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the directory.

Step 3: get the number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of filled circle

Step 6: Every file is connected with the given directory

Step 7: Display the connected gragh along with name using graphics

Step 8: Stop the program.


PROGRAM:
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
} }
OUTPUT:

RESULT

Thus the program has been executed successfully.


C)HIERARCHICAL DIRECTORY
AIM:

To write a program to stimulate hierarchical level file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the directories.

Step 3: get the number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of fill circle

Step 6: Every file is connected with respective directory

Step 7: Display the connected graph along with name in hierarchical


way usinggraphics .

Step 8: Stop the program.


PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct node
{
char N[25];
int df;
struct node*pc;
struct node*ps;
};
struct node*A[20];
int in=0,c=0;
void create(struct node*P,int N)
{
int i;
struct node*Tmp,*T;
Tmp=P;
for(i=0;i<N;i++)
{
T=malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0):");
scanf("%d",&T→df);
if(T->df==1)
{
A[c]=T;
c++;
}
T->pc=NULL;
T->ps=NULL;
if(i==0)
{
Tmp->pc=T;
Tmp=T;
}
else
{
Tmp->ps=T;
Tmp=T;
}
}
}
void display(struct node*P)
{
int i;
P=P->pc;
do
{
printf("\n%s(%d)",P->N,P->df);
if(P->df==1 && P→pc!=NULL)
display(P);
P=P->ps;
}
while(P!=NULL);
}
int main()
{
int nu,nc;
int i,j,k;
struct node*Hdr;
Hdr=malloc(sizeof(struct node));
Hdr->df=1;
Hdr->pc=NULL;
Hdr->ps=NULL;
printf("Enter number of users:");
scanf("%d",&nu);
create(Hdr,nu);
for(in=0;in<c;in++)
{
printf("\nEnter number of child nodes for %s:",A[in]->N);
scanf("%d",&nc);
create(A[in],nc);
}
printf("\nHierarchical\n");
display(Hdr);
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


(b) DAG

AIM:

To write a program to stimulate Direct Acyclic Grph level file technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the name of the

directories.Step 3: get the

number of files.

Step 4: Get the name of the each file.

Step 5: Now each file is in the form of fill circle

Step 6: Every file is connected with respective directory

Step 7: Display the connected graph along with name using

graphicsStep 8: Stop the program.


PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char N[25];
int df;
struct node *Ptr;
};
struct node *A[20];
int in=0,c=0;
void display()
{
int i;
struct node *P;
for(i=0;i<c;i++)
{
P=A[i];
printf("\n%s(%d)",P->N,P->df);
P=P->Ptr;
while(P!=NULL)
{
printf("->%s(%d)",P->N,P->df);
P=P->Ptr;}}}
void DAG()
{
struct node *T,*P,*Tmp;
int i,j,Flag,nv;
for(in=0;in<c;in++)
{
P=A[in];
printf("\nEnter number of adjacent vertices for %s:",A[in]->N);
scanf("%d",&nv);
for(i=0;i<nv;i++)
{
T=malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0):");
scanf("%d",&T->df);
T->Ptr=NULL;
P->Ptr=T;
P=T;
if(T->df==1)
{
Flag=1;
for(j=0;j<c;j++)
{
if(strcmp(A[j]->N,T->N)==0)
{
Flag=0;
break; } }
if(Flag==1)
{
Tmp=malloc(sizeof(struct node));
strcpy(Tmp->N,T->N);
Tmp->df=T->df;
Tmp->Ptr=NULL;
A[c]=Tmp;
c++;
}}}}}
void create(int N)
{
int i;
struct node *T;
for(i=0;i<N;i++)
{
T=malloc(sizeof(struct node));
printf("Enter name:");
scanf("%s",T->N);
printf("Enter dir(1) or file(0):");
scanf("%d",&T->df);
T->Ptr=NULL;
A[c]=T;
c++;
}
}
int main()
{
int nu;
printf("Enter number of users:");
scanf("%d",&nu);
create(nu);
DAG();
printf("\nDAG- Adjacency list representation\n");
display();
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO : 7
IMPLEMENT BANKER’S ALGORITHM FOR DEADLOCK
DATE: AVOIDANCE

AIM:

To write a C program to implement bankers algorithm for dead


lockavoidance.

ALGORITHM:

STEP 1: Start the Program.

STEP 2: Obtain the required data through char and int data types.

STEP 3: Enter the file name, index block.

STEP 4: Print the file name index loop.

STEP 5: File is allocated to the unused index blocks.

STEP 6: This is allocated to the unused linked allocation.

STEP 7: Stop the execution.


PROGRAM:

#include<stdio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,temp=0,c=0,cz=0;
printf("\n ENTER THE NO.OF PROCESSES :");
scanf("%d",&n);
printf("\n ENTER THE NO.OF RESOURCES :");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("PROCESS %d\n",i+1);
for(j=0;j<r;j++)
{
printf("MAXIMUM VALUE FOR RESOURCES %d :",j+1);
scanf("%d",&p[i].max[j]);
}
for(j=0;j<r;j++)
{
printf("ALLOCATED FROM RESOURCES %d :",j+1);
scanf("%d",&p[i].a1[j]);
p[i].need[j]=p[i].max[j]-p[i].a1[j];
}
}
for(i=0;i<r;i++)
{
printf("ENTER TOTAL VALUE OF RESOURCES %d :",i+1);
scanf("%d",&tot[i]);
}
for(i=0;i<r;i++)
{
for(j=0;j<n;j++)
temp=temp+p[j].a1[i];
av[i]=tot[i]-temp;
temp=0;
}
printf("\n \t RESOURCE ALLOECATED NEEDED TOTAL AVAIL");
for(i=0;i<n;i++)
{
printf("\nP%d\t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].max[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].a1[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].need[j]);
printf("\t");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",tot[j]);
}
printf(" ");
for(j=0;j<r;j++)
{
if(i==0)
printf("%d",av[j]);
}
}
printf("\n\n\tAVAIL BEFORE \t AVAIL AFTER");
for(i=0;i<n;i++)
{
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
if(p[i].need[j]>av[j])
cn++;
if(p[i].max[j]==0)
cz++;
}
if(cn==0 && cz!=r)
{
for(j=0;j<r;j++)
{
p[i].before[j]=av[j]-p[i].need[j];
p[i].after[j]=p[i].before[j]+p[i].max[j];
av[j]=p[i].after[j];
p[i].max[j]=0;
}
printf("\nP%d\t",i+1);
for(j=0;j<r;j++)
printf("%d",p[i].before[j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d",p[i].after[j]);
cn=0;
cz=0;
c++;
break;;
}
else
{
cn=0,cz=0;
}}}
if(c==n)
printf("\n THE ABOVE SEQUENCE IS A SAFE SEQUENCE");
else
printf("\nDEADLOCK OCCURED");
}
Output:

RESULT

Thus the program has been executed successfully.


EX.NO: 8
DATE: AN ALGORITHM FOR DEADLOCK DETECTION

AIM:

To write a C program to implement Deadlock Detection algorithm.

ALGORITHM:

STEP 1: Start the Program.

STEP 2: Obtain the required data through char and in data


types.STEP 3: Enter the filename, index block.

STEP 4: Print the file name index loop.

STEP 5: File is allocated to the unused index blocks.

STEP 6: This is allocated to the unused linked allocation.

STEP 7: Stop the execution.


PROGRAM:

#include<stdio.h>
static int mark[20];
int i,j,np,nr;
int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];
printf("\nEnter the no.of process:");
scanf("%d",&np);
printf("\nEnter the no.of resources:");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("Total Amount of thew Resources R%d:",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the request matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);
printf("\nEnter the allocation matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
/*Available Resources calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];
}
}
//making processes with zero allocation
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
//initialize w with avail
for(j=0;j<nr;j++)
w[j]=avail[j];
//mark processes with request than or equal to w
for(i=0;i<np;i++)
{
int canbeprocessed=0;
if(mark[i]!=1)
{
for(j=0;j<nr;j++)
{
if(request[i][j]<=w[j])
canbeprocessed=1;
else
{
canbeprocessed=0;
break;
} }
if(canbeprocessed)
{
mark[i]=1;
for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
} } }
//checking for unmarked processes
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\nDeadlock Detected!");
else
printf("\nNo Deadlock Possible!");
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO: 9
DATE: IMPLEMENT ALL PAGE REPLACEMENT ALGORITHMS

(a) FIFO PAGE REPLACEMENT ALGORITHM

AIM:
To implement page replacement algorithms FIFO (First In First Out).

ALGORITHM:

STEP 1: Create a queue to hold all pages in memory.


STEP 2: When the page is required replace the page at the head of the queue.
STEP 3: Now the new page is inserted at the tail of the queue.

PROGRAM:

#include<stdio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
printf("\n\t\t\tFIFO PAGE REPLACEMENT ALGORITHM");
printf("\n\t\t\t*******************************");
printf("\nENTER no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string...\n");
scanf("%d",&nor);
printf("Enter the refenrence string...");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tReference np%d→\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
pf++;
victim++;
victim=victim%nof;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n\n\t\tNo.of pages faults...%d",pf);
}

Output:

RESULT

Thus the program has been executed successfully.


(b) LRU PAGE REPLACEMENT ALGORITHM

AIM:
To implement page replacement algorithm LRU (Least Recently Used).
ALGORITHM:
STEP 1: Create a queue to hold all pages in memory.
STEP 2: When the page is required replace the page at the head of the queue.
STEP 3: Now the new page is inserted at the tail of the queue.
STEP 4: Create a stack.

STEP 5: When the page fault occurs replace page present at the bottom of thestack.

PROGRAM :
#include<stdio.h>
void main()
{
int i,j,k,min,rs[25],m[10],count[10],flag[25],n,f,pf=0,next=1;
printf("Enter the length of reference string --");
scanf("%d",&n);
printf("Enter the reference string --");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames --");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement Process is --\n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min]>count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t",m[j]);
if(flag[i]==0)
printf("PF No. -- %d",pf);
printf("\n");
}
printf("\nThe number of page fault using LRU are %d",pf);
}
OUTPUT :

RESULT

Thus the program has been executed successfully.


(c) LFU(OPTIMAL PAGE REPLACEMENT ALGORITHM)

AIM:
To implement page replacement algorithms Optimal (The page which is not
used for longest time).
ALGORITHM:
STEP 1: Create a array.
STEP 2: When the page fault occurs replace page that will not be used

for theLongest period of time.

STEP 3: Create a stack.

STEP 4: When the page fault occurs replace page present at the bottom of thestack.

PROGRAM :

#include<stdio.h>
void main()
{
int rs[50],i,j,k,m,f,cntr[20],a[20],min,pf=0;
printf("\nEnter number of page references --");
scanf("%d",&m);
printf("\nEnter the refence string--");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames --");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
printf("\nThe Page Raplacement Process is -- \n");
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min=0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf("\tPF No. %d",pf);
}
printf("\n\nTotal number of page faults -- %d",pf);
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO: 10 IMPLEMENT SHARED MEMORY AND IPC
DATE:

AIM:
To write a c program to implement IPC using shared memory.

ALGORITHM:

Step 1: Start the process


Step 2: Declare the segment size
Step 3: Create the shared memory
Step 4: Read the data from the shared memory

Step 5: Write the data to the shared memory

Step 6: Edit the data

Step 7: Stop the process

PROGRAM :
#include<stdio.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<string.h>
void main()
{
int len,msgid;
char msg[15];
msgid=msgget(7,IPC_CREAT|0666);
if(msgid<0)
perror("msgget failed");
else
{
system("clear");
printf("\n\n\t enter the message:");
scanf("%[a-z,az]",msg);
len=strlen(msg);
}
if(msgsnd(msgid,&msg,len,0)==-1)
perror("message sent failed");
else
printf("\n message sent:%s\n",msg);
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO: 11 IMPLEMENT PAGING TECHNIQUE OF MEMORY MANAGEMENT
DATE:

AIM:

To write a c program to implement paging technique of memory management.

ALGORITHM:

STEP 1: Read all the necessary input from the keyboard.

STEP 2: Pages - Logical memory is broken into fixed - sized blocks.

STEP 3: Frames – Physical memory is broken into fixed – sized blocks.


STEP 4: Calculate the physical address using the following

Physical address = ( Frame number * Frame size ) + offset


STEP 5: Display the physical address.
STEP 6: Stop the process.
PROGRAM :

#include<stdio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmize,imsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\t\tMEMORY MANAGEMENT USING PAGING");
printf("\n\t\t******************************");
printf("\n\nEnter the Size of Physical memory:");
scanf("%d",&pmize);
printf("Enter the size of logical memory:");
scanf("%d",&imsize);
printf("\n\nEnter the partition size:");
scanf("%d",&psize);
frame = (int)pmize/psize;
page = (int)imsize/psize;
printf("\nThe Physical memory is divided into %d no.of frames\n",frame);
printf("\nThe Logical memory is divided into %d no.of pages\n",page);
}
void assign()
{
int i;
for(i=0;i<page;i++)
{
ptable[i].fno = -1;
ptable[i].pbit = -1;
}
for(i=0;i<frame;i++)
ftable[i] = 32555;
for(i=0;i<page;i++)
{
printf("\nEnter the Frame number where page %d must be placed:",i);
scanf("%d",&frameno);
ftable[frameno] = i;
if(ptable[i].pbit == -1)
{
ptable[i].fno=frameno;
ptable[i].pbit = 1;
}
}
printf("\n\t\tPAGE TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for(i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\t\tFRAME TABLE\n");
printf("\nFrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int laddr,paddr,disp,phyaddr,baddr;
printf("\n\tProcess to create the Physical Adddress\n\n");
printf("\nEnter the Base Address:");
scanf("%d",&baddr);
printf("\nEnter the Logical Address:");
scanf("%d",&laddr);
paddr = laddr / psize;
disp = laddr % psize;
if(ptable[paddr].pbit == 1)
phyaddr = baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe physical Address where the instruction present:%d",phyaddr);
}
void main()
{
info();
assign();
cphyaddr();
}
OUTPUT:

RESULT

Thus the program has been executed successfully.


EX.NO: 12
DATE: IMPLEMENTING THREADING & SYNCHRONIZATION
APPLICATIONS

AIM:

To write a c to implement threading and synchronization.

ALGORITHM:

Step 1: Start the process

Step 2: Declare process thread, thread-id.

Step 3: Read the process thread and thread state.

Step 4: Check the process thread equals to thread-id by using if condition.


Step 5: Check the error state of the thread.
Step 6: Display the completed thread process.

Step 7: Stop the process

PROGRAM:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
pthread_t tid[2];
int counter;
pthread_mutex_t lock;
void* trythis(void* arg)
{
pthread_mutex_lock(&lock);
unsigned long i = 0;
counter += 1;
printf("\n Job %d has started\n", counter);
for (i = 0; i < (0xFFFFFFFF); i++);
printf("\n Job %d has finished\n", counter);
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
int i = 0;
int error;
if (pthread_mutex_init(&lock, NULL) != 0) {
printf("\n mutex init has failed\n");
return 1;
}
while (i < 2) {
error = pthread_create(&(tid[i]),NULL,&trythis, NULL);
if (error != 0)
printf("\nThread can't be created :[%s]",
strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);
return 0;
}

Output:

RESULT

Thus the program has been executed successfully.

You might also like