Os Record
Os Record
3 SHELL PROGRAMS
4 ‘C’ PROGRAMS
5 SYSTEM CALLS
6(D) PRIORITY
9(A) FIFO
9(B) LRU
9(C) LFU
10(D) DAG
Ex.NO:1 (A)
DATE:
INTRODUCTION
An operating system is software that acts as an interface between the user and the computer
hardware. It is considered as the brain of the computer. It controls and co-ordinates the internal
activities of the computer and provides user interface.
(i) Hardware: This includes the physical components such as CPU, Keyboard, Hard disk
and Printer.
(ii) Application Software: These are the programs that are used to accomplish specific tasks.
(iii) Operating System: It is the component or the set of programs to manage and control the
hardware as well as co-ordinate the applications. Each system must have at least have
the hardware and the OS.
Functions of an Operating System
· Command interpretation: The CPU can’t understand the commands keyed in by a user. It is
the function of the OS to make it understand.
· Peripheral Interfaces: The OS also has to take care of the devices attached to the system.
· Memory management: The OS handles the extremely important job of allocating memory
for various processes running on the system.
· Process management: This is required if several programs must run concurrently. CPU time
would then have to be rationed out by the OS to ensure that no programs get more than its
fair share of the processor time.
Services of an OS:
1. Process Management
2. File Management
3. I/O Management
4. Scheduling
5. Security Management
In the mid-1960s, AT &T Bell Laboratories developed a new OS called Multics. Multics was
intended to supply large scale computing services as a utility; much like electrical power. In 1969
Ken Thompson, Dennis Ritchie and others developed and simulated an initial design for a file
system that later evolved into the UNIX file system. The whole UNIX was rewritten in ‘C’ language
in 1973.Today, UNIX is a giant Os and is much powerful than most of its counter parts.tld
UNIX operating System is like a layer between the hardware and the applications that run on
the computer .It has functions that run on the computer. It has functions that manage the executing
applications.
UNIX system is an OS, which includes the traditional system components. UNIX system
includes a set of libraries and a set of applications.
KERNEL is the heart of UNIX OS that manages the hardware and the executing process. The
UNIX system views each device as a file called a device file. It implements security controls to
protect the safety and privacy of information. The Unix System allocates resources including use of
the CPU and mediates accesses to the hardware.
Application portability is the ability of a single application to be executed on various types of
computer hardware without being modified. This is one of the important advantages of UNIX.
FEATURES OF UNIX:
1. Multitasking
Multitasking is the capability of the Os to perform various tasks simultaneously. i.e. A single
user can run multiple programs concurrently.
Multi User capability allows several users to use the same computer to perform their tasks.
Several terminals are connected to a single powerful computer and each user can work with their
terminals.
3. Security
Unix allows sharing of data. Every user must have a Login name and a password. So,
Unix is portable because it is written in high level languages so it can run on different
computers.
5. Communication
Unix supports communication between different terminals connected to the Unix server and
also between the users of one computer to the users of another computer located elsewhere in the
network.
6. Programming Facility
Unix is highly programmable, the Unix shell programming language has the conditional
statements and control structures and variables.
UNIX ARCHITECTURE
Kernel
It is the core of the OS. It controls all the tasks and carries out all the functions of an OS such
as memory and file management etc., and it keep track of the programs that are executing. It also
handles the information exchange between the terminals such as tape drives and printers etc.,
Functions of Kernel
Shell
It is the command interpreter of the OS. The commands given from the user are moved to the
shell. The shell analyses and interprets these commands into the machine understandable form. The
commands can be either typed in through the command line or contained in a file called shell script.
Hence, Shell acts as an interface between the user and the kernel.
(i) Bourne Shell: It is developed by Steve Bourne and it is the most popular shell and
widely used. This shell comes bundled with almost every Unix system.
(ii) Korn shell: It is developed by David G.Korn. This is superset of Bourne shell an
it has more capabilities.
(iii) C Shell: It is developed by Bil Joy. It is similar to the C Programming language.
Features of Shell
1. All communications between user and Kernel takes place through the shell.
2. It allows the tasks to run in the background.
3. It also enables us to construct scripts like a programming language.
4. A group of files can be executed using a single command.
A user of a Unix based system works as a user terminal. After the boot procedure is completed, that is the
operating system is loaded in memory, the following message appears at each user terminal:
Logging
Each user has an identification called the user name are the login name which has to be
entered when the login: message appears. The user is then asked to enter the password. Unix keeps
track of all the Unix user names and the information about identity in a special file. If the login name
entered does not match with any of the user names it displays the login message again. This ensures
that, only authorized people use the system. When a valid user name is entered at the terminal the
dollar symbol is displayed on the screen this is the Unix prompt.
Once a user has logged into the system the users works session continues until the user
instructs the shell to terminate the session. This is done by pressing the ctrl and ‘d keys’ together or
typing exit at the dollar prompt. Then the systems display the login:
RESULT:
Ex.NO:1(B) STUDY OF VI EDITOR & SHELL PROGRAMMING
DATE:
An editor is a program that allows users to see a portion of a file on the screen and to modify
characters and lines by simply typing at the cursor position. There are a number of editors that may be
included with the Unix system, including ed, ex, vi and EMACS.
The vi Editor
The vi editor was written in the University of California at Berkeley by Bill Joy, who is on
of the co-founder of Sun Microsystems.
Features of vi Editor
1. It is very easy to learn and has more powerful and exciting features.
2. It works in great speed.
3. vi is case sensitive.
4. vi has powerful undo features than most other word processors,but it has no formatting
features.
Modes of vi Editor
Insert Mode: This mode permits inserting new text, editing and replacement of existing text. Once
the vi editor is in the insert mode, letters typed in the keyboard are echoed on the screen.
The ex or escape colon ( : ) Mode: This mode allow us to give commands at the command
line.The bottom line of the vi editor is called the command line.vi uses the command line to display
messages and commands.
Starting with Vi editor
Syntax: vi filename
Command Action
¨
¨ Open the file using $ vi filename
¨ To add text at the end of the file, position the cursor at the last character of the file.
¨ The editor accepts and inserts the typed character until Esc key is pressed.
Command Purpose
Deleting Text
For deleting a character, move the cursor to the character , press ‘x’. The character will
disappear.
Command Purpose
Saving text
Quitting vi
SHELL PROGRAMMING
The format for the various conditional statements and looping statements and the relational
operators used for those conditions are given below.
if condition
if condition then
execute commands
fi
if – else condition
if condition then
execute commands
else
execute commands
fi
if condition then
Operat Meaning
or
-eq Equal to
Case condition
case expression in
……………..
esac while looping
While expression Do
execute commands
done
for looping
RESULT:
Ex.NO:2(A) BASIC UNIX COMMANDS
DATE:
AIM:
Command : Cat
Purpose : It is used to display the contents of the file as well as used to create a new
file.
Command : More
Purpose : It is used to display the contents of the file on the screen at a time.
Command : Wc
Purpose : It is used to count the number of lines ,words and characters in a file or
group of files.
Example : $ wc –l devi
Command : File
Purpose : It is used to determine the type of the file.
Command : Spell
Command : Split
Purpose : It is used to split the given file into smaller pieces of given size.
Syntax : split –size <file name > < splitted file name >
Command : Cp
Command : Mv
Purpose : It is used to move a file within a directory with different names and also used to
move a file to different directory with its original name.
Example : $ rm devi
Command : Banner
Command : Who
Purpose : It is used to get the information about all the users currently working in
the system.
Syntax : who
Example : $ who
Command : Who am i
Purpose : It is used to know in which terminal the user is currently logged on.
Syntax : who am i
Example : $ who am I
Command : Date
Syntax : date
Example : $ date
Command : Cal
Purpose : It prints the calender for the specified year and month.
Command : Id
Syntax : id
Example : $ id
Command : Clear
Syntax : clear
Example : $ clear
Command : Tput
Command : Uname
Purpose : It is used to display the details about the OS in which we are working.
Example : $ uname –n
Command : Tty
Example : $ tty
Command : Pwd
Example : $ pwd
Command : Bc
Syntax : bc <operation>
Example : $ bc 3+5 8 ^d
Command : Ls
Example : $ ls –p
Command : Echo
Command : Man
Command : Head
Command : Tail
Command : Pr
Purpose : It is used to display the contents of the file by separating them into pages
and each page begins with the header information.
Example : $ pr devi
Command : Cut
Purpose : It is used to extract selected fields or columns from each line of one or
more files and display them on the standard output device.
Command : Paste
Purpose : It concatenates the line from each input file column by column with tab
characters in between them.
Command : Join
Purpose : It is used to extracts common lines from two sorted files and there should
be the common field in both file.
Command : Uniq
Purpose : It compares adjacent lines in the file and displays the output by
eliminating duplicate adjacent lines in it.
Command : Sort
Purpose : It sorts one or more files based on ASCII sequence and also to merge the
file.
Command : Nl
Example : $ nl devi
Command : Tr
Command : Tee
Purpose : It is used to read the contents from standard input or from output of another
command and reproduces the output to boyh in standard output and direct into
output to one or more files.
Command : grep
Purpose : It is used to search the specified pattern from one or more files.
RESULT:
Ex.NO:2 (B) DIRECTORY COMMANDS AND PROCESS MANAGEMENT
COMMANDS
DATE:
AIM:
Command : mkdir
Command : rmdir
Command : cd
Purpose : It is used to change the control from one working directory to another
specified directory.
Example : $ cd riya
Command : cd ..
Purpose : It is used to quit from current directory and move to the previous
directory.
Syntax : cd ..
Example : $ cd ..
Process Commands
Command : echo $$
Syntax : echo $$
Example : $ echo $$
Command : ps
Syntax : ps
Example : $ ps
$ ps –f ( Display the ancestry of a process )
$ ps –u ( Display the activities of a user )
$ ps –a ( Lists processes of all users but not the system processes )
Command :&
Command : nohup
Purpose : It permits the execution of the process even after the user has logged out.
Command : kill
Command : kill $!
Purpose : $! is the system variable which stores the process id of the last background
job. The command kill $! is used to kill the last process.
Syntax : kill $!
Example : $ kill $!
Command : at
RESULT:
Ex.No. 3 (A) SIMPLE SHELL PROGRAMS
DATE:
Getting and displaying the academic and personal details
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex No. 3 (B)
DATE: ARITHMETIC OPERATION
AIM:
ALGORITHM:
PROGRAM:
RESULT:
PROGRAMS USING CONDITIONAL STATEMENTS
AIM:
ALGORITHM:
Program
RESULT:
Ex. No. 3(D) POSITIVE OR NEGATIVE NUMBER
DATE:
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex. No: 3(E) BIGGEST AMONG THREE NUMBERS
DATE:
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex No.3(F) ARITHMETIC OPERATION - SWITCH CASE
DATE:
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex No.3(G) FINDING AREA OF CIRCLE, SQUARE ,RECTANGLE AND TRIANGLE
DATE:
AIM:
ALGORITHM:
PROGRAM:
echo 1.circle
echo 2. rectangle
echo 3.square
echo 4.triangle
Ex. No.3(H)
DATE: Fibonacci Series
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex. No.3(I)
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex. No.3(J)
DATE: SUM OF N EVEN NUMBERS
AIM:
ALGORITHM:
PROGRAM:
RESULT:
Ex. No: 3(K)
DATE: COMBINATIONS OF 1 2 3
AIM:
ALGORITHM:
PROGRAM:
for I in 1 2 3 do
for j in 1 2 3 do
for k in 1 2 3 do
echo $I $j $k
done
done
done
RESULT:
Ex. No: 3(L) Nth POWER OF THE GIVEN NUMBER
DATE:
AIM:
ALGORITHM:
program
echo enter the number and its power
read a pow
i=2
ans=$a
while [ $i -le $pow ] do
ans=`expr $ans \* $a`
i=`expr $i + 1`
done
echo $ans
RESULT:
COMMAND LINE SUBSTITUTION
Ex. No: 3(M) Palindrome checking
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
[it@localhost ~]$ ./a.out
Enter a string
how
RESULT:
Ex. No:3(N) CONCATENATION OF TWO STRINGS
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
main(int argc, char *argv[])
{
char str1[10],str2[20],str3[10];
strcpy(str1,argv[1]);
strcpy(str2,argv[2]);
printf("%s",strcat(str1,str2));
}
RESULT:
C PROGRAMMING ON UNIX
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
float largest(float a[],int n);
void main()
{
int n;
float value[10];
clrscr();
for(i=1;i<n;i++)
{
if(max<a[i])
{
max= a[i];
}
}
return(max);
}
RESULT:
Ex.No.4(B) SWAPPING OF TWO NUMBERS USING POINTERS
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void swap1(int,int);
void main()
{
int a,b,c,d;
printf("Enter the values of a and b:= ");
scanf("%d %d",&a,&b);
printf("Enter the values of c and d:= ");
scanf("%d %d",&c,&d);
printf("\n BEFORE SWAPPING : ");
printf("\n The value of a and b is : %d\t %d ",a,b);
printf("\n The value of c and d is : %d\t %d ",c,d);
printf("\n AFTER SWAPPING : ");
swap(a,b);
swap1(&c,&d);
printf("\n Method is:-Call by Value");
printf("\n **************************");
printf("\n The value of a and b is : %d\t %d",a,b);
printf("\n Method is:-Call by Address or Reference");
printf("\n ***************************");
printf("\n The value of c and d is : %d\t %d",c,d);
}
void swap(int *c,int *d)
{ int t; t=*c; *c=*d; *d=t; }
void swap1(int a,int b)
{ int t; t=a; a=b; b=a; }
Ex.No.5(A) Is- SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the directory name.
3. Check the directory in unix.
4. Display the directory name.
5. Stop.
PROGRAM:
#include<dirent.h>
#include<stdio.h>
main()
{
char dirname[10];
DIR *p;
struct dirent *d;
printf("Enter directory name");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find dir.");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}
RESULT:
Ex.No.5(B) grep – SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the file name.
3. Read the pattern to be searched.
4. Check whether the pattern present in the input file or not.
5. If present, display the line which has the pattern.
6. Stop.
PROGRAM:
#include<stdio.h>
#include<string.h>
main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("\n Enter file name : ");
scanf("%s",fn);
printf("Enter the pattern to be searched :“);
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
RESULT:
Ex.No.5(C) execl – SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Assign fork() to pid.
3. If pid equals to zero, then it becomes child process.
4. Else cannot create process.
5. Else it is parent.
6. Stop.
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf(“child process”);
execl(“/bin/ls”,”ls”,”-r”,(char*)0);
}
else if(pid<0)
{
printf(“cannot create process”);
}
else
{
printf(“parent process”);
}
}
RESULT:
Ex.No.5(D) execv – SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Assign fork() to pid.
3. If pid equals to zero the display the files in long format.
4. Else display parent or cannot create process.
5. Stop.
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
char *argv[]={“ls”,”-l”,”-r”,(char*)0};
execv(“bin/ls”,argv);
printf("child process");
}
else if(pid<0)
{
printf("cannot create");
}
else
{
printf("parent process");
}
}
RESULT:
Ex.No.5(E) wait – SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Assign pid as fork().
3. If it equals to zero then print child process.
4. Else wait for some time and display parent process.
5. Stop
PROGRAM:
#include<stdio.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("child process");
}
else if(pid<0)
{
printf("cannot create");
}
else
{wait();
printf("parent process");
}
}
RESULT:
Ex.No.5(F) getpid – SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Assign fok() to pid.
3. If pid equals to zero, then call sleep with 3 minutes and print the pid for parnt and child
process.
4. Else print the original process id and their parent id.
5. Stop.
PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid==0)
{
sleep(3);
printf(“parent process id is %d”,getppid());
printf("child process id is %d”,getpid());
}
else if(pid<0)
{
printf("cannot create");
}
else
{ printf(“parent process id is %d”,getppid());
printf("child process id is %d”,getpid());
}
}
RESULT:
Ex.No.5(G) lstat – SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read file name to display the status.
3. Display file name, uid,gid, file size and inode number.
4. Stop.
PROGRAM:
#include<stdio.h>
#include<sys/stat.h>
main(int argc,char **argv)
{
struct stat statbuf;
if(lstat(argv[1],&statbuf)==-1)
printf("Error : cannot find file information ");
else
{
printf("\n File %s ", argv[1]);
printf("\n Inode number %d",statbuf.st_ino);
printf("\n UID %d",statbuf.st_uid);
printf("\n GID %d",statbuf.st_gid);
printf("\n File size in bytes %d",statbuf.st_size);
}
}
RESULT:
Ex.No.5(H) OPEN,READ AND CLOSE DIRECTORY - SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the directory name.
3. Display the directory name with ino.
4. Stop.
PROGRAM:
#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)
{
printf("a single argument (the directory name) is required\n");
exit(1);
}
if ( (dp = opendir(argv[1])) == NULL)
{
printf("can't open %s\n",argv[1]);
exit(1);
}
while ( (dirp = readdir(dp)) != NULL)
printf("%s %d\n",dirp->d_name,dirp->d_ino);
closedir(dp);
exit(0);
}
RESULT:
Ex.No.5(I) OPEN,READ AND WRITE - SYSTEM CALLS
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the source and destination file name.
3. Open source file and copy the contents into destination file.
4. Open the destination file in write mode and copy the contents.
5. Close both files
6. Stop.
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
main()
{
char buf[1000],fn1[10],fn2[10];
int fd1,fd2,n;
printf("Enter source file name ");
scanf("%s",fn1);
printf("Enter destination file name “):
scanf("%s",fn2);
fd1=open(fn1,O_RDONLY);
n=read(fd1,buf,1000);
fd2=open(fn2,O_CREAT|0666);
n=write(fd2,buf,n);
close(fd1);
close(fd2);
}
RESULT:
Ex.No 6(A) ROUND ROBIN SCHEDULING
DATE;
AIM:
ALGORITHM:
1. Start.
2. Read the no.of process, time quantum.
3. Read the processes arrival time and burst time.
4. Based on the round robin scheduling display the job scheduling process.
5. Stop.
PROGRAM:
#include<stdio.h>
int main()
{
int g[20],p[20],w[20],t[20],a1[20],tq,a=0,q[20],wait_tot=0,turn_tot=0,tot_brust=0,
s=0,n,i,j,p1[20];
float wait_avg,turn_avg;
printf("\nROUND ROBIN SCHEDULING\n");
printf("\nEnter the no of process : ");
scanf("%d",&n);
printf("\nEnter the time quantum : ");
scanf("%d",&tq);
for(i=1;i<=n;i++)
{
printf("\nEnter the burst time for p%d : ",i);
scanf("%d",&p[i]);
printf("\nEnter the arrival time for p%d : ",i);
scanf("%d",&a1[i]);
tot_brust+=p[i];
p1[i]=p[i];
}
g[0]=0;
w[1]=0;
do
{
for(i=1;i<=n;i++)
{
if(p[i]>tq)
{
p[i]=p[i]-tq;
a=a+1;
g[a]=g[a-1]+tq;
q[a]=i;
}
else if((p[i]<=tq)&&(p[i]!=0))
{
a=a+1;
g[a]=g[a-1]+p[i];
q[a]=i;
w[i]=g[a]-p1[i];
t[i]=g[a];
p[i]=0;
s++;
} }
}while(s!=n);
for(i=1;i<=n;i++)
{
w[i]=w[i]-a1[i];
t[i]=t[i]-a1[i];
wait_tot+=w[i];
turn_tot+=t[i];
}
wait_avg=wait_tot/n;
turn_avg=turn_tot/n;
printf("\nGANTT CHART");
printf("\n----------------- \n");
for(i=1;i<=a;i++)
printf("\t\tp%d",q[i]);
printf("\n0\t");
for(i=1;i<=a;i++)
printf("\t\t%d",g[i]);
printf("\nPROCESS\tBURSTTIME\tARRIVALTIME\tWAITINGTIME\tTURNAROUNDTIME\n");
for(i=1;i<=n;i++)
printf("\np%d\t%9d\t%11d\t%11d\t%14d",i,p1[i],a1[i],w[i],t[i]);
printf("\nAverage waiting time : %f ms",wait_avg);
printf("\nAverage turn around time : %f ms",turn_avg);
}
RESULT:
Ex.No. 6(B) SHORTEST JOB FIRST SCHEDULING
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the no.of process.
3. Read the processes arrival time and burst time.
4. Based on the shortest job first scheduling display the job scheduling process.
5. Stop.
PROGRAM:
#include<stdio.h>
main()
{
int i,j,temp,index[20],b[20],t[20],w[20],tot_burst=0,tot_wait=0,tot_turn=0;
float avg_wait,avg_turn,n;
int a[20],count=0,enter[20],c_wait[20],time=0,name[20];
printf("Enter the no of process : ");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for p%d : ",i+1);
scanf("%d",&b[i]);
printf("Enter the arrival time for p%d : ",i+1);
scanf("%d",&a[i]);
tot_burst+=b[i];
enter[i]=0;
}
for(i=0;i<n;i++)
{
temp=0;
for(j=0;j<n;j++)
{
if((i!=j)&&(b[i]>=b[j]))
{
temp++;
}
if((i<j)&&(b[i]==b[j]))
{
temp--;
}
}
index[temp]=i;
for(i=0;count<n;i++)
{
for(j=0;j<n;j++)
{
if((time>=a[index[j]])&&(enter[index[j]]!=1))
{
enter[index[j]]=1;
time+=b[index[j]];
name[count]=index[j];
count++;
break;
}
}
}
c_wait[name[0]]=w[name[0]]=0;
tot_turn=t[name[0]]=b[name[0]];
for(i=1;i<n;i++)
{ c_wait[name[i]]=c_wait[name[i-1]]+b[name[i-1]];
w[name[i]]=c_wait[name[i]]-a[name[i]];
t[name[i]]=w[name[i]]+b[name[i]];
tot_turn+=t[name[i]];
tot_wait+=w[name[i]];
}
avg_wait=tot_wait/n;
avg_turn=tot_turn/n;
printf("\nGANTT CHART\n");
for(i=0;i<n;i++)
{
printf("\t\tp%d",name[i]+1);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",c_wait[name[i]]);
}
printf("%d",tot_burst);
printf("\nProcess\tBurst Time\tArrival Time\tWait Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
printf("p%d\t%10d\t%12d\t%9d\t%15d\n",i,b[i],a[i],w[i],t[i]);
}
printf("Average Waiting Time = %f ms\nAverage Turnaround Time=%f
ms\n",avg_wait,avg_turn);
}
RESULT:
Ex.No. 6(C) FIRST COME FIRST SERVE
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the no.of process.
3. Read the processes arrival time and burst time.
4. Based on the first come first scheduling display the job scheduling process.
5. Stop.
PROGRAM:
#include <stdio.h>
int wt[10],bt[10],at[10],tat[10],n;
float awt,atat;
void input()
{
printf("Enter Number of processes:");
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
{
printf("Enter Burst Time of process %d:",i+1);
scanf("%d",&bt[i]);
printf("Enter Arrival Time of process %d:",i+1);
scanf("%d",&at[i]);
}
}
void calculate()
{
wt[0]=0;
atat=tat[0]=bt[0];
int btt=bt[0];//to store total burst time sum
int i;
for(i=1;i<n;i++){
wt[i]=btt-at[i];
btt+=bt[i];
awt+=wt[i];
tat[i]= wt[i]+bt[i];
atat+=tat[i];
}
atat/=n;
awt/=n;
}
void display()
{
int i;
printf("PROCESS NO\tARRIVAL TIME.\tBURST TIME.\tWAITING TIME\tTURN AROUND
TIME.\n");
for(i=0;i<n;i++)
{
printf("%3d\t%11d\t%11d\t%11d\t%11d\n",i+1,at[i],bt[i],wt[i],tat[i]);
}
printf("Average Waiting Time: %f\nAverage Turn Around Time:%f",awt,atat);
}
int main()
{
printf("FCFS CPU Scheduling Algorithm\n");
input();
calculate();
display();
}
RESULT:
Ex.No. 6(D) PRIORITY SCHEDULING
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the no.of process.
3. Read the processes Priority, arrival time and burst time.
4. Based on the Priority scheduling display the job scheduling process.
5. Stop.
PROGRAM:
#include<stdio.h>
main()
{
int i,j,temp,index[20],b[20],t[20],w[20],tot_wait=0,tot_turn=0;
float avg_wait,n,avg_turn;
int a[20],count=0,pri[20],enter[20],c_wait[20],time=0,tot_burst=0,name[20];
printf("Enter the no process:");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time for p%d : ",i+1);
scanf("%d",&b[i]);
printf("Enter the arrival time for p%d : ",i+1);
scanf("%d",&a[i]);
printf("Enter the priority for p%d : ",i+1);
scanf("%d",&pri[i]);
tot_burst+=b[i];
enter[i]=0;
}
for(i=0;i<n;i++)
{ temp=0;
for(j=0;j<n;j++)
{
if((i!=j)&&(pri[i]>=pri[j]))
{
temp++;
}
if((i<j)&&(pri[i]==pri[j]))
{
temp--;
}
}
index[temp]=i;
}
for(i=0;count<n;i++)
{
for(j=0;j<n;j++)
{
if((time>=a[index[j]])&&(enter[index[j]]!=1))
{
enter[index[j]]=1;
time+=b[index[j]];
name[count]=index[j];
count++;
break;
}
}
}
c_wait[name[0]]=w[name[0]]=0;
tot_turn=t[name[0]]=b[name[0]];
for(i=1;i<n;i++)
{ c_wait[name[i]]=c_wait[name[i-1]]+b[name[i-1]];
w[name[i]]=c_wait[name[i]]-a[name[i]];
t[name[i]]=w[name[i]]+b[name[i]];
tot_turn+=t[name[i]];
tot_wait+=w[name[i]];
}
avg_wait=tot_wait/n;
avg_turn=tot_turn/n;
printf("\nGANTT CHART\n");
for(i=0;i<n;i++)
{
printf("\t\tp%d",name[i]+1);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",c_wait[name[i]]);
}
printf("%d",tot_burst);
printf("\nProcess\tBurst Time\tArrival Time\tPriority\tWait Time\tTurnaround Time\n");
for(i=0;i<n;i++)
{
printf("p%d\t%10d\t%12d\t%8d\t%9d\t%15d\n",i,b[i],a[i],pri[i],w[i],t[i]);
}
printf("Average Waiting Time = %f \nAverage Turnaround Time=%f\n",avg_wait,avg_turn);
RESULT:
Ex.No. 7 (A) PRODUCER – CONSUMER PROBLEM USING SEMAPHORE
DATE:
AIM:
ALGORITHM:
1. Start
2. create semaphore using semget( ) system call
3. if successful it returns positive value
4. create two new processes
5. first process will produce
6. until first process produces second process cannot consume
7. End.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define num_loops 2
int main(int argc,char* argv[])
{
int sem_set_id;
int child_pid,i,sem_val;
struct sembuf sem_op;
int rc;
struct timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(“main:semget”);
exit(1);
}
printf(“semaphore set created,semaphore setid‘%d’\n ”, sem_set_id);
child_pid=fork();
switch(child_pid)
{
case -1: perror(“fork”);
exit(1);
case 0: for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“producer:’%d’\n”,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(“consumer:’%d’\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0; delay.tv_nsec=10; nanosleep(&delay,null);
}
}
break;
}
return 0;
}
RESULT:
Ex.No. 7(B) INTER PROCESS COMMUNICATION USING SHARED MEMORY
DATE:
AIM:
ALGORITHM:
1. Start
2. create shared memory using shmget( ) system call
3. if success full it returns positive value
4. attach the created shared memory using shmat( ) system call
5. write to shared memory using shmsnd( ) system call
6. read the contents from shared memory using shmrcv( ) system call
7. End .
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<sys/shm.h>
#define shm_size 1024
int main(int argc,char * argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if(argc>2)
{
fprintf(stderr,”usage:stdemo[data_to_writte]\n”);
exit(1);
}
if((shmid=shmget(key,shm_size,0644/ipc_creat))==-1)
{
perror(“shmget”);
exit(1);
}
data=shmat(shmid,(void *)0,0);
if(data==(char *)(-1))
{
perror(“shmat”);
exit(1);
}
if(argc==2)
printf(writing to segment:\”%s”\”\n”,data);
if(shmdt(data)==-1)
{
perror(“shmdt”);
exit(1);
}
return 0;
}
RESULT:
Ex.No. 7(C) BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
DATE:
AIM:
ALGORITHM:
1. Start
2. Read the no.of process
3. Read the n no.of process allocated resources and maximum resources
4. Read the available vector.
5. Based on banker’s algorithm check whether the state is safe or not.
6. End.
PROGRAM:
#include<stdio.h>
Struct process
{
int allocation[3];
int max[3];
int need[3];
int finish;
}p[10];
int main()
{
int n,i,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf(“\n Enter the number of process:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the %dth process allocated resources”,i);
scanf(“%d%d%d”,&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf(“\n Enter the %d process maxium resources:”,i);
scanf(“%d%d%d”,&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[0]=p[i].max[1]-p[i].allocation[1];
p[i].need[0]=p[i].max[2]-p[i].allocation[2];
}
printf(“\n Enter the available vector:”);
scanf(“%d%d%d”,&avail[1],&avail[2]);
for(i=0;i<3;i++);
work[i]=avail[i];
while(count!=n)
{
count=0
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j]
p[i].finish=1;
sequence[k++]=I;
flag=0;
}
if(flag==1)
count++;
}
}
for(i=0;i<n;i++)
if(p]i].finish==1)
count++;
printf(“\n The safe sequence is:\t”);
if(count++n)
for(i=0;i<k;i++)
printf(“ %d\t”,sequence[i]);
else
printf(“SYSTEM IS NOT IN A SAFE STATE \n\n”);
return 0;
RESULT:
Ex.No. 7(D) DEADLOCK DETECTION
DATE:
AIM:
ALGORITHM:
1. Start
2. Raed the total no of processes.
3. Enter claim and allocation matrix for the n no.of processes.
4. Read resource vector & available vector.
5. Based on deadlock avoidance check whether deadlock are caused or not.
6. Display the deadlocked process.
7. End.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("enter total no of processes");
scanf("%d",&tp);
printf("enter claim matrix");
for(i=1;i<=4;i++)
for(j=1;j<=5;j++)
{
scanf("%d",&c[i][j]);
}
printf("enter allocation matrix");
for(i=1;i<=4;i++)
for(j=1;j<=5;j++)
{
scanf("%d",&p[i][j]);
}
printf("enter resource vector:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&r[i]);
}
printf("enter availability vector:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=4;i++)
{
sum=0;
for(j=1;j<=5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=4;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=5;j++)
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
RESULT:
Ex.No. 7(E) THREADING & SYNCHRONIZATION APPLICATION DATE
AIM:
ALGORITHM:
PROGRAM:
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
RESULT:
Ex.No. 8(A) MEMORY ALLOCATION MANAGEMENT – FIRST FIT
DATE:
AIM:
PROGRAM:
#include<stdio.h>
struct mainblock
{
int size,occupied;
struct mainblock *next;
}*blocks=NULL,*first=NULL;
struct process
{
int size,blockused,accomodated;
}p[10];
main()
{
int noblocks,noprocess,i,j,t,count=0;
struct mainblock *temp;
printf("\nEnter the no of blocks :");
scanf("%d",&noblocks);
blocks=(struct mainblock*)malloc(noblocks);
printf("\nEnter the size of blocks\n");
for(i=0;i<noblocks;i++)
{
scanf("%d",&t);
if(first==NULL)
{
first=(struct mainblock*)malloc(1);
first->size=t;
first->occupied=0;
first->next=NULL;
blocks=first;
}
else
{
temp=(struct mainblock*)malloc(1);
temp->size=t;
temp->occupied=0;
temp->next=NULL;
blocks->next=temp;
blocks=temp;
}
}
printf("\nEnter the no of process :");
scanf("%d",&noprocess);
printf("\nEnter the size of process \n");
for(i=0;i<noprocess;i++)
{
scanf("%d",&p[i].size);
p[i].accomodated=0;
}
for(i=0;i<noprocess;i++)
{
for(j=0,temp=first;j<noblocks&&p[i].accomodated!=1;j++,temp=temp->next)
{
if(p[i].size<=temp->size&&temp->occupied==0)
{
count++;
temp->occupied=1;
p[i].accomodated=1;
p[i].blockused=j+1;
printf("%d",i+1);
}
}
}
if(count>0)
{
printf("\nAccomodated process\t\tblock number");
for(i=0;i<noprocess;i++)
{
if(p[i].accomodated==1)
printf("\n\t%d\t\t\t%d",i+1,p[i].blockused);
}
}
if(count!=noprocess)
{
printf("\nProcess not accomodated");
for(i=0;i<noprocess;i++)
if(p[i].accomodated==0)
printf("%d",i+1);
}
}
RESULT:
Ex.No. 8(B) MEMORY ALLOCATION MANAGEMENT– BEST FIT
DATE:
AIM:
PROGRAM:
#include<stdio.h>
struct mainblock
{
int size,occupied;
struct mainblock *next;
}*block=NULL,*best=NULL;
struct process
{
int size,blockused,accommodated;
}p[10];
main()
{
int noblocks,noprocess,i,j,t1,t2,t3,count=0;
struct mainblock *temp1,*temp2;
printf(“\nEnter no of blocks :”);
scanf(“%d”,&noblocks);
blocks=(struct mainblock*)malloc(noblocks);
printf(“\nEnter the size of the blocks :\n”);
for(i=0;i<noblocks;i++)
{
scanf(“%d”,&t1);
if(best==NULL)
{
best=(struct mainblock*)malloc(1);
best->size=t1;
best->occupied=0;
best->next=NULL;
blocks=first;
}
else
{
temp1=(struct mainblock*)malloc(1);
temp1->size=t1;
temp1->occupied=0;
temp1->next=NULL;
blocks->next=temp1;
blocks=temp1;
}
}
printf(“\nEnter the no of process :”);
scanf(“%d”,&noprocess);
printf(“\nEnter the size of the process :”);
for(i=0;i<noprocess;i++)
{
scanf(“%d”,&p[i].size);
p[i].accomodated=0;
}
for(i=0;i<noprocess;i++)
{
for (j=0,temp1=best,t2=0;j<noblocks;j++,temp1=temp1->next)
{
if(p[i].size<=temp1->size&& temp1->occupied==0)
{
t2++;
if(t2==1)
{
t3=j+1;
temp2=temp1;
}
if(temp1->size<temp2->size)
{
temp2=temp1;
t3=j+1;
}
}
` if(t2>0)
{
count++;
temp2->occupied=1;
p[i].accomodated=1;
p[i].blockused=t3;
}
}
}
if(count>0)
{
printf(“\nAccomodated process\t\tBlock number”);
for(i=0;i<noprocess;i++)
if(p[i].accomodated==1)
printf(“\n%d\t\t%d”,i+1,p[i].blockused);
}
if(count!=noprocess)
{
printf(“\nProcess not accommodated”);
for(i=0;i<noprocess;i++)
if(p[i].accomodated==0)
printf(“\n%d”,i+1);
}
}
RESULT:
Ex.No. 8(C) MEMORY ALLOCATION MANAGEMENT – WORST FIT
DATE:
AIM:
PROGRAM:
#include<stdio.h>
struct mainblock
{
int size,occupied;
struct mainblock *next;
}*block=NULL,*worst=NULL;
struct process
{
int size,blockused,accommodated;
}p[10];
main()
{
int noblocks,noprocess,i,j,t1,t2,t3,count=0;
struct mainblock *temp1,*temp2;
printf(“\nEnter no of blocks :”);
scanf(“%d”,&noblocks);
blocks=(struct mainblock*)malloc(noblocks);
printf(“\nEnter the size of the blocks :\n”);
for(i=0;i<noblocks;i++)
{
scanf(“%d”,&t1);
if(worst==NULL)
{
worst=(struct mainblock*)malloc(1);
worst->size=t1;
worst->occupied=0;
worst->next=NULL;
blocks=worst;
}
else
{
temp1=(struct mainblock*)malloc(1);
temp1->size=t1;
temp1->occupied=0;
temp1->next=NULL;
blocks->next=temp1;
blocks=temp1;
}
}
printf(“\nEnter the no of process :”);
scanf(“%d”,&noprocess);
printf(“\nEnter the size of the process :”);
for(i=0;i<noprocess;i++)
{
scanf(“%d”,&p[i].size);
p[i].accomodated=0;
}
for(i=0;i<noprocess;i++)
{
for (j=0,temp1=worst,t2=0;j<noblocks;j++,temp1=temp1->next)
{
if(p[i].size<=temp1->size&& temp1->occupied==0)
{
t2++;
if(t2==1)
{
t3=j+1;
temp2=temp1;
}
if(temp1->size>temp2->size)
{
temp2=temp1;
t3=j+1;
}
}
` if(t2>0)
{
count++;
temp2->occupied=1;
p[i].accommodated=1;
p[i].blockused=t3;
}
}
}
if(count>0)
{
RESULT:
Ex.No. 8(D) MEMORY MANAGEMENT USING PAGING TECHNIQUE
DATE:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],n,i,pa,p,a,d;
printf("\nProgram for paging");
printf("\n enter the no.of pages");
scanf("%d",&n);
printf("\nEnter the base address:");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
printf("\nEnter the logical address:");
scanf("%d",&d);
printf("\n enter the page no");
scanf("%d",&p);
for(i=1;i<=n;i++)
{
if(i==p)
{
pa=b[i]+d;
a=b[i];
printf("\n\tPageNo.\t BaseAdd. PhysicalAdd. \n\t %d \t %d \t %d \t ",p,a,pa);
exit(0);
}
}
printf("\nInvalid page");
}
RESULT:
Ex.No. 9(A) PAGE REPLACEMENT ALGORITHM – FIFO
DATE:
AIM:
ALGORITHM:
1. Start
2. Read no.of pages, frames and page size for each pages.
3. Check whether the page is already in frame or not.
4. If no pages fount then increase the page fault by replacing the pages based on fifo order.
5. Display the no.of page faults.
6. End.
PROGRAM:
#include<graphics.h>
#include<stdlib.h>
void main()
{
//declare pages for store page nos, frames to store frame details
int pages[10], frames[10][10],ppos[10],fillcount=0,least;
//no_p stands for no of pages, no_f stands for no of frames
int i,j,k,m,pos=0,no_p,no_f,faults=0;
int gd=DETECT,gm,no;
int x1=20,y1=100;
float ratio;
char found,
str[30],ch;
clrscr();
//GRAPHICS initialise method
initgraph(&gd,&gm,"..//bgi");
setbkcolor(BLUE);
puts("Enter no of pages do u have");
scanf("%d",&no_p);
puts("Enter no of frames do u have");
scanf("%d",&no_f);
//initializing each frame with 0
for(i=0;i<no_f;i++)
for(j=0;j<no_p;j++)
frames[i][j]=0;
for(i=0;i<no_p;i++)
{
puts("Enter page num");
scanf("%d",&pages[i]);
clrscr();
cleardevice();
x1=20,y1=100;
found='f';
for(j=0;j<no_f;j++)
{
if(i!=0) frames[j][i]=frames[j][i-1];
//checking whether page is there in frames or not
if(frames[j][i]==pages[i])
found='t';
}
//if PAGE is not there in frames
if(found=='f')
{
faults++;
fillcount++;
if(fillcount<=no_f)
{
frames[pos][i]=pages[i];
pos++;
}
else
{
for(j=0;j<no_f;j++)
ppos[j]=0;
for(j=0;j<no_f;j++)
{
for(k=i-3;k<i;k++)
{
if(frames[j][i]==pages[k])
ppos[j]=k;
}
}
least=ppos[0];
for(j=0;j<no_f;j++)
{
if(ppos[j]<least)
least=ppos[j];
}
for(j=0;j<no_f;j++)
if(pages[least]==frames[j][i])
pos=j;
frames[pos][i]=pages[i];
}
}
//printing frames each time we enter a no
settextstyle(2,0,6);
for(k=0;k<no_f;k++)
{
for(j=0;j<=i;j++)
{
rectangle(x1,y1,x1+40,y1+45);
if(frames[k][j]!=0)
{
//changing text color in case of replacement
if(j==i&&frames[k][j]==pages[i]&&found=='f')
setcolor(MAGENTA);
else
setcolor(WHITE);
itoa(frames[k][j],str,10);
outtextxy(x1+15,y1+15,str);
}
else
outtextxy(x1+10,y1+10,"");
setcolor(WHITE);
x1+=55;
}
y1+=45;
x1=20;
}
}
//printing page fault ratio
printf("/n/n page fault ratio=%f",(float)faults/(float)no_p);
getch();
}
RESULT:
Ex.No. 9(B) PAGE REPLACEMENT ALGORITHM– LRU
DATE:
AIM:
ALGORITHM:
1. Start
2. Read no.of pages, frames and page size for each pages.
3. Check whether the page is already in frame or not.
4. If no pages fount then increase the page fault by replacing the pages based on LRU order.
5. Display the no.of page faults.
6. End.
PROGRAM:
#include<graphics.h>
#include<stdlib.h>
void main()
{
//declare pages for stores page nos, frames to store frame details
int pages[10],frames[10][10],ppos[10],fillcount=0,least;
//no_pstands for no of pages, no_f stands for no of frames
int i,j,k,m,pos=0;
no_p,no_f,faults=0;
gd=DETECT,gm,no;
int x1=20,y1=100;
float ratio;
char found,
str[30],ch,occur;
clrscr();
//GRAPHICS initialise method
initgraph(&gd,&gm,"..//bgi");
setbkcolor(BLUE);
RESULT:
Ex.No. 9(C) PAGE REPLACEMENT ALGORITHM – LFU
DATE:
AIM:
ALGORITHM:
1. Start
2. Read no.of pages, frames and page size for each pages.
3. Check whether the page is already in frame or not.
4. If no pages fount then increase the page fault by replacing the pages based on LFU order.
5. Display the no.of page faults.
6. End.
PROGRAM:
#include<graphics.h>
#include<stdlib.h>
void main()
{
//declare pages for stores page nos, frames to store frame details
int pages[10],frames[10][10];
//no_pstands for no of pages, no_f stands for no of frames
int i,j,k,m,pos=0;
no_p,no_f,faults=0;
gd=DETECT,gm,no;
int x1=20,y1=100;
float ratio;
char found,
str[30],ch;
clrscr();
//GRAPHICS initialise method
initgraph(&gd,&gm,"..//bgi");
setbkcolor(BLUE);
puts("Enter no of pages do u have");
scanf("%d",&no_p);
AIM:
ALGORITHM:
1. Start
2. Read the no of files and store it as in count variable.
3. Read the root directory
4. Read the files to be inserted.
5. Display the structure.
6. End.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,count,i,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
cleardevice();
setbkcolor(GREEN);
puts("Enter no of files do u have?");
scanf("%d",&count);
for(i=0;i<count;i++)
{
cleardevice();
setbkcolor(GREEN);
printf("Enter file %d name",i+1);
scanf("%s",fname[i]);
setfillstyle(1,MAGENTA);
mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextjustify(1,1);
outtextxy(320,125,"Root Directory");
setcolor(BLUE);
for(j=0;j<=i;j++,cir_x+=mid)
{
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[j]);
}
getch();
}
}
RESULT:
Ex.No. 10(B) FILE ORGANIZATION TECHNIQUE – TWO LEVEL DIRECTORY
DATE:
AIM:
ALGORITHM:
1. Start
2. Read the root and first level directory
3. Read the respective files to be stored in root and first level directories.
4. Display the two level directory structures.
5. End.
PROGRAM:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"null",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\bgi");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node*)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
if(lev==0||lev==1)
(*root)->ftype=1;
else
(*root)->ftype=2;
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
if(lev==0||lev==1)
{
if((*root)->level==0)
printf("How many users");
else
printf("how many files");
printf("(for%s):",(*root)->name);
scanf("%d",&(*root)->nc);
}
else
(*root)->nc=0;
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name, lx+gap*i, lx+gap*i+gap, lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}}
RESULT:
Ex.No. 10(C) FILE ORGANIZATION TECHNIQUE –HIERARCHICAL
DIRECTORY
DATE:
AIM:
ALGORITHM:
1. Start
2. Read the root, first and second level directory
3. Read the respective files to be stored in root, first, second level directories.
4. Display the hierarchical structure for giving the input.
5. End.
PROGRAM:
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
{
create(&((*root)->link[i]),lev+1,(*root)->name, lx+gap*i, lx+gap*i+gap, lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}
Ex.No. 10(D) FILE ORGANIZATION TECHNIQUE – DAG
AIM:
ALGORITHM:
1. Start
2. Read the root, first, second and three level directories.
3. Read the respective files to be stored in root, first, second and three level directories.
4. Display the direct acyclic graph structures.
5. End.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element node;
typedef struct
{
char from[20];
char to[20];
}link;
link L[10];
int nofl;
node * root;
void main()
{
int gd=DETECT,gm;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
read_links();
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}
read_links()
{
int i;
printf("how many links");
scanf("%d",&nofl);
for(i=0;i<nofl;i++)
{
printf("File/dir:");
fflush(stdin);
gets(L[i].from);
printf("user name:");
fflush(stdin);
gets(L[i].to);
}
}
draw_link_lines()
{
int i,x1,y1,x2,y2;
for(i=0;i<nofl;i++)
{
search(root,L[i].from,&x1,&y1);
search(root,L[i].to,&x2,&y2);
setcolor(LIGHTGREEN);
setlinestyle(3,0,1);
line(x1,y1,x2,y2);
setcolor(YELLOW);
setlinestyle(0,0,1);
}
}
search(node *root,char *s,int *x,int *y)
{
int i;
if(root!=NULL)
{
if(strcmpi(root->name,s)==0)
{
*x=root->x;
*y=root->y;
return;
}
else
{
for(i=0;i<root->nc;i++)
search(root->link[i],s,x,y);
}}}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("enter name of dir/file(under %s):",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for dir/ 2 for file:");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("no of sub directories /files (for %s):",(*root)->name);
scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create( & ( (*root)->link[i] ) , lev+1 , (*root)->name, lx+gap*i,l x+gap*i+gap, lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
/* displays the constructed tree in graphics
mode */ display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
{
display(root->link[i]); }}}
Ex.No. 11(A) CONTINUOUS MEMORY ALLOCATION
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the no.of blocks.
3. Read the processes size and name to insert.
4. Check the continuous availability of blocks.
5. Allocate the process continuously.
6. Display the allocated process.
7. Stop.
PROGRAM:
#include<stdio.h>
#include<string.h>
struct block
{
int b_id;
int b_alloted;
};
struct block b[50];
void main()
{
int i,no,n,bname,j,count=0,eblock,sblock,ch,num;
int psize,flag=1,pname;
printf("Enter no of blocks");
scanf("%d",&n);
for(i=0;i<n;i++)
{
b[i].b_id=i;
b[i].b_alloted=0;
}
do
{
RESULT:
Ex.No. 11(B) INDEXED MEMORY ALLOCATION
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the no.of blocks.
3. Read the allocated blocks and yet to be allocated.
4. Read the processes size and name to insert.
5. Check the free blocks.
6. Index the free blocks with availability.
7. Display the allocated process.
8. Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int b_allocated[30];
void display(int n)
{
int i,count=0;
printf("\n\n");
for(i=0;i<n;i++)
{
if(count<3)
{
printf("\tb[%2d]--%d",i,b_allocated[i]);
count=count+1;
}
else
{
printf("\n\tb[%2d]--%d",i,b_allocated[i]);
count=1;
}
}
}
main()
{
int j=0,index[50],indexloc,i,n,no,psize,pname,block,freespace=0,count=0;
printf("Enter the no of blocks");
scanf("%d",&n);
freespace=n;
printf("Enter the no of blocks allocated");
scanf("%d",&no);
for(i=0;i<no;i++)
{
b_allocated[i]=0;
}
printf("Enter the no of blocks that are 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("Enter the name of the process");
scanf("%d",&pname);
printf("Enter 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[i]=0;
index[j]=i;
j=j+1;
count=count+1;
}
}
}
display(n);
printf("\n\nBlocks stored are\n");
for(i=0;i<j;i++)
printf("%d",index[i]);
printf("\nDirectory structure");
printf("\nProcess \t Index\n");
printf("\n%d\t%d",pname,indexloc);
}
else
{
printf("Process cannot be allocated");
}
getch();
}
RESULT:
Ex.No. 11(C) LINKED FILE ALLOCATION
DATE:
AIM:
ALGORITHM:
1. Start.
2. Read the no.of blocks.
3. Read the allocated blocks and yet to be allocated.
4. Read the processes size and name to insert.
5. Check the free blocks.
6. Index the free blocks with availability.
7. Display the allocated process.
8. Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n;
for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks already allocated");
scanf("%d",&p);
printf("\nenter the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter index starting block & length"); scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1; printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j); k++;
}
}
}
else
printf("\nif u enter one more (yes-1/no-0)"); scanf("%d",&c);
if(c==1) goto X; else exit();
getch( );
}
RESULT: