Sie sind auf Seite 1von 40

Orphan process

#include<stdio.h>

main()
{
int id;

printf("Before fork()\n");
id=fork();

if(id==0)
{
printf("Child has started: %d\n ",getpid());
printf("Parent of this child : %d\n",getppid());
printf("child prints 1 item :\n ");
sleep(10);
printf("child prints 2 item :\n");
}
else
{
printf("Parent has started: %d\n",getpid());
printf("Parent of the parent proc : %d\n",getppid());
}

printf("After fork()");
}

Write a program to create a thread to find the factorial of a natural


number ‘n’.

#include <stdio.h>
void main()
{
int i, fact = 1, num;

printf("Enter the number \n");


scanf("%d", &num);
if (num <= 0)
fact = 1;
else
{
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
}
printf("Factorial of %d = %5d\n", num, fact);
}

PROCESS CREATION
#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <sys/wait.h>

#include <stdlib.h>

int var_glb; /* A global variable*/

int main(void)

pid_t childPID;

int var_lcl = 0;

childPID = fork();

if(childPID >= 0) // fork was successful


{

if(childPID == 0) // child process

var_lcl++;

var_glb++;

printf("\n Child Process :: var_lcl = [%d], var_glb[%d]\n", var_lcl,


var_glb);

else //Parent process

var_lcl = 10;

var_glb = 20;

printf("\n Parent process :: var_lcl = [%d], var_glb[%d]\n", var_lcl,


var_glb);

else // fork failed

{
printf("\n Fork failed, quitting!!!!!!\n");

return 1;

return 0;

ZOMBIE PROCESSS
#include<stdio.h>

int main()

int pid;

pid=fork();

if(pid>0)

sleep(30);

printf("parent process id %d",getpid());

printf("in parent process");

else if(pid==0)

printf("child process id %d",getpid());

printf("in child process");

return 1;
ORPHAN PROCESS

#include<stdio.h>

int main()

int pid;

pid=fork();

if(pid>0)

printf("parent process id %d",getpid());

printf("in parent process");

else if(pid==0)

printf("child process id %d",getpid());

sleep(30);

printf("in child process");

return 1;

simple program to create Orphan process

x@ubuntu:~/ds/unix$ cat orp.c


/*
* Program to create orphan process @ Linux
* getpid() gives process PID and
* getppid() gives process's parent ID
* here main() process ID is parent id is current shells PID
* once process becomes orphan it is adopted by init process(it's PID is 1)
*/

#include<stdio.h>
#include<unistd.h>
int main()
{

pid_t p;

/* create child process */


p=fork();

if(p==0) {
/* fork() returns Zero to child */
sleep(10);
}
printf("The child process pid is %d parent pid %d\n", getpid(), getppid());
/*parent/child waits for 20 secs and exits*/
sleep(20);
printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());

return 0;
}
Server Program

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

#define SHMSZ 27

main()

char c;

int shmid;

key_t key;

char *shm, *s;


/*

* We'll name our shared memory segment

* "5678".

*/

key = 5678;

/*

* Create the segment.

*/

if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {

perror("shmget");

exit(1);

/*

* Now we attach the segment to our data space.

*/

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {

perror("shmat");

exit(1);

/*

* Now put some things into the memory for the

* other process to read.

*/

s = shm;

for (c = 'a'; c <= 'z'; c++)

*s++ = c;
*s = NULL;

/*

* Finally, we wait until the other process

* changes the first character of our memory

* to '*', indicating that it has read what

* we put there.

*/

while (*shm != '*')

sleep(1);

exit(0);

Client Program

/*

* shm-client - client program to demonstrate shared memory.

*/

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

#define SHMSZ 27

main()

int shmid;
key_t key;

char *shm, *s;

/*

* We need to get the segment named

* "5678", created by the server.

*/

key = 5678;

/*

* Locate the segment.

*/

if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {

perror("shmget");

exit(1);

/*

* Now we attach the segment to our data space.

*/

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {

perror("shmat");

exit(1);

/*

* Now read what the server put in the memory.

*/

for (s = shm; *s != NULL; s++)

putchar(*s);

putchar('\n');
/*

* Finally, change the first character of the

* segment to '*', indicating we have read

* the segment.

*/

*shm = '*';

exit(0);

//FCFS, Round Robin and Shortest Job First


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class cpuschedule
{
int n,bu[20];
float twt,awt,wt[20],tat[20];
public:
void Getdata();
void fcfs();
void sjf();
void roundrobin();
};
//Getting no of processes and Burst time
void cpuschedule::Getdata()
{
int i;
cout<<“Enter the no of processes:”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<“\nEnter The BurstTime for Process p”<<i<<“=”;
cin>>bu[i];
}
}
//First come First served Algorithm
void cpuschedule::fcfs()
{
int i,b[10];
float sum=0.0;
twt=0.0;
for(i=1;i<=n;i++)
{
b[i]=bu[i];
cout<<“\nBurst time for process p”<<i<<“=”;
cout<<b[i];
}
wt[1]=0;
for(i=2;i<=n;i++)
{
wt[i]=b[i-1]+wt[i-1];
}
for(i=1;i<=n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
sum+=tat[i];
}
awt=twt/n;
sum=sum/n;
cout<<“\nTotal Waiting Time=”<<twt;
cout<<“\nAverage Waiting Time=”<<awt;
cout<<“\nAverage Turnaround time=”<<sum;
}
//Shortest job First Algorithm
void cpuschedule::sjf()
{
int i,j,temp,b[10];
float sum=0.0;
twt=0.0;
for(i=1;i<=n;i++)
{
b[i]=bu[i];
cout<<“\nBurst time for process p”<<i<<“=”;
cout<<b[i];
}
for(i=n;i>=1;i–)
{
for(j=2;j<=n;j++)
{
if(b[j-1]>b[j])
{
temp=b[j-1];
b[j-1]=b[j];
b[j]=temp;
}
}
}
wt[1]=0;
for(i=2;i<=n;i++)
{
wt[i]=b[i-1]+wt[i-1];
}
for(i=1;i<=n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
sum+=tat[i];
}
awt=twt/n;
sum=sum/n;
cout<<“\nTotal Waiting Time=”<<twt;
cout<<“\nAverage Waiting Time=”<<awt;
cout<<“\nAverage turnaround time=”<<sum;
}
//Round Robin Algorithm
void cpuschedule::roundrobin()
{
int i,j,tq,k,b[10],Rrobin[10][10],count[10];
int max=0;
int m;
float sum=0.0;
twt=0.0;
for(i=1;i<=n;i++)
{
b[i]=bu[i];
cout<<“\nBurst time for process p”<<i<<“=”;
cout<<b[i];
if(max<b[i])
max=b[i];
wt[i]=0;
}
cout<<“\nEnter the Time Quantum=”;
cin>>tq;
//TO find the dimension of the Round robin array
m=max/tq+1;
//initializing Round robin array
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
Rrobin[i][j]=0;
}
}
//placing value in the Rrobin array
i=1;
while(i<=n)
{
j=1;
while(b[i]>0)
{
if(b[i]>=tq)
{
b[i]=b[i]-tq;
Rrobin[i][j]=tq;
j++;
}
else
{
Rrobin[i][j]=b[i];
b[i]=0;
j++;
}
}
count[i]=j-1;
i++;
}
cout<<“Display”;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<“\nRr[“<<i<<“,”<<j<<“]=”<<Rrobin[i][j];
cout<<” “;
}
cout<<“\ncount=”<<count[i];
}
for(j=1;j<=n;j++)
{
for(i=1;i<=count[j];i++)
{
if(i==count[j])
{
for(k=1;k<j;k++)
{
if(k!=j)
wt[j]+=Rrobin[k][i];
}
}
else
for(k=1;k<=n;k++)
{
if(k!=j)
wt[j]+=Rrobin[k][i];
}
}
}
for(i=1;i<=n;i++)
cout<<“\nWaiting Time for process P”<<i<<“=”<<wt[i];
//calculating Average Weighting Time
for(i=1;i<=n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
sum+=tat[i];
}
awt=twt/n;
sum=sum/n;
cout<<“\nTotal Waiting Time=”<<twt;
cout<<“\nAverage Waiting Time=”<<awt;
cout<<“\nAverage turnaround time=”<<sum;
}
void main()
{
int ch=0,cho;
cpuschedule c;
clrscr();
do
{
switch(ch)
{
case 0:
cout<<“\n0.MENU”;
cout<<“\n1.Getting BurstTime”;
cout<<“\n2.FirstComeFirstServed”;
cout<<“\n3.ShortestJobFirst”;
cout<<“\n4.RoundRobin”;
cout<<“\n5.EXIT”;
break;
case 1:
c.Getdata();
break;
case 2:
cout<<“FIRST COME FIRST SERVED SCHEDULING”;
c.fcfs();
break;
case 3:
cout<<“SHORTEST JOB FIRST SCHEDULING”;
c.sjf();
break;
case 4:
cout<<“ROUND ROBIN SCHEDULING”;
c.roundrobin();
break;
case 5:
break;
}
cout<<“\nEnter your choice:”;
cin>>ch;
getch();
}while(ch<5);
}
PRODUCER CONSUMER
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
#define buffersize 10
pthread_mutex_t mutex;
pthread_t tidP[20],tidC[20];
sem_t full,empty;
int counter;
int buffer[buffersize];

void initialize()
{
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}

void write(int item)


{
buffer[counter++]=item;
}
int read()
{
return(buffer[--counter]);
}

void * producer (void * param)


{
int waittime,item,i;
item=rand()%5;
waittime=rand()%5;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
printf("\nProducer has produced item: %d\n",item);
write(item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}

void * consumer (void * param)


{
int waittime,item;
waittime=rand()%5;
sem_wait(&full);
pthread_mutex_lock(&mutex);
item=read();
printf("\nConsumer has consumed item: %d\n",item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}

int main()
{
int n1,n2,i;
initialize();
printf("\nEnter the no of producers: ");
scanf("%d",&n1);
printf("\nEnter the no of consumers: ");
scanf("%d",&n2);
for(i=0;i<n1;i++)
pthread_create(&tidP[i],NULL,producer,NULL);
for(i=0;i<n2;i++)
pthread_create(&tidC[i],NULL,consumer,NULL);
for(i=0;i<n1;i++)
pthread_join(tidP[i],NULL);
for(i=0;i<n2;i++)
pthread_join(tidC[i],NULL);

//sleep(5);
exit(0);
}
DINING PHILOSPHER
Problem Description

Develop a program to implement the solution of the dining philosopher’s

problem using threads. The input to the program is the number of philosophers to be seated

around the table. Output shows the various stages that each philosopher passes through

within a certain time. A philosopher can be in anyone of the three stages at a time: thinking,

eating or finished eating.

Data Structures and Functions

The main data structures used here are: Arrays

The arrays represent the philosophers and corresponding chopsticks for them.Each element

in the philosopher’s array corresponds to a thread and each element in the chopstick’s array

corresponds to a mutex variable.

The functions used here are:

1. pthread_mutex_init (&mutex, NULL) – initialization of mutex variable

2. pthread_mutex_lock (&mutex) – attempt to lock a mutex

3. pthread_mutex_unlock (&mutex) – unlock a mutex

4. pthread_create (ptr to thread, NULL, (void*) func, (void*) )

5. pthread_join (ptr to thread, &msg)-This function will make the main program

wait until the called thread is finished executing it’s task.

6. pthread_mutex_destroy (ptr to thread)-

7. pthread_exit(NULL)

Note: while compiling this program use the following:

[root@Linux philo]# gcc –o dining dining.c -lpthread

Algorithm

Algorithm for process:


1. Start.

2. Declare and initialize the thread variables (philosophers) as required.

3. Declare and initialize the mutex variables (chopsticks) as required.

4. Create the threads representing philosophers.

5. Wait until the threads finish execution.

6. Stop.

Algorithm for thread (philosopher i) function:

1. Start.

2. Philosopher i is thinking.

3. Lock the left fork spoon.

4. Lock the right fork spoon.

5. Philosopher i is eating.

6. sleep

7. Release the left fork spoon.

8. Release the right fork spoon.

9. Philosopher i Finished eating.

10. Stop.

CODE USINGS THREADS ONLY

1. #include<stdio.h>

#include<stdlib.h>

#include<pthread.h>

#include<semaphore.h>

void *func(int n);

pthread_t philosopher[5];

pthread_mutex_t chopstick[5];

int main()
{

int i,k;

void *msg;

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

k=pthread_mutex_init(&chopstick[i],NULL);

if(k==-1)

printf(“\n Mutex initialization failed”);

exit(1);

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

k=pthread_create(&philosopher[i],NULL,(void *)func,(int *)i);

if(k!=0)

printf(“\n Thread creation error \n”);

exit(1);

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

k=pthread_join(philosopher[i],&msg);

if(k!=0)

printf(“\n Thread join failed \n”);

exit(1);

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

k=pthread_mutex_destroy(&chopstick[i]);

if(k!=0)

{
printf(“\n Mutex Destroyed \n”);

exit(1);

return 0;

}void *func(int n)

printf(“\nPhilosopher %d is thinking “,n);

pthread_mutex_lock(&chopstick[n]);//when philosopher 5 is eating he takes

fork 1 and fork 5

pthread_mutex_lock(&chopstick[(n+1)%5]);

printf(“\nPhilosopher %d is eating “,n);

sleep(3);

pthread_mutex_unlock(&chopstick[n]);

pthread_mutex_unlock(&chopstick[(n+1)%5]);

printf(“\nPhilosopher %d Finished eating “,n);

BANKERS ALGORITHM
#include<iostream>

using namespace std;

int pt[10];

int * safety(int need[][10],int avlb[],int alloc[][10], int p, int r)

int work[5],finish[5],i,j,k=0;

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

work[i]=avlb[i];

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

finish[i]=0;
i=0;

while(k<p)

if(finish[i]==0)

j=0;

while(j<r)

if(need[i][j]>work[j]) break;

j++;

if(j==r)

j=0;

while(j<r)

work[j]+=alloc[i][j];

j++;

pt[k]=i;

k++;

finish[i]=1;

i=(i+1)%p;

return pt;
}

int main()

int avlb[5],alloc[5][10],need[5][10],max[5][10],rq[5],i,j,r,t,p,rp,flag;

cout<<"Enter the number of processes: ";

cin>>p;

cout<<"Enter the number of resources: ";

cin>>r;

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

cout<<"Enter the instances of resource "<<i<<": ";

cin>>avlb[i];

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

cout<<"\nallocation vector for the process "<<i<<": ";

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

cin>>alloc[i][j];

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

cout<<"\nmaximum vector for the process "<<i<<": ";

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

cin>>max[i][j];

cout<<"Avialable Vector: ";

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

t=0;

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

{
t+=alloc[j][i];

need[j][i]=max[j][i]-alloc[j][i];

avlb[i]-=t;

cout<<"\t "<<avlb[i];

cout<<endl<<"Need Matrix:" <<endl;

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

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

cout<<"\t"<<need[i][j];

cout<<endl;

safety(need,avlb,alloc,p,r);

cout<<"Safe Sequence: ";

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

cout<<"P"<<pt[i]<<"\t";

cout<<endl;

//safety sequence ends here.... following code not necessary

cout<<"Enter the requesting process: ";

cin>>rp;

cout<<"Enter the resource allocation: ";

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

flag=0;
cin>>rq[i];

if(rq[i]>need[rp][i])

flag=1;

break;

if(flag==1) cout<<"Error!! Request exceeds maximum claim! ";

else

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

if(rq[i]>avlb[i])

flag=2;

break;

if(flag==2)

{cout<<"Request can't be granted since it exeeds the availability !!";}

else{

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

avlb[j]-=rq[j];

alloc[rp][j]+=rq[j];

need[rp][j]-=rq[j];

safety(need,avlb,alloc,p,r);

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

cout<<"P"<<pt[i]<<"\t";

}
return 0;

Best Fit

#include<stdio.h>
#include<conio.h>

int main()
{
int ns,np,i,j,count,temp1,temp2;
int segments[10],request[10],flag[10],position[10];
clrscr();

printf("\nEnter the number of Page segments:");


scanf("%d",&ns);

for(i=0;i<ns;i++)
{
printf("\nEnter the Size of page segment%d:",(i+1));
scanf("%d",&segments[i]);
flag[i]=0;
position[i]=(i+1);
}

printf("\nEnter the number of Process:");


scanf("%d",&np);

for(i=0;i<np;i++)
{
printf("\nEnter the Size of required by segment%d:",(i+1));
scanf("%d",&request[i]);
}
clrscr();

printf("MEMORY MANAGEMENT ALGORITHMS");

printf("\n\nSize of page Segments");


for(i=0;i<ns;i++)
printf("\nPage%d:%d",(i+1),segments[i]);
printf("\n\nSize of page Segments REQUESTED");
for(i=0;i<np;i++)
printf("\nProcess P%d:%d",(i+1),request[i]);

printf("\n\nFIRST FIT MEMORY MANAGEMENT ALOGRITH:");

printf("\n\nPROCESS ARE ALLOCATED AS FOLLOWS:");

if(np<=ns)
{

for(i=0;i<np;i++)
{

count=0;

while(count<ns)
{

if(segments[count]>=request[i] && flag[count]==0)


{
printf("\nThe process p%d is allocated to partision:%d and space
left in the partition:%d",(i+1),(count+1),(segments[count]-
request[i]));
flag[count]=1;
break;
}

count++;
}

if(count==ns)
printf("\nThere is no page available of size process p%d to
allocate!!!!",(i+1));
}
}

else
{
printf("\nThe number of requried pages is more than number of pages
available");
}

printf("\n\nBest FIT MEMORY MANAGEMENT ALOGRITH:");

printf("\n\nPROCESS ARE ALLOCATED AS FOLLOWS:");

for(i=0;i<ns;i++)
{
flag[i]=0;
for(j=0;j<ns;j++)
{

if(segments[i]<segments[j])
{
temp1=segments[i];
segments[i]=segments[j];
segments[j]=temp1;

temp2=position[i];
position[i]=position[j];
position[j]=temp2;

if(np<=ns)
{

for(i=0;i<np;i++)
{

count=0;

while(count<ns)
{

if(segments[count]>=request[i] && flag[count]==0)


{
printf("\nThe process p%d is allocated to partision:%d and space
left in the partition:%d",(i+1),position[count],(segments[count]-
request[i]));
flag[count]=1;
break;
}

count++;
}

if(count==ns)
printf("\nThere is no page available of size process p%d to
allocate!!!!",(i+1));
}
}

else
{
printf("\nThe number of requried pages is more than number of pages
available");
}

getch();
return 0;

}
First Fit
#include<stdio.h>
#include<conio.h>

int main()
{
int ns,np,i,j,count;
int segments[10],request[10],flag[10];
clrscr();

printf("\nEnter the number of Page segments:");


scanf("%d",&ns);

for(i=0;i<ns;i++)
{
printf("\nEnter the Size of page segment%d:",(i+1));
scanf("%d",&segments[i]);
flag[i]=0;
}

printf("\nEnter the number of Process:");


scanf("%d",&np);

for(i=0;i<np;i++)
{
printf("\nEnter the Size of required by segment%d:",(i+1));
scanf("%d",&request[i]);
}
clrscr();
printf("\n\nFIRST FIT MEMORY MANAGEMENT ALOGRITH:");

printf("\n\nSize of page Segments");


for(i=0;i<ns;i++)
printf("\nPage%d:%d",(i+1),segments[i]);

printf("\n\nSize of page Segments REQUESTED");


for(i=0;i<np;i++)
printf("\nProcess P%d:%d",(i+1),request[i]);

printf("\n\nPROCESS ARE ALLOCATED AS FOLLOWS:");

if(np<=ns)
{

for(i=0;i<np;i++)
{
count=0;

while(count<ns)
{

if(segments[count]>=request[i] && flag[count]==0)


{
printf("\nThe process p%d is allocated to partision:%d and space
left in the partition:%d",(i+1),count,(segments[count]-request[i]));
flag[count]=1;
break;
}

count++;
}

if(count==ns)
printf("\nThere is no page available of size process p%d to
allocate!!!!",(i+1));
}
}

else
{
printf("\nThe number of requried pages is more than number of pages
available");
}

getch();
return 0;

FIFO PAGE REPLACEMENT ALGORITHM

#include<stdio.h>
#include<conio.h>

int main()
{
int n,m,i,j,k,a,flag=0,pos,hits=0,flag1=0,pos1=0;
int fr[30][10],p[30];

printf("Enter the no of pages :");


scanf("%d",&n);

printf("Enter the no of frames in memory:");


scanf("%d",&m);

printf("\nEnter the page numbers :\n ");


for(i=0;i<n;i++)
{
scnaf("%d",&p[i]);
}
for(i=0;i<n;i++)
{ for(j=0;j<m;j++)
{
fr[i][j]=0;
}
}

for(i=0;i<n;i++)
{
flag=0;flag1=0;
if(i>0)
{
a=i;
for(k=0;k<m;k++)
{
fr[i][k]=fr[a-1][k];
}
}
for(j=0;j<m;j++)
{
if(p[i]==fr[i][j])
{
flag=1;
hits++; }
}

if(flag==0)
{
for(k=0;k<m;k++)
{
if(fr[i][k]==0)
{ pos=k;
flag1=1;
break;
}
}

if(flag1==1)
{
fr[i][pos]=p[i];
pos1=pos+1;
if(pos1>=m) pos1=0;
}
else
{
fr[i][pos1]=p[i];
pos1=pos1+1;
if(pos1>=m) pos1=0;
}
}
else continue;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d",fr[i][j]);
}
printf("\n");
}
printf("\n\nThe no of page faults is:%d",n-hits);
getch();
return 0;
}

LRU PAGE REPLACEMENT ALGORITHM

#include<conio.h>
#include<stdio.h>

int pa[30];
int fr[30][10];
int i=0;
int nf,np;

int getpos()
{
int j,k;
int min,ret;
int temp[10][2];
for(j=0;j<nf;j++)
temp[j][0]=fr[i-1][j];

for(k=0;k<nf;k++)
for(j=0;j<i;j++)
if(temp[k][0]==pa[j])
{
temp[k][1]=j;
}

min=temp[0][1];
ret=0;
for(j=0;j<nf;j++)
{
if(temp[j][1]<min)
{
min=temp[j][1];
ret=j;
}
}
return ret;
}
void main()
{
int j;
int pos=0,temp,hits=0,flag=0,flag1=0;
printf("Enter number of pages");
scanf("%d",&np);
printf("Enter frame size");
scanf("%d",&nf);

printf("Enter all pages\n");


for(i=0;i<np;i++)
scanf("%d",&pa[i]);

for(i=0;i<np;i++)
for(j=0;j<nf;j++)
fr[i][j]=0;

for(i=0;i<np;i++)
{
flag=0;
flag1=0;
if(i>0)
{
temp=i;
for(j=0;j<nf;j++)
fr[i][j]=fr[temp-1][j];
}

for(j=0;j<nf;j++)
{
if(fr[i][j]==pa[i])
{
hits++;
flag=1;
}
}

if(flag==0)
{
for(j=0;j<nf;j++)
{
if(fr[i][j]==0)
{
flag1=1;
}
}
if(flag1==1)
{
fr[i][pos]=pa[i];
pos+=1;
}
else if(flag1==0)
{
pos=getpos();
fr[i][pos]=pa[i];
}
}
}

clrscr();
printf("\nLEAST RECENTLY USED ALGORITH");
printf("\nFrame1 frame2 frame3\n");
for(i=0;i<np;i++)
{
for(j=0;j<nf;j++)
printf("%d\t",fr[i][j]);
printf("\n");
}
printf("\nNO of page faults:%d",np-hits);
}

Aim:

Simulate all File allocation strategies:


a)Sequential
b)Indexed
c)Linked

Theory

a)Sequential file allocation strategy: In this type of strategy, the files are allocated in a sequential
manner such that there is a continuity among the various parts or fragments of the file.

b)Indexed file allocation strategy: In this type of strategy, the files are allocated based on the indexes
that are created for each fragment of the file such that each and every similar indexed file is
maintained by the primary index thereby providing flow to the file fragments.

c)Linked file allocation strategy: In this type of strategy, the files are allocated in a linked list format
where each and every fragment is linked to the other file through either addresses or pointers. Thus,
the starting location of the file serves for the purpose of extraction of the entire file because every
fragment is linked to each other.

Sequential File Allocation Program:

#include

#include

main()

int f[50],i,st,j,len,c,k;

clrscr();

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

f[i]=0;

X:

printf("\n Enter the starting block & length of file");

scanf("%d%d",&st,&len);

for(j=st;j<(st+len);j++)

if(f[j]==0)

f[j]=1;

printf("\n%d->%d",j,f[j]);

else

printf("Block already allocated");

break;

if(j==(st+len))

printf("\n the file is allocated to disk");

printf("\n if u want to enter more files?(y-1/n-0)");

scanf("%d",&c);
if(c==1)

goto X;

else

exit();

getch();

b)Linked File Allocation Program:

#include

#include

main()

int f[50],p,i,j,k,a,st,len,n,c;

clrscr();

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

f[i]=0;

printf("Enter how many blocks that are already allocated");

scanf("%d",&p);

printf("\nEnter the blocks no.s that are already allocated");

for(i=0;i{

scanf("%d",&a);

f[a]=1;

X:

printf("Enter the starting index block & length");

scanf("%d%d",&st,&len);

k=len;

for(j=st;j<(k+st);j++)

{
if(f[j]==0)

f[j]=1;

printf("\n%d->%d",j,f[j]);

else

printf("\n %d->file is already allocated",j);

k++;

printf("\n If u want to enter one more file? (yes-1/no-0)");

scanf("%d",&c);

if(c==1)

goto X;

else

exit();

getch( );

Output:

Linked File Allocation Strategy

c)Indexed File Allocation Program:

#include

int f[50],i,k,j,inde[50],n,c,count=0,p;

main()

clrscr();

for(i=0;i<50;i++)
f[i]=0;

x:

printf("enter index block\t");

scanf("%d",&p);

if(f[p]==0)

f[p]=1;

printf("enter no of files on index\t");

scanf("%d",&n);

else

printf("Block already allocated\n");

goto x;

for(i=0;iscanf("%d",&inde[i]);

for(i=0;iif(f[inde[i]]==1)

printf("Block already allocated");

goto x;

for(j=0;jf[inde[j]]=1;

printf("\n allocated");

printf("\n file indexed");

for(k=0;kprintf("\n %d->%d:%d",p,inde[k],f[inde[k]]);

printf(" Enter 1 to enter more files and 0 to exit\t");

scanf("%d",&c);

if(c==1)

goto x;

else

exit();
getch();

Output:

Indexed File Allocation Strategy

/* FCFS Disk Scheduling Algorithm *


/ #include #include void main() { int queue[25],n,head,i,j,k,seek=0,diff; float avg; // clrscr();
printf("*** FCFS Disk Scheduling Algorithm ***\n"); printf("Enter the size of Queue\t");
scanf("%d",&n); printf("Enter the Queue\t"); for(i=1;i<=n;i++) { scanf("%d",&queue[i]); } printf("Enter
the initial head position\t"); scanf("%d",&head); queue[0]=head; printf("\n"); for(j=0;j<=n-1;j++)
{ diff=abs(queue[j+1]-queue[j]); seek+=diff; printf("Move from %d to %d with Seek
%d\n",queue[j],queue[j+1],diff); } printf("\nTotal Seek Time is %d\t",seek); avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg); getch(); } /* SCAN Disk Scheduling Algorithm */
#include #include int main() { int i,j,sum=0,n; int d[20]; int disk; //loc of head int temp,max; int
dloc; //loc of disk in array clrscr(); printf("enter number of location\t"); scanf("%d",&n); printf("enter
position of head\t"); scanf("%d",&disk); printf("enter elements of disk queue\n"); for(i=0;id[j])
{ temp=d[i]; d[i]=d[j]; d[j]=temp; } } } max=d[n]; for(i=0;i=0;i--) { printf("%d -->",d[i]); } printf("0 -->");
for(i=dloc+1;i #include int main() { int i,n,j=0,k=0,x=0,l,req[50],mov=0,cp,ub,end,
lower[50],upper[50], temp,a[50]; printf("enter the current position\n"); scanf("%d",&cp);
printf("enter the number of requests\n"); scanf("%d",&n); printf("enter the request order\n");
for(i=0;icp) { upper[k]=req[i]; k++; } } //sort the lower array in reverse order for(i=0;i<=k;i++)
{ for(l=0;lupper[l+1]) { temp=upper[l]; upper[l]=upper[l+1]; upper[l+1]=temp; } } } printf("Enter the
end to which the head is moving (0 - for lower end(zero) and 1 - for upper end\n");
scanf("%d",&end); switch(end) { case 0: for(i=0;i

Program to implement LOOK disk scheduling algorithm /*


Variable description : req[] - array for taking the request, lower[]-array to store and sort elements
lower than current position, upper[]- array to store and sort requests higher than current position of
head, temp-temporary variable to help in sorting, a[]- array to store final order of processing
requests, mov - to calculate total head movement */ #include #include int main() { int
i,n,j=0,k=0,x=0,l,req[50],mov=0,cp,ub,end, lower[50],upper[50], temp,a[50]; printf("enter the current
position\n"); scanf("%d",&cp); printf("enter the number of requests\n"); scanf("%d",&n);
printf("enter the request order\n"); for(i=0;icp) { upper[k]=req[i]; k++; } } //sort the lower array in
reverse order for(i=0;i<=k;i++) { for(l=0;lupper[l+1]) { temp=upper[l]; upper[l]=upper[l+1];
upper[l+1]=temp; } } } printf("Enter the end to which the head is moving (0 - for lower end(zero) and
1 - for upper end\n"); scanf("%d",&end); switch(end) { case 0: for(i=0;i

Das könnte Ihnen auch gefallen