0% found this document useful (0 votes)
31 views82 pages

Lab Doc Manual

The document outlines the installation procedures for Windows and Linux operating systems, detailing steps for creating bootable drives and configuring settings during installation. It also covers basic Unix commands, shell programming examples for employee payroll and mathematical functions, and various system calls like fork, exec, and wait. Additionally, it includes an implementation of the First Come First Serve (FCFS) CPU scheduling algorithm in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views82 pages

Lab Doc Manual

The document outlines the installation procedures for Windows and Linux operating systems, detailing steps for creating bootable drives and configuring settings during installation. It also covers basic Unix commands, shell programming examples for employee payroll and mathematical functions, and various system calls like fork, exec, and wait. Additionally, it includes an implementation of the First Come First Serve (FCFS) CPU scheduling algorithm in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXNO: 1 Installation of Operating system : Windows/ Linux

Aim:

To study Installation of Operating system: Windows/ Linux.

Procedure:

Installing an operating system (OS) is a process that typically involves creating a


bootable drive with the installation files and then booting from that drive to start
the installation process. Here are the basic steps for installing Windows and Linux
operating systems:

Installing Window:

1.Obtain the installation media (e.g. a Windows installation USB drive or DVD).
2. Connect the installation media to your computer and restart the machine.
3. Boot the machine from the installation media by changing the boot order in the
BIOS or UEFI settings.
4. Follow the on-screen instructions to install Windows. You will need to accept
the license agreement, select the installation type (e.g. custom or upgrade), choose
the installation location (e.g. which hard drive and partition), and configure basic
settings such as language, time and currency format, and keyboard input method.
5. Once the installation is complete, the machine will restart and guide you through
the initial setup process.
Installing Linux:

1. Obtain the installation media (e.g. a Linux distribution ISO file).


2. Create a bootable drive with the installation media.
3. Connect the bootable drive to your computer and restart the machine.
4. Boot the machine from the bootable drive by changing the boot order in the
BIOS or UEFI settings.
5. Follow the on-screen instructions to install the Linux distribution. You will need
to select the language, keyboard layout, and time zone. You may also need to
partition the hard drive, select the installation location, and configure basic settings
such as username, password, and network configuration.
6. Once the installation is complete, the machine will restart and you will be able to
log in to your new Linux system

2. i ,UNIX COMMANDS
Aim:
To execute the basic Unix commands
Commands
1. date command
The date command is used to display the current date with day of weak,
month, day, time and the year.
Syntax : date
The date command can also be used with the following format specification.
Format Purpose
Date +%m It displays the month number
Date +%h It displays the month name
Date +%d It displays the day of month
Date +%y It displays the last two digits of the year
Date +%H It displays the hour
Date +%M It displays the minutes
Date +%S It displays the seconds

2. who command
It is used to display the data about all the users, who are currently logged in to the
system. It displays the output format as login date with time.
Syntax : who
3. who am i
It displays the username, terminal line, login date and time.
Syntax : who am i
4. cal command
It is used to displays the calander.
Syntax : cal month and year
5.clear command
It is used to clear the screen
Syntax : clear
6. echo command
It is used to print the message on the screen whatever you have typed on the screen
Syntax : echo text
7.id command
It is used to displays the numerical value that corresponds to your login name.
Syntax : id
8.bc command
unix offer an online calculator and can be invoked by the command bc.
Syntax :bc
9. cat command
It is used to create a file
Syntax : cat >file name
Ex: cat >abc
Raja
Ragu
Cat file name
It is used to displays the contents of the file.
Syntax : cat filename
10. ctrl+z
It is used to save the file
11.cp command
It is used to copy the content of one file to another file.
Syntax : cp source filename destination filename
12.rm command
It is used to delete the file
Syntax :rm file name
13.mv command
It is used to rename a file
Syntax : mv filename new filename
14.word count
It is used to count and displays the number of words or sentences or characters
Syntax :wc file name
Ex :wc –l file name
It displays the number of lines
wc –w file name
It displays the number of words
wc –c file name
It displays the number of characters
15. ls command
It is used to lists all the files in current directory.
Syntax :ls
Ex: ls –a
It displays the all the files including hidden files in the current directory
ls –i
It displays all the files with I as node number
ls –t
it list the files sorted by last modified time
ls –tt
it list the files sorted by last access time
ls –lt
it list files in long form sorted by last modified time
Ls -r

it displays the regressive list of all files in all sub directory

ls ?

It displays the files with the name having single letter

ls ??

It displays the files with the name having double letter

ls *

It displays all the files including sub directory

16 .diff command

it compares the two file and displays the differences

syntax : diff file1 file 2

17.rmdir command

it is used to remove the empty directories


syntax :rmdir directory name

rm -r

it is used to remove the directories with files.

syntax :rm -r directory name

18.mk directory

it is used to create a new directory

syntax :mkdir name

19.cd command
it is used to change the current directory
syntax : cd directory name

cd.

it retrieves current directory

cd..

it is used to move parent directory

20.mv command

it is used to move one directory to another directory.

2. ii SHELL PROGRAM

Aim :

To implement the following program Employee Payroll, Simple Function, Greatest among three
numbers, Factorial of given number using shell program.

Employee Payroll:

Algorithm :

Step 1 : Start the program


Step 2 : Declare the function.
Step 3 : Get the input values employee id, allowance, deduction and basic bay from the user.
Step 4 :Calculate the gross salary by using gross=allowance+basic pay.
Step 5 : Calculate the net salary by using net=gross-deduction.
Step 6 : Display the result
Step 7 : Stop the program.
Program :

get()
{
echo “Enter id, allowance, deduction, basic pay”
read id
read all
readded
read basic
}
display()
{
echo “the id is $id”
echo “the allowance is $all”
echo “the deduction is $ded”
echo “the Basic pay is $basic”
}
get $id,$all,$ded,$basic
display
gross=$(($basic + $all))
net=$((gross - $ded))
echo “the gross is $gross
echo “the net is $net”
Simple function using shell:
Algorithm :

Step 1 : Start the program


Step 2 : Declare the function.
Step 3 : Assign the values.
Step 4 : Call the function.
Step 5 : Display the result
Step 6 : Stop the program.
Program :
program()
{
echo “1 is $a”
echo “2 is $b”
a=”good bye”
}
a=hello
b=world
program $a,$b
echo “a is $a”
echo “b is $b”
Greatest among three numbers :
Algorithm :

Step 1 : Start the program


Step 2 : Get the input A,B,C values from the user.
Step 3 : Check the values which is greater.
Step 4 : Display the result
Step 5 : Stop the program.
Program :
echo “enter A value”
read a
echo “enter B value”
read b
echo “enter C value”
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo “ A is greater”
elif[ $b -gt $c ]
then
echo “ B is greater”
else
echo “ C is greater”
fi
Factorial of given number :
Step 1 : Start the program
Step 2 : Get the input from the user.
Step 3 : Calculate the factorial values.
Step 4 : Display the result
Step 5 : Stop the program.
Program :
echo “ Enter the Number ”
read n
f=1
a=0
i=1
if[ $n -eq $a ]
then
echo $f
else
while[i -le $n ]
do
f=$(($f * $i))
i=$(($i + 1))
done
fi
echo “ The Factorial of N is”
echo $f
Result :
Thus the shell programs are executed successfully

EXP: 3A FORK SYSTEM CALL

Aim

To create a new child process using fork system call.

fork()

The fork system call is used to create a new process called child process.

o The return value is 0 for a child process.

o The return value is negative if process creation is unsuccessful.

o For the parent process, return value is positive

The child process is an exact copy of the parent process.

Both the child and parent continue to execute the instructions following fork call.

The child can start execution before the parent or vice-versa.

getpid() and getppid()

The getpid system call returns process ID of the calling process

The getppid system call returns parent process ID of the calling process
Algorithm

1. Declare a variable x to be shared by both child and parent.

2. Create a child process using fork system call.

3. If return value is -1 then

Print "Process creation unsuccessfull"

Terminate using exit system call.

4. If return value is 0 then

Print "Child process"

Print process id of the child using getpid system call

Print value of x

Print process id of the parent using getppid system call

5. Otherwise

Print "Parent process"

Print process id of the parent using getpid system call

Print value of x

Print process id of the shell using getppid system call.

6. Stop

Program

/* Process creation - fork.c */

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

main()

pid_tpid;

int x = 5;
pid = fork();

x++;

if (pid< 0)

printf("Process creation error");

exit(-1);

else if (pid == 0)

printf("Child process:");

printf("\nProcess id is %d", getpid());

printf("\nValue of x is %d", x);

printf("\nProcess id of parent is %d\n", getppid());

else

printf("\nParent process:");

printf("\nProcess id is %d", getpid());

printf("\nValue of x is %d", x);

printf("\nProcess id of shell is %d\n", getppid());

}
Result

Thus a child process is created with copy of its parent's address space.

Exp:3b exec system call

Aim

To load an executable program in a child processes exec system call.

execl()

The exec family of function (execl, execv, execle, execve, execlp, execvp) is used
by thechild process to load a program and execute.

execl system call requires path, program name and null pointer

Algorithm

1. Create a child process using fork system call.

2. If return value is -1 then

a. Print "Process creation unsuccessfull"

3. Terminate using exit system call.

4. If return value is > 0 then

a. Suspend parent process until child completes using wait system call

b. Print "Child Terminated".

c. Terminate the parent process.

5. If return value is 0 then

a. Print "Child starts"


b. Load date program into child process using exec system call.

c. Terminate the child process.

6. Stop

Program

/* Load a program in child process - exec.c */

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

main()

pid_tpid;

switch(pid = fork())

case -1:

perror("Fork failed");

exit(-1);

case 0:

printf("Child process\n");

execl("/bin/date", "date", 0);

exit(0);

default:

wait(NULL);

printf("Child Terminated\n");
exit(0);

Result

Thus the child process loads a binary executable file into its address space.

Exp: 3c wait system call

Aim

To block a parent process until child completes using wait system call.

wait()

The wait system call causes the parent process to be blocked until a child
terminates.

When a process terminates, the kernel notifies the parent by sending the
SIGCHLD signal to the parent.

Without wait, the parent may finish first leaving a zombie child, to be adopted by
init process

Algorithm

1. Create a child process using fork system call.

2. If return value is -1 then

a. Print "Process creation unsuccessfull"

3. Terminate using exit system call.

4. If return value is > 0 then

a. Suspend parent process until child completes using wait system call

b. Print "Parent starts"

c. Print even numbers from 0–10


d. Print "Parent ends"

5. If return value is 0 then

a. Print "Child starts"

b. Print odd numbers from 0–10

c. Print "Child ends"

6. Stop

Program

/* Wait for child termination - wait.c */

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

main()

inti, status;

pid_tpid;

pid = fork();

if (pid< 0)

printf("\nProcess creation failure\n");

exit(-1);

}
else if(pid> 0)

wait(NULL);

printf ("\nParent starts\nEvenNos: ");

for (i=2;i<=10;i+=2)

printf ("%3d",i);

printf ("\nParent ends\n");

else if (pid == 0)

printf ("Child starts\nOddNos: ");

for (i=1;i<10;i+=2)

printf ("%3d",i);

printf ("\nChild ends\n");

Result

Thus using wait system call zombie child processes were avoided.
Exno: 4Write C programs to implement the various CPU Scheduling
Algorithms a.FCFS SCHEDULING ALGORITHM

Aim:
To write a program to implement First Come First Serve scheduling algorithm using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 : Get the input no. of process, processes, processes burst time.
Step 4 :Assign the initial value of waiting process is 0.
Step 5 :Calculate process waiting time and generate gantt chart.
Step 6 :Calculate averagewaiting time of processes.
Step 7 : Calculate turnaround time of processes.
Step 8 : Display the waiting time of processes.
Step 9 : Display averagewaiting time of processes.
Step 10 :Display turnaround time of processes.
Step 11 :Display turnaround time of processes.
Step 12 : Stop the program.
Program:
#include<stdio.h>
#include <conio.h>
void main()
{
intn,wt[10],bt[10],i,p[10],tat[10];
int sum=0,avg=0;
clrscr();
printf("\nEnter the number of Processes: ");
scanf("%d",&n);
printf("\nEnter the process & Burst time");
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i],&bt[i]);
}
wt[0]=0;
for(i=0;i<=n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
printf("\n Gantt Chart\n ");
for(i=0;i<n;i++)
{
printf("\tP%d",i+1);
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("\t%d",wt[i]);
}
printf("\n Process Name \t Burst time");
for(i=0;i<n;i++)
{
printf("\nP%d\t%d",i,bt[i]);
}
for(i=0;i<n;i++)
{
sum=sum+wt[i];
}
avg=sum/n;
printf("\n average waiting time :%d",avg);
printf("\n Process Name \t Turn around time");
for(i=1;i<=n;i++)
{
printf("\nP%d\t%d",i,tat[i]);
}
sum=0;
avg=0;
for(i=0;i<n;i++)
{
sum=sum+wt[i];
}
avg=sum/n;
printf("\n average Turn arounf time :%d",avg);
getch();
}

Output :
Enter the number of processes : 3
Enter the processes and burst time :
1 10
2 4
3 6
Gantt Chart
P1 P2 P3
0 10 14 20
Process Waiting time
P1 0
P2 10
P3 14
Average waiting time of processes is 8.0000
Process turn around time
P1 10
P2 14
P3 20
Average turn around time of processes is 14.666667
Result :
Thus the program FCFS scheduling algorithm is implemented.

4.b SJF SCHEDULING ALGORITHM


Aim:
To write a program to implement Shortest Job First scheduling algorithm using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 : Get the input no. of processes, processes, processes burst time.
Step 4 :Sort the shortest burst time and processes.
Step 5 :Assign the initial value of waiting process is 0.
Step 6 :Calculate process waiting time and generate gantt chart.
Step 7 :Calculate averagewaiting time of processes.
Step 8 : Calculate turnaround time of processes.
Step 9 : Display the waiting time of processes.
Step 10 : Display averagewaiting time of processes.
Step 11:Display turnaround time of processes.
Step 12 :Display turnaround time of processes.
Step 13 : Stop the program.
Program:
#include<stdio.h>
#include <conio.h>
void main()
{
intn,wt[10],bt[10],i,p[10],tat[10],t1,t2;
int sum=0,avg=0;
clrscr();
printf("\nEnter the number of Processes: ");
scanf("%d",&n);
printf("\nEnter the process & Burst time");
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i],&bt[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(bt[i]<bt[j])
{
t1=bt[i];
bt[i]=bt[j]
bt[j]=t1;
t2=p[i];
p[i]=p[j]
p[j]=t2;
}
}
}
wt[0]=0;
for(i=0;i<=n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
printf("\n Gantt Chart\n ");
for(i=0;i<n;i++)
{
printf("\tP%d",i+1);
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("\t%d",wt[i]);
}
printf("\n Process Name \t Burst time");
for(i=0;i<n;i++)
{
printf("\nP%d\t%d",i,bt[i]);
}
for(i=0;i<n;i++)
{
sum=sum+wt[i];
}
avg=sum/n;
printf("\n average waiting time :%d",avg);
printf("\n Process Name \t Turn around time");
for(i=1;i<=n;i++)
{
printf("\nP%d\t%d",i,tat[i]);
}
sum=0;
avg=0;
for(i=0;i<n;i++)
{
sum=sum+wt[i];
}
avg=sum/n;
printf("\n average Turn arounf time :%d",avg);
getch();
}

Output :
Enter the number of processes : 3
Enter the processes and burst time :
1 10
2 6
3 12
Gantt Chart
P2 P1 P3
0 6 16 28
Process Waiting time
P2 0
P1 6
P3 16
Average waiting time of processes is 7.3333
Process turn around time
P2 6
P1 16
P3 28
Average turn around time of processes is 16.666667

Result :
Thus the program FCFS scheduling algorithm is implemented.
4.C PRIORITY SCHEDULING ALGORITHM
Aim:
To write a program to implement priority scheduling algorithm using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 : Get the input no. of processes, processes, processes burst time and priority of processes.
Step 4 :Sort the shortest priority, burst time and processes.
Step 5 :Assign the initial value of waiting process is 0.
Step 6 :Calculate process waiting time and generate gantt chart.
Step 7 :Calculate averagewaiting time of processes.
Step 8 : Calculate turnaround time of processes.
Step 9 : Display the waiting time of processes.
Step 10 : Display averagewaiting time of processes.
Step 11:Display turnaround time of processes.
Step 12 :Display turnaround time of processes.
Step 13 : Stop the program.
Program:
#include<stdio.h>
#include <conio.h>
void main()
{
intn,wt[10],bt[10],i,p[10],tat[10],t1,t2,t3,pr[10];
int sum=0,avg=0;
clrscr();
printf("\nEnter the number of Processes: ");
scanf("%d",&n);
printf("\nEnter the process,Burst time& priority of processes ");
for(i=0;i<n;i++)
{
scanf("%d%d%d",&p[i],&bt[i],&pr[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pr[i]<pr[j])
{
t3=pr[i]
pr[j]=pr[i];
pr[j]=t3;
t1=bt[i];
bt[i]=bt[j]
bt[j]=t1;
t2=p[i];
p[i]=p[j]
p[j]=t2;
}
}
}
wt[0]=0;
for(i=0;i<=n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
printf("\n Gantt Chart\n ");
for(i=0;i<n;i++)
{
printf("\tP%d",i+1);
}
printf("\n");
for(i=0;i<=n;i++)
{
printf("\t%d",wt[i]);
}
printf("\n Process Name \t Burst time");
for(i=0;i<n;i++)
{
printf("\nP%d\t%d",i,bt[i]);
}
for(i=0;i<n;i++)
{
sum=sum+wt[i];
}
avg=sum/n;
printf("\n average waiting time :%d",avg);
printf("\n Process Name \t Turn around time");
for(i=1;i<=n;i++)
{
printf("\nP%d\t%d",i,tat[i]);
}
sum=0;
avg=0;
for(i=0;i<n;i++)
{
sum=sum+wt[i];
}
avg=sum/n;
printf("\n average Turn arounf time :%d",avg);
getch();
}

Output :
Enter the number of processes : 3
Enter the process, Burst time& priority of processes
1 10 3
2 5 1
3 3 2
Gantt Chart
P2 P3 P1
0 5 8 18
Process Waiting time
P2 0
P3 5
P1 8
Average waiting time of processes is 4.3333
Process turn around time
P2 5
P3 8
P1 18
Average turn around time of processes is 10.33333

Result :
Thus the program priority scheduling algorithm is implemented.
4.d ROUND ROBIN SCHEDULING ALGORITHM
Aim:
To write a program to implement Round Robin scheduling algorithm using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 : Get the input no. of processes, processes, processes burst time and time quantum.
Step 4 :Assign the initial value of waiting process is 0.
Step 5 :Calculate process waiting time and generate gantt chart.
Step 6 :Calculate averagewaiting time of processes.
Step 6 : Calculate turnaround time of processes.
Step 6 : Display the waiting time of processes.
Step 6 : Display averagewaiting time of processes.
Step 6 :Display turnaround time of processes.
Step 6 :Display turnaround time of processes.
Step 7 : Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
intn,i,j,tq,t[10],t2[10],t1[10];
int p[10],b[10],b1[10],wt[10],c[100],wc[100];
int temp=0,temp1,temp2,tp[100],x=0,ch,tp1[100];
floatawt=0,atat=0,tot=0,tat=0;
clrscr();
printf("\n Enter the number of processes:");
scanf("%d",&n);
printf("\nEnter the Processes:");
for(i=0;i<n;i++)
scanf("%d",&c[i]);
printf("\nEnter the Processes Burst Time:");
for(i=0;i<n;i++)
{
scanf("%d",&t[i]);
t2[i]=t[i];
if(temp<=t[i])
{
temp=t[i];
}
}
temp1=temp%2;
if(temp1!=0)
temp2=((temp/2)+1);
else
temp2=temp/2;
printf("\nEnter the Time Quantum:");
scanf("%d",&tq);
for(i=0;i<temp2;i++)
{
for(j=0;j<n;j++)
{
if(t[j]>0)
{
ch=t[j]-tq;
if(ch>0)
{
tp[x]=tq;
p[x]=c[j];
}
else
{
tp[x]=tq+ch;
p[x]=c[j];
}
t[j]=ch;
x=x+1;
}
}
}
printf("\nGantt Chart:\n");
for(i=0;i<x;i++)
{
printf("\t P%d",p[i]);
}
printf("\n");
tp1[0]=0;
for(i=0;i<x;i++)
{
tp1[i+1]=tp1[i]+tp[i];
}
printf("\n");
for(i=0;i<=x;i++)
{
printf("\t%d",tp1[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<x;j++)
{
if(c[i]==p[j])
{
wt[i]=0;
wt[i]=tp1[j+1];
wc[i]=c[i];
}
}
}
printf("\nProcess \t waiting Time\n");
for(i=0;i<n;i++)
{
printf("P%d",wc[i]);
tot=tot+(wt[i]-t2[i]);
printf("\t\t%d",wt[i]-t2[i]);
printf("\n");
}
awt=tot/n;
printf("\n the average waiting time is :%f",awt);
getch();
printf("\nProcess \t Turn Around Time\n");
for(i=0;i<n;i++)
{
printf("P%d",wc[i]);
tat=tat+wt[i];
printf("\t\t%d",wt[i]);
printf("\n");
}
atat=tat/n;
printf("\n the average turn around time is :%f",atat);
getch();
}

Output :
Enter the number of processes : 3
Enter the processes
1
2
3
Enter the burst time
6
5
4
Enter the time quantum : 2
Gantt Chart
P1 P2 P3 P1 P2 P3 P1 P2
0 2 4 6 8 10 12 14 15
Process Waiting time
P1 8
P2 10
P3 8
Average waiting time of processes is 8.66667
Process turn around time
P1 14
P2 15
P3 12
Average turn around time of processes is 13.66667

Result :
Thus the program Round Robin scheduling algorithm is implemented.

EXNO:5 Illustrate the inter process communication strategy

Aim:
To Illustrate the inter process communication strategy
Algorithm:
Step 1: Start the program.
Step 2: Create a pipe using the pipe() system call and store the read and write ends of the pipe in
fd[0] and
fd[1], respectively.
Step 3: Fork the current process using the fork() system call and store the return value in pid.
Step 4: If pid is negative, an error occurred during forking, so print an error message and exit the
program.
Step 5: If pid is greater than zero, this is the parent process.
Step 6: Generate a random number using the rand() function and print it to the console.
Step 7: Write the number to the write end of the pipe using the write() system call.
Step 8: Close the write end of the pipe using the close() system call.
Step 9: If pid is zero, this is the child process.
Step 10: Read the number from the read end of the pipe using the read() system call.
Step 11: Multiply the number by 2.
Step 12: Write the result to the write end of the pipe using the write() system call.
Step 13: Close the read end of the pipe using the close() system call.
Step 14: Exit the child process using the exit() system call.
Step 15: Back in the parent process, wait for the child process to complete using the wait()
system call.
Step 16: Read the result from the read end of the pipe using the read() system call.
Step 17: Print the result to the console.

Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int fd[2];
pid_t pid;
if (pipe(fd) == -1) {
perror("pipe");
exit(1);
}
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid > 0) {
// Parent process
int number;
close(fd[0]); // Close read end of the pipe
number = rand() % 100; // Generate a random number
printf("Parent process generated number: %d\n", number);
write(fd[1], &number, sizeof(number)); // Write number to the pipe
close(fd[1]); // Close write end of the pipe
}
else {
// Child process
int number, result;
close(fd[1]); // Close write end of the pipe
read(fd[0], &number, sizeof(number)); // Read number from the pipe
printf("Child process received number: %d\n", number);
result = number * 2; // Multiply the number by 2
write(fd[1], &result, sizeof(result)); // Write the result to the pipe
close(fd[0]); // Close read end of the pipe
}
return 0;
}

Output:

Result:
EXNO:6 Implement mutual exclusion by Semaphores
Aim:
To write a program to implement semaphore using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 :Assign the initial value semaphore variable is 0.
Step 4 :Declare the functions prosig and consig.
Step 5 :Assign the buffer size.
Step 6 :Enter the choice producer.
Step 7 : Get the input values and store it into the buffer.
Step 8 :If buffer is full then call the function prosig and increment the semaphore value 1.
Step 9 : Display the message buffer is full.
Step 10 : Enter the choice consumer.
Step 11 :Display the buffer values.
Step 12 : Display the message buffer is empty.
Step 13 : Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
charbuf[20],p[20];
intmutex=0,i,k,c;
void main()
{
int op;
void producer();
void consumer();
clrscr();
do
{
printf("\n1.producer\n2.consumer\n3.exit\n");
printf("\nenter your option");
scanf("%d",&op);
switch(op)
{
case1:
producer();
break;
case2:
consumer();
break;
case3:
exit(0);
break;
}
}
while(op<=3);
}
voidprosig()
{
mutex=mutex+1;
}
voidconsig()
{
mutex=mutex-1;
}
int buffer(intmutex)
{
if(mutex==0)
return 1;
else
return 0;
}
void producer()
{
int c;
c=buffer(mutex);
if(c==1)
{
printf("\nproducer can produce item&give $ for end\n");
i=0;
while((p[i]=getchar())!='$')
{
buf[i]=p[i];
i++;
}
k=i;
printf("\n%c",buf[i]);
prosig();
printf("\nproduction successfully done");
}
else
{
printf("\n buffer is full cannot produce item\n");
}
}
void consumer()
{
c=buffer(mutex);
if(c==0)
{
printf("\nconsumer can consume the item\n");
for(i=0;i<k;i++)
{
printf("%c",buf[i]);
}
consig();
printf("\n successfully consumed\n");
}
else
printf("\n buffer is empty cannot consume\n");
getch();
}
Output :
1. Producer
2. Consumer
3. Exit
Enter your option: 1
Producer can produce item $ for end
10
20
30
40
50
$
Production successfully done
1. Producer
2. Consumer
3. Exit
Enter your option: 2
Consumer can consume the items
10
20
30
40
50
Successfully consumed
1. Producer
2. Consumer
3. Exit
Enter your option: 2
buffer is empty cannot consume
1. Producer
2. Consumer
3. Exit
Enter your option: 3

Result :
Thus the program semaphore is implemented.

EXNO: 7 Write a C program to avoid Deadlock using Banker's Algorithm

Aim:
To write a program to implement Bankers algorithm using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 : Get the input no. of processes, processes, resources and resource allocation of processes .
Step 4 : Get the input available resources, maximum, need of processes.
Step 5 : Calculate the need matrix.
Step 6 : Need Matrix=Maximum need – allocation of resources.
Step 7 :Check the sequence of processes need with the available resources.
Step 8 : If process need is <=available.
Step 9 : Display the message process is successfully completed.
Step 10 :Add completed process allocation resources to available.
Step 11 :Check all the processes completed or not.
Step 12 : If all processes completed then print the safe sequence i.e) completed process order.
Step 13 : Otherwise print the message the system is unsafe state.
Step 14 : Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k=0,k1=0,l,r,all[10][10],max[10][10],avail[10],need[10][10];
int p[10],ss[10],count=0;
clrscr();
printf("\n Enter the number of processes:");
scanf("%d",&n);
printf("\nEnter the Processes:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("\nEnter the Number of Resources:");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("\n Enter the Process P%d Allocation : ",i+1);
for(j=0;j<r;j++)
{
scanf("%d",&all[i][j]);
}
printf("\n Enter the Process P%d Maximum : ",i+1);
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("\nEnter the Available:");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-all[i][j];
}
}
printf("\nNeed matrix is:");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf(" %d",need[i][j]);
}
}
for(l=0;l<3;l++)
{
for(i=0;i<n;i++)
{
if(p[i]!=0)
{
count=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[j])
{
count=count+1;
}
if(count==r)
{
k1=k1+1;
printf("\nP%d is completed",i+1);
for(j=0;j<r;j++)
avail[j]=avail[j]+all[i][j];
ss[k]=p[i];
p[i]=0;
k++;
}
}
}
}
}
if(k1==n)
{
printf("\nThe Safe Sequence is : ");
for(i=0;i<k;i++)
{
printf("P%d\t ",ss[i]);
}
}
else
{
for(i=0;i<n;i++)
{
if(p[i]!=0)
{
printf("\n the process P%d is not completed",p[i]);
}
}
printf("\n So the system is unsafe state");
}
getch();
}
Output :
Enter the number of processes : 5
Enter the processes : 1 2 3 4 5
Enter the number of resources : 3
Enter process P1 allocation : 0 1 0
Enter process P1 maximum : 7 5 3
Enter process P2 allocation : 2 0 0
Enter process P2 maximum : 3 2 2
Enter process P3 allocation : 3 0 2
Enter process P3 maximum : 9 0 2
Enter process P4 allocation : 2 1 1
Enter process P4 maximum : 2 2 2
Enter process P5 allocation : 0 0 2
Enter process P5 maximum : 4 3 3
Enter the available : 3 3 2
Need Matrix
743
122
600
011
431
Process P2 is completed
Process P3 is completed
Process P4 is completed
Process P5 is completed
Process P6 is completed
The system is safe state
Result :
Thus the program Bankers algorithm is implemented.

EXNO: 8 Write a C program to Implement Deadlock Detection Algorithm

Aim:
To Write a C program to Implement Deadlock Detection Algorithm
Algorithm :

1. Let Work and Finish be vectors of length m and n respectively. Initialize Work=
Available. For i=0, 1, …., n-1, if Requesti = 0, then Finish[i] = true; otherwise, Finish[i]=
false.

2. Find an index i such that both


a) Finish[i] == false
b) Requesti <= Work
If no such i exists go to step 4.

3. Work= Work+ Allocationi


Finish[i]= true
Go to Step 2.
4. If Finish[i]== false for some i, 0<=i<n, then the system is in a deadlocked state.
Moreover, if Finish[i]==false the process Pi is deadlocked.

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 the process: ");


scanf("%d",&np);
printf ("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr; i++)
{
printf("\nTotal Amount of the Resource 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 Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];

}
}
//marking 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 less 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("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}

OUTPUT:

Enter the no of process: 3

Enter the no of resource: 3

Total Amount of the Resource R1: 1

Total Amount of the Resource R2: 2

Total Amount of the Resource R3: 4

Enter the request matrix : 1 0 2

209

110

Enter the allocation matrix: 0 0 1

136

951

Deadlock Detected

Result:

Thus the program Deadlock Detection Algorithm was successfully completed.


EXNO: 9 Write C program to implement Threading

Aim:

To Write C program to implement Threading

Algorithm:

Step 1: Define a function func that takes a void* argument and returns a void* pointer.
Step 2: Inside the func function, detach the current thread using pthread_detach(pthread_self())
so that it can continue running independently of the parent thread.
Step 3: Print a message indicating that the function is running.
Step 4: Exit the thread using pthread_exit(NULL) so that it terminates.
Step 5: Define a function fun.
Step 6: Declare a pthread_t variable named ptid to store the ID of the new thread that will be
created.
Step 7: Inside the fun function, create a new thread using pthread_create(&ptid, NULL, &func,
NULL) with the func function as the thread function.
Step 8: Print a message indicating that the current line may be printed before the thread
terminates.
Step 9: Compare the ID of the current thread with the ID of the newly created thread using
pthread_equal(ptid, pthread_self()).
Step 10: If the IDs are equal, print a message indicating that the threads are equal.
Step 11: If the IDs are not equal, print a message indicating that the threads are not equal.
Step 12: Wait for the newly created thread to terminate using pthread_join(ptid, NULL).
Step 13: Print a message indicating that the current line will be printed after the thread ends.
Step 14: Exit the thread using pthread_exit(NULL).

Program:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* func(void* arg)
{
// detach the current thread
// from the calling thread
pthread_detach(pthread_self());
printf("Inside the thread\n");
// exit the current thread
pthread_exit(NULL);
}
void fun()
{
pthread_t ptid;
// Creating a new thread
pthread_create(&ptid, NULL, &func, NULL);
printf("This line may be printed"
" before thread terminates\n");
// The following line terminates
// the thread manually
// pthread_cancel(ptid);
// Compare the two threads created
if(pthread_equal(ptid, pthread_self())
printf("Threads are equal\n");
else
printf("Threads are not equal\n");
// Waiting for the created thread to terminate
pthread_join(ptid, NULL);
printf("This line will be printed"
" after thread ends\n");
pthread_exit(NULL);
}
int main()
{
fun();
return 0;
}

OUTPUT:

Result:
EXNO: 10 Implement the paging Technique using C program
Aim:
To write a program to implement paging using C program
Algorithm :

Step 1 : Start the program


Step 2 : Declare the required variables.
Step 3 : Get the input no. of frames, no. of pages.
Step 4 : Get the input number of pages assigned for processes.
Step 5 : Select the option page table.
Step 6 : Display the details of pages assigned for processes.
Step 7 :Select the option frame allotment.
Step 8 : Display the details of pages allocated for frames.
Step 9 : Select the option free frame list.
Step 10 :Display the details of free frames.
Step 11 : Stop the program.
Program:
#include<stdio.h>
void main()
{
int ps,fs,pp1,pp2,i,j,ch,l,k,x=0,y=0;
char pc1[10],pc2[10],temp1[20],temp2[20];
printf("\nenter the page size");
scanf("%d",&ps);
printf("\nenter the frame size");
scanf("%d",&fs);
printf("\n enter the pages for process1:");
scanf("%d",&pp1);
printf("\nenter the pages for process2:");
scanf("%d",&pp2);
printf("\n process1: \n............\n");
for(int i=0;i<pp1;i++)
{
scanf("%c,&pc1[i]);
temp1[5]=pc1[j];
x++;
}
}
printf("process2: \n**************\n);
for(i=0;i<pp2;i++)
{
printf("enter the values or character for page %d\n", i);
for(j=0;j<ps*2;j++)
{
scanf("%c",&pc2[j]);
temp2[y]=pc2[j]);
y++;
}
}
while(1)
{
printf("\n choices:\n******\n");
printf("1.page table for process1 \n2.page table for process 2\n");
printf("3.free frame \n 4.frame allotment \n 5.exit\n");
printf("enter yuour choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n page table \n************\n");
printf("page table \t frame number \n");
for(i=0;i<pp1;i++)
printf("%d\t\t%d\n",i,j);
break;
case 2:
printf("page table: \n*****\n");
printf("\n page number \t frame number \n");
k=0;
for(j=1;k<pp2;j++)
{
printf("%d\t\t %d\n",k, j);
k++;
}
break;
case 3:
printf("\n free frame:\n*****\n");
for(i=j;i<s;i++)
printf("%d\n",i);
break;
case 4:
printf("\n frame allotment:\n****\n\t");
x=0,y=0;
for(i=0;i<pp1;i++)
{
printf("\n frame %d",i);
for(j=0;j<ps*2;j++)
{
printf("\t %c",temp1[x]);
x++;
}
}
k=0;
for(j=i;k<pp2;j++)
{
printf("\n frame %d",j);
for(l=0;l<ps*2;l++)
{
printf("\t %c",temp2[y]);
y++;
}
k++;
break;
case 5:
exit(0);
break;
}
}
}
Output :
Enter the page size : 3
Enter the frame size : 5
Enter the page for process 1 : 2
Enter the page for process 2 : 1
Process 1
Enter the values or character for page 0
Q
W
E
Enter the values or character for page 1
A
S
D
Process 2
Enter the values or character for page 0
1
2
3
1. Page table for process 1
2. Page table for process 2
3. Free frame
4. Frame allotment
5. Exit
Enter your choice : 1
Page No. Frame No.
0 0
1 1
Enter your choice : 2
Page No. Frame No.
0 2
Enter your choice : 3
Free frame
3
4
Enter your choice : 4
Frame Allotment
Frame 0
Q
W
E
Frame 1
A
S
D
Frame 3
1
2
3

Result :
Thus the program Paging is implemented.
EXNO: 11 Write C programs to implement the following Memory Allocation
Methods a. First Fit

Aim:

To Write C programs to implement the following Memory Allocation Methods

a. First Fit b. Worst Fit c. Best Fit

Algorithm:

Step 1: Initialize the memory and memory status arrays to indicate that all
memory blocks are free.

Step 2: Search for the first contiguous block of free memory that is large enough to
hold the requested size.

Step 3: If a block of sufficient size is found, allocate the block by setting the status
of each memory location in the block to "allocated" and return a pointer to the
beginning of the block.

Step 4: If a block of sufficient size is not found, return NULL to indicate that the
allocation failed.

Program:

#include <stdio.h>

#define MAX_MEMORY 10000

#define FREE 0

#define ALLOCATED 1

int memory[MAX_MEMORY];

int memoryStatus[MAX_MEMORY];

void initMemory() {

for(int i = 0; i < MAX_MEMORY; i++) {

memory[i] = 0;

memoryStatus[i] = FREE;
}

void displayMemory() {

printf("Memory:\n");

for(int i = 0; i < MAX_MEMORY; i++) {

if(memoryStatus[i] == ALLOCATED) {

printf("%d ", memory[i]);

else {

printf("- ");

printf("\n");

void *firstFit(int size) {

int startIndex = -1;

int count = 0;

for(int i = 0; i < MAX_MEMORY; i++) {

if(memoryStatus[i] == FREE) {

if(count == 0) {

startIndex = i;

count++;

}
else {

if(count >= size) {

for(int j = startIndex; j < startIndex + size; j++) {

memoryStatus[j] = ALLOCATED;

return &memory[startIndex];

count = 0;

if(count >= size) {

for(int j = startIndex; j < startIndex + size; j++) {

memoryStatus[j] = ALLOCATED;

return &memory[startIndex];

return NULL;

int main() {

initMemory();

displayMemory();

int *p1 = (int*)firstFit(10);

if(p1 != NULL) {

*p1 = 10;
}

int *p2 = (int*)firstFit(20);

if(p2 != NULL) {

*p2 = 20;

displayMemory();

return 0;

OUTPUT

RESULT

EXNO: 11 Write C programs to implement the following Memory Allocation


Methods b. Worst Fit c. Best Fit

ALGORITHM:

Step 1:Define the max as 25.

Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,


bf[max],ff[max].

Step 3: Get the number of blocks,files,size of the blocks using for loop.

Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]

Step 5: Check temp>=0,if so assign ff[i]=j break the for loop.

Step 6: Assign frag[i]=temp,bf[ff[i]]=1;

Step 7: Repeat step 4 to step 6.

Step 8: Print file no,size,block no,size and fragment.

Step 9: Stop the program.


PROGRAM:

#include<stdio.h>

#include<conio.h>

#define max 25

void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp;

static int bf[max],ff[max];

clrscr();

printf("\n\tMemory Management Scheme - First Fit");

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

printf("Enter the number of files:");

scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");

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

printf("Block %d:",i);

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

printf("Enter the size of the files :-\n");

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

printf("File %d:",i);
scanf("%d",&f[i]);

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

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

if(bf[j]!=1)

temp=b[j]-f[i];

if(temp>=0)

ff[i]=j;

break;

frag[i]=temp;

bf[ff[i]]=1;

printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");

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

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);

getch();

}
OUTPUT:

Result:

EXNO: 11 Write C programs to implement the following Memory Allocation


c. Best Fit

ALGORITHM:

Step 1:Define the max as 25.

Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,


bf[max],ff[max].

Step 3: Get the number of blocks,files,size of the blocks using for loop.

Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]

Step 5: Check lowest>temp,if so assign ff[i]=j,highest=temp

Step 6: Assign frag[i]=lowest, bf[ff[i]]=1,lowest=10000

Step 7: Repeat step 4 to step 6.

Step 8: Print file no,size,block no,size and fragment.

Step 9: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#define max 25

void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;

static int bf[max],ff[max];


clrscr();

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

printf("Enter the number of files:");

scanf("%d",&nf);

printf("\nEnter the size of the blocks:-\n");

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

printf("Block %d:",i);

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

printf("Enter the size of the files :-\n");

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

printf("File %d:",i);

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

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

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

if(bf[j]!=1)

temp=b[j]-f[i];
if(temp>=0)

if(lowest>temp)

ff[i]=j;

lowest=temp;

frag[i]=lowest;

bf[ff[i]]=1;

lowest=10000;

printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");

for(i=1;i<=nf && ff[i]!=0;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);

getch();

OUTPUT:

RESULT:

EXNO:12 Write C programs to implement the various Page Replacement


Algorithms

AIM:

To write a c program to implement the various Page Replacement Algorithms.

ALGORITHM:
Step 1: Start the process

Step 2: Declare page number, page table, frame number and process size.

Step 3: Read the process size, total number of pages

Step 4: Read the relative address

Step 5: Calculate the physical address

Step 6: Display the address

Step 7: Stop the process

PROGRAM:

#include<stdio.h>

#include<string.h>

#include<pthread.h>

#include<stdlib.h>

#include<unistd.h>

pthread_t tid[2];

int counter;

pthread_mutex_t lock;

void* doSomeThing(void *arg)

pthread_mutex_lock(&lock);

unsigned long i = 0;

counter += 1;

printf("\n Job %d started\n", counter);

for(i=0; i<(0xFFFFFFFF);i++);

printf("\n Job %d finished\n", counter);


pthread_mutex_unlock(&lock);

return NULL;

int main(void)

int i = 0;

int err;

if (pthread_mutex_init(&lock, NULL) != 0)

{ printf("\n mutex init failed\n");

return 1;

while(i < 2)

err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);

if (err != 0)

printf("\ncan't create thread :[%s]", strerror(err));

i++;

pthread_join(tid[0], NULL);

pthread_join(tid[1], NULL);

pthread_mutex_destroy(&lock);

return 0;}

OUTPUT:

Result:
EXNO: 13 Write C programs to Implement the various File Organization
Techniques a. Single Level Directory

Aim:

To Write C programs to Implement the various File Organization Techniques using


Single Level Directory.

ALGORITHM:

Step-1: Start the program.

Step-2: Declare the count, file name, graphical interface.

Step-3: Read the number of files

Step-4: Read the file name

Step-5: Declare the root directory

Step-6: Using the file eclipse function define the files in a single level

Step-7: Display the files

Step-8: Stop the program

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);

printf("enter number of files");

scanf("&d",&count);

if(i<count)

cleardevice();

setbkcolor(GREEN);

printf("enter %d file 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);

i++;

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[i]);
}}

getch();

OUTPUT:

RESULT:

EXNO: 13 Write C programs to Implement the various File Organization


Techniques b. Two Level Directory

AIM:

To write C program to organize the file using two level directory.

ALGORITHM:

Step-1: Start the program.

Step-2: Declare the count, file name, graphical interface.

Step-3: Read the number of files

Step-4: Read the file name

Step-5: Declare the root directory

Step-6: Using the file eclipse function define the files in a single level

Step-7: Display the files

Step-8: Stop the program

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 the name of dir file name %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]);

}}}

OUTPUT

RESULT

EXNO: 14 Write C programs to Implement the various File Organization


Techniques a. SEQUENTIAL

AIM:

To write a C program for sequential file for processing the student information.

ALGORITHM:

Step-1: Start the program.

Step-2: Get the number of records user want to store in the system.

Step-3: Using Standard Library function open the file to write the data into the file.

Step-4: Store the entered information in the system.

Step-5: Using do..While statement and switch case to create the options such as 1-
DISPLAY, 2.SEARCH, 3.EXIT.

Step-6: Close the file using fclose() function.

Step-7: Process it and display the result.

Step-8: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>

typedef struct

{int sno;

char name[25];int m1,m2,m3;

}STD;

void display(FILE *);

int search(FILE *);

void main()

{int i,n,sno_key,opn;

FILE *fp;

clrscr();

printf(“How many records ?”);

scanf(“%d”,&n);

fp=fopen(“stud.dat”,”w”);

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

{printf(“Enter the student information : %d(sno,Name,M1,M2,M3):”,i+1);

scanf(“%d%s%d%d%d,&s.sno,s.name,&s.m1,&s.m2,&s.m3);

fwrite(&s,sizeof(s),1,fp);

fclose(fp);

fp=fopen(“stdu.dat”,”r”);

do

{printf(“1-DISPLAY\n2.SEARCH\n 3.EXIT\n YOUR OPTION: “);

scanf(“%d”,&open);
switch(opn)

case 1:

printf(“\n Student Records in the file \n”);

display(fp);

break;

case 2:

printf(“Read sno of the student to be searched :”);

scanf(“%d”,&sno_key);

if(search(fp,sno_key)){

printf(“success!! Record found in the file\n”);

printf(“%d\t%s\t%d\t%d\t%d\n”, s.sno,s.name,s.m1,s.m2,s.m3);

else

printf(“Failure!! Record %d not found\n”,sno_key);

break;

case 3:

printf(“Exit !! press key”);

getch();

break;

default:

printf(“Invalid option!!! Try again!!\n”);

break;

}
}while(opn!=3);

fclose(fp);

Void display(FILE *fp)

{rewind(fp);

while(fread(&s,sizeof(s),1,fp))

printf(“%d\t%s\t%d\t%d\t%d\n”,s.sno,s.name,s.m1,s.m2,s.m3);

int search(FILE *fp,int sno_key)

{rewind(fp);

while(fread(&s,sizeof(s),1,fp))

If(s.sno==sno_key)

return 1;

return 0;

OUTPUT:

Result:

EXNO: 14 Write C programs to Implement the various File Organization


Techniques b. Indexed

AIM:

To write a C program for random access file for processing the employee details.

ALGORITHM:

Step-1: Start the program.

Step-2: Get the number of records user want to store in the system.

Step-3: Using Standard Library function open the file to write the data into the file.
Step-4: Store the entered information in the system.

Step-5: Using do..While statement and switch case to create the options such as

1-DISPLAY, 2.SEARCH, 3.EXIT.

Step-6: Close the file using fclose() function.

Step-7: Process it and display the result.

Step-8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

int f[50],i,k,j,inde[50],n,c,count=0,p;

main()

clrscr();

for(i=0;i<50;i++)

f[i]=0;

x:

printf("enter index block\t");

scanf("%d",&p);

if(f[p]==0)

f[p]=1;

printf("enter no of files on index\t");

scanf("%d",&n);

}
else

printf("Block already allocated\n");

goto x;

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

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

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

if(f[inde[i]]==1)

printf("Block already allocated");

goto x;

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

f[inde[j]]=1;

printf("\n allocated");

printf("\n file indexed");

for(k=0;k<n;k++)

printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);

printf(" Enter 1 to enter more files and 0 to exit\t");

scanf("%d",&c);

if(c==1)

goto x;

else
exit();

getch();

EXNO: 14 Write C programs to Implement the various File Organization


Techniques c. Linked

AIM:

To write a C program for random access file for processing the employee details.

ALGORITHM:

Step-1: Start the program.

Step-2: Get the number of records user want to store in the system.

Step-3: Using Standard Library function open the file to write the data into the file.

Step-4: Store the entered information in the system.

Step-5: Using do..While statement and switch case to create the options such as

1-DISPLAY, 2.SEARCH, 3.EXIT.

Step-6: Close the file using fclose() function.

Step-7: Process it and display the result.

Step-8: Stop the program.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct record

char empname[20];

int age;
float salary;

};

typedef struct record person

FILE *people;

void main()

person employee;

int I,n;

FILE *fp;

printf(“How many records:”);

scanf(“%d”,&n);

fp=fopen(“PEOPLE.txt”,”w”);

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

printf(“Enter the employee information :%d(EmpName, Age,Salary):”,i+1);

scanf(“%s%d%f”,employee.empname,&employee.age,& employee.salary);

fwrite(,&employee.sizeof(employee,1,people);

fclose(fp);

int rec,result;

people=fopen(“PEOPLE.txt”,”r”);

printf(“Which record do you want to read from file?);

scanf(“%d”,&rec);

while(rec>=0)
{

fseek(people,rec*sizeof(employee),SEEK_SET);

result=fread(&em[ployee,sizeof(employee),1,people)

if(result==1)

printf(“\n RECORD %d\n”,rec);

printf(“Given name:%s\n”, employee.empname);

printf(“Age:%d years\n”,employee.age);

printf(“Current salary:$ %8.2f\n\n”,employee.salary);

else

printf( “\n RECORD %d not found !\n\n”,rec);

printf(“Which record do you want(0to3)”):

scanf(“%d”<&rec):

fclose(people):

getch():

OUTPUT:

RESULT:
EXNO:15 Write C programs for the implementation of various disk
scheduling algorithms

Aim:
Write C programs for the implementation of various disk scheduling algorithms

Algorithm:

Step 1: Determine the current head position on the disk.


Step 2: Select a disk scheduling algorithm to use: FCFS, SSTF, SCAN, C-SCAN, LOOK, or C-
LOOK.
Step 3: FCFS (First-Come-First-Serve): Traverse the list of requests in the order they were
received
Step 4: SSTF (Shortest-Seek-Time-First): Sort the list of requests by their distance from the
current head position
Step 5: SCAN: Sort the list of requests by their location on the disk and process the requests in
the sorted order in one direction
Step 6: Output the order in which the requests were processed

Program
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int queue[20], n, head, i;
float avg_seek_time;
printf("Enter the number of requests: ");
scanf("%d", &n);
printf("Enter the queue of disk positions to be read: ");
for (i = 0; i < n; i++) {
scanf("%d", &queue[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
int distance = abs(head - queue[0]);
for (i = 1; i < n; i++) {
distance += abs(queue[i] - queue[i-1]);
}
avg_seek_time = (float)distance / n;
printf("Average seek time = %f\n", avg_seek_time);
return 0;
}

OUTPUT:
RESULT:

You might also like