Ques 3
Ques 3
int main() {
int n;
fcfs(processes, n, burst_time);
return 0;
}
2. Non-Preemptive SJF CPU Scheduling Algorithm:
c
Copy code
#include <stdio.h>
int main() {
int n;
sjf(processes, n, burst_time);
return 0;
}
3. Non-Preemptive Priority CPU Scheduling Algorithm:
c
Copy code
#include <stdio.h>
// 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;
}
}
}
int main() {
int n;
return 0;
}
4. Round Robin CPU Scheduling Algorithm:
c
Copy code
#include <stdio.h>
printf("\nProcess\tTurnaround Time\n");
// Process i is completed
completed_processes++;
}
}
}
int main() {
int n, burst_time, time_quantum;
int processes[n];
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;
pthread_mutex_lock(&mutex);
buffer[index++] = item;
pthread_mutex_unlock(&mutex);
int main() {
pthread_t producerThread, consumerThread;
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];
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
pthread_mutex_init(&mutex, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#define NUM_PROCESSES 5
#define NUM_RESOURCES 3
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];
}
}
}
work[i] -= request[i];
allocation[process][i] += request[i];
need[process][i] -= request[i];
finish[process] = true;
if (can_finish) {
finish[j] = true;
for (int k = 0; k < NUM_RESOURCES; ++k) {
work[k] += allocation[j][k];
}
}
}
}
}
int main() {
calculate_need();
int process;
int request[NUM_RESOURCES];
printf("Enter process number (0 to %d): ", NUM_PROCESSES - 1);
scanf("%d", &process);
request_resources(process, request);
return 0;
}