C CODE: (PRIORITY) without arrival time
#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);
printf("\nEnter Burst Time and Priority\n");
for(i = 0; i < n; i++)
{
printf("\nP[%d]\n", i + 1);
printf("Burst Time:");
scanf("%d", &bt[i]);
printf("Priority:");
scanf("%d", &pr[i]);
p[i] = i + 1;
}
for(i = 0; i < n; i++)
{
pos = i;
for(j = i + 1; j < n; j++)
{
if(pr[j] > pr[pos])
pos = j;
}
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;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
total += tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d", p[i], bt[i], wt[i], tat[i]);
}
avg_tat = total / n;
printf("\n\nAverage Waiting Time=%d", avg_wt);
printf("\nAverage Turnaround Time=%d\n", avg_tat);
return 0;
}
C CODE: (SRTF) without arrival time
#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;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
for(i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &bt[i]);
p[i] = i + 1;
}
for(i = 0; i < n; i++) {
pos = i;
for(j = i + 1; j < n; j++) {
if(bt[j] < bt[pos])
pos = j;
}
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 = (float)total / n;
total = 0;
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
total += tat[i];
printf("\nP%d\t\t%d\t\t%d\t\t%d", p[i], bt[i], wt[i], tat[i]);
}
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];
for (int i = 0; i < n; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
while (1)
int done = 1;
for (int i = 0; i < n; i++)
if (rem_bt[i] > 0)
done = 0;
if (rem_bt[i] > quantum)
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[])
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i];
void findavgTime(int processes[], int n, int bt[], int quantum)
int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
printf("PN\t BT\t WT\t TAT\n");
for (int i = 0; i < n; i++)
total_wt += wt[i];
total_tat += tat[i];
printf("%d\t %d\t %d\t %d\n", i + 1, bt[i], wt[i], tat[i]);
printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);
printf("Average turn around time = %.2f\n", (float)total_tat / (float)n);
int main()
int processes[] = {1, 2, 3};
int n = sizeof(processes) / sizeof(processes[0]);
int burst_time[] = {10, 5, 8};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
C CODE: (SHARED MEMORY):
#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]);
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &bt[i]);
}
int wt[n];
wt[0] = 0;
for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
printf("Process ID\tBurst Time\tWaiting Time\tTurnAround Time\n");
float total_wt = 0.0, total_tat = 0.0;
for (int i = 0; i < n; i++) {
int tat = bt[i] + wt[i];
printf("%d\t\t%d\t\t%d\t\t%d\n", pid[i], bt[i], wt[i], tat);
total_wt += wt[i];
total_tat += tat;
}
float avg_wt = total_wt / n;
float avg_tat = total_tat / n;
printf("Avg. waiting time= %.2f\n", avg_wt);
printf("Avg. turnaround time= %.2f\n", avg_tat);
return 0;
}
C CODE (FIFO PAGEREPLACEMENT):
#include < stdio.h >
int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
C CODE (BANKER’S ALGO):
// 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
int max[5][3] = {{7, 5, 3}, // P0 // MAX Matrix
{3, 2, 2}, // P1
{9, 0, 2}, // P2
{2, 2, 2}, // P3
{4, 3, 3}}; // P4
int avail[3] = {3, 3, 2}; // Available Resources
int f[n], ans[n], ind = 0;
for (k = 0; k < n; k++)
{
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++)
{
for (i = 0; i < n; i++)
{
if (f[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > avail[j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
for (int i = 0; i < n; i++)
{
if (f[i] == 0)
{
flag = 0;
printf("The following system is not safe");
break;
}
}
if (flag == 1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}
SHELL SCRIPTING:
PALINDROME:
#!/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 ARRAY ELEMENTS
#!/bin/bash
echo "Enter the number of elements in the array:"
read n
echo "Enter the elements of the array:"
for (( i=0; i<n; i++ ))
do
read arr[i]
done
sum=0
for (( i=0; i<n; i++ ))
do
sum=$((sum + arr[i]))
done
echo "The sum of the array elements is: $sum"
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]);
}
}
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 preemptiveSJF(int n, int pid[], int arrival_time[], int burst_time[]) {
int remaining_time[n], time = 0, completed = 0, min_time, flag = 0, finish_time;
int waiting_time[n], turnaround_time[n];
for (int i = 0; i < n; i++) {
remaining_time[i] = burst_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;
}
for (int i = 0; i < n; i++) {
if (remaining_time[i] == min_time) {
remaining_time[i]--;
if (remaining_time[i] == 0) {
completed++;
finish_time = time + 1;
waiting_time[i] = finish_time - burst_time[i] - arrival_time[i];
if (waiting_time[i] < 0)
waiting_time[i] = 0;
turnaround_time[i] = burst_time[i] + waiting_time[i];
}
break;
}
}
time++;
}
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);
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]);
}
preemptiveSJF(n, pid, arrival_time, burst_time);
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]);
}
}
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 nonPreemptiveSJF(int n, int pid[], int arrival_time[], int burst_time[]) {
int time = 0, completed = 0, min_time, shortest = 0;
int waiting_time[n], turnaround_time[n], completed_flag[n];
for (int i = 0; i < n; i++) {
completed_flag[i] = 0;
}
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);
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]);
}
nonPreemptiveSJF(n, pid, arrival_time, burst_time);
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;
}
Fcfs with arrival time:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id, bt, at, ct, tat, wt;
} Process;
void input(Process* p, int n);
void calc(Process* p, int n);
void show(Process* p, int n);
void sort(Process* p, int n);
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;
}
}
void calc(Process* p, int n) {
int sum = 0;
sum = sum + p[0].at;
for (int i = 0; i < n; i++) {
sum = sum + p[i].bt;
p[i].ct = sum;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
if (sum < p[i + 1].at) {
int t = p[i + 1].at - sum;
sum = sum + t;
}
}
}
void sort(Process* p, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].at > p[j + 1].at) {
int temp = p[j].bt;
p[j].bt = p[j + 1].bt;
p[j + 1].bt = temp;
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);
}
}