54
SYSTEM CALLS LISTING THE FILES IN A DIRECTORY Aim: T o write and execute a C Program to list the files in a directory with the use of exec () system calls. Algorithm: 1. Using f unction fork (), create a ne w proces s and its return value is st ored in a variable called pid. 2. If pi d is equa l to 0, t hen it i s the ch ild pr ocess and usi ng execp a ctivities command. 3. else, it is parent proces s, do the following. 4. W ait un til t he chil d proce ss st op it s execu tion. 5. Print that the parent proces s completed its execut ion. 6. St op th e progr am. Program: #include<stdio.h> #include<sys/types.h> int main() { int pid; if(pid==0)//CHILD PROCESS { else//PARENT PROCESS {  printf(“\nParent process”); wait(pid);  printf(“completed\n”); exit(0); }} 1

o Slab Manual

Embed Size (px)

Citation preview

Page 1: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 1/54

SYSTEM CALLS

LISTING THE FILES IN A DIRECTORY

Aim:

To write and execute a C Program to list the files in a directory with the use of 

exec () system calls.

Algorithm:

1. Using function fork (), create a new process and its return value is stored in a

variable called pid.

2. If pid is equal to 0, then it is the child process and using execp activities

command.

3. else, it is parent process, do the following.

4. Wait until the child process stop its execution.

5. Print that the parent process completed its execution.

6. Stop the program.

Program:

#include<stdio.h>

#include<sys/types.h>

int main(){

int pid;

if(pid==0)//CHILD PROCESS

{

else//PARENT PROCESS

{

 printf(“\nParent process”);

wait(pid);

 printf(“completed\n”);

exit(0);

}}

1

Page 2: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 2/54

Output:

[user13@ws002~]$ cc sys3.c

[user13@ws002~]$. /a.out

a.out fork.c loop.c loop3.sh

sys.c file.c lexp1.sh open.c

add.c

Parent process completed.

[user13@ws002~]$

Result:

Thus the C program in LINUX to illustrate the use of exec() system call was

executed successfully.

2

Page 3: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 3/54

I/O SYSTEM CALLS

COPYING ONE CONTENT FROM ONE FILE TO ANOTHER 

Aim:

To write and execute a C Program in LINUX, using the I/O system calls

namely open (), read (), write () and close ().

Algorithm:

1. Open a file, sample is read-only using open ().

2. Open a file, output is write-only (or) truncated mode using open().

3. Read the Content of the file using read()

4. Write input file content the output file using write ().

5. Display the numbers of bytes read and write successfully.

Program:

#include<stdio.h>

#include<functl.h>

int main()

{

int fp,fd,sz,I;

char *c;

c=(char *)calloc(50,sizeof(char));

fp=open(“output”,O-WRONLY|O_TRUNC);

fd=open(“Sample”,O_RDONLY);

sz=read(fd,c,20);

i=write(fp,c,strlen©);

 printf(“\n number of bytes read=%d\n”,sz);

 printf(“\n number of byted written=%d\n”,i);

close(fd);

close(fp);

}

3

Page 4: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 4/54

OUTPUT

[user13@ws002~]$ cc io.c

[user13@ws002~]$ ./a.out

 Number of bytes read=-1

 Number of bytes written=0

Result:

Thus the C Program in LINUX, Using the I/O system calls namely open (), read (),

write (), close () was executed successfully.

4

Page 5: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 5/54

SIMULATION OF UNIX COMMAND

STIMULATION OF LS COMMAND

Aim:

To write and execute a c program in LINUX, to stimulate the LS the command.

Algorithm:

1. Get the current working directory and store in the variable, path name.

2. scan the directory in the path specified in the path name and sort the directory

content alphabetically

3. Display the directory content.

4. Stop

Program:

#include<dirent.h>

main ()

{

struct dirent **namelist;

int n,I;

char *pathname;

getcwd(pathname);

n=scandir(pathname,&namelist,o,alphasort);

if(n<0)

 perror(“scandir”);

else

{

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

{

 printf(“%s\n”,namlist[i]->d_name);

free(namelist[i]);

}

free(namelist);

}}

5

Page 6: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 6/54

OUTPUT:

[user46@ws002~]$cc sim1.c

[user46@ws002~]$ ./a.out

Desktop

a.out

arith.sh

 b

 big.sh

c

cmd.sh

command

dir.c

for.sh

input.c

input.txt

io.c

lex.1

list.c

 pat.sh

 pattern.sh

 p

Result:

thus the c program in linux, to stimulate a LS command was executed

successfully.

6

Page 7: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 7/54

SIMULATION OF GRED COMMAND

Aim:

  To write and execute a C Program in LINUX, to stimulate the grep command.

Algorithm:

1. Initialize count and occurrence as 0.

2. If the no. of command line argument is not equal to 3, then print the error message

and exit.

3. Open the i/p in read mode.

4. Read a max of 1024 characters from the o/p line and store in error line until end

of error.

5. if end of file (or) error message

6. Increment the count by one.

7. Scan the string is forward direction looking for a line character and return the

 pointer value to newline.

8. If line not equal to NULL, the initialize newline as NULL string.

9. Find the first occurrence of the substring into the string fline and print the

substring and increment occurrence by 1.

10. goto step 4.

11. Stop the program.

7

Page 8: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 8/54

Program:

#include<stdio.h>

#include<string.h>

#define max 1024

int main(int argc,char *argv[])

{

File *fp;

char fline[max];

char *newline;

int count=0;

int occurrences =0;

if(!(fp=fopen(argv[1],”r”)))

{

 printf(“grep:could not open file:%s\n”,argv[1]);

exit(1);

}

while(fgets(fline,max,fp)!=NULL)

{

count++;

if(newline=strchr(fline,’\n’)

*newline=’\0’;

if(strstr(fline,argv[2])!=NULL)

{

 printf(“%s:%d%s\n”,argv[1],count,fline);

occurrences++;

}

8

Page 9: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 9/54

OUTPUT:

[user13@ws002~]$ cc six1.c

[user13@ws002~]$. /a.out sankar1 operating

Sankar: 1an operating system is layer of software takes care of technical aspects of a

computer’s operating.

Sankar: 4UNIX is a operating system designed for multiprocessing, multitasking and

multi-user.

Result:

Thus the c Program in LINUX to stimulate grep command is executed successfully .

9

Page 10: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 10/54

SCHEDULING ALGORITHMS

FIRST COME FIRST SERVED SCHEDULING

Aim:

To write and excute a c program to implement the first come first served

scheduling algorithm

Algorithm:

1. call getdata() to get process name, burst time and arrival time from the user 

2. call fcfs() to perform the first come first served scheduling algorithm

getdata()

1. get the number of processes from the user 

2. for each process get the process name, burst time and arrival time from the user 

fcfs()

1. swap the process according to the arrival time

2. call calculate()

3. call ganttchart()

ganttchart()

1. Display the process names

2. Display the waiting time of each process

calculate()

1. initialize the waiting time and turn around time and time of first process as 0

2. for each process do the following

/ calculate the wait time of each process

/ calculate total wait time

/ calculate total turn around time

3. calculate the average wait time and average turn around time.

4. Display the output

10

Page 11: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 11/54

Program:

#include<conio.h>

#include<conio.h>

#include<string.h>

int n,Bu[20],Twt,Ttt,A[10],Wt[10],w;

float Awt,Att;

char pname[20][20],c[20][20];

void getdata();

void ganttchart();

void calculate()

void fcfs();

void getdata()

{

int I;

 printf(“Enter the number of process”);

scanf(“%d”,&n);

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

{

fflush(stdin);

 printf(“enter the process name”);

scanf(“%s”,&pname[i]);

 printf(“Enter the burst time process %s”,pname[i]);

scanf(“%d”,&Bu[i]);

 printf(“Enter the arrival time process %s”,pname[i]);

scanf(“%d”,A[i});

}

}

11

Page 12: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 12/54

void gantt_chart()

{

int I;

 printf(“Gauntchart”);

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

 printf(“%s”,pname[i]);

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

 printf(“%d”,Wt[i});

 printf(“%d”,Wt[n]+Bu[n]);

}

void calculate()

{

int I;

Wt[1]=0;

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

{

Twt=Twt+(Wt[i]-A[i]);

Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);

Att=(float)Ttt/n;

Awt=(float)Twt/n;

 printf(“Average Turn Around Time=%3.2f ms”,Att);

 printf(“Average waiting time=3.2f ms”,Awt);

}

}

void fcfs()

{

int I, temp,temp1;

Twt=0;

Ttt=0;

for(i=1;i<=n;j++)

{

12

Page 13: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 13/54

for(j=i+1;j<=n;j++)

{

if(A[i]>;j<=n;j++)

{

if(A[i]>A[j])

{

temp=Bu[i];

temp1=A[i];

Bu[i]=Bu[j];

A[i]=A[j];

Bu[j]=temp;

A[j]=temp1;

strcpy(c[i],pname[j]);

strcpy(pname[i],pname[j]);

strcpy(pname[j],c[i]);

}

}

}

calculate();

ganttchart();

}

main()

{

int ch;

clrscr()

 printf(FIRST COME FIRST SERVED ALGORITHM);

getdata();

fcfs();

getch();

}

Output:

13

Page 14: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 14/54

Enter the number of processes: 3

Enter the process name: a

Enter the burst time for process a: 12

Enter the arrival time for process a: 0

Enter the process name: b

Enter the burst time for process b: 24

Enter the arrival time for process b: 2

Enter the process name: c

Enter the burst time for process a: 2

Enter the arrival time for process a: 1

Average Turn around time= 20.33 ms

Average waiting time=7.67 ms

GANTT CHART

A B C

0 12 14 38

Result

Thus the c program for FFS algorithm was executed successfully.

14

Page 15: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 15/54

SHORTEST JOB FIRST SCHEDULING

Aim:

  To write and execute a C Program to implement the Shortest job first scheduling

algorithm.

Algorithm:

1. Call getdata () to get process name, burst time and arrival time from the user.

2. call sjf() to perform the sjf scheduling algorithm

getdata ()

1. Get the number of process from the user.

2. For each process get the process name, burst time and arrival time for the user.

sjf ()

1. Store the burst time for each process in a temporary arry.

2. Swap the process according to their burst time and calculate it as sum of it and

 burst time.

3. Initialize s[i] for each process as ‘7’

4. Initialize wait time and turn around time of first process as 0.

5. Set w as WTB[1] and s[1] as F

(i)While w<Tt,do the following

Set I as 2

While i<=n do the following

15

Page 16: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 16/54

(ii) if s[i] =’T’ and A[i] <=t, do the following

1. Set waiting time of I ith process as w and s[i] as ‘F’

2. Set w as w+B[i] and t as w.

3. Set I as 2.

6. Else increment I by 1.

7. For each process, do the following steps.

8. Calculate total waiting time, twt, as the sum of bwt and the difference between the

wait time and arrival time of current process.

9. calculate the total sum around time, Ttt as the difference between sum of Ttt, wait

time and burst time and the arrival time of the current process

10. Calculate the average wait time.

11. Calculate the average turn around time.

12. Display the average waiting and turn around time.

13. cal Gantt-chart()

Ganttchart ()

1. Display the all process times

2. Display the waiting time of each process.

16

Page 17: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 17/54

Program:

#include<stdio.h>

#include<conio.h>

int n,bu[20],twt,ttt,a[20],b[20];

float awt,att;

char pname[20][20];

void getdata();

void gantt_chart();

void sjf();

void getdata()

{

int I;

 printf(“\n Enter the number of procesors:”);

scanf(“%d”,&n);

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

{

fflush(stdin);

 printf(“\n\n Enter the process name:”);

scanf(“%s”,&pname[i]);

 printf(“\n Enter burst time for process %s”,pname[i]);

scanf(“%d”,&bu[i]);

 printf(“\n Enter arrival time for process %s”,pname[i]);

scanf(“%d”,&a[i]);

}

}

void gantt_chart()

{

int I;

 printf(“\n \n\t\t\t GANTT CHART\n”);

 printf(“\n-------------------------------\n”);

17

Page 18: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 18/54

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

 printf(“\t%s\t”,pname[i]);

 printf(“\t\n”);

 printf(“\n--------------------------------\n”);

 printf(“\n”);

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

 printf(“%d\t\t”,wt[i]);

 printf(“%d”,wt[n]+b[n]);

 printf(“\n----------------------------------\n”);

 printf(“\n”);

}

void sjf()

{

int w,t,I,tt=0,temp,j;

char s[10],c[20][20];

int temp1;

twt=ttt=0;

w=0;

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

{

 b[i]=bu[i];

s[i]=’T’;

tt=tt+b[i];

}

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

{

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

{

if(b[j-1]>b[j])

{

temp=b[j-1];

18

Page 19: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 19/54

temp1=a[j-1];

 b[j-1]=b[j];

a[j-1]=a[j];

 b[j]=temp;

a[j]=temp1;

strcpy(c[j-1],pname[j-1]);

strcpy(pname[j-1],pname[j]);

strcpy(pname[j],c[j-1]);

}

}

}

wt[1]=0;

w=w+b[1];

t=w;

s[1]=’F’;

while(w<tt)

{

i=2;

while(i<=n)

{

if(s[i]==’T’&&a[i]<=t)

{

wt[i]=w;

s[i]=’F’;

w=w+b[i];

t=w;

i=2;

}

else

i++;

}

19

Page 20: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 20/54

}

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

{

twt=twt+([i]-a[i]);

ttt=ttt+(wt[i]+bu[i]-a[i]);

}

att=(float)ttt/n;

awt=(float)twt/n;

 printf(“\n\nAvg Turnaround time :%3.2f ms”,att);

 printf(“\n\n Avg waiting time :%3.2f ms”awt);

gantt_chart();

}

void main()

{

 printf(“\n\n shortest job first \n\n”);

getdata();

sjf();

}

20

Page 21: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 21/54

Output:

Shortest job first Algorithm

Enter number of processors:3

Enter the process name:aa

Enter burst Time for process aa: 56

Enter arrival time for process aa: 1

Enter the process name:bb

Enter burst time for process bb: 77

Enter arrival time for process bb: 3

Enter the process name:cc

Enter burst Time for process cc: 89

Enter arrival time for process cc: 5

Avg Turn around time: 134.00 ms

Avg waiting time: 60.00 ms

GANTT CHART

-------------------------------

| aa | bb | cc |

---------------------------------

0 56 133 222

-------------------------------

Result:

Thus the C program to implement the shortest Job First algorithm was executed

successfully.

21

Page 22: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 22/54

PRIORITY SCHEDULING ALGORITHM

Aim:

To write and execute a c program to implement the priority scheduling algorithm.

Algorithm:

1. Call getdata () to get process name, burst time and arrival time for the user.

2 Call prior () to perform the priority scheduling algorithm.

 getdata ()

1. Get the number of process from the user.

2. For each process get the process name, burst time and arrival time from the user.

3. For each process get the process name, burst time and arrival time from the user.

prior ()

1. Start the burst time of each process in temp array.

2. Swap the processes according to their priorities and calculate it has sum of Tt and

 burst time of the current process.

3. Initialize s[i] for each process as ‘T’.

Initialize the wait time and turn around time of first process as 0.

4. set w as w+B[i] and S[i] as F

While w<Tt, do the following

i) Set i as 2.

ii)) until i<=n do the following

1)if s[i]=’T’ and T[i] <=t,do the following

a) Set waiting time of ith process as w and s[i] as f 

 b) Set w as w+B[i] and t as w

c) Set I as 2

ii) Else increment I by 1.

22

Page 23: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 23/54

8. For each process do the following steps

a) Calculate total wait time twt as the sum of twt and the difference between the

wait time and the arrival time of the current process.

 b) Calculate total turn around time ttt and the difference between sum of the Ttt

wait time and burst time and arrival time of current process.

9. Calculate the average turn around time

10. Display the average waiting turn around time

11. Call Gantt chart

Program:

#include<stdio.h>

#include<string.h>

#include<string.h>

int n,bu[20],twt,ttt,a[20],wt[20],p[20],b[20];

float awt,att;

char pname[20[20];

void getdata();

void ganttchart();

void prior();

void getdata()

{

int I;

 printf(“\n enter the no of the processes”);

scanf(“%d”,&n);

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

{

fflush(stdin);

 printf(“\n\n enter the process name”);

scanf(“%d”,&pname);

printf(“\n\n enter the burst time for process %s”,pname[i]);

scanf(“%d”,&bu[i]);

 printf(“\n\n enter the arrival time for process %s”,pname[i]);

23

Page 24: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 24/54

scanf(“%d”,&a[i]);

}}

void gantt_chart()

{

int I;

 printf(“\n\n Gant chart”);

 printf(\n------------------------------\n”);

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

 printf(“/t %s \t”,pname[i]);

 printf(“\t\n”);

 printf(\n------------------------------\n”);

 printf(“\t\n”);

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

 printf(“/t %d \t”wt[i]);

 printf(“%d”,wt[n]+b[n]);

 printf(\n------------------------------\n”);

 printf(“/n”);

}

void prior()

{

int w,t,tt=0,temp,j

char s[10],c[20][20];

int temp1;

twt =ttt=0;

w=0;

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

{

 b[i]=bu[i];

s[i]=’T’;

tt=tt+b[i];

}

24

Page 25: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 25/54

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

{

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

{

if(b[j-1]>b[j])

{

temp=b[j-1];

temp1=a[j-1];

 b[j-1]=b[j];

a[j-1]=a[j];

 b[j]=temp;

a[j]=temp1;

temp=p[j-1];

 p[j-1]=p[j];

 p[j]=temp;

strcpy(c[j-1),pname[j-1});

strcpy(pname[j-1],pname[j]);

strcpy(pname[j],c[j-1]);

}

}

}

wt[1]=0;

w=w+b[l];

t=w;

s[l]=’F’;

while(w<tt)

{

i=2;

while(i<=n)

{

if(s[i]==’T’ && a[i]<=t)

25

Page 26: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 26/54

{

wt[i]=w;

s[i]=’F’;

w=w+b[i];

t=w;

i=2;

}

else

i++;

}

}

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

{

twt=twt+(wt[i]-a[i]);

ttt=ttt+((wt[i]+bu[i]-a[i]);

}

att=(float)ttt/n;

awt=(float)twt/n;

 printf(“\n\n Avg turn around time:%3.2f ms”,att);

 printf(“\n \n avg waiting time :%3.2f ms”,awt);

gantt_chart();

}

void main()

{

clrscr();

 printf(“\n \n priority scheduling algorithm”);

 pruior();

getdata();

}

26

Page 27: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 27/54

Output:

Priority scheduling algorithm:

Enter the number of processors: 3

Enter the process name: a

Enter burst time for process a: 12

Enter arrival time for process a: 0

Enter the priority for process a: 1

Enter the process name:a

Enter burst time for process a: 12

Enter arrival time for process a: 0

Enter the priority for process a: 1

Enter the process name:a

Enter burst time for process a: 12

Enter arrival time for process a: 0

Enter the priority for process a: 1

Avg Turn around time=9.00ms

Avg waiting time=4.00ms

Result:

Thus the C program to implement the priority scheduling algorithm was executed

successfully.

27

Page 28: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 28/54

ROUND ROBIN SCHEDULING ALGORITHM

Aim:

To write and execute a C Program to implement the Round Robin Scheduling.

Algorithm:

1. Call getdata() to get process name, burst time and arrival time from the user.

2. Call rr() to perform the RRS algorithm.

get data()

1. Get the number of processor from the user.

2. For each process get the process name and burst time from the user.

3. Get the time quantum from the user.

rr ()

1. Synchronize the process such that each process is allocated with cpu for a time

slice.

2. If process time is less than time slice means assigns the number of slice as 1.

3. Similarly calculate the number of slice for each process.

4. Display the result.

5. Call calculates ().

Calculate ()

1. Calculate the wait time and turnaround time.

2. Calculate the average wait time and average time around.

3. Display the average a wait time and average turn around.

28

Page 29: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 29/54

Program:

#include<stdio.h>

#include<conio.h>

int Bu[15],a[15][15],pname[15][15],I,j,n,k=0,q,Tu=0,Twt=0;

float Att,Awt;

void getdata();

void rr();

void calculate();

void getdata()

{

 printf(“\n\n Enter the number of process”);

scanf(“%d”,&n);

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

{

 j=0;

 printf(“\n\n Enter the process name”);

scanf(“%s”,&pname[i]);

 printf(“\n Enter the Burst Time for Process %s”,pname[i]);

scanf(“%d”,&Bu[i][j]);

}

}

void cal;cuate()

{

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

{

i=1;

while(a[i][j]!=0)

i+=1;

Ttt+a[i-1][j];

}

29

Page 30: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 30/54

Att=(float)Ttt/n;

 printf(“\n\n Average Turn around time=%3.2f ms”,Att);

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

Twt+=a[0][i];

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

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

if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))

Awt=(float)Twt/n;

 printf(“\n\n average Waiting Time=%3.2f ms”,Awt);

}

void rr()

{

 printf(“\n\n”);

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

{

A[2*j][i]=k;

if((Bu[i][j]<=q)&&(Bu[i][j]!=0)

{

Bu[i][j+1]=G;

 printf(“%d\t%s\t%d\n”,K,pname[i],K+bu[i][j]);

K+=Bu[i][j];

A[2*j+1][i]=k;

}

else if([i][j]!=0

{

Bu[i][j+1]=Bu[i][j]-q;

 printf(“5d\t%s\t%d\n”,K,pname[i],(k+q));

K+=q;

A[2*j+1][i]=k;

}

30

Page 31: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 31/54

else

{

A[2*j][i]=0;

A[2*j+1][i]=0;

}

}

calculate();

}

void main()

{

clrscr();

 printf(“\n\n Round Robin Scheduling Algorithm\n\n”);

getdate();

rr();

getch();

}

31

Page 32: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 32/54

OUTPUT:

ROUND ROBIN SCHEDULING ALGORITHM

Enter the number of processes: 3

Enter the process name: A

Enter the burst Time for Process: 24

Enter the Process name: B

Enter the Burst Time for the Process: 3

Enter the process name: C

Enter the burst Time for the Process: 3

Enter the Time Quantum: 4

0 A 4

4 B 7

7 C 10

10 A 14

14 A 18

18 A 22

22 A 26

26 A 30

Average Turn Around time=15.67ms

Average Waiting Time=5.67ms

Result:

Thus the C program to implement the Round Robin Scheduling algorithm was

executed.

32

Page 33: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 33/54

INTER PROCESS COMMUNICATION USING PIPES

Aim

To write a C program for implementation of pipes.

Algorithm

1. Initialize the feildes vector.

2. Call the fork()

3. Check the value of k 

i) If the value of k is equal to zero , then execute the placement process.

ii) Parent process gets the total number.

4. i) If the value of k is !equal to -1, then execute the child process.

ii) Get the number from parent process and divide the number by 2. if the

remainder value is not equal to zero, then it is consider as odd number.

iii) Repeat the step 2 for all the numbers in parent process.

5. Display the odd number.

Program

#include<stdio.h>

#include<unistd.h>

main()

{

int k,n,n2,a[15]={0},b[15]={0},I,j=0,p,feildes[2];

 p=pipe(feildes);

k=fock();

if(k==0)

{

 printf(“Parent process”);

33

Page 34: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 34/54

 printf(“the parent process ID is %d”,getpid());

 printf(“Enter the total number”);

scanf(%d”,&n);

write(feildes[1],&n,sizeof(n));

 printf(“Enter the total number”);

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

{

scanf(%d”,&a[i]);

write(feildes[1],&a[i],sizeof(n));

}

}

else if(k!=-1)

{

 printf(“the child process ID is : %d”,getpid());

read(feildes[1],&n2,sizeof(n));

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

{

read(feildes[0],&b[i],sizeof(n2));

if((b[i]%2)!=0)

{

c[j]=b[i];

 j++;

}

}

 printf(“Odd numers are”)

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

 printf(“%d”,c[i]);

}

else

 printf(“error infoking”);

}

34

Page 35: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 35/54

Output

Parent process

the parent process ID is 2755

the child process ID is 2754

Enter the total number 5

Enter the total numbers 1 3 4 6 7

Odd numers are 1 3 7

Result

Thus the program for pipes are implemented and verified successfully.

35

Page 36: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 36/54

PRODUCER CONSUMER PROBLEM

Aim:

To write and execute a c program to implement producer consumer relationship.

Algorithm:

1. Read the buffer length from the user.

2. Repeat the following steps,until a key stroke is not available.

3. if the buffer is empty the items is producer and is store in the buffer.

4. the buffer is not empty the item is consumed from the buffer 

5. Stop the program.

Program:

#include<stdio.h>

int n,flag=1,f=0,kbhit;

void producer()

{

n=n+1;

f=f+1;

 printf(“\n Producer has produced %d\n”,f);

}

void consumer()

{

 printf(“\n consumer has consumed %d\n”,f);

n=n+1;

}

36

Page 37: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 37/54

void main()

{

char s;

 printf(“\n Enter the buffer state”);

scanf(“%c”,&s);

if(s==’e’)

n=0;

else

n=1;

while(!kbhit)

{

if(n==0)

{

 producer();

if(n==1

consumer();

}

}

}

37

Page 38: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 38/54

Output:

Enter the Buffer state:e

Producer has produced 1

Consumer has consumed 1

Producer has produced 2

Consumer has consumed 2

Producer has produced 3

Consumer has consumed 3

Producer has produced 4

Consumer has consumed 4

Result:

Thus the C Program to implement Producer consumer relationship was executed

successfully.

38

Page 39: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 39/54

FIRST FIT ALGORITHM

Aim:

To write and execute a c program to stimulate placement technique of first fit

algorithm.

Algorithm

1 Consider a matrix and assume itself as it is maintain the no of pages from user.

2 Fill element in each page randomly to the wish of the user.

3 Get the element to be placed in main memory.

4 Depending upon the element size place is first phase display it.

Program

#include <stdio.h>

#include <conio.h>

void main ()

{

int c,a[10][10],I,j,k=0,n[10],m,size3=0,small;

clrscr();

 printf(“Simulation of paging”);

 printf(“……………………..”);

 printf(“Enter the max size”);

scanf(“%d”,&m);

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

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

a[i][j]=0;

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

{

 printf(“Enter the number of elements to be inserted in page %d”,i+1);

scanf(“%d”,&k);

39

Page 40: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 40/54

n[i]=k;

}

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

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

a[i][j]=n[i];

 printf(“APPEARANCE OF MAIN MEMORY”);

 printf(“………………………………………..”);

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

 printf(“page %d”,i+1);

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

{

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

 printf(“%d”,a[i][j]);

}

 printf(“FIRST FIT ALGORITHM”);

 printf(“Enter the element size”);

scanf(“%d”,&size3);

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

{

if(size3<=(m-n[i])

{

for(j=n[i];j<(size3+n[i]);j++)

{

a[i][j]=i+1;

}

 break;

}

}

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

 printf(“page %d”,i+1);

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

40

Page 41: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 41/54

{

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

 printf(“%d”,a[i][j]);

}

getch();

}

Output

SIMULATION OF PAGING

Enter the maximum size 4

Enter the number of elements to be inserted in page 1=1

Enter the number of elements to be inserted in page 2=2

Enter the number of elements to be inserted in page 3=3

Enter the number of elements to be inserted in page 4=4

41

Page 42: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 42/54

APPEARENCES OF MAIN MEMORY

PAGE1 PAGE2 PAGE3 PAGE4

1 2 3 4

0 2 3 4

0 0 3 4

0 0 0 4

FIRST FIT ALGORITHM

Enter the element size 2

PAGE1 PAGE2 PAGE3 PAGE4

1 2 3 4

1 2 3 4

1 0 3 4

0 0 0 4

Result

Thus the c program in Linux to stimulate the placement technique is executed and output

verified successfully.

42

Page 43: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 43/54

BEST FIT ALGORITHM

Aim

To write and execute a c program in Linux to stimulate the placement technique in

 best fit algorithm.

Algorithm

1 Consider the matrix and assume the matrix itself as if maintain the pages from

user.

2 Fill element in each page randomly.

3 Cut the element to be placed in main memory.

4 Depending upon the element size.

5 Display it.

Program

#include <stdio.h>

#include <conio.h>

void main ()

{

int c,a[10][10],I,j,k=0,n[10],m,z,size3=0,p=i;

clrscr();

 printf(“Simulation of paging”);

 printf(“……………………..”);

 printf(“Enter the max size”);

scanf(“%d”,&m);

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

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

a[i][j]=0;

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

43

Page 44: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 44/54

{

 printf(“Enter the number of elements to be inserted in page %d”,i+1);

scanf(“%d”,&k);

n[i]=k;

}

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

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

a[i][j]=n[i];

 printf(“APPEARANCE OF MAIN MEMORY”);

 printf(“………………………………………..”);

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

 printf(“page %d”,i+1);

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

{

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

 printf(“%d”,a[i][j]);

}

 printf(“BEST FIT ALGORITHM”);

 printf(“Enter the element size”);

scanf(“%d”,&size3);

for(z=m;z>0;z--)

{

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

{

if(size3<=(z-n[i])

{

c=n[i];

for(j=z-size3;j<(size3+n[i]);j++)

a[i][j]=size3;

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

 printf(“page %d”,i+1);

44

Page 45: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 45/54

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

{

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

 printf(“%d”,a[j][i]);

}

getch();

}

Output

SIMULATION OF PAGING

Enter the maximum size 4

Enter the number of elements to be inserted in page 1=1

Enter the number of elements to be inserted in page 2=2

Enter the number of elements to be inserted in page 3=3

Enter the number of elements to be inserted in page 4=4

45

Page 46: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 46/54

APPEARENCES OF MAIN MEMORY

PAGE1 PAGE2 PAGE3 PAGE4

1 2 3 4

0 2 3 4

0 0 3 4

0 0 0 4

BEST FIT ALGORITHM

Enter the element size 2

PAGE1 PAGE2 PAGE3 PAGE4

1 2 3 4

0 2 3 4

0 2 3 4

0 2 0 4

Result

Thus the c program in Linux to stimulate the placement technique is executed

successfully.

46

Page 47: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 47/54

WORST FIT ALGORITHM

Aim

To write and execute a c program in Linux to stimulate the worst fit algorithm.

Algorithm

1 Consider a matrix and assume the matrix itself as it maintains the pages.

2 Fill element in each page randomly.

3 Cut the element to be placed in it.

4 Depending upon the element size and place.

5 Display it.

Program

#include <stdio.h>

#include <conio.h>

void main ()

{

int c,a[10][10],I,j,k=0,q=0,n[10],m,z,size3=0,small;

clrscr();

 printf(“Simulation of paging”);

 printf(“……………………..”);

Printf(“Enter the max size”);

scanf(“%d”,&m);

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

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

a[i][j]=0;

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

{

 printf(“Enter the number of elements to be inserted in page %d”,i+1);

47

Page 48: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 48/54

scanf(“%d”,&k);

n[i]=k;

}

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

for(j=0;j<n[i];j++)

a[i][j]=n[i];

 printf(“APPEARANCE OF MAIN MEMORY”);

 printf(“………………………………………..”);

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

 printf(“page %d”,i+1);

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

{

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

 printf(“%d”,a[i][j]);

}

 printf(“WORST FIT ALGORITHM”);

 printf(“Enter the element size”);

scanf(“%d”,&size3);

small=n[0];

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

{

if(n[i]<small)

{

q=i;

}

for(j=n[q];j<(size3+n[q]);j++)

a[q][j]=q+1;

}

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

 printf(“page %d”,i+1);

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

48

Page 49: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 49/54

{

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

 printf(“%d”,a[i][j]);

}

getch();

}

Output

SIMULATION OF PAGING

Enter the maximum size 4

Enter the number of elements to be inserted in page 1=1

Enter the number of elements to be inserted in page 2=2

Enter the number of elements to be inserted in page 3=3

Enter the number of elements to be inserted in page 4=4

49

Page 50: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 50/54

APPEARENCES OF MAIN MEMORY

PAGE1 PAGE2 PAGE3 PAGE4

1 2 3 4

0 2 3 4

0 0 3 4

0 0 0 4

WORST FIT ALGORITHM

Enter the element size 2

PAGE1 PAGE2 PAGE3 PAGE4

1 2 3 4

1 2 3 4

1 0 3 4

0 0 0 4

Result

Thus the c program to stimulate the technique of worst fit algorithm is executed

successfully.

50

Page 51: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 51/54

FIFO PAGE REPLACEMENT ALGORITHM

Aim:

To write and execute a C++ Program of Page Replacement algorithm

Algorithm:

1. start

2. get the page frame and reference string from the user.

3. initialize rfstr[25],pgfrm[5]

4. check rfstr=pgfrm then

5. continue the loop

6. find the page faults

7. Display the pgfrm.

8. stop

Program

#include<iostream.h>

#include<conio.h>

void main()

{

int i, j, k, npgfrm, nrfstr, pgflt;

int pgfrm[5], rfstr[25];

clrscr();

cout << "\n FIFO PAGE REPLACEMT TECHNIQUE";

cout << "\nEnter the Number of Page Frames : ";

cin >> npgfrm;

cout << "\nEnter the Number of Reference String : ";

cin >> nrfstr;

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

{

cout << "\n Enter the reference String - " << i << " : ";

51

Page 52: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 52/54

cin >> rfstr[i];

}

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

 pgfrm[i] = -1;

 pgflt=0;

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

{

for(j = 0 ; j < npgfrm && i < nrfstr; ++i )

{

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

if(pgfrm[k] == rfstr[i])

++i;

if(pgfrm[j] != rfstr[i])

{

 pgfrm[j++] = rfstr[i];

++pgflt;

}

}

--i;

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

cout << endl << pgfrm[j];

cout << "\n Press Any key to Continue...\n\n"; getch();

}

cout << "\nNumber of Page Faults = " << pgflt;getch();

}

Output

52

Page 53: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 53/54

Enter the Number of page Frames : 3

Enter the Number of Reference String : 20

FIFO PAGE REPLACEMENT TECHNIQUE

Enter the reference String - 0 : 7

Enter the reference String - 1 : 0

Enter the reference String - 2 : 1

Enter the reference String - 3 : 2

Enter the reference String - 4 : 0

Enter the reference String - 5 : 3

Enter the reference String - 6 : 0

Enter the reference String - 7 : 4

Enter the reference String - 8 : 2

Enter the reference String - 9 : 3

Enter the reference String - 10 : 0

Enter the reference String - 11 : 3

Enter the reference String - 12 : 2

Enter the reference String - 13 : 1

Enter the reference String - 14 : 2

Enter the reference String - 15 : 0

Enter the reference String - 16 : 1

Enter the reference String - 17 : 7

Enter the reference String - 18 : 0

Enter the reference String - 19 : 1

53

Page 54: o Slab Manual

7/28/2019 o Slab Manual

http://slidepdf.com/reader/full/o-slab-manual 54/54

7 0 1

Press Any Key To Continue...

2 3 0

Press Any Key To Continue...

4 2 3

Press Any Key To Continue...

0 1 2

Press Any Key To Continue...

7 0 1

Number of Page Faults = 15

Result:

Thus the c++ program to page replacement algorithm is executed successfully.