0% found this document useful (0 votes)
170 views

Operating System Lab Operating System Lab

This document contains a record notebook for experiments conducted in an Operating Systems lab class. It includes 12 experiments covering topics like process system calls, I/O system calls, IPC using message queues, and CPU scheduling algorithms like first come first serve, shortest job first, priority scheduling, and round robin. Each experiment contains the aim, algorithm, program code, output and result. The document is authored by R. Cheran, who has an M.Sc. in Computer Science from Thiruvalluvar University.

Uploaded by

Yuvaraj Yuvaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views

Operating System Lab Operating System Lab

This document contains a record notebook for experiments conducted in an Operating Systems lab class. It includes 12 experiments covering topics like process system calls, I/O system calls, IPC using message queues, and CPU scheduling algorithms like first come first serve, shortest job first, priority scheduling, and round robin. Each experiment contains the aim, algorithm, program code, output and result. The document is authored by R. Cheran, who has an M.Sc. in Computer Science from Thiruvalluvar University.

Uploaded by

Yuvaraj Yuvaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

lOMoARcPSD|10484226

Operating system lab

B.sc(Computer Science) (Thiruvalluvar University)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by Yuvaraj Yuvaraj ([email protected])
lOMoARcPSD|10484226

GOVERNMENT ARTS COLLEGE


CHIDAMBARAM-608 102
Re-Accredited by NAAC with ‘B’ Grade
(Affiliated to Thiruvalluvar University, Vellore)

OPERATING SYSTEM LAB

RECORD NOTE BOOK

PG DEPARTMENT OF COMPUTER SCIENCE


CBCS PATTERN (2017-18)

R.Cheran [M.sc computer science] Page 1

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

About the Author


Mr.cheran has a M.Sc. in Computer Science study in Thiruvalluvar
University.

R.Cheran [M.sc computer science] Page 2

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

CONTENTS
OPERATING SYSTEM LAB
EXP. PAGE
DATE NAME OF THE PROGRAM SIGNATURE
NO. NO.

1. PROCESS SYSTEM CALLS

2. I/O SYSTEM CALLS

3. IPC USING MESSAGE QUEUES

CPU& SCHEDULING ALGORITHM


4. FOR FIRST COME FIRST SERVE
SCHEDULING

CPU SCHEDULING ALGORITHM FOR


5. SHORTEST JOB FIRST SCHEDULING

6. PERFORM PRIORITY SCHEDULING

CPU SCHEDULING FOR ROUND


7. ROBIN SCHEDULING

8. PIPE PROCESSING

FIRST FIT, BEST FIT ALGORITHM


9. FOR MEMORY MANAGEMENT

PRODUCER-CONSUMER PROBLEM
10.
USING SEMAPHORES

R.Cheran [M.sc computer science] Page 3

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

11. FACTORIAL OF A GIVEN NUMBER

12. FIBONACCI NUMBER

R.Cheran [M.sc computer science] Page 4

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

PROCESS SYSTEM CALLS


AIM:

To write c program to implement the Process system calls.

ALGORITHM:

1. Start the program.

2. Declare the pid and get the pid by using the getpid() method.

3. Create a child process by calling the fork() system call

4. Check if(pid==0) then print the child process id and then print the parent

process value.

5. Stop the program.

R.Cheran [M.sc computer science] Page 5

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

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


{
pid_t cpid = waitpid(pid[i], &stat, 0);
if (WIFEXITED(stat))
printf("Child %d terminated with status: %d\n",
cpid, WEXITSTATUS(stat));
}
}

int main()
{
waitexample();
return 0;
}

R.Cheran [M.sc computer science] Page 6

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:
Thus the process system call program was executed and verified
successfully.

R.Cheran [M.sc computer science] Page 7

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

R.Cheran [M.sc computer science] Page 8

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

R.Cheran [M.sc computer science] Page 9

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the I/O system call program was executed and verified successfully.

R.Cheran [M.sc computer science] Page 10

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

IPC USING MESSAGE QUEUES

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.

5. msgsnd(): Data is placed on to a message queue by calling msgsnd().

6. msgrcv(): messages are retrieved from a queue.

7. msgctl(): It performs various operations on a queue. Generally it is use


to
8. destroy message queue.

9. Stop the program

R.Cheran [M.sc computer science] Page 11

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

R.Cheran [M.sc computer science] Page 12

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

die("msgsnd");
}
else
printf("message sent\n");
exit(1);
}

MESSAGE QUEUE FOR READER PROCESS


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

R.Cheran [M.sc computer science] Page 13

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the message queues program was executed and verified successfully.

R.Cheran [M.sc computer science] Page 14

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

FIRST COME FIRST SERVE SCHEDULING

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

R.Cheran [M.sc computer science] Page 15

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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;

R.Cheran [M.sc computer science] Page 16

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the FIFO process scheduling program was executed and verified
successfully.

R.Cheran [M.sc computer science] Page 17

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

SHORTEST JOB FIRST SCHEDULING

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.

2. Initialize the waiting time for process 1 as 0.

3. The processes are stored according to their burst time.

4. The waiting time for the processes are calculated a follows:

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.

7. Stop the program.

R.Cheran [M.sc computer science] Page 18

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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;

R.Cheran [M.sc computer science] Page 19

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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:

Thus the SJF program was executed and verified successfully.

R.Cheran [M.sc computer science] Page 20

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

PRIORITY SCHEDULING
AIM:

To write a ‘C’ program to perform priority scheduling.

ALGORITHM:

1. Start the program.

2. Read burst time, waiting time, turn the around time and priority.

3. Initialize the waiting time for process 1 and 0.

4. Based up on the priority process are arranged

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.

7. Stop the program.

R.Cheran [M.sc computer science] Page 21

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

printf("\n process to \t bt \t wt \t tt");


while(i<=n)
{
printf("\n%d\t %d\t %d\t %d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
arg1=totwt/n;
arg2=tottt/n;
printf("\n arg1=%d \t arg2=%d\t",arg1,arg2);
getch();
return 0;
}

R.Cheran [M.sc computer science] Page 24

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:
Thus the priority scheduling program was executed and verified
successfully.

R.Cheran [M.sc computer science] Page 25

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

ROUND ROBIN SCHEDULING

AIM:
To write a program to implement cpu scheduling for Round Robin
Scheduling.

ALGORITHM:

1. Get the number of process and their burst time.

2. Initialize the array for Round Robin circular queue as ‘0’.

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.

6. Stop the program.

R.Cheran [M.sc computer science] Page 26

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

R.Cheran [M.sc computer science] Page 28

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the Round Robin scheduling program was executed and verified
successfully.

R.Cheran [M.sc computer science] Page 29

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

PIPE PROCESSING
AIM :

To write a program for create a pope processing.

ALGORITHM:

1. Start the program.


2. Declare the variables.
3. Read the choice.
4. Create a piping processing using IPC.
5. Assign the variable lengths
6. “strcpy” the message lengths.
7. To join the operation using IPC .
8. Stop the program

R.Cheran [M.sc computer science] Page 30

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

}
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.

R.Cheran [M.sc computer science] Page 32

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

FIRST FIT MEMORY MANAGEMENT

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

R.Cheran [M.sc computer science] Page 33

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

R.Cheran [M.sc computer science] Page 34

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

}
}
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.

R.Cheran [M.sc computer science] Page 35

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

PRODUCER-CONSUMER PROBLEM USING


SEMOPHERES

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.

R.Cheran [M.sc computer science] Page 36

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

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

R.Cheran [M.sc computer science] Page 37

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the producer consumer program was executed and verified


successfully.
R.Cheran [M.sc computer science] Page 38

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

FIND FACTORIAL OF A GIVEN NUMBER

PROGRAM:

echo "Enter a number"

read fact

ans=1

counter=0
while [ $fact -ne $counter ]
do

counter=`expr $counter + 1`
ans=`expr $ans \* $counter`

done

echo " the factorial is $ans"

R.Cheran [M.sc computer science] Page 39

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the Shell Program to find factorial of a given number program was
executed and verified successfully.

R.Cheran [M.sc computer science] Page 40

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

FIBONACCI NUMBER

PROGRAM:

echo "program to fine fibonacci series"


echo "how many number of terms to be generated?"

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

R.Cheran [M.sc computer science] Page 41

Downloaded by Yuvaraj Yuvaraj ([email protected])


lOMoARcPSD|10484226

OUTPUT:

RESULT:

Thus the shell program to generate Fibonacci number program was executed
and verified successfully.

R.Cheran [M.sc computer science] Page 42

Downloaded by Yuvaraj Yuvaraj ([email protected])

You might also like