0% found this document useful (0 votes)
125 views22 pages

Os Lab Manual

The document contains 7 examples of C programs implementing various process scheduling algorithms: 1) FCFS scheduling 2) SJF scheduling 3) Priority scheduling 4) Round robin scheduling 5) Implementing I/O and process system calls 6) Implementing IPC using message queues 7) Additional examples of scheduling algorithms and system calls The document provides the code, input, output and results for each example program to demonstrate how to code and test different scheduling algorithms and system calls in C.

Uploaded by

UMA SIKAMANI
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)
125 views22 pages

Os Lab Manual

The document contains 7 examples of C programs implementing various process scheduling algorithms: 1) FCFS scheduling 2) SJF scheduling 3) Priority scheduling 4) Round robin scheduling 5) Implementing I/O and process system calls 6) Implementing IPC using message queues 7) Additional examples of scheduling algorithms and system calls The document provides the code, input, output and results for each example program to demonstrate how to code and test different scheduling algorithms and system calls in C.

Uploaded by

UMA SIKAMANI
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
You are on page 1/ 22

Ex.No.

01
Date :
PROCESS SYSTEM CALL

AIM
To write a C program to implement process system call.

PROGRAM

#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t ret_val;
printf("\n The process id is%d\n",getpid());
ret_val =fork();
if(ret_val<0)
{
printf("fork failure");
}
else if(ret_val==0)
{
printf("\nChild Process\n");
printf("\nThe parent id is%d and process id is%d",getppid(),getpid());

}
else
{
wait();
printf("\nparent process");
printf("\nThe process id is%d",getpid());
}
return 0;
}

OUTPUT

The process id is44

Child Process

The parent id is 44 and process id is 45

parent process

The process id is44

RESULT
The process system calls are implemented successfully.

1
Ex.No. 02
Date :

I/O SYSTEM CALL


AIM
To write a C program to implement I/O system call.

PROGRAM

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd;
char buffer[80];
char msg[30]="Welcome To TVU College";
fd=open("check.txt",O_RDWR);
printf("fd=%d",fd);
if(fd!=-1)
{
printf("\n check.txt opened with read write access\n");
write(fd,msg,sizeof(msg));
lseek(fd,0,SEEK_SET);
read(fd,buffer,sizeof(msg));
printf("\n %s was written to my file\n",buffer);
}
}

OUTPUT

fd=3
check.txt opened with read write access

Welcome To TVU College was written to my file

RESULT
The I/O system calls are implemented successfully.

2
Ex.No. 03
Date :

IMPLEMENTING IPC USING MESSAGE QUEUES


AIM
To write a C program to implement IPC using Message Queues

PROGRAM

SEND

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
typedef struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbufsbuf;
size_tbuflen;
key = 1234;
if ((msqid = msgget(key, msgflg )) < 0) //Get the message queue ID for the given key
die("msgget");
//Message Type
sbuf.mtype = 1;
printf("Enter a message to add to message queue : ");
scanf("%[^\n]",sbuf.mtext);
getchar();
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");

3
}
else
printf("Message Sent\n");
exit(0);
}
RECEIVE

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
typedef struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
key_t key;
struct msgbufrcvbuffer;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
die("msgget()");
//Receive an answer of message type 1.
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
die("msgrcv");
printf("%s\n", rcvbuffer.mtext);
exit(0);
}

OUTPUT

elcot@boss:~$ ./sendop
Enter a message to add to message queue : computer science
Message Sent

elcot@boss:~$ ./recop
computer science

RESULT :
IPC using Message Queues are implemented successfully.

4
Ex.No. 04
Date :

FIRST COME FIRST SERVE SCHEDULING


AIM
To write a C program to implement the FCFS Scheduling.
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

5
INPUT

Enter the number of processes -- 3


Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3

OUTPUT

PROCESS BURST TIME WAITING TIME TURNAROUND TIME

P0 24 0 24
P1 3 24 27
P2 3 27 30

Average Waiting Time -- 17.000000


Average Turnaround Time -- 27.000000

RESULT
FCFS Scheduling was implemented successfully.

6
Ex.No. 05
Date :

SHORTEST JOB FIRST SCHEDULING


AIM
To write a C program to implement the SJF Scheduling.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}

7
INPUT

Enter the number of processes -- 4


Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3

OUTPUT

PROCESS BURST TIME WAITING TIME TURNAROUND TIME


P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24

Average Waiting Time -- 7.000000


Average Turnaround Time -- 13.000000

RESULT
SJF Scheduling was implemented successfully

8
Ex.No. 06
Date :

PRIORITY SCHEDULING
AIM
To write a C program to implement the Priority Scheduling.
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] >pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURSTTIME\tWAITINGTIME\tTURNAROUND TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);

9
printf("\nAverage Turnaround Time is --- %f",tatavg/n);
getch();
}

INPUT

Enter the number of processes --- 5


Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2

OUTPUT
PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
1 1 1 0 1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19

Average Waiting Time is --- 8.200000


Average Turnaround Time is --- 12.000000

RESULT
Priority Scheduling was implemented successfully

10
Ex.No. 07
Date :

ROUND ROBIN SCHEDULING


AIM
To write a C program to implement the Round Robin Scheduling.
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);
max=bu[0];
for(i=1;i<n;i++)
if(max<bu[i])
max=bu[i];
for(j=0;j<(max/t)+1;j++)
for(i=0;i<n;i++)
if(bu[i]!=0)
if(bu[i]<=t)
{
tat[i]=temp+bu[i];
temp=temp+bu[i];
bu[i]=0;
}
else
{
bu[i]=bu[i]-t;
temp=temp+t;
}
for(i=0;i<n;i++)
{
wa[i]=tat[i]-ct[i];
att+=tat[i];
awt+=wa[i];
}

11
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\t%d \t %d \t\t %d \t\t %d \n",i+1,ct[i],wa[i],tat[i]);
printf("\nThe Average Turnaround time is -- %f",att/n);
printf("\nThe Average Waiting time is -- %f ",awt/n);
getch();
}

INPUT:-

Enter the no of processes -- 3


Enter Burst Time for process 1 -- 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 -- 3

Enter the size of time slice -- 3

OUTPUT

PROCESS BURST TIME WAITING TIME TURNAROUND TIME


1 24 6 30
2 3 3 6
3 3 6 9

The Average Turnaround time is -- 15.000000


The Average Waiting time is -- 5.000000

RESULT
Round-Robin Scheduling was implemented successfully

12
Ex.No. 08
Date :

PIPE PROCESSING
AIM
To write a C program to implement the IPC using Pipes.
PROGRAM

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MSG_LEN 64
int main(){
int result;
int fd[2];
char message[MSG_LEN];
char recvd_msg[MSG_LEN];
result = pipe (fd);
//Creating a pipe//fd[0] is for reading and fd[1] is for writing
if (result < 0)
{
perror("pipe ");
exit(1);
}
strncpy(message,"Linux World!! ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
strncpy(message,"Understanding ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
strncpy(message,"Concepts of ",MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)
{
perror("write");
exit(2);
}
strncpy(message,"Piping ", MSG_LEN);
result=write(fd[1],message,strlen(message));
if (result < 0)

13
{
perror("write");
exit(2);
}
result=read (fd[0],recvd_msg,MSG_LEN);
if (result < 0)
{
perror("read");
exit(3);
}
printf("%s\n",recvd_msg);
return 0;
}

OUTPUT

Linux World!! Understanding Concepts of Piping

RESULT :
IPC using Pipes are implemented successfully.

14
Ex.No. 09.A
Date :

FIRST-FIT MEMORY MANAGEMENT


AIM
To write a C program to implement the First-Fit Memory allocation Strategy.
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;

15
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

Memory Management Scheme - First Fit


Enter the number of blocks:3
Enter the number of files:2

Enter the size of the blocks:-


Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4

File_no: File_size :Block_no: Block_size: Fragement


1 1 3 7 6
2 4 1 5 1

RESULT

First-Fit Memory allocation Strategy was implemented successfully.

16
Ex.No. 09.B
Date :

BEST-FIT MEMORY MANAGEMENT


AIM
To write a C program to implement the Best-Fit Memory allocation Strategy.
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];
printf("\n\tMemory Management Scheme - Best 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)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;

17
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

Memory Management Scheme - Best Fit


Enter the number of blocks:3
Enter the number of files:2

Enter the size of the blocks:-


Block 1:5
Block 2:2
Block 3:7
Enter the size of the files :-
File 1:1
File 2:4

File No File Size Block No Block Size Fragment


1 1 2 2 1
2 4 1 5 1

RESULT

Best-Fit Memory allocation Strategy was implemented successfully.

18
Ex.No. 10
Date :

PRODUCER-CONSUMER PROBLEM USING SEMAPHORES


AIM
To write a C program to implement producer-consumer problem using semaphores.
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;
}
}
}

19
OUTPUT

1. Produce 2. Consume 3. Exit


Enter your choice: =2

Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: =1

Enter the value: 100

1. Produce 2. Consume 3. Exit


Enter your choice: =2

The consumed value is 100


1. Produce 2. Consume 3. Exit
Enter your choice: =3

RESULT

Producer-Consumer problem using semaphores was implemented successfully,

20
Ex.No. 11
Date :

A SHELL PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER

AIM
To write a shell program to find the factorial of a given number
PROGRAM

#shell script for factorial of a number


#factorial using while loop

echo "Enter a number"


read num
fact=1
while [ $num –gt 1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done
echo $fact

OUTPUT
enter a number:
5
120

RESULT
Factorial of a given number was found successfully.

21
Ex.No. 12
Date :

A SHELL PROGRAM TO GENERATE FIBONACCI NUMBER

AIM
To write a shell program to generate fibonacci number.
PROGRAM

clear
echo "Program to Find Fibonacci Series"
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci 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

Program to Find Fibonacci Series


How many number of terms to be generated ?
5
Fibonacci Series up to 5 terms :
0
1
1
2
3

RESULT

Fibonacci sequence for a given number was generated successfully.

22

You might also like