OS Lab Manual
OS Lab Manual
d) [cselab@svclinux]$ ls –x
111 1.ccc 1.docx 1.prn 2.prn a.out
array.c CP cpgm.c duplicate.c fib.c fun.c
HelloWorld.class HelloWorld.java inc.c JAVA mac.c Makefile
OS pr.c print.c sample.c streverse.c vin
[cselab@svclinux]$
6) Command : mv (Move)
a) mv (Move a File to Directory)
Syntax : mv SourceFile DestinationDirectory
[cselab@svclinux]$ mv coll.txt programs
[cselab@svclinux]$ cd programs
[cselab@svclinux programs]$ ls
coll.txt shellprogram
[cselab@svclinux programs]$
b) mv (Move Contents of one File to Another File)
Syntax : mv SourceFile DestinationFile
[cselab@svclinux]$ cat one.txt
Hi, Welcome to CSE Lab
SVCET
[cselab@svclinux]$ mv one.txt new.txt
[cselab@svclinux]$ cat new.txt
3
11) Command : head (Prints required no. of lines in the File Counting from the
Beginning of the File)
Syntax : head –n filename
n : Number of Lines to be displayed
[cselab@svclinux]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$ head -2 one.txt
Welcome to SVCET
Puliyangudi
[cselab@svclinux]$
4
12) Command : tail (Prints required no. of lines in the File Counting from
the End of the File)
Syntax : tail –n filename
n : Number of Lines to be displayed
[cselab@svclinux]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$ tail -2 one.txt
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$
13) Command : who (Displays the Users Who Logged into the System)
Syntax: who
[cselab@svclinux]$ who
root tty7 2015-01-06 02:14 (:0)
cselab pts/20 2016-12-01 16:07 (172.16.67.30)
[cselab@svclinux]$
Command : who am i (Displays the Name of the Current User of this
System)
Syntax: who am i
[cselab@svclinux]$ who am i
cselab pts/20 2016-12-01 16:07 (172.16.67.30)
[cselab@svclinux]$
Syntax: cal
[cselab@svclinux]$ cal
[cselab@svclinux]$ cal
December 2016
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
16) Command : grep (Displays a Line from the file Containing the Given String)
Syntax : grep “String” filename
Syntax : grep –i “String” filename
i – Ignore Case of the Given String
[cselab@svclinux]$ cat one.txt
Welcome to SVCET
Puliyangudi
CSE Dept
Computer Practices Laboratory
[cselab@svclinux]$ grep "Ramco" one.txt
Welcome to SVCET
[cselab@svclinux]$
Result
Thus the study and execution of UNIX commands has been completed successfully.
6
Ex.No : 2.1 Implement System Calls fork(), Exec(), getpid(), getppid(), wait()
DATE:
Aim:
To write the program using System Calls fork(), Exec(), getpid(), getppid(), wait() .
Algorithm:
Step 1 : Declare the variable pid.
Step 2 : Get the pid value using system call fork().
Step 3 : If pid value is less than zero then print as “Fork failed”.
Step 4 : Else if pid value is equal to zero include the new process in the
system‟s file using execlp system call.
Step 5 : Else if pid is greater than zero then it is the parent
process and it waits till the child completes using the system call
wait() Step 6 : Then print “Child complete”.
Program:
#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sysexits.h>
#include<stdlib.h>
main()
{
int pid;
pid=fork();
if(pid<0)
printf("error");
else if(pid>0)
{
wait(1000);
printf("\n Parent Process:\n");
printf("\n\tParent Process id:%d\t\n",getppid());
execlp("/bin/ls","ls",NULL);
}
else
{
printf("\nChild process:%d\n",getpid());
printf("\n\tChildprocess parent id:\t %d\n",getppid());
execlp("cal","cal",NULL);
exit(0);
}
}
Output:
[cse6@localhost Pgm]$ cc prog4a.c
[cse6@localhost Pgm]$ ./a.out
Result
Thus the program using System Calls fork(), Exec(), getpid(), getppid(), wait() has
been completed successfully.
7
Output:
first.c
pk6.c f2
abc FILE1
Result
Thus the program using System Calls opendir(),readdir() has been completed
successfully.
8
ALGORITHM:
Step 1 : Include the necessary header files.
Step 2 : Define the buffer size as 1024.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
Step 7 : Close the file.
Program:
#include<stdio.h>
#include<dirent.h>
int main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
scanf("%s",&pathname);
getcwd(pathname);
n=scandir(pathname,&namelist,0,alphasort);
if(n<0)
printf("Error");
else
for(i=0;i<n;i++)
printf("%s\n",namelist[i]->d_name);
}
Output:
Enter the file name: Auto.txt
Result:
Thus the program was executed successfully
9
ALGORITHM:
Program:
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp;
char c[100],pat[10];
int l,i,j=0,count=0,len,k,flag=0;
printf("\n enter the pattern");
scanf("%s",pat);
len=strlen(pat);
fp=fopen("nn.txt","r");
while(!feof(fp))
{
fscanf(fp,"%s",c);
l=strlen(c);
count++;
for(i=0;i<count;i++)
{
if(c[i]==pat[j])
{
flag=0;
for(k=1;k<i;k++)
{
if(c[i+k]!=pat[k])
flag=1;
}
if(flag==0)
printf("\n the pattern %s is present in word %d",pat,count);
}}}}
Output:
Enter the pattern: svc
The pattern svc is present in word 5 at count.
Result:
Shell Programming
EX.NO:4.1 SUM OF “n” NATURAL NUMBERS
DATE:
AIM:
To write a shell script to find the sum of “N” natural numbers.
ALGORITHM:
Step1:Start the program.
Step2:Enter the number.
Step3:Assign “S” is equal to zero and i is even to one.
Step4:Using while loop calculate the sum, display the sum.
Step5:Stop the program.
PROGRAM:
echo " enter n "
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo " the sum is : $sum "
OUTPUT:
enter n
10
the sum is : 55
RESULT:
Thus a shell script to find the sum of “N” natural numbers was executed successfully.
11
AIM:
To write a shell script to generate Fibonacci series.
ALGORITHM:
Step1:Start the program.
Step2:Enter the number.
Step3:Initialize “b” and “d” is equal to zero.
Step4:Using while loop, the Fibonacci series is generated.
Step5:Terminate the program.
PROGRAM:
echo " enter the limit "
read n
echo " the fib series is "
b=0
c=1
d=0
i=0
if [ $n -ge 2 ]
then
echo " $b $c "
n=`expr $n - 2`
while [ $i -lt $n ]
do
a=`expr $b + $c`
b=$c
c=$a
echo " $c "
i=`expr $i + 1`
done
else
echo " $b "
fi
Output
Enter the limit
10
the fib series is
0 1
1
2
3
5
8
13
21
RESULT:
Thus a shell script to generate Fibonacci series was executed successfully.
12
RESULT:
Thus a shell script to find net and gross pay was executed successfully.
13
RESULT:
Thus the shell script program to find the given number is Armstrong or not was
executed successfully.
15
PROGRAM:
echo " enter the string "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
echo " the length of the string is $len "
while [ $len -gt 0 ]
do
temp=`echo $str | cut -c $len`
rev=`echo $rev$temp`
len=`expr $len - 1`
done
echo " the reversed sytring is $rev "
if [$rev –eq $str ]
then
echo ”$str is a palindrome”
else
echo ”$str is not a palindrome”
fi
Output
enter the string
madam
the length of the string is 5
the reversed sytring is madam
madam is a palindrome
Result:
Thus the shell script to find the given string is palindrome or not was executed
successfully.
16
AIM:
To write a C program to implement round robin scheduling algorithm.
ALGORITHM:
1. Read no. of processes and time quantum( TQ).
2. Read process name and burst time(BT) for each process.
3. Ready queue is treated as circular queue.CPU schedules all processes (according to their
of order of arrival) only up to given time quantum.
4. A timer is set to interrupt the scheduling if time quantum expires for a process.
5. If BT of process is greater than TQ then after executing upto TQ, it gets added to tail of
ready queue.
6. If BT of process is less than TQ then CPU gets released from it and schedules next process
in ready queue.
7. Set waiting time (WT) of first process as zero and turnaround time(TAT) as burst time.
8. Calculate waiting time and turnaround time of other processes as follows:
Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i (BT) + P i (WT)
9. Calculate and display average WT and TAT.
10. Display order of execution of processes ie. Process name, burst time, WT and TAT.
PROGRAM:
#include<stdio.h>
int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);
printf("Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
printf("\nEnter Time Quantum:\t");
scanf("%d", &time_quantum);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
17
Output
[cselab@svclinux ~]$ cc round.c
[cselab@svclinux ~]$ ./a.out
Enter Total Number of Processes: 3
Enter Details of Process[1]
Arrival Time: 1
Burst Time: 24
Enter Details of Process[2]
Arrival Time: 2
Burst Time: 3
Enter Details of Process[3]
Arrival Time: 3
Burst Time: 3
Enter Time Quantum: 3
Process ID Burst Time Turnaround Time Waiting Time
Process[2] 3 4 1
Process[3] 3 6 3
Process[1] 24 29 5
RESULT:
Thus the Round Robin CPU scheduling algorithms was executed successfully.
19
AIM:
To write a C program to implement SJF (Shortest Job First) scheduling algorithm.
ALGORITHM:
Step1:Read no. of processes.
Step2:Read process name and burst time for each process.
Step3:Sort the processes in ready queue according to burst time.CPU schedules process
with shortest burst time first followed by other processes.
Step4:Set waiting time(WT) of first process as zero and turnaround time(TAT) as burst
time.
Step5:Calculate waiting time and turnaround time of other processes as follows:
Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i ( BT) + P i (WT)
Step6:Calculate and display average WT and TAT.
Step7:Display order of execution of processes ie. Process name, burst time, WT and
TAT.
PROGRAM:
#include<stdio.h>
#include<string.h>
struct job
{
char jn[20];
int bt;
int wt,tat;
}j[20];
main()
{
int i,tmp,n;
char t[20];
float avgwt,avgtat;
printf("\n\t SJF SCHEDULING ALGORITHM \n\n");
printf("Enter no. of jobs\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter job name of job %d : ",i);
scanf("%s",j[i].jn);
printf("\nEnter burst time of job %d : ",i);
scanf("%d",&j[i].bt);
}
for(i=1;i<n;i++)
{
20
for(j=i+1;j<=n;j++)
{
if( j[i].bt > j[j].bt)
{
strcpy( t, j[i].jn);
strcpy(j[i].jn, j[j].jn);
strcpy(j[j].jn, t);
tmp= j[i].bt;
j[i].bt=j[j].bt;
j[j].bt=tmp;
}
}
}
j[1].wt=0;
j[1].tat=j[1].bt;
for(i=2;i<=n;i++)
{j[i].wt=j[i-1].wt+j[i-1].bt;
j[i].tat=j[i].wt+j[i].bt;
}
printf("\n\n\tORDER OF EXECUTION OF JOBS\n\n");
printf("\tJN\tBT\tWT\tTAT\n\n");
for(i=1;i<=n;i++)
{
printf("\t%s\t%d\t%d\t%d\n",j[i].jn,j[i].bt,j[i].wt,j[i].tat);
avgwt=avgwt+j[i].wt;
avgtat=avgtat+j[i].tat;
}
avgwt=avgwt/n; avgtat=avgtat/n;
printf("\n\nAVG WAITING TIME: %f",avgwt);
printf("\n\nAVG TURN AROUND TIME : %f\n\n",avgtat);
}
21
Output
[cselab@svclinux ~]$ cc sjf.c
[cselab@svclinux ~]$ ./a.out
RESULT:
Thus the Shortest Job First CPU scheduling algorithms was executed successfully.
22
AIM:
To write a C program to implement FCFS scheduling algorithm.
ALGORITHM:
Step1:Read no. of processes.
Step2:Read process name and burst time for each process.
Step3:CPU schedules processes according to their order of arrival in read queue (ie It
first executes process which is at head of ready queue)
Step4:Set waiting time(WT) of first process as zero and turnaround time(TAT) as burst
time.
Step5:Calculate waiting time and turnaround time of other processes as follows:
Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i ( BT) + P i (WT)
Step6:Calculate and display average WT and TAT.
Step7:Display order of execution of processes ie. Process name, burst time, WT and TAT.
PROGRAM:
#include<stdio.h>
int main()
printf("Enter The Number of Processes To Execute:\t");
scanf("%d", &total_process);
printf("\nEnter The Burst Time of Processes:\n\n");
for(count = 0; count < total_process; count++)
{
printf("Process [%d]:", count + 1);
scanf("%f", &burst_time[count]);
}
waiting_time[0] = 0;
for(count = 1; count < total_process; count++)
{
waiting_time[count] = 0;
for(j = 0; j < count; j++)
{
waiting_time[count] = waiting_time[count] + burst_time[j];
}
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(count = 0; count < total_process; count++)
{
turnaround_time[count] = burst_time[count] + waiting_time[count];
average_waiting_time = average_waiting_time + waiting_time[count];
average_turnaround_time = average_turnaround_time + turnaround_time[count];
printf("\nProcess [%d]\t\t%.2f\t\t%.2f\t\t%.2f", count + 1,
burst_time[count], waiting_time[count], turnaround_time[count]);
}
printf("\n");
average_waiting_time = average_waiting_time / count;
average_turnaround_time = average_turnaround_time / count;
23
Process [1]:24
Process [2]:3
Process [3]:3
RESULT:
Thus the First in First Out CPU scheduling algorithms was executed successfully.
24
AIM:
To write a C program to implement priority scheduling algorithm.
ALGORITHM:
Step1:Read no. of processes.
Step2:Read process name, burst time and priority for each process.
Step3:Sort the processes in ready queue according to priority. (i.e. Process with high
priority get placed at head of ready queue) CPU schedules process with high priority first
followed by other processes.
Step4:Set waiting time (WT) of first process as zero and turnaround time (TAT) as burst
time.
Step5:Calculate waiting time and turnaround time of other processes as follows:
Pi (WT) = P i-1(WT) +P i-1(BT)
P i (TAT) = P i (BT) + P i (WT)
Step6:Calculate and display average WT and TAT.
Step7:Display order of execution of processes ie. Process name, burst time, priority, WT
and TAT.
Step8:Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int burst_time[20], process[20], waiting_time[20], turnaround_time[20],
priority[20];
int i, j, limit, sum = 0, position, temp;
float average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes:\t");
scanf("%d", &limit);
printf("\nEnter Burst Time and Priority For %d Processes\n", limit);
for(i = 0; i < limit; i++)
{
printf("\nProcess[%d]\n", i + 1);
printf("Process Burst Time:\t");
scanf("%d", &burst_time[i]);
printf("Process Priority:\t");
scanf("%d", &priority[i]);
process[i] = i + 1;
}
for(i = 0; i < limit; i++)
{
position = i;
for(j = i + 1; j < limit; j++)
{
if(priority[j] < priority[position])
25
{
position = j;
}
}
temp = priority[i];
priority[i] = priority[position];
priority[position] = temp;
temp = burst_time[i];
burst_time[i] = burst_time[position];
burst_time[position] = temp;
temp = process[i];
process[i] = process[position];
process[position] = temp;
}
waiting_time[0] = 0;
for(i = 1; i < limit; i++)
{
waiting_time[i] = 0;
for(j = 0; j < i; j++)
{
waiting_time[i] = waiting_time[i] + burst_time[j];
}
sum = sum + waiting_time[i];
}
average_wait_time = sum / limit;
sum = 0;
printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n");
for(i = 0; i < limit; i++)
{
turnaround_time[i] = burst_time[i] + waiting_time[i];
sum = sum + turnaround_time[i];
printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i],
burst_time[i], waiting_time[i], turnaround_time[i]);
}
average_turnaround_time = sum / limit;
printf("\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}
26
Output
[cselab@svclinux ~]$ cc prior.c
[cselab@svclinux ~]$ ./a.out
Enter Total Number of Processes: 3
Enter Burst Time and Priority For 3 Processes
Process[1]
Process Burst Time: 5
Process Priority: 3
Process[2]
Process Burst Time: 8
Process Priority: 2
Process[3]
Process Burst Time: 7
Process Priority: 1
Process ID Burst Time Waiting Time Turnaround Time
Process[3] 7 0 7
Process[2] 8 7 15
Process[1] 5 15 20
RESULT:
Thus the Priority CPU scheduling algorithms was executed successfully.
27
AIM:
To write a C program to implement PCP (Producer Consumer Problem) using
semaphores.
ALGORITHM:
Step1:Read size of buffer
Step2:Producer process produces and buffers the items using shmget() and shmctl()
Step3:Consumer process consumes item from buffer using semop() and semrel()
Step4:Producer process waits if buffer is full and consumer process waits if buffer is
empty.
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdlib.h>
union semum
{
int array[3];
};
main()
{
int k,f,i,x,n,t,sid,num[10],j,in=0,out=0;
int *l;
union semum arg;
struct sembuf a;
sid=shmget(50010,3,IPC_CREAT | 00700);
if(sid == -1)
{
printf("Not created");
exit(0);
}
printf("Enter the no of number");
scanf("%d",&n);
arg.array[0]=1;
arg.array[1]=5;
arg.array[2]=0;
semctl(sid,0,SETALL,arg);
28
k=shmget(IPC_PRIVATE,5*sizeof(int),00700);
l=(int*) shmat(k,NULL,0);
f=fork();
printf(" fork value=%d",f);
if(f != 0)
{
for(i=0;i<n;i++)
{
printf("Enter the number\n");
scanf("%d\n",&num[i]);
a.sem_num=1;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,-1);
a.sem_num=0;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,-1);
l[in]=num[i];
in=(in+1)%n;
a.sem_num=0;
a.sem_op=1;
a.sem_flg=0;
semop(sid,&a,1);
a.sem_num=2;
a.sem_op=1;
a.sem_flg=0;
semop(sid,&a,1);
}
shmctl(k,IPC_RMID,NULL);
}
if(f==0)
{
for(j=0;j<n;j++)
{
a.sem_num=2;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,1);
a.sem_num=0;
a.sem_op=-1;
a.sem_flg=0;
semop(sid,&a,1);
x=l[out];
out=(out+1)%n;
a.sem_num=0;
a.sem_op=1;
29
a.sem_flg=0;
semop(sid,&a,1);
a.sem_num=0;
a.sem_op=1;
a.sem_flg=0;
semop(sid,&a,1);
printf("\n Consumer %d",x);
}
}
}
Output
[cselab@svclinux ~]$ cc semaphore.c
[cselab@svclinux ~]$ ./a.out
Enter the no of number3
Enter the number 6 3 4
Consumer consumes 4
Consumer consumes 3
Consumer consumes 6
Result
Thus the program to implement PCP (Producer Consumer Problem) using semaphores
was written and executed successfully.
30
DATE:
AIM:
To write a C program to implement IPC (Inter Process Communication) via message queue.
ALGORITHM:
Sender side
Step1:Establish a message queue using msgegt() with unique msg id.
Step2:Read 5 messages from user using fgets( ) and transmit it to receiver using
msgsnd( ).
Receiver side
Step1:Establish a connection with message queue of sender.
Step2:Receive and print messages from queue using msgrcv( ).
PROGRAM:
Sender Side
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<errno.h>
#include<string.h>
#define NMSGS 5
extern int errno;
struct msgbuf
{
long mtype;
char mtext[100];
};
int main()
{
int msgid;
int i,nloop;
struct msgbuf msgp;
char tmp_msg[100];
tmp_msg[0]='\0';
31
msgid=msgget(9999,IPC_CREAT|0666);
if(msgid<0)
{
printf("%d:Error number is %d\n",__LINE__,errno);
exit(1);
}
printf("Enter messsage(max 5lines)\n");
for(nloop=1;nloop<=NMSGS;nloop++)
{
msgp.mtype=1;
fgets(tmp_msg,100,stdin);
strncpy(msgp.mtext,tmp_msg,strlen(tmp_msg));
i=msgsnd(msgid,&msgp,strlen(tmp_msg),IPC_NOWAIT);
if(i<0)
{
printf("%d:Error number is%d\n",__LINE__,errno);
exit(1);
}
tmp_msg[0]='\0';
}
return 0;
}
Receiver Side
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<errno.h>
#include<string.h>
extern int errno;
struct msgbuf
{
long mtype;
32
char mtext[100];
};
int main()
{
int msgid;
int i,nloop;
struct msgbuf msgp;
msgid=msgget(9999,0444);
if(msgid<0)
{
printf("%d:Error number is %d\n",__LINE__,errno);
exit(1);
}
for(nloop=0;nloop<5;nloop++)
{
bzero(msgp.mtext,100);
i=msgrcv(msgid,&msgp,100,1,IPC_NOWAIT);
if(i<0)
{
printf("%d:Error number is %d\n",__LINE__,errno);
exit(1);
}
msgp.mtext[strlen(msgp.mtext)]='\0';
fwrite(msgp.mtext,sizeof(char),strlen(msgp.mtext),stdout);
printf("Message is %s\n",msgp.mtext);
}
if(msgctl(msgid,IPC_RMID,NULL)<0)
{
printf("%d:Error number is %d\n",__LINE__,errno);
exit(1);
}
return 0;
}
33
Output:
"ex31.c" 44L, 734C written
[cselab@svclinux ~]$ cc ex31.c
[cselab@svclinux ~]$ ./a.out
Enter messsage(max 5lines)
welcome
to SVC
Welcome to OS Lab
Welcome to Second year
Good
[cselab@svclinux ~]$ cc ex32.c
[cselab@svclinux ~]$ ./a.out
welcome
Message is welcome
To SVC
Message is to SVC
Welcome to OS Lab
Message is Welcome to OS Lab
Welcome to Second year
Message is Welcome to Second year
Good
Message is Good
[cselab@svclinux ~]$
RESULT:
Thus the Inter Process Communication program was executed successfully.
34
ExNo 7.1) To perform matrix addition in child process using shared memory
AIM:
To write a C program to perform matrix addition in child process using shared memory
Algorithm
Step1:Enter the order of matrix
Step2:Using shmget create shared memory for parent and child process and attach shared
memory to processes.
Step3:Enter the element of matricse stored in to shared memory variable.
Step4:Calculate addition of matrices using shared memory.
Step5:Stop the program.
Program
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
int main()
{
int p,k,i,n,l,m,*c;
int *a,*b;
printf("enter the order of matrix");
scanf("%d",&n);
k=shmget(IPC_PRIVATE,n*sizeof(int),00700);
a=(int*) shmat(k,0,0);
l= shmget(IPC_PRIVATE,n*sizeof(int),00700);
b=(int*) shmat(k,0,0);
m= shmget(IPC_PRIVATE,n*sizeof(int),00700);
c=(int*) shmat(k,0,0);
printf("enter the elements of a");
for(i=0;i<n*n;i++)
{
scanf("%d",(a+i));
}
35
Output
[cselab@svclinux ~]$ cc matadd.c
[cselab@svclinux ~]$ ./a.out
enter the order of matrix3
enter the elements of a1 2 3
456
789
enter the elements of b4 5 6
213
345
Result:
Thus the program to perform matrix addition in child process using shared memory
was written and executed successfully.
37
printf("%d",*(a+i));
}
}
if(b==0)
{
for(i=n/2;i<n;i++)
{
if(*(a+i)%2!=0)
printf("%d",*(a+i));
}
}
shmdt(a);
}
Output
[cselab@svclinux os]$ cc odd.c
[cselab@svclinux os]$ ./a.out
enter the no of elements3
enter the elements1
2
3
odd number13[cselab@svclinux os]$
Result
Thus the Program to To generate ODD number using Semaphore was written and
executed successfully.
39
PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Banker's Algorithm ************\n");
input();
show();
cal();
// getch();
return 0;
}
void input()
40
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
41
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
while(flag)
42
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
43
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
44
Output
********** Banker's Algorithm ************
Enter the no of Processes 5
Enter the no of resources instances 3
Enter the Max Matrix
753
322
902
222
433
Enter the Allocation Matrix
010
200
302
211
002
Enter the available Resources
332
Process Allocation Max Available
P1 010 753 332
P2 200 322
P3 302 902
P4 211 222
P5 002 433
P1->P3->P4->P2->P0->
The system is in safe state
RESULT:
Thus the program for implementing deadlock avoidance algorithm was implemented
has been executed successfully.
45
return 0;
}
void input()
{
int i,j;
46
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
{
flag=0;
for(i=0;i<n;i++)
48
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
if(finish[i]==1)
{
i=n;
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock
49
process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}}
else
{
printf("\nNo Deadlock Occur");
}}
Output
}
void *mythread(void *vargp)
{
printf("Thread %d is running",vargp);
pthread_exit(NULL);
}
int main()
51
{
int i,x;
pthread_t a[6];
printf("Before Thread\n");
for(i=0;i<5;i++)
{
x=pthread_create(&a[i],NULL,myThreadFun,(void *)i);
if(x!=0)
printf("Thread not created");
}
//for(i=0;i<5;i++)
//{
//x=pthread_create(&a[i],NULL,mythread,(void *)i);
//if(x!=0)
// printf("Thread is not created\n");
//}
pthread_exit(NULL);
}
52
Output
RESULT:
Thus the program for implementing thread and synchronization application was
executed successfully
53
scanf("%d",&page[i]);
}
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);
if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
printf("\nPhysical Address is %d",(page[pno]*ps)+off);
return 1;
}
Output
Enter the no of pages in memory4
Enter page size2
Enter no of frames4
Enter the page table
(Enter frame no as -1 if that page is not present in any frame)
pageno frameno
------- -------
0 3
1 5
2 1
3 7
RESULT:
Thus C program for implementing paging concept for memory management has been
executed successfully.
55
AIM:
To write a C program to implement FIFO page replacement algorithm.
ALGORITHM:
Step1:Read the size of the frame, no. of elements and elements one by one.
Step2:Initialize the frames with value -1.
Step3:Insert each element into frame, if it’s already not present.
Step4:If the frame is full and the new element is not already present then replace
the oldest element by the new element.
Step5:Increment no. of page faults by one while inserting each element into the
frames.
Step6:Display the contents of frames during processing and the total no. of page
faults.
PROGRAM:
#include<stdio.h>
int main()
{
int reference_string[10], page_faults = 0, m, n, s, pages, frames;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter values of Reference String:\n");
for(m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
{
scanf("%d", &frames);
}
int temp[frames];
for(m = 0; m < frames; m++)
56
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(reference_string[m] == temp[n])
{
s++;
page_faults--;
}
}
page_faults++;
if((page_faults <= frames) && (s == 0))
{
temp[m] = reference_string[m];
}
else if(s == 0)
{
temp[(page_faults - 1) % frames] = reference_string[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", page_faults);
return 0;
}
57
Output
Result
Thus the program to implement FIFO page replacement algorithm was written and
executed successfully.
58
AIM:
To write a C program to implement LRU page replacement algorithm.
ALGORITHM:
Step1:Read the size of the frame, no. of elements and elements one by one.
Step2:Initialize the frames with value -1.
Step3:Insert each element into frame, if it’s already not present.
Step4:If the frame is full and new element is not already present then replace the least
recently used element by the new element.
Step5:Increment no. of page faults by one while inserting each element into the frames.
Step6:Display the contents of frames during processing and the total no. of page faults.
PROGRAM:
#include<stdio.h>
int main()
{
int frames[10], temp[10], pages[10];
int total_pages, m, n, position, k, l, total_frames;
int a = 0, b = 0, page_fault = 0;
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frames[m] = -1;
}
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Values for Reference String:\n");
for(m = 0; m < total_pages; m++)
{
printf("Value No.[%d]:\t", m + 1);
59
scanf("%d", &pages[m]);
}
for(n = 0; n < total_pages; n++)
{
a = 0, b = 0;
for(m = 0; m < total_frames; m++)
{
if(frames[m] == pages[n])
{
a = 1;
b = 1;
break;
}
}
if(a == 0)
{
for(m = 0; m < total_frames; m++)
{
if(frames[m] == -1)
{
frames[m] = pages[n];
b = 1;
break;
}
}
}
if(b == 0)
{
for(m = 0; m < total_frames; m++)
{
temp[m] = 0;
}
for(k = n - 1, l = 1; l <= total_frames - 1; l++, k--)
{
for(m = 0; m < total_frames; m++)
60
{
if(frames[m] == pages[k])
{
temp[m] = 1;
}
}
}
for(m = 0; m < total_frames; m++)
{
if(temp[m] == 0)
position = m;
}
frames[position] = pages[n];
page_fault++;
}
printf("\n");
for(m = 0; m < total_frames; m++)
{
printf("%d\t", frames[m]);
}
}
printf("\nTotal Number of Page Faults:\t%d\n", page_fault);
return 0;
}
61
Output
5 -1 -1
5 6 -1
5 6 3
2 6 3
2 5 3
2 5 1
8 5 1
Total Number of Page Faults: 4
Result
Thus the program to implement LRU page replacement algorithm was written and
executed successfully.
62
ALGORITHM:
Step1:Read the size of the frame, no. of elements and elements one by one.
Step2:Initialize the frames with value -1.
Step3:Insert each element into frame, if it’s already not present.
Step4:If the frame is full and new element is not already present then replace the least
frequently used element by the new element.
Step5:Increment no. of page faults by one while inserting each element into the frames.
Step6:Display the contents of frames during processing and the total no. of page faults.
PROGRAM:
#include<stdio.h>
int main()
{
int reference_string[25], frames[25], interval[25];
int pages, total_frames, page_faults = 0;
int m, n, temp, flag, found;
int position, maximum_interval, previous_frame = -1;
printf("\nEnter Total Number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter Values of Reference String\n");
for(m = 0; m < pages; m++)
{
printf("Value No.[%d]:\t", m + 1);
scanf("%d", &reference_string[m]);
}
printf("\nEnter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
63
frames[m] = -1;
}
for(m = 0; m < pages; m++)
{
flag = 0;
for(n = 0; n < total_frames; n++)
{
if(frames[n] == reference_string[m])
{
flag = 1;
printf("\t");
break;
}
}
if(flag == 0)
{
if (previous_frame == total_frames - 1)
{
for(n = 0; n < total_frames; n++)
{
for(temp = m + 1; temp < pages; temp++)
{
interval[n] = 0;
if (frames[n] == reference_string[temp])
{
interval[n] = temp - m;
break;
}
}
}
found = 0;
for(n = 0; n < total_frames; n++)
{
if(interval[n] == 0)
{
64
position = n;
found = 1;
break;
}
}
}
else
{
position = ++previous_frame;
found = 1;
}
if(found == 0)
{
maximum_interval = interval[0];
position = 0;
for(n = 1; n < total_frames; n++)
{
if(maximum_interval < interval[n])
{
maximum_interval = interval[n];
position = n;
}
}
}
frames[position] = reference_string[m];
printf("FAULT\t");
page_faults++;
}
for(n = 0; n < total_frames; n++)
{
if(frames[n] != -1)
{
printf("%d\t", frames[n]);
}
}
65
printf("\n");
}
printf("\nTotal Number of Page Faults:\t%d\n", page_faults);
return 0;
}
Output
Result
Thus the program to implement optimal page replacement algorithm was written and
executed successfully.
66
AIM:
To write a C Program to implement Sequential File Allocation method.
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the number of files.using for loop get the file name
Step 3: Get the starting block of each filefrom that allocate blocks sequentially.
Step 4: Print the File name and blocks occupied by file will be displayed.
Step 5: Stop the program.
PROGRAM:
#include<stdio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
67
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
Output
Enter no.of files:2
Enter no. of blocks occupied by file1 5
Enter the starting block of file1 1
Enter no. of blocks occupied by file2 5
Enter the starting block of file2 6
Filename Start block length
1 1 5
2 6 5
Enter file name:1
File name is:1 length is:5 blocks occupied: 1 2 3 4 5
Result
Thus the following file allocation technique of Sequential File Allocation was written
and executed successfully.
68
ALGORITHM:
Step 1: Start.
Step 2: Enter the filename and blocks occupied by a file index block using loop.
Step 3: Allocate file to index block.
Step 4: print the file name and index block.
Step 5: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
}
printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
69
}
printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupiedare:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}
Output
[cselab@svclinux ~]$ ./a.out
Enter no. of files:2
Enter starting block and size of file1:2 5
Enter blocks occupied by file1:10
enter blocks of file1:3
254672647
Enter starting block and size of file2:3 4
Enter blocks occupied by file2:5
enter blocks of file2:2 3 4 5 6
Result
Thus the following file allocation technique of Indexed File Allocation was written
and executed successfully.
70
AIM:
To write a C Program to implement Linked File Allocation method.
ALGORITHM:
Step 1: Start the Program
Step 2:Obtain the required data through char and int datatypes.
Step 3:Enter the filename,starting block ending block.
Step 4: Print the free block using loop.
Step 5:‟for‟ loop is created to print the file utilization of linked type of entered type .
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
71
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
}
72
Output
Result
Thus the following file allocation technique of Linked File Allocation was written and
executed successfully.