Operating System Lab Operating System Lab
Operating System Lab Operating System Lab
CONTENTS
OPERATING SYSTEM LAB
EXP. PAGE
DATE NAME OF THE PROGRAM SIGNATURE
NO. NO.
8. PIPE PROCESSING
PRODUCER-CONSUMER PROBLEM
10.
USING SEMAPHORES
ALGORITHM:
2. Declare the pid and get the pid by using the getpid() method.
4. Check if(pid==0) then print the child process id and then print the parent
process value.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
void waitexample()
{
int i, stat;
pid_t pid[5];
for (i=0; i<5; i++)
{
if ((pid[i] = fork()) == 0)
{
sleep(1);
exit(100 + i);
}
}
int main()
{
waitexample();
return 0;
}
OUTPUT:
RESULT:
Thus the process system call program was executed and verified
successfully.
IO SYSTEM CALLS
AIM:
To write a ‘c’ program for I/O system calls.
ALGORITHM:
1. Start the program.
2. Open a file for O_RDWR for R/W,O_CREATE for creating a file ,
O_TRUNC for truncate a file
3. Using get char (), read the character and stored in the string [] array
4. The string [] array is write into a file close it.
5. Then the first is opened for read only mode and read the characters and
displayed It and close the file
6. Stop the program
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
int main()
{
int fd=open("foo.txt",O_RDONLY|O_CREAT);
printf("fd=%d\n",fd);
if(fd==-1)
{
printf("Error number %d\n",errno);
perror("program");
}
if(fd<0)
{
perror("c1");
exit(1);
}
printf("opened the fd=%d\n",fd);
if(close(fd)<0)
{
perror("c1");
exit(1);
}
printf("closed the fd.\n");
return 0;
}
OUTPUT:
RESULT:
Thus the I/O system call program was executed and verified successfully.
AIM:
To write the program to Implementing IPC using message queues.
ALGORITHM:
1. Start the program.
2. ftok(): is use to generate a unique key
3. msgget(): either returns the message queue identifier for a newly
created message
4. queue or returns the identifiers for a queue which exists with the same
key value.
PROGRAM :
MESSAGE QUEUE FOR WRITER PROCESS
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
typedef struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
int main()
{
int msgid;
int msgflg=IPC_CREAT|0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
key=1234;
if((msgid=msgget(key,msgflg))<0)
die("msgget");
sbuf.mtype=1;
printf("enter a message to add message queue:");
scanf("%[^\n]",sbuf.mtext);
getchar();
buflen=strlen(sbuf.mtext)+1;
if(msgsnd(msgid,&sbuf,buflen,IPC_NOWAIT)<0)
{
printf("%d,%d,%s,%d\n",msgid,sbuf.mtype,sbuf.mtext,buflen);
die("msgsnd");
}
else
printf("message sent\n");
exit(1);
}
OUTPUT:
RESULT:
Thus the message queues program was executed and verified successfully.
AIM:
To write the program to implement CPU & scheduling algorithm for first
come first servescheduling.
ALGORITHM:
1. Start the program.
2. Get the number of processes and their burst time.
3. Initialize the waiting time for process 1 and 0.
4. Process for(i=2;i<=n;i++),wt.p[i]=p[i-1]+bt.p[i-1].
5. The waiting time of all the processes is summed then average value time
is calculated.
6. The waiting time of each process and average times are displayed
7. Stop the program
PROGRAM :
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt,tt;
}
p[10];
int main()
{
int i,n,totwt,tott,avg1,avg2;
printf("enter the no of process \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time");
scanf("%d",&p[i].bt);
}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt; p[i].tt=p[i].bt+p[i].wt;
i ++;
}
i=1;
totwt=tott=0;
printf("\n processid \t bt\t wt\t tt\n"); while(i<=n)
{
printf("\n\t%d \t%d \t%d \t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tott=p[i].tt+tott;
i++;}
avg1=totwt/n; avg2=tott/n; printf("\navg1=%d \t avg2=%d\t",avg1,avg2);
return 0;
OUTPUT:
RESULT:
Thus the FIFO process scheduling program was executed and verified
successfully.
AIM:
To write a program to implement cpu scheduling algorithm for shortest job
firstscheduling.
ALGORITHM:
1. Start the program. Get the number of processes and their burst time.
for(i=2;i<=n;i++).wt.p[i]=p[i=1]+bt.p[i-1].
5. The waiting time of all the processes summed and then the average time is
calculate
6. The waiting time of each processes and average time are displayed.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
}p[10],temp;
int main()
{
int i,j,n,totwt,tottt;
float avg1,avg2;
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\nEnter the burst time:\t");
scanf("%d",&p[i].bt);
}
for(i=1;i<n;i++){
for(j=i+1;j<=n;j++)
{
if(p[i].bt>p[j].bt)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;p[i].bt=p[j].bt;
p[j].bt=temp.bt;
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n){
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\nProcess id \tbt \twt \ttt");
while(i<=n){
printf("\n\t%d \t%d \t%d t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
} avg1=totwt/n;
avg2=tottt/n;
printf("\nAVG1=%f\t AVG2=%f",avg1,avg2);
getch();
return 0;
}
OUTPUT:
RESULT:
PRIORITY SCHEDULING
AIM:
ALGORITHM:
2. Read burst time, waiting time, turn the around time and priority.
5. The waiting time of all the processes is summed and then the average
waiting time
6. The waiting time of each process and average waiting time are displayed
based on the priority.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
int prior;
}
p[10],temp;
int main()
{
int i,j,n,totwt,tottt,arg1,arg2;
printf("enter the number of process");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time");
scanf("%d",&p[i].bt);
printf("\n enter the priority");
scanf("%d",&p[i].prior);
}
for(i=1;i<n;i++)
{
R.Cheran [M.sc computer science] Page 22
for(j=i+1;j<=n;j++)
{
if(p[i].prior>p[j].prior)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
}
}
}
p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
R.Cheran [M.sc computer science] Page 23
OUTPUT:
RESULT:
Thus the priority scheduling program was executed and verified
successfully.
AIM:
To write a program to implement cpu scheduling for Round Robin
Scheduling.
ALGORITHM:
3. The burst time of each process is divided and the quotients are stored on
the round Robin array.
4. According to the array value the waiting time for each process and the
average time are calculated as line the other scheduling.
5. The waiting time for each process and average times are displayed.
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid,bt,tt,wt;
};
int main()
{
struct process x[10],p[30];
int i,j,k,tot=0,m,n;
float wttime=0.0,tottime=0.0,a1,a2;
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++){
x[i].pid=i;
printf("\nEnter the Burst Time:\t");
scanf("%d",&x[i].bt);
tot=tot+x[i].bt;
}
printf("\nTotal Burst Time:\t%d",tot);
p[0].tt=0;
k=1;
printf("\nEnter the Time Slice:\t");
scanf("%d",&m);
for(j=1;j<=tot;j++)
{
for(i=1;i<=n;i++)
{
if(x[i].bt !=0)
{
p[k].pid=i;
if(x[i].bt-m<0)
{
p[k].wt=p[k-1].tt;
p[k].bt=x[i].bt;
p[k].tt=p[k].wt+x[i].bt;
x[i].bt=0;
k++;
R.Cheran [M.sc computer science] Page 27
}
else
{
p[k].wt=p[k-1].tt;
p[k].tt=p[k].wt+m;
x[i].bt=x[i].bt-m;
k++;
}
}
}
}
printf("\nProcess id \twt \ttt");
for(i=1;i<k;i++){
printf("\n\t%d \t%d \t%d",p[i].pid,p[i].wt,p[i].tt);
wttime=wttime+p[i].wt;
tottime=tottime+p[i].tt;
a1=wttime/n;
a2=tottime/n;
}
printf("\n\nAverage Waiting Time:\t%f",a1);
printf("\n\nAverage TurnAround Time:\t%f",a2);
getch();
return 0;
}
OUTPUT:
RESULT:
Thus the Round Robin scheduling program was executed and verified
successfully.
PIPE PROCESSING
AIM :
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"hi","hello"};
char readmessage[20];
returnstatus=pipe(pipefds);
if(returnstatus==-1)
{
printf("unable to create pipe\n");
return 1;
}
pid=fork();
if(pid==0)
{
read(pipefds[0],readmessage,sizeof(readmessage));
printf("child proccess-reading form pipe-message 1 is
%s\n",readmessage);
read(pipefds[0],readmessage,sizeof(readmessage));
printf("child process-reading from pipe-message 2 is
%s\n",readmessage);
R.Cheran [M.sc computer science] Page 31
}
else
{
printf("parent process-writing to pipe-message 1 is
%s\n",writemessages[0]);
write(pipefds[1],writemessages[0],sizeof(writemessages[0]));
printf("parent process-writing to pipe-message 2 is
%s\n",writemessages[1]);
write(pipefds[1],writemessages[1],sizeof(writemessages[1]));
}
return 0;
}
OUTPUT:
RESULT:
Thus the Piping process using IPC program was executed and verified
successfully.
AIM:
To implement first fit, best fit algorithm for memory management.
ALGORITHM:
1. Start the program.
2. Get the segment size, number of process to be allocated and their
corresponding size.
3. Get the options. If the option is ‘2’ call first fit function.
4. If the option is ‘1’ call best fit function. Otherwise exit.
5. For first fit, allocate the process to first possible segment which is free and
set the
personnel slap as ‘1’. So that none of process to be allocated to segment
which is already
allocated and vice versa.
6. For best fit, do the following steps,.
7. Sorts the segments according to their sizes.
8. Allocate the process to the segment which is equal to or slightly greater
than the process
size and set the flag as the ‘1’ .So that none of the process to be allocated to
the segment
which is already allocated and vice versa. Stop the program.
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,highest=0;
static int bf[max],ff[max];
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) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
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:
Thus the First Bit and Best Fit program was executed and verified
successfully.
AIM:
To implement producer/consumer problem using semaphore.
ALGORITHM:
1. Declare variable for producer & consumer as pthread-t-tid produce tid
consume.
2. Declare a structure to add items, semaphore variable set as struct.
3. Read number the items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer.
8. Stop the execution.
PROGRAM:
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf("\n1. Produce \t 2. Consume \t3. Exit");
printf("\nEnter your choice: =");
scanf("%d", &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT:
RESULT:
PROGRAM:
read fact
ans=1
counter=0
while [ $fact -ne $counter ]
do
counter=`expr $counter + 1`
ans=`expr $ans \* $counter`
done
OUTPUT:
RESULT:
Thus the Shell Program to find factorial of a given number program was
executed and verified successfully.
FIBONACCI NUMBER
PROGRAM:
read n
x=0
y=1
i=2
echo "fibonancci series up to $n terms:"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1`
z=`expr $x + $y`
echo "$z"
x=$y
y=$z
done
OUTPUT:
RESULT:
Thus the shell program to generate Fibonacci number program was executed
and verified successfully.