Ass 3
Ass 3
Soln->
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t child_pid;
if (child_pid < 0) {
// Fork failed
fprintf(stderr, "Fork failed\n");
return 1;
} else if (child_pid == 0) {
// Child process
printf("Child process PID: %d\n", getpid());
// Child process terminates
exit(0);
} else {
// Parent process
printf("Parent process PID: %d\n", getpid());
printf("Waiting for child process to complete...\n");
// Wait for the child process to complete
wait(NULL);
printf("Child process has completed.\n");
}
return 0;
}
Q2) Write a C program to demonstrate the concept of process synchronization
using semaphores or mutex locks. Implement a scenario where multiple processes
are trying to access a shared resource. Use semaphores or mutex locks to ensure
that only one process can access the shared resource at a time.
Soln- >
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
// Function to represent the critical section where the shared resource is accessed
void *access_shared_resource(void *thread_id) {
long tid = (long)thread_id;
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
long t;
return 0;
}
Q3) Implement a simple producer consumer problem using shared memory or message queues.
Create two separate programs :one for the producer and one for the consumer. The producer
should generate data items and store them in a shared memory segment or send them through
a message queue. The consumer should retrieve data items from the shared memory segment
or message queue and process them
Soln->
(Producer Code)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
data = shm;
return 0;
}
(Consumer Code)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
int shmid;
key_t key;
char *shm, *data;
// Generate the same key used by the producer to access the shared memory segment
key = ftok(".", 'X');
data = shm;
printf("\n");
return 0;
}
Q4) Simulate different scheduling algorithms such as first come first serve(FCFS), Shortest job
first (SJF) , round robin (RR) and priority scheduling (PSC)
Soln)
// Display results
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
int main() {
int bt[] = {10, 5, 8, 12};
int n = sizeof(bt) / sizeof(bt[0]);
fcfs(bt, n);
return 0;
}
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
}
}
}
// Display results
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", p[i] + 1, bt[p[i]], wt[i], tat[i]);
}
int main() {
int bt[] = {6, 3, 8, 7, 4};
int n = sizeof(bt) / sizeof(bt[0]);
sjf(bt, n);
return 0;
}
#define MAX_PROCESS 10
// Print results
printf("Process\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\n", processes[i].pid, processes[i].rt, processes[i].bt + processes[i].rt);
}
int main() {
int n, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
roundRobin(processes, n, quantum);
return 0;
}
#define MAX_PROCESS 10
// Print results
printf("Process\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].bt, processes[i].priority, wt[i], tat[i]);
}
int main()
{ int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
priorityScheduling(processes, n);
return 0;
}
Q5) Write a program to create multiple processes with different CPU burst times and priorities.
Implement each scheduling algorithm separately and compare their performance based on
metrics such as average waiting time , turn around time etc
Soln->
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS 10
// Print results
printf("First Come First Serve (FCFS) Scheduling:\n");
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].bt, wt[i], tat[i]);
}
// Print results
printf("Shortest Job First (SJF) Scheduling:\n");
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].bt, wt[i], tat[i]);
}
// Print results
printf("Priority Scheduling:\n");
printf("Process\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].bt, processes[i].priority, wt[i], tat[i]);
}
// Print results
printf("Round Robin (RR) Scheduling with Quantum %d:\n", quantum);
printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].bt, wt[i], tat[i]);
}
int main() {
int n, quantum;
struct Process processes[MAX_PROCESS];