Gpcet OS Lab Manual
Gpcet OS Lab Manual
b)SJF
#include <stdio.h>
int main()
{
int n, i, j;
int bt[20], wt[20], tat[20], p[20];
float avg_wt = 0, avg_tat = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter burst times:\n");
for(i = 0; i < n; i++)
{
printf("P%d: ", i+1);
scanf("%d", &bt[i]);
p[i] = i+1; // Process number
}
// Sort processes by burst time
for(i = 0; i < n-1; i++)
{
for(j = i+1; j < n; j++)
{
if(bt[i] > bt[j])
{
int temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
wt[0] = 0;
// Calculate waiting time
for(i = 1; i < n; i++)
wt[i] = wt[i-1] + bt[i-1];
// Calculate turnaround time
for(i = 0; i < n; i++)
tat[i] = wt[i] + bt[i];
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i = 0; i < n; i++)
{
printf("P%d\t%d\t\t%d\t\t%d\n", p[i], bt[i], wt[i], tat[i]);
avg_wt += wt[i];
avg_tat += tat[i];
}
printf("\nAverage Waiting Time = %.2f", avg_wt/n);
printf("\nAverage Turnaround Time = %.2f\n", avg_tat/n);
return 0;
}
OutPut:
Enter number of processes: 4
Enter burst times:
P1: 6
P2: 8
P3: 7
P4: 3
c)Priority
#include <stdio.h>
int main()
{
int n, i, j;
int bt[20], wt[20], tat[20], p[20], pr[20];
float avg_wt = 0, avg_tat = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter Burst Time and Priority:\n");
for(i = 0; i < n; i++)
{
printf("P%d Burst Time: ", i+1);
scanf("%d", &bt[i]);
printf("P%d Priority: ", i+1);
scanf("%d", &pr[i]);
p[i] = i+1;
}
// Sort by priority
for(i = 0; i < n-1; i++)
{
for(j = i+1; j < n; j++)
{
if(pr[i] > pr[j])
{ // smaller number = higher priority
int temp;
temp = pr[i];
pr[i] = pr[j];
pr[j] = temp;
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
wt[0] = 0;
// Calculate Waiting Time
for(i = 1; i < n; i++)
wt[i] = wt[i-1] + bt[i-1];
// Calculate Turnaround Time
for(i = 0; i < n; i++)
tat[i] = wt[i] + bt[i];
printf("\nProcess\tBurst\tPriority\tWaiting\tTurnaround\n");
for(i = 0; i < n; i++)
{
printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i], bt[i], pr[i], wt[i], tat[i]);
avg_wt += wt[i];
avg_tat += tat[i];
}
printf("\nAverage Waiting Time = %.2f", avg_wt/n);
printf("\nAverage Turnaround Time = %.2f\n", avg_tat/n);
return 0;
}
OutPut:
Enter number of processes: 4
Enter Burst Time and Priority:
P1 Burst Time: 5
P1 Priority: 2
P2 Burst Time: 3
P2 Priority: 1
P3 Burst Time: 8
P3 Priority: 4
P4 Burst Time: 6
P4 Priority: 3
sem_t port_semaphore;
// Port allocated
printf("Process %d allocated a port\n", pid);
sleep(2); // Simulate port usage
// Release port
sem_post(&port_semaphore);
return NULL;
}
int main() {
pthread_t threads[TOTAL_PROCESSES];
int pids[TOTAL_PROCESSES];
return 0;
}
Output:
Process 1 requesting a port...
Process 1 allocated a port
Process 2 requesting a port...
Process 2 allocated a port
Process 3 requesting a port...
Process 4 requesting a port...
Process 5 requesting a port...
Process 2 releasing the port
Process 1 releasing the port
Process 3 allocated a port
Process 4 allocated a port
Process 3 releasing the port
Process 5 allocated a port
Process 4 releasing the port
Process 5 releasing the port
b)Monitors.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define PORTS 2
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
int available = PORTS;
pthread_mutex_lock(&m);
while (available == 0)
pthread_cond_wait(&c, &m);
available--;
printf("Process %d got port\n", id);
pthread_mutex_unlock(&m);
pthread_mutex_lock(&m);
available++;
printf("Process %d released port\n", id);
pthread_cond_signal(&c);
pthread_mutex_unlock(&m);
return NULL;
}
int main() {
pthread_t t[4];
int id[4] = {1,2,3,4};
return 0;
}
Output:
Process 1 got port
Process 2 got port
Process 1 released port
Process 3 got port
Process 2 released port
Process 4 got port
Process 3 released port
Process 4 released port
6. Write a program to illustrate concurrent execution of threads using pthreads
library.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// Thread function
void* thread_function(void* arg)
{
int thread_id = *(int*)arg;
for (int i = 1; i <= 5; i++)
{
printf("Thread %d is executing iteration %d\n", thread_id, i);
sleep(1); // simulate work
}
pthread_exit(NULL);
}
int main()
{
pthread_t t1, t2;
int id1 = 1, id2 = 2;
// Create threads
pthread_create(&t1, NULL, thread_function, &id1);
pthread_create(&t2, NULL, thread_function, &id2);
// Wait for threads to finish
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Both threads have finished execution.\n");
return 0;
}
Output:
Thread 1 is executing iteration 1
Thread 2 is executing iteration 1
Thread 1 is executing iteration 2
Thread 2 is executing iteration 2
Thread 1 is executing iteration 3
Thread 2 is executing iteration 3
Thread 1 is executing iteration 4
Thread 2 is executing iteration 4
Thread 1 is executing iteration 5
Thread 2 is executing iteration 5
Both threads have finished execution.
7. Write A Program To Solve Producer-consumer problem using Semaphores.
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
// Semaphores
sem_t empty, full, mutex;
// Producer thread
void* producer(void* arg)
{
int item;
for (item = 1; item <= 5; item++)
{
sem_wait(&empty); // wait for empty slot
sem_wait(&mutex); // enter critical section
buffer[in] = item;
printf("Producer produced: %d\n", item);
in = (in + 1) % BUFFER_SIZE;
sem_post(&mutex); // exit critical section
sem_post(&full); // increment full slots
sleep(1);
}
return NULL;
}
// Consumer thread
void* consumer(void* arg)
{
int item;
for (int i = 1; i <= 5; i++)
{
sem_wait(&full); // wait for item
sem_wait(&mutex); // enter critical section
item = buffer[out];
printf("Consumer consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE;
sem_post(&mutex); // exit critical section
sem_post(&empty); // increment empty slots
sleep(1);
}
return NULL;
}
int main()
{
pthread_t prod, cons;
// Initialize semaphores
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
// Create threads
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
// Wait for threads
pthread_join(prod, NULL);
pthread_join(cons, NULL);
// Destroy semaphores
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);
return 0;
}
Output:
Producer produced: 1
Consumer consumed: 1
Producer produced: 2
Consumer consumed: 2
Producer produced: 3
Consumer consumed: 3
Producer produced: 4
Consumer consumed: 4
Producer produced: 5
Consumer consumed: 5
8. Implement the following memory allocation methods for fixed partition
a) First fit
#include <stdio.h>
int main()
{
int m, p, i, j;
int blockSize[10], processSize[10];
int allocation[10];
// Initialize allocation array
for (i = 0; i < 10; i++)
{
allocation[i] = -1;
}
// Input number of memory blocks
printf("Enter number of memory blocks: ");
scanf("%d", &m);
printf("Enter size of each memory block:\n");
for (i = 0; i < m; i++)
{
scanf("%d", &blockSize[i]);
}
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &p);
printf("Enter size of each process:\n");
for (i = 0; i < p; i++)
{
scanf("%d", &processSize[i]);
}
// First Fit Allocation
for (i = 0; i < p; i++)
{
for (j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
allocation[i] = j;
blockSize[j] = 0; // Mark block as allocated
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < p; i++)
{
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
return 0;
}
Output:
Enter number of memory blocks: 5
Enter size of each memory block:
100 500 200 300 600
Enter number of processes: 4
Enter size of each process:
212 417 112 426
b)Worst fit
#include <stdio.h>
int main()
{
int m, p, i, j;
int blockSize[10], processSize[10];
int allocation[10];
int worstIndex;
// Initialize allocation array
for (i = 0; i < 10; i++)
{
allocation[i] = -1;
}
// Input number of memory blocks
printf("Enter number of memory blocks: ");
scanf("%d", &m);
printf("Enter size of each memory block:\n");
for (i = 0; i < m; i++)
{
scanf("%d", &blockSize[i]);
}
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &p);
printf("Enter size of each process:\n");
for (i = 0; i < p; i++)
{
scanf("%d", &processSize[i]);
}
// Worst Fit Allocation
for (i = 0; i < p; i++)
{
worstIndex = -1;
for (j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (worstIndex == -1 || blockSize[j] > blockSize[worstIndex])
{
worstIndex = j;
}
}
}
if (worstIndex != -1)
{
allocation[i] = worstIndex;
blockSize[worstIndex] = 0; // Mark block as allocated
}
}
// Output
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < p; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
return 0;
}
Output:
Enter number of memory blocks: 5
Enter size of each memory block:
100
500
200
300
600
Enter number of processes: 4
Enter size of each process:
212
417
112
426
c)Bestfit
#include <stdio.h>
int main()
{
int m, p, i, j;
int blockSize[10], processSize[10];
int allocation[10];
int bestIndex;
// Initialize allocation array
for (i = 0; i < 10; i++)
{
allocation[i] = -1;
}
// Input number of memory blocks
printf("Enter number of memory blocks: ");
scanf("%d", &m);
printf("Enter size of each memory block:\n");
for (i = 0; i < m; i++)
{
scanf("%d", &blockSize[i]);
}
// Input number of processes
printf("Enter number of processes: ");
scanf("%d", &p);
printf("Enter size of each process:\n");
for (i = 0; i < p; i++)
{
scanf("%d", &processSize[i]);
}
// Best Fit Allocation
for (i = 0; i < p; i++)
{
bestIndex = -1;
for (j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (bestIndex == -1 || blockSize[j] < blockSize[bestIndex])
{
bestIndex = j;
}
}
}
if (bestIndex != -1)
{
allocation[i] = bestIndex;
blockSize[bestIndex] = 0; // Mark block as allocated
}
}
// Output
printf("\nProcess No.\tProcess Size\tBlock No.\n");
for (i = 0; i < p; i++)
{
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
return 0;
}
Output:
Enter number of memory blocks: 5
Enter size of each memory block:
100
500
200
300
600
Enter number of processes: 4
Enter size of each process:
212
417
112
426
4
5
3
Enter number of frames: 3
Page Frames
1 1 - -
2 1 2 -
3 1 2 3
4 4 2 3
1 4 1 3
2 4 1 2
5 5 1 2
1 5 1 2
2 5 1 2
4 4 1 2
5 4 5 2
3 4 5 3
Page Frames
1 1 - -
2 1 2 -
3 1 2 3
4 4 2 3
1 1 2 3
2 1 2 3
5 5 2 3
1 1 2 3
2 1 2 3
3 1 2 3
4 4 2 3
5 5 2 3
int main() {
int n, i, j;
int request[n];
int visited[n];
int head;
printf("Enter initial head position: ");
scanf("%d", &head);
int total_seek_time = 0;
int current = head;
visited[index] = 1;
total_seek_time += abs(current - request[index]);
current = request[index];
return 0;
}
OutPut:
Enter number of requests: 8
Enter the request sequence:
98 183 37 122 14 124 65 67
Enter initial head position: 53
Seek Sequence: 53 -> 65 -> 67 -> 37 -> 14 -> 98 -> 122 -> 124 -> 183
int main() {
int n, head, size, direction;
int request[n];
sort(request, n);
int total_seek = 0;
int current = head;
// Move to end
total_seek += abs(current - (size - 1));
current = size - 1;
printf(" -> %d", current);
// Move to beginning
total_seek += abs(current - 0);
current = 0;
printf(" -> %d", current);
return 0;
}
OutPut:
Enter number of requests: 8
Enter request sequence:
98 183 37 122 14 124 65 67
Enter initial head position: 53
Enter total disk size (max cylinder number): 200
Enter direction (0 = left, 1 = right): 1
Seek Sequence: 53 -> 65 -> 67 -> 98 -> 122 -> 124 -> 183 -> 199 -> 37 -> 14
int main() {
int fd;
char buffer[50];
pid_t pid;
lseek(fd, 0, SEEK_SET);
read(fd, buffer, 13);
return 0;
}
OutPut:
Child Process ID: 2924
Child Read: Parent Process ID: 2923
pthread_mutex_t chopstick[N];
sem_t room; // To avoid deadlock (only 4 philosophers allowed at a time)
while(1) {
printf("Philosopher %d is Thinking\n", id);
sleep(1);
int main() {
pthread_t ph[N];
int id[N];
return 0;
}
OutPut:
Philosopher 0 is Thinking
Philosopher 1 is Thinking
Philosopher 2 is Thinking
Philosopher 3 is Thinking
Philosopher 4 is Thinking
Philosopher 0 is Eating
Philosopher 2 is Eating
Philosopher 2 is Thinking….