About
SCAN
include<stdio.h>
include <math.h>
int main() { int queue[20],n,head,i,j,k,seek=0,maxi,diff,temp; float avg; printf("Enter the max range of disk\n"); scanf("%d",&maxi); printf("Enter the initial head position\n"); scanf("%d",&head); printf("Enter the size of queue request\n"); scanf("%d",&n); printf("Enter the queue of disk positions to be read\n"); for(i=0;i<n;i++){ scanf("%d",&queue[i]); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(queue[i]>queue[j]){ temp=queue[i]; queue[i]=queue[j]; queue[j]=temp; } } } i=0; while(queue[i]<head){ i++; } diff=abs(head-queue[i]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",head,queue[i],diff); for(j=i;j<n-1;j++){ diff=abs(queue[j+1]-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff); } diff=abs(maxi-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],maxi,diff); i=i-1; diff=abs(maxi-queue[i]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",maxi,queue[i],diff); for(j=i-1;j>-1;j--){ diff=abs(queue[j]-queue[j+1]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j+1],queue[j],diff); } printf("Total seek time is %d\n",seek); avg=seek/(float)n; printf("Average seek time is %f\n",avg); return 0; }
C SCAN
include<stdio.h>
include <math.h>
int main() { int queue[20],n,head,i,j,k,seek=0,maxi,diff,temp; float avg; printf("Enter the max range of disk\n"); scanf("%d",&maxi); printf("Enter the initial head position\n"); scanf("%d",&head); printf("Enter the size of queue request\n"); scanf("%d",&n); printf("Enter the queue of disk positions to be read\n"); for(i=0;i<n;i++){ scanf("%d",&queue[i]); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(queue[i]>queue[j]){ temp=queue[i]; queue[i]=queue[j]; queue[j]=temp; } } } i=0; while(queue[i]<head){ i++; } diff=abs(head-queue[i]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",head,queue[i],diff); for(j=i;j<n-1;j++){ diff=abs(queue[j+1]-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff); } diff=abs(maxi-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],maxi,diff); seek+=maxi; printf("Disk head moves from %d to %d with seek %d\n",maxi,0,maxi); seek+=queue[0]; printf("Disk head moves from %d to %d with seek %d\n",queue[0],queue[0]); for(j=0;j<i-1;j++){ diff=abs(queue[j]-queue[j+1]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff); } printf("Total seek time is %d\n",seek); avg=seek/(float)n; printf("Average seek time is %f\n",avg); return 0; }
LOOK
include<stdio.h>
include <math.h>
int main() { int queue[20],n,head,i,j,k,seek=0,maxi,diff,temp; float avg; printf("Enter the initial head position\n"); scanf("%d",&head); printf("Enter the size of queue request\n"); scanf("%d",&n); printf("Enter the queue of disk positions to be read\n"); for(i=0;i<n;i++){ scanf("%d",&queue[i]); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(queue[i]>queue[j]){ temp=queue[i]; queue[i]=queue[j]; queue[j]=temp; } } } maxi=queue[n-1]; i=0; while(queue[i]<head){ i++; } diff=abs(head-queue[i]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",head,queue[i],diff); for(j=i;j<n-1;j++){ diff=abs(queue[j+1]-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff); } diff=abs(maxi-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],maxi,diff); i=i-1; diff=abs(maxi-queue[i]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",maxi,queue[i],diff); for(j=i-1;j>-1;j--){ diff=abs(queue[j]-queue[j+1]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j+1],queue[j],diff); } printf("Total seek time is %d\n",seek); avg=seek/(float)n; printf("Average seek time is %f\n",avg); return 0; }
C LOOK
include<stdio.h>
include <math.h>
int main() { int queue[20],n,head,i,j,k,seek=0,maxi,diff,temp; float avg; printf("Enter the initial head position\n"); scanf("%d",&head); printf("Enter the size of queue request\n"); scanf("%d",&n); printf("Enter the queue of disk positions to be read\n"); for(i=0;i<n;i++){ scanf("%d",&queue[i]); } for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(queue[i]>queue[j]){ temp=queue[i]; queue[i]=queue[j]; queue[j]=temp; } } } maxi=queue[n-1]; i=0; while(queue[i]<head){ i++; } diff=abs(head-queue[i]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",head,queue[i],diff); for(j=i;j<n-1;j++){ diff=abs(queue[j+1]-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff); } diff=abs(maxi-queue[j]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],maxi,diff); diff=abs(maxi-queue[0]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",maxi,queue[0],diff); for(j=0;j<i-1;j++){ diff=abs(queue[j]-queue[j+1]); seek+=diff; printf("Disk head moves from %d to %d with seek %d\n",queue[j],queue[j+1],diff); } printf("Total seek time is %d\n",seek); avg=seek/(float)n; printf("Average seek time is %f\n",avg); return 0; }
FCFSDISK
include<stdio.h>
include<stdlib.h>
int main()
{
int RQ[100],i,n,totalheadmoment=0,initial;
printf("Enter no.of requests\n");
scanf("%d",&n);
printf("Enter the requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
for(i=0;i<n;i++)
{
totalheadmoment=totalheadmoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is %d", totalheadmoment);
return 0;
}
PRODUCER CONSUMER
// C program for the above approach
include <stdio.h>
include <stdlib.h>
// Initialize a mutex to 1 int mutex = 1;
// Number of full slots as 0 int full = 0;
// Number of empty slots as size // of buffer int empty = 10, x = 0;
// Function to produce an item and // add it to the buffer void producer() { // Decrease mutex value by 1 --mutex;
// Increase the number of full
// slots by 1
++full;
// Decrease the number of empty
// slots by 1
--empty;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
// Increase mutex value by 1
++mutex;
}
// Function to consume an item and // remove it from buffer void consumer() { // Decrease mutex value by 1 --mutex;
// Decrease the number of full
// slots by 1
--full;
// Increase the number of empty
// slots by 1
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;
// Increase mutex value by 1
++mutex;
}
// Driver Code int main() { int n, i; printf("\n1. Press 1 for Producer" "\n2. Press 2 for Consumer" "\n3. Press 3 for Exit");
// Using '#pragma omp parallel for' // can give wrong value due to // synchronization issues.
// 'critical' specifies that code is // executed by only one thread at a // time i.e., only one thread enters // the critical section at a given time
pragma omp critical
for (i = 1; i > 0; i++) {
printf("\nEnter your choice:");
scanf("%d", &n);
// Switch Cases
switch (n) {
case 1:
// If mutex is 1 and empty
// is non-zero, then it is
// possible to produce
if ((mutex == 1)
&& (empty != 0)) {
producer();
}
// Otherwise, print buffer
// is full
else {
printf("Buffer is full!");
}
break;
case 2:
// If mutex is 1 and full
// is non-zero, then it is
// possible to consume
if ((mutex == 1)
&& (full != 0)) {
consumer();
}
// Otherwise, print Buffer
// is empty
else {
printf("Buffer is empty!");
}
break;
// Exit Condition
case 3:
exit(0);
break;
}
}
}
Producer-consumer Problem C using Semaphore and Mutex
include <pthread.h>
include <semaphore.h>
include <stdlib.h>
include <stdio.h>
/ This program provides a possible solution for producer-consumer problem using mutex and semaphore. I have used 5 producers and 5 consumers to demonstrate the solution. You can always play with these values./
define MaxItems 5 // Maximum items a producer can produce or a consumer can consume
define BufferSize 5 // Size of the buffer
sem_t empty; sem_t full; int in = 0; int out = 0; int buffer[BufferSize]; pthread_mutex_t mutex;
void producer(void pno)
{
int item;
for(int i = 0; i < MaxItems; i++) {
item = rand(); // Produce an random item
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
printf("Producer %d: Insert Item %d at %d\n", ((int )pno),buffer[in],in);
in = (in+1)%BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void consumer(void cno)
{
for(int i = 0; i < MaxItems; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
int item = buffer[out];
printf("Consumer %d: Remove Item %d from %d\n",((int )cno),item, out);
out = (out+1)%BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main() {
pthread_t pro[5],con[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&empty,0,BufferSize);
sem_init(&full,0,0);
int a[5] = {1,2,3,4,5}; //Just used for numbering the producer and consumer
for(int i = 0; i < 5; i++) {
pthread_create(&pro[i], NULL, (void *)producer, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) {
pthread_create(&con[i], NULL, (void *)consumer, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) {
pthread_join(pro[i], NULL);
}
for(int i = 0; i < 5; i++) {
pthread_join(con[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
Dining philosopher:
include<stdio.h>
include<stdlib.h>
include<pthread.h>
include<semaphore.h>
include<unistd.h>
sem_t chopstick[5]; void philos(void ); void eat(int); int main() { int i,n[5]; pthread_t T[5]; for(i=0;i<5;i++) sem_init(&chopstick[i],0,1); for(i=0;i<5;i++){ n[i]=i; pthread_create(&T[i],NULL,philos,(void )&n[i]); } for(i=0;i<5;i++) pthread_join(T[i],NULL); } void philos(void n) { int ph=(int *)n; printf("Philosopher %d wants to eat\n",ph); printf("Philosopher %d tries to pick left chopstick\n",ph); sem_wait(&chopstick[ph]); printf("Philosopher %d picks the left chopstick\n",ph); printf("Philosopher %d tries to pick the right chopstick\n",ph); sem_wait(&chopstick[(ph+1)%5]); printf("Philosopher %d picks the right chopstick\n",ph); eat(ph); sleep(2); printf("Philosopher %d has finished eating\n",ph); sem_post(&chopstick[(ph+1)%5]); printf("Philosopher %d leaves the right chopstick\n",ph); sem_post(&chopstick[ph]); printf("Philosopher %d leaves the left chopstick\n",ph); } void eat(int ph) { printf("Philosopher %d begins to eat\n",ph); }
C++ Implementation Of reader writer problem using semaphore
include<semaphore.h>
include<stdio.h>
include<pthread.h>
include<bits/stdc++.h>
using namespace std;
void reader(void ); void writer(void );
int readcount=0,writecount=0,sh_var=5,bsize[5]; sem_t x,y,z,rsem,wsem; pthread_t r[3],w[2];
void reader(void i) { cout << "\n-------------------------"; cout << "\n\n reader-" << i << " is reading";
sem_wait(&z);
sem_wait(&rsem);
sem_wait(&x);
readcount++;
if(readcount==1)
sem_wait(&wsem);
sem_post(&x);
sem_post(&rsem);
sem_post(&z);
cout << "\nupdated value :" << sh_var;
sem_wait(&x);
readcount--;
if(readcount==0)
sem_post(&wsem);
sem_post(&x);
}
void writer(void i) { cout << "\n\n writer-" << i << "is writing"; sem_wait(&y); writecount++; if(writecount==1) sem_wait(&rsem); sem_post(&y); sem_wait(&wsem);
sh_var=sh_var+5;
sem_post(&wsem);
sem_wait(&y);
writecount--;
if(writecount==0)
sem_post(&rsem);
sem_post(&y);
}
int main() { sem_init(&x,0,1); sem_init(&wsem,0,1); sem_init(&y,0,1); sem_init(&z,0,1); sem_init(&rsem,0,1);
pthread_create(&r[0],NULL,(void *)reader,(void *)0);
pthread_create(&w[0],NULL,(void *)writer,(void *)0);
pthread_create(&r[1],NULL,(void *)reader,(void *)1);
pthread_create(&r[2],NULL,(void *)reader,(void *)2);
pthread_create(&r[3],NULL,(void *)reader,(void *)3);
pthread_create(&w[1],NULL,(void *)writer,(void *)3);
pthread_create(&r[4],NULL,(void *)reader,(void *)4);
pthread_join(r[0],NULL);
pthread_join(w[0],NULL);
pthread_join(r[1],NULL);
pthread_join(r[2],NULL);
pthread_join(r[3],NULL);
pthread_join(w[1],NULL);
pthread_join(r[4],NULL);
return(0);
}
Reader Process
wait (mutex); rc ++; if (rc == 1) wait (wrt); signal(mutex); . . READ THE OBJECT . wait(mutex); rc --; if (rc == 0) signal (wrt); signal(mutex);
Writer Process
wait(wrt); . . WRITE INTO THE OBJECT . signal(wrt);