Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ques 3

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

1.

Non-Preemptive FCFS CPU Scheduling Algorithm:


c
Copy code
#include <stdio.h>

void fcfs(int processes[], int n, int burst_time[]) {


int waiting_time = 0, turnaround_time = 0;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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


turnaround_time += burst_time[i];
printf("%d\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i], waiting_time,
turnaround_time);
waiting_time += burst_time[i];
}

printf("\nAverage Waiting Time: %.2f\n", (float)waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)turnaround_time / n);
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

int processes[n], burst_time[n];

printf("Enter burst time for each process:\n");


for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
processes[i] = i + 1;
}

fcfs(processes, n, burst_time);

return 0;
}
2. Non-Preemptive SJF CPU Scheduling Algorithm:
c
Copy code
#include <stdio.h>

void sjf(int processes[], int n, int burst_time[]) {


int waiting_time = 0, turnaround_time = 0;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

// Sorting processes based on burst time (Shortest Job First)


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (burst_time[j] > burst_time[j + 1]) {
// Swap burst times
int temp = burst_time[j];
burst_time[j] = burst_time[j + 1];
burst_time[j + 1] = temp;
// Swap processes
temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}

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


turnaround_time += burst_time[i];
printf("%d\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i], waiting_time,
turnaround_time);
waiting_time += burst_time[i];
}

printf("\nAverage Waiting Time: %.2f\n", (float)waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)turnaround_time / n);
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

int processes[n], burst_time[n];

printf("Enter burst time for each process:\n");


for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
processes[i] = i + 1;
}

sjf(processes, n, burst_time);

return 0;
}
3. Non-Preemptive Priority CPU Scheduling Algorithm:
c
Copy code
#include <stdio.h>

void priority(int processes[], int n, int burst_time[], int priority[]) {


int waiting_time = 0, turnaround_time = 0;

printf("\nProcess\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");

// Sorting processes based on priority


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (priority[j] > priority[j + 1]) {
// Swap burst times
int temp = burst_time[j];
burst_time[j] = burst_time[j + 1];
burst_time[j + 1] = temp;

// Swap priorities
temp = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = temp;

// Swap processes
temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}

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


turnaround_time += burst_time[i];
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], burst_time[i],
priority[i], waiting_time, turnaround_time);
waiting_time += burst_time[i];
}

printf("\nAverage Waiting Time: %.2f\n", (float)waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)turnaround_time / n);
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

int processes[n], burst_time[n], priority[n];

printf("Enter burst time and priority for each process:\n");


for (int i = 0; i < n; i++) {
printf("Process %d (Burst Time Priority): ", i + 1);
scanf("%d %d", &burst_time[i], &priority[i]);
processes[i] = i + 1;
}

priority(processes, n, burst_time, priority);

return 0;
}
4. Round Robin CPU Scheduling Algorithm:
c
Copy code
#include <stdio.h>

void round_robin(int processes[], int n, int burst_time, int time_quantum) {


int waiting_time = 0, turnaround_time = 0;
int remaining_time[n];

// Initialize remaining time with burst time


for (int i = 0; i < n; i++) {
remaining_time[i] = burst_time;
}

printf("\nProcess\tTurnaround Time\n");

int t = 0; // Current time


int completed_processes = 0;
// Continue until all processes are completed
while (completed_processes < n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0) {
// Process i gets the CPU for time quantum or remaining time,
whichever is smaller
int execute_time = (remaining_time[i] < time_quantum) ?
remaining_time[i] : time_quantum;

// Update remaining time for the process


remaining_time[i] -= execute_time;

// Update current time


t += execute_time;

// Update waiting and turnaround times for the process


waiting_time += t - execute_time;
turnaround_time += t;

printf("%d\t%d\n", processes[i], turnaround_time);

// Process i is completed
completed_processes++;
}
}
}

printf("\nAverage Turnaround Time: %.2f\n", (float)turnaround_time / n);


}

int main() {
int n, burst_time, time_quantum;

printf("Enter the number of processes: ");


scanf("%d", &n);

int processes[n];

printf("Enter burst time for each process: ");


scanf("%d", &burst_time);

printf("Enter time quantum for Round Robin: ");


scanf("%d", &time_quantum);

printf("Enter process IDs (1 to %d) for Round Robin: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &processes[i]);
}

round_robin(processes, n, burst_time, time_quantum);

return 0;
}

5).
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_SIZE 5

int buffer[BUFFER_SIZE];
int index = 0;

int producerFlag = 1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* producer(void* arg) {


while (producerFlag) {
if (index < BUFFER_SIZE) {
int item = rand() % 100; // replace with your data source

pthread_mutex_lock(&mutex);
buffer[index++] = item;
pthread_mutex_unlock(&mutex);

printf("Produced: %d\n", item);


}
}
pthread_exit(NULL);
}

void* consumer(void* arg) {


while (1) {
if (index > 0) {
pthread_mutex_lock(&mutex);
int item = buffer[--index];
pthread_mutex_unlock(&mutex);

printf("Consumed: %d\n", item);


}
}
}

int main() {
pthread_t producerThread, consumerThread;

pthread_create(&producerThread, NULL, producer, NULL);


pthread_create(&consumerThread, NULL, consumer, NULL);

// Sleep for a while to allow some production and consumption


sleep(2);

// Set flag to stop producer


producerFlag = 0;

pthread_join(producerThread, NULL);
pthread_join(consumerThread, NULL);

pthread_mutex_destroy(&mutex);

return 0;
}

6).
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define NUM_PHILOSOPHERS 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (philosopher_id + NUM_PHILOSOPHERS - 1) % NUM_PHILOSOPHERS
#define RIGHT (philosopher_id + 1) % NUM_PHILOSOPHERS

int state[NUM_PHILOSOPHERS];
pthread_mutex_t mutex;
pthread_cond_t condition[NUM_PHILOSOPHERS];

void test(int philosopher_id) {


if (state[philosopher_id] == HUNGRY &&
state[LEFT] != EATING &&
state[RIGHT] != EATING) {
state[philosopher_id] = EATING;
printf("Philosopher %d is Eating\n", philosopher_id);
pthread_cond_signal(&condition[philosopher_id]);
}
}

void take_forks(int philosopher_id) {


pthread_mutex_lock(&mutex);
state[philosopher_id] = HUNGRY;
printf("Philosopher %d is Hungry\n", philosopher_id);
test(philosopher_id);
while (state[philosopher_id] != EATING)
pthread_cond_wait(&condition[philosopher_id], &mutex);
pthread_mutex_unlock(&mutex);
}

void put_forks(int philosopher_id) {


pthread_mutex_lock(&mutex);
state[philosopher_id] = THINKING;
printf("Philosopher %d is Thinking\n", philosopher_id);
test(LEFT);
test(RIGHT);
pthread_mutex_unlock(&mutex);
}

void* philosopher(void* arg) {


int philosopher_id = *((int*)arg);
while (1) {
sleep(rand() % 3 + 1); // thinking
take_forks(philosopher_id);
sleep(rand() % 3 + 1); // eating
put_forks(philosopher_id);
}
}

int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
pthread_mutex_init(&mutex, NULL);

for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {


pthread_cond_init(&condition[i], NULL);
philosopher_ids[i] = i;
pthread_create(&philosophers[i], NULL, philosopher, &philosopher_ids[i]);
}

for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {


pthread_join(philosophers[i], NULL);
pthread_cond_destroy(&condition[i]);
}

pthread_mutex_destroy(&mutex);

return 0;
}

#include <stdio.h>
#include <stdbool.h>

#define NUM_PROCESSES 5
#define NUM_RESOURCES 3

int available[NUM_RESOURCES] = {10, 5, 7};


int maximum[NUM_PROCESSES][NUM_RESOURCES] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2,
2, 2}, {4, 3, 3}};
int allocation[NUM_PROCESSES][NUM_RESOURCES] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2},
{2, 1, 1}, {0, 0, 2}};
int need[NUM_PROCESSES][NUM_RESOURCES];

void calculate_need() {
for (int i = 0; i < NUM_PROCESSES; ++i) {
for (int j = 0; j < NUM_RESOURCES; ++j) {
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
}

bool is_safe(int process, int request[]) {


int work[NUM_RESOURCES];
int finish[NUM_PROCESSES];

for (int i = 0; i < NUM_RESOURCES; ++i) {


work[i] = available[i];
}

for (int i = 0; i < NUM_PROCESSES; ++i) {


finish[i] = false;
}

// Try to allocate resources


for (int i = 0; i < NUM_RESOURCES; ++i) {
if (request[i] > need[process][i] || request[i] > work[i]) {
return false; // Request exceeds need or available resources
}

work[i] -= request[i];
allocation[process][i] += request[i];
need[process][i] -= request[i];
finish[process] = true;

// Check if the new state is safe


for (int j = 0; j < NUM_PROCESSES; ++j) {
if (!finish[j]) {
bool can_finish = true;
for (int k = 0; k < NUM_RESOURCES; ++k) {
if (need[j][k] > work[k]) {
can_finish = false;
break;
}
}

if (can_finish) {
finish[j] = true;
for (int k = 0; k < NUM_RESOURCES; ++k) {
work[k] += allocation[j][k];
}
}
}
}
}

// Check if all processes can finish


for (int i = 0; i < NUM_PROCESSES; ++i) {
if (!finish[i]) {
return false;
}
}

return true; // Request can be granted safely


}

void request_resources(int process, int request[]) {


if (is_safe(process, request)) {
printf("Request granted for Process %d\n", process);
printf("New allocation matrix:\n");

for (int i = 0; i < NUM_PROCESSES; ++i) {


for (int j = 0; j < NUM_RESOURCES; ++j) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}

for (int i = 0; i < NUM_RESOURCES; ++i) {


available[i] -= request[i];
}
} else {
printf("Request denied for Process %d\n", process);
}
}

int main() {
calculate_need();

int process;
int request[NUM_RESOURCES];
printf("Enter process number (0 to %d): ", NUM_PROCESSES - 1);
scanf("%d", &process);

printf("Enter resource request (e.g., R1 R2 R3): ");


for (int i = 0; i < NUM_RESOURCES; ++i) {
scanf("%d", &request[i]);
}

request_resources(process, request);

return 0;
}

You might also like