C CODE
C CODE
#include<stdio.h>
int main()
{
int bt[20], p[20], wt[20], tat[20], pr[20], i, j, n, total = 0, pos, temp,
avg_wt, avg_tat;
printf("Enter Total Number of Process:");
scanf("%d", &n);
temp = pr[i];
pr[i] = pr[pos];
pr[pos] = temp;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
}
wt[0] = 0;
for(i = 1; i < n; i++)
{
wt[i] = 0;
for(j = 0; j < i; j++)
wt[i] += bt[j];
total += wt[i];
}
avg_wt = total / n;
total = 0;
avg_tat = total / n;
printf("\n\nAverage Waiting Time=%d", avg_wt);
printf("\nAverage Turnaround Time=%d\n", avg_tat);
return 0;
}
#include<stdio.h>
int main() {
int bt[20], p[20], wt[20], tat[20], i, j, n, total = 0, pos, temp;
float avg_wt, avg_tat;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
}
wt[0] = 0;
avg_wt = (float)total / n;
total = 0;
avg_tat = (float)total / n;
printf("\nAverage Waiting Time = %.2f", avg_wt);
printf("\nAverage Turnaround Time = %.2f\n", avg_tat);
return 0;
}
ROUND ROBIN WITHOUT WAITING TIME:
#include <stdio.h>
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
int rem_bt[n];
rem_bt[i] = bt[i];
while (1)
int done = 1;
if (rem_bt[i] > 0)
done = 0;
t += quantum;
rem_bt[i] -= quantum;
else
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
if (done == 1)
break;
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
total_wt += wt[i];
total_tat += tat[i];
int main()
return 0;
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
//creates shared memory segment with key 2345, having size 1024 bytes. IPC_CREAT
is used to create the shared segment if it does not exist. 0666 are the
permissions on the shared segment
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
//process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
//this prints the address where the segment is attached with this process
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf("You wrote : %s\n",(char *)shared_memory);
}
CREAT.C
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
C CODE: (FCFS SCHEDULING): without arrival time
#include <stdio.h>
int main() {
int pid[15], bt[15], n;
printf("Name: S.SUMANTH REDDY REG.NO:22BKT0075\n");
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter process ID for process %d: ", i + 1);
scanf("%d", &pid[i]);
int wt[n];
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
total_wt += wt[i];
total_tat += tat;
}
return 0;
}
C CODE (FIFO PAGEREPLACEMENT):
// Banker's Algorithm
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = {{0, 1, 0}, // P0 // Allocation Matrix
{2, 0, 0}, // P1
{3, 0, 2}, // P2
{2, 1, 1}, // P3
{0, 0, 2}}; // P4
#!/bin/bash
echo "Enter a string:"
read str
reverse_str=$(echo "$str" | rev)
if [ "$str" == "$reverse_str" ]; then
echo "$str is a palindrome."
else
echo "$str is not a palindrome."
fi
ADD OF N NUMBERS
#!/bin/bash
echo "Enter the number of values:"
read n
sum=0
echo "Enter the numbers:"
for (( i=0; i<n; i++ ))
do
read num
sum=$((sum + num))
done
echo "The sum is: $sum"
PRIME NUMBER
#!/bin/bash
echo "Enter a number:"
read num
if [ "$num" -le 1 ]; then
echo "$num is not a prime number."
exit 0
fi
is_prime=1
for (( i=2; i*i<=num; i++ ))
do
if [ $((num % i)) -eq 0 ]; then
is_prime=0
break
fi
done
if [ $is_prime -eq 1 ]; then
echo "$num is a prime number."
else
echo "$num is not a prime number."
Fi
LEAP YEAR
#!/bin/bash
echo "Enter a year:"
read year
if (( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )); then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
FIBINACO
#!/bin/bash
echo "Enter the number of terms:"
read n
a=0
b=1
echo "Fibonacci sequence up to $n terms:"
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
SJF – PREMPTIVE: (with arrival time)
#include <stdio.h>
#include <limits.h>
void printTable(int n, int pid[], int arrival_time[], int burst_time[], int waiting_time[], int
turnaround_time[]) {
printf("| Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time |\n");
for (int i = 0; i < n; i++) {
printf(" P%-7d %-12d %-10d %-12d %-15d\n", pid[i], arrival_time[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
}
while (completed != n) {
min_time = INT_MAX;
for (int i = 0; i < n; i++) {
if (arrival_time[i] <= time && remaining_time[i] > 0 && remaining_time[i] < min_time) {
min_time = remaining_time[i];
flag = 1;
}
}
if (flag == 0) {
time++;
continue;
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
return 0;
}
SJF –NON- PREMPTIVE: (with arrival time)
#include <stdio.h>
#include <limits.h>
void printTable(int n, int pid[], int arrival_time[], int burst_time[], int waiting_time[], int
turnaround_time[]) {
printf("| Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time |\n");
for (int i = 0; i < n; i++) {
printf(" P%-7d %-12d %-10d %-12d %-15d\n", pid[i], arrival_time[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
}
while (completed != n) {
min_time = INT_MAX;
for (int i = 0; i < n; i++) {
if (arrival_time[i] <= time && completed_flag[i] == 0 && burst_time[i] < min_time) {
min_time = burst_time[i];
shortest = i;
}
}
if (min_time == INT_MAX) {
time++;
continue;
}
time += burst_time[shortest];
waiting_time[shortest] = time - burst_time[shortest] - arrival_time[shortest];
if (waiting_time[shortest] < 0)
waiting_time[shortest] = 0;
turnaround_time[shortest] = burst_time[shortest] + waiting_time[shortest];
completed_flag[shortest] = 1;
completed++;
}
printTable(n, pid, arrival_time, burst_time, waiting_time, turnaround_time);
calculateAverageTime(n, waiting_time, turnaround_time);
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
return 0;
}
PRIORITY: (with arrival time)
#include <stdio.h>
#include <limits.h>
void printTable(int pid[], int arrival_time[], int burst_time[], int priority[], int waiting_time[],
int turnaround_time[], int n) {
printf("| Process ID | Arrival Time | Burst Time | Priority | Waiting Time | Turnaround Time
|\n");
for (int i = 0; i < n; i++) {
printf(" P%-7d %-12d %-10d %-8d %-12d %-15d\n", pid[i], arrival_time[i],
burst_time[i], priority[i], waiting_time[i], turnaround_time[i]);
}
}
void calculateAverageTime(int waiting_time[], int turnaround_time[], int n) {
int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
total_wt += waiting_time[i];
total_tat += turnaround_time[i];
}
printf("\nAverage Waiting Time: %.2f", (float)total_wt / n);
printf("\nAverage Turnaround Time: %.2f\n", (float)total_tat / n);
}
void priorityScheduling(int pid[], int arrival_time[], int burst_time[], int priority[], int n) {
int completed = 0, time = 0, max_priority;
int waiting_time[n], turnaround_time[n], completed_process[n];
int highest_priority = 0, flag = 0;
for (int i = 0; i < n; i++) {
completed_process[i] = 0;
waiting_time[i] = 0;
turnaround_time[i] = 0;
}
while (completed != n) {
max_priority = INT_MAX;
flag = 0;
for (int i = 0; i < n; i++) {
if (arrival_time[i] <= time && completed_process[i] == 0 && priority[i] < max_priority) {
max_priority = priority[i];
highest_priority = i;
flag = 1;
}
}
if (flag == 0) {
time++;
continue;
}
time += burst_time[highest_priority];
waiting_time[highest_priority] = time - burst_time[highest_priority] -
arrival_time[highest_priority];
if (waiting_time[highest_priority] < 0) waiting_time[highest_priority] = 0;
turnaround_time[highest_priority] = burst_time[highest_priority] +
waiting_time[highest_priority];
completed_process[highest_priority] = 1;
completed++;
}
printTable(pid, arrival_time, burst_time, priority, waiting_time, turnaround_time, n);
calculateAverageTime(waiting_time, turnaround_time, n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int pid[n], arrival_time[n], burst_time[n], priority[n];
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
printf("Enter Burst Time, Arrival Time, and Priority for Process %d: ", i + 1);
scanf("%d %d %d", &burst_time[i], &arrival_time[i], &priority[i]);
}
priorityScheduling(pid, arrival_time, burst_time, priority, n);
return 0;
}
ROUND ROBIN: with arrival time
#include <stdio.h>
void printTable(int n, int pid[], int arrival_time[], int burst_time[], int waiting_time[], int
turnaround_time[]) {
printf("| Process ID | Arrival Time | Burst Time | Waiting Time | Turnaround Time |\n");
for (int i = 0; i < n; i++) {
printf(" P%-7d %-12d %-10d %-12d %-15d\n", pid[i], arrival_time[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
}
void calculateAverageTime(int n, int waiting_time[], int turnaround_time[]) {
int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
total_wt += waiting_time[i];
total_tat += turnaround_time[i];
}
printf("\nAverage Waiting Time: %.2f", (float)total_wt / n);
printf("\nAverage Turnaround Time: %.2f\n", (float)total_tat / n);
}
void roundRobin(int n, int pid[], int arrival_time[], int burst_time[], int quantum) {
int remaining_time[n], time = 0, waiting_time[n], turnaround_time[n];
int completed = 0;
for (int i = 0; i < n; i++) {
remaining_time[i] = burst_time[i];
}
while (completed != n) {
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0) {
if (remaining_time[i] <= quantum) {
time += remaining_time[i];
waiting_time[i] = time - burst_time[i] - arrival_time[i];
turnaround_time[i] = waiting_time[i] + burst_time[i];
remaining_time[i] = 0;
completed++;
} else {
time += quantum;
remaining_time[i] -= quantum;
}
}
}
}
printTable(n, pid, arrival_time, burst_time, waiting_time, turnaround_time);
calculateAverageTime(n, waiting_time, turnaround_time);
}
int main() {
int n, quantum;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter time quantum: ");
scanf("%d", &quantum);
int pid[n], arrival_time[n], burst_time[n];
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
printf("Enter Burst Time and Arrival Time of process %d: ", i + 1);
scanf("%d %d", &burst_time[i], &arrival_time[i]);
}
roundRobin(n, pid, arrival_time, burst_time, quantum);
return 0;
}
typedef struct {
int id, bt, at, ct, tat, wt;
} Process;
int main() {
int n;
printf("\nEnter the number of processes in your system:\n");
scanf("%d", &n);
Process* p = (Process*)malloc(n * sizeof(Process));
input(p, n);
sort(p, n);
calc(p, n);
show(p, n);
free(p);
return 0;
}
void input(Process* p, int n) {
for (int i = 0; i < n; i++) {
printf("\nEnter arrival time for process %d:\n", i + 1);
scanf("%d", &p[i].at);
printf("Enter burst time for process %d:\n", i + 1);
scanf("%d", &p[i].bt);
p[i].id = i + 1;
}
}
temp = p[j].at;
p[j].at = p[j + 1].at;
p[j + 1].at = temp;
temp = p[j].id;
p[j].id = p[j + 1].id;
p[j + 1].id = temp;
}
}
}
}
void show(Process* p, int n) {
printf("Process\tArrival\tBurst\tWaiting\tTurn Around\tCompletion\n");
for (int i = 0; i < n; i++) {
printf(" P[%d]\t %d\t%d\t%d\t %d\t\t%d\n", p[i].id, p[i].at, p[i].bt, p[i].wt, p[i].tat,
p[i].ct);
}
}