4 pr fork()
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
if (fork() == 0) {
// Child
printf("Child PID: %d\n", getpid());
printf("Parent PID: %d\n", getppid());
} else {
// Parent
wait(NULL);
printf("Parent PID: %d\n", getpid());
}
return 0;
}
6 : Write a C program to implement a solution of producer consumer problem using
semaphores.
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
} int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit"); while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
else
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
8 th Write a program to demonstrate the concept of MVT and MFT memory
management techniques.
MVT.
PROGRAM:
#include<stdio.h>
void main()
{
int m=0,m1=0,m2=0,p,count=0,i;
printf("enter the memory capacity:");
scanf("%d",&m);
printf("enter the no of processes:");he concept of MVT and MFT memory management
techniques.
scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("\nenter memory req for process%d: ",i+1);
scanf("%d",&m1);
count=count+m1;
if(m1<=m)
{
if(count==m)
printf("there is no further memory remaining:");
printf("the memory allocated for process%d is: %d ",i+1,m);
m2=m-m1;
printf("\nremaining memory is: %d",m2);
m=m2;
}
else
{
printf("memory is not allocated for process%d",i+1);
}
printf("\nexternal fragmentation for this process is:%d",m2);
}
}
A Program to simulate the MFT
PROGRAM:
#include<stdio.h>
int main()
{
int m,p,s,p1;
int m1[4],i,f,f1=0,f2=0,fra1,fra2,s1,pos;
printf("Enter the memory size:");
scanf("%d",&m);
printf("Enter the no of partitions:");
scanf("%d",&p);
s=m/p;
printf("Each partn size is:%d",s);
printf("\nEnter the no of processes:");
scanf("%d",&p1);
pos=m;
for(i=0;i<p1;i++)
{
if(pos<s)
{
printf("\nThere is no further memory for process%d",i+1);
m1[i]=0;
break;
}
else
{
printf("\nEnter the memory req for process%d:",i+1);
scanf("%d",&m1[i]);
if(m1[i]<=s)
{
printf("\nProcess is allocated in partition%d",i+1);
fra1=s-m1[i];
printf("\nInternal fragmentation for process is:%d",fra1);
f1=f1+fra1;
pos=pos-s;
}
else
{
printf("\nProcess not allocated in partition%d",i+1);
s1=m1[i];
while(s1>s)
{
s1=s1-s;
pos=pos-s;
}
pos=pos-s;
fra2=s-s1;
f2=f2+fra2;
printf("\nExternal Fragmentation for this process is:%d",fra2);
}
}
}
printf("\nProcess\t allocate memory");
for(i=0;i<p1;i++)
printf("\n%5d\t%5d",i+1,m1[i]);
f=f1+f2;
printf("\nThe tot no of fragmentation is:%d",f);
return 0;
}
10 Write a program of FCFS disk scheduling Algorithm in C.
#include <stdio.h>
void fcfs_disk_scheduling(int queue[], int head, int n)
{
//Resultant variable which are used to store information of number of time or operation
double seek_time = 0.0;
int distance = 0;
// Loop controlling variable
int i = 0;
printf("\n Starting Head : %d ", head);
printf("\n Queue Sequence : ");
//Display given queue elements
for (i = 0; i < n; i++)
{
printf(" %d", queue[i]);
}
for (i = 0; i < n; i++)
{
//Get distance
distance = queue[i] - head;
if (distance < 0)
{
//When distance is negative then convert into it absolute value
distance = -distance;
}
// Update head data into current track value
head = queue[i];
// Add current distance into seek
seek_time += distance;
}
//Display result
printf("\n Total Seek Time : %lf", seek_time);
printf("\n Average Seek Time : %lf\n", seek_time / n);
}
int main()
{
// Request queue elements
int queue[] = {
64,
12,
42,
19,
62,
32,
16,
86
};
//Get the number of elements in request queue
int n = sizeof(queue) / sizeof(queue[0]);
//Initial head position
int head = 25;
fcfs_disk_scheduling(queue, head, n);
return 0;
}