0% found this document useful (0 votes)
19 views22 pages

Osss Fatt

Uploaded by

jin346563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views22 pages

Osss Fatt

Uploaded by

jin346563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Banker's Algorithm

#include <stdio.h>
#define MAX 10
int allocation[MAX][MAX], max[MAX][MAX], need[MAX][MAX], available[MAX];
int n, m;
void input() {
int i, j;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resource types: ");
scanf("%d", &m);
printf("Enter the Allocation Matrix:\n");
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &allocation[i][j]);
printf("Enter the Maximum Matrix:\n");
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &max[i][j]);
printf("Enter the Available Resources:\n");
for (j = 0; j < m; j++)
scanf("%d", &available[j]);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - allocation[i][j];
}
void display() {
int i, j;
printf("Process\tAllocation\tMax\tNeed\n");
for (i = 0; i < n; i++) {
printf("P%d\t", i);
for (j = 0; j < m; j++)
printf("%d ", allocation[i][j]);
printf("\t");
for (j = 0; j < m; j++)
printf("%d ", max[i][j]);
printf("\t");
for (j = 0; j < m; j++)
printf("%d ", need[i][j]);
printf("\n");
}
}
int is_safe() {
int finish[MAX] = {0}, work[MAX], safe_sequence[MAX], count = 0, i, j;
for (j = 0; j < m; j++)
work[j] = available[j];

while (count < n) {


int found = 0;
for (i = 0; i < n; i++) {
if (!finish[i]) {
for (j = 0; j < m; j++)
if (need[i][j] > work[j])
break;
if (j == m) {
for (j = 0; j < m; j++)
work[j] += allocation[i][j];
safe_sequence[count++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (!found)
return 0;
}
printf("Safe Sequence: ");
for (i = 0; i < n; i++)
printf("P%d ", safe_sequence[i]);
printf("\n");
return 1;
}
int main() {
input();
display();
if (is_safe())
printf("The system is in a safe state.\n");
else
printf("The system is not in a safe state.\n");
return 0;
}
Producer consumer gcc producer.c -o producer -pthread
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
sem_t empty, full;
pthread_mutex_t mutex;
void *producer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
item = i + 1;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
printf("Producer produced: %d\n", item);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
}
void *consumer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
printf("Consumer consumed: %d\n", item);
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(2);
}
}
int main() {
pthread_t prod, cons;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
Dining philosphers:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
sem_t chopsticks[NUM_PHILOSOPHERS];
void *philosopher(void *arg) {
int id = *(int *)arg;
while (1) {
printf("Philosopher %d is thinking.\n", id);
sleep(1);
sem_wait(&chopsticks[id]);
sem_wait(&chopsticks[(id + 1) % NUM_PHILOSOPHERS]);
printf("Philosopher %d is eating.\n", id);
sleep(2);
sem_post(&chopsticks[id]);
sem_post(&chopsticks[(id + 1) % NUM_PHILOSOPHERS]);
printf("Philosopher %d finished eating.\n", id);
sleep(1);
}
}
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int ids[NUM_PHILOSOPHERS];
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&chopsticks[i], 0, 1);
ids[i] = i;
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_create(&philosophers[i], NULL, philosopher, &ids[i]);
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(philosophers[i], NULL);
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&chopsticks[i]);
}
return 0;
}
Page replacement: FIFO:
#include <stdio.h>
void fifo(int pages[], int n, int frames) {
int frame[frames], count = 0, pageFaults = 0, i, j, k;
for (i = 0; i < frames; i++) frame[i] = -1;
for (i = 0; i < n; i++) {
int found = 0;
for (j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;
}
}
if (!found) {
frame[count] = pages[i];
count = (count + 1) % frames;
pageFaults++;
}
printf("Page %d: ", pages[i]);
for (k = 0; k < frames; k++) {
if (frame[k] == -1)
printf("-- ");
else
printf("%d ", frame[k]);
}
printf("\n");
}
printf("Total Page Faults (FIFO): %d\n", pageFaults);
}
int main() {
int n, frames;
printf("Enter number of pages: ");
scanf("%d", &n);
int pages[n];
printf("Enter the pages: ");
for (int i = 0; i < n; i++) scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &frames);
fifo(pages, n, frames);
return 0;
}
LRU:
#include <stdio.h>
void lru(int pages[], int n, int frames) {
int frame[frames], age[frames], pageFaults = 0, i, j, k, minAge, minIndex;
for (i = 0; i < frames; i++) frame[i] = -1;
for (i = 0; i < n; i++) {
int found = 0;
for (j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
age[j] = i;
break;
}
}
if (!found) {
if (i < frames) {
frame[i] = pages[i];
age[i] = i;
} else {
minAge = age[0];
minIndex = 0;
for (k = 1; k < frames; k++) {
if (age[k] < minAge) {
minAge = age[k];
minIndex = k;
}
}
frame[minIndex] = pages[i];
age[minIndex] = i;
}
pageFaults++;
}
printf("Page %d: ", pages[i]);
for (k = 0; k < frames; k++) {
if (frame[k] == -1) printf("-- ");
else printf("%d ", frame[k]);
}
printf("\n");
}
printf("Total Page Faults (LRU): %d\n", pageFaults);}
int main() {
int n, frames;
printf("Enter number of pages: ");
scanf("%d", &n);
int pages[n];
printf("Enter the pages: ");
for (int i = 0; i < n; i++) scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &frames);
lru(pages, n, frames);
return 0; }
Optimal
#include <stdio.h>
void optimal(int pages[], int n, int frames) {
int frame[frames], pageFaults = 0, i, j, k, farthest, replaceIndex;
for (i = 0; i < frames; i++) frame[i] = -1;
for (i = 0; i < n; i++) {
int found = 0;
for (j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;}}
if (!found) {
if (i < frames) {
frame[i] = pages[i];
} else {
farthest = -1;
for (j = 0; j < frames; j++) {
int foundLater = 0;
for (k = i + 1; k < n; k++) {
if (frame[j] == pages[k]) {
if (k > farthest) {
farthest = k;
replaceIndex = j;}
foundLater = 1;
break;
}
}
if (!foundLater) {
replaceIndex = j;
break;}}
frame[replaceIndex] = pages[i];}
pageFaults++;}
printf("Page %d: ", pages[i]);
for (k = 0; k < frames; k++) {
if (frame[k] == -1) printf("-- ");
else printf("%d ", frame[k]); }
printf("\n");
}
printf("Total Page Faults (Optimal): %d\n", pageFaults);}
int main() {
int n, frames;
printf("Enter number of pages: ");
scanf("%d", &n);
int pages[n];
printf("Enter the pages: ");
for (int i = 0; i < n; i++) scanf("%d", &pages[i]);
printf("Enter number of frames: ");
scanf("%d", &frames);
optimal(pages, n, frames);
return 0;
}
Memory allocation: FIRST FIT:
#include <stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter number of memory blocks: ");
scanf("%d", &m);
int blockSize[m];
printf("Enter the sizes of memory blocks: ");
for (int i = 0; i < m; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the sizes of processes: ");
for (int i = 0; i < n; i++) {
scanf("%d", &processSize[i]);
}
firstFit(blockSize, m, processSize, n);
return 0;
}
Best fit
#include <stdio.h>
void bestFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;
for (int i = 0; i < n; i++) {
int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx])
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter number of memory blocks: ");
scanf("%d", &m);
int blockSize[m];
printf("Enter the sizes of memory blocks: ");
for (int i = 0; i < m; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the sizes of processes: ");
for (int i = 0; i < n; i++) {
scanf("%d", &processSize[i]);
}
bestFit(blockSize, m, processSize, n);
return 0;
}
Worst fit
#include <stdio.h>
void worstFit(int blockSize[], int m, int processSize[], int n) {
int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;
for (int i = 0; i < n; i++) {
int worstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx])
worstIdx = j;
}
}
if (worstIdx != -1) {
allocation[i] = worstIdx;
blockSize[worstIdx] -= processSize[i];
}
}
printf("Process No.\tProcess Size\tBlock No.\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main() {
int m, n;
printf("Enter number of memory blocks: ");
scanf("%d", &m);
int blockSize[m];
printf("Enter the sizes of memory blocks: ");
for (int i = 0; i < m; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter number of processes: ");
scanf("%d", &n);
int processSize[n];
printf("Enter the sizes of processes: ");
for (int i = 0; i < n; i++) {
scanf("%d", &processSize[i]);
}
worstFit(blockSize, m, processSize, n);
return 0;
}
CPU Scheduling:
FCFS:
#include <stdio.h>
void fcfs(int processes[], int n, int burstTime[], int arrivalTime[]) {
int waitingTime[n], turnaroundTime[n];
int totalWaitingTime = 0, totalTurnaroundTime = 0;
waitingTime[0] = 0;
for (int i = 1; i < n; i++) {
waitingTime[i] = waitingTime[i - 1] + burstTime[i - 1] - (arrivalTime[i] - arrivalTime[i - 1]);
if (waitingTime[i] < 0) waitingTime[i] = 0;
}
for (int i = 0; i < n; i++) {
turnaroundTime[i] = waitingTime[i] + burstTime[i];
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
printf("Processes\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], arrivalTime[i], burstTime[i],
waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burstTime[n], arrivalTime[n];
printf("Enter the burst times for the processes: ");
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
scanf("%d", &burstTime[i]);
}
printf("Enter the arrival times for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &arrivalTime[i]);
fcfs(processes, n, burstTime, arrivalTime);
return 0;
}
SJF non preemptive
#include <stdio.h>
void sjfNonPreemptive(int processes[], int n, int burstTime[], int arrivalTime[]) {
int waitingTime[n], turnaroundTime[n], isCompleted[n];
int totalWaitingTime = 0, totalTurnaroundTime = 0, completed = 0, currentTime = 0;
for (int i = 0; i < n; i++) isCompleted[i] = 0;

while (completed < n) {


int shortestIdx = -1, shortestBurst = 1e9;
for (int i = 0; i < n; i++) {
if (!isCompleted[i] && arrivalTime[i] <= currentTime && burstTime[i] < shortestBurst) {
shortestIdx = i;
shortestBurst = burstTime[i];
}
}
if (shortestIdx == -1) {
currentTime++;
} else {
waitingTime[shortestIdx] = currentTime - arrivalTime[shortestIdx];
if (waitingTime[shortestIdx] < 0) waitingTime[shortestIdx] = 0;
currentTime += burstTime[shortestIdx];
turnaroundTime[shortestIdx] = waitingTime[shortestIdx] + burstTime[shortestIdx];
totalWaitingTime += waitingTime[shortestIdx];
totalTurnaroundTime += turnaroundTime[shortestIdx];
isCompleted[shortestIdx] = 1;
completed++;
}
}
printf("Processes\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], arrivalTime[i], burstTime[i],
waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burstTime[n], arrivalTime[n];
printf("Enter the burst times for the processes: ");
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
scanf("%d", &burstTime[i]);
}
printf("Enter the arrival times for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &arrivalTime[i]);
sjfNonPreemptive(processes, n, burstTime, arrivalTime);
return 0;
}
Sjf preepmtive
#include <stdio.h>
void sjfPreemptive(int processes[], int n, int burstTime[], int arrivalTime[]) {
int remainingTime[n], waitingTime[n], turnaroundTime[n];
int totalWaitingTime = 0, totalTurnaroundTime = 0, completed = 0, currentTime = 0, shortest = -1,
minRemainingTime = 1e9;
int isCompleted[n];
for (int i = 0; i < n; i++) {
remainingTime[i] = burstTime[i];
isCompleted[i] = 0;}
while (completed < n) {
shortest = -1;
minRemainingTime = 1e9;
for (int i = 0; i < n; i++) {
if (!isCompleted[i] && arrivalTime[i] <= currentTime && remainingTime[i] <
minRemainingTime) {
shortest = i;
minRemainingTime = remainingTime[i];} }
if (shortest == -1) {
currentTime++;
} else {
remainingTime[shortest]--;
currentTime++;
if (remainingTime[shortest] == 0) {
completed++;
isCompleted[shortest] = 1;
waitingTime[shortest] = currentTime - arrivalTime[shortest] - burstTime[shortest];
if (waitingTime[shortest] < 0) waitingTime[shortest] = 0;
turnaroundTime[shortest] = waitingTime[shortest] + burstTime[shortest];
totalWaitingTime += waitingTime[shortest];
totalTurnaroundTime += turnaroundTime[shortest];} }}
printf("Processes\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], arrivalTime[i], burstTime[i],
waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burstTime[n], arrivalTime[n];
printf("Enter the burst times for the processes: ");
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
scanf("%d", &burstTime[i]);}
printf("Enter the arrival times for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &arrivalTime[i]);
sjfPreemptive(processes, n, burstTime, arrivalTime);
return 0;}
Priority Non preemptive
#include <stdio.h>
void priorityNonPreemptive(int processes[], int n, int burstTime[], int arrivalTime[], int priority[]) {
int waitingTime[n], turnaroundTime[n], isCompleted[n];
int totalWaitingTime = 0, totalTurnaroundTime = 0, completed = 0, currentTime = 0;
for (int i = 0; i < n; i++) isCompleted[i] = 0;
while (completed < n) {
int highestPriorityIdx = -1;
for (int i = 0; i < n; i++) {
if (!isCompleted[i] && arrivalTime[i] <= currentTime) {
if (highestPriorityIdx == -1 || priority[i] < priority[highestPriorityIdx]) {
highestPriorityIdx = i;}}}
if (highestPriorityIdx == -1) {
currentTime++;
} else {
waitingTime[highestPriorityIdx] = currentTime - arrivalTime[highestPriorityIdx];
if (waitingTime[highestPriorityIdx] < 0) waitingTime[highestPriorityIdx] = 0;
currentTime += burstTime[highestPriorityIdx];
turnaroundTime[highestPriorityIdx] = waitingTime[highestPriorityIdx] +
burstTime[highestPriorityIdx];
totalWaitingTime += waitingTime[highestPriorityIdx];
totalTurnaroundTime += turnaroundTime[highestPriorityIdx];
isCompleted[highestPriorityIdx] = 1;
completed++;}}
printf("Processes\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], arrivalTime[i], burstTime[i],
priority[i], waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);
}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burstTime[n], arrivalTime[n], priority[n];
printf("Enter the burst times for the processes: ");
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
scanf("%d", &burstTime[i]);
}
printf("Enter the arrival times for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &arrivalTime[i]);
printf("Enter the priorities for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &priority[i]);
priorityNonPreemptive(processes, n, burstTime, arrivalTime, priority);
return 0;
}
Priority preemptive
#include <stdio.h>
void priorityPreemptive(int processes[], int n, int burstTime[], int arrivalTime[], int priority[]) {
int remainingTime[n], waitingTime[n], turnaroundTime[n], isCompleted[n];
int totalWaitingTime = 0, totalTurnaroundTime = 0, currentTime = 0, completed = 0;
for (int i = 0; i < n; i++) {
remainingTime[i] = burstTime[i];
isCompleted[i] = 0;}
while (completed < n) {
int highestPriorityIdx = -1;
for (int i = 0; i < n; i++) {
if (!isCompleted[i] && arrivalTime[i] <= currentTime) {
if (highestPriorityIdx == -1 || priority[i] < priority[highestPriorityIdx]) {
highestPriorityIdx = i;}}}
if (highestPriorityIdx == -1) {
currentTime++;
} else {
remainingTime[highestPriorityIdx]--;
currentTime++;
if (remainingTime[highestPriorityIdx] == 0) {
isCompleted[highestPriorityIdx] = 1;
completed++;
waitingTime[highestPriorityIdx] = currentTime - arrivalTime[highestPriorityIdx] -
burstTime[highestPriorityIdx];
if (waitingTime[highestPriorityIdx] < 0) waitingTime[highestPriorityIdx] = 0;
turnaroundTime[highestPriorityIdx] = waitingTime[highestPriorityIdx] +
burstTime[highestPriorityIdx];
totalWaitingTime += waitingTime[highestPriorityIdx];
totalTurnaroundTime += turnaroundTime[highestPriorityIdx];}}}
printf("Processes\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], arrivalTime[i], burstTime[i],
priority[i], waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);}
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burstTime[n], arrivalTime[n], priority[n];
printf("Enter the burst times for the processes: ");
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
scanf("%d", &burstTime[i]);}
printf("Enter the arrival times for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &arrivalTime[i]);
printf("Enter the priorities for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &priority[i]);
priorityPreemptive(processes, n, burstTime, arrivalTime, priority);
return 0;}
Round Robin
include <stdio.h>
void roundRobin(int processes[], int n, int burstTime[], int arrivalTime[], int quantum) {
int remainingTime[n], waitingTime[n], turnaroundTime[n];
int totalWaitingTime = 0, totalTurnaroundTime = 0, currentTime = 0, completed = 0;
for (int i = 0; i < n; i++) remainingTime[i] = burstTime[i];
while (completed < n) {
int allIdle = 1;
for (int i = 0; i < n; i++) {
if (remainingTime[i] > 0 && arrivalTime[i] <= currentTime) {
allIdle = 0;
if (remainingTime[i] > quantum) {
currentTime += quantum;
remainingTime[i] -= quantum;
} else {
currentTime += remainingTime[i];
waitingTime[i] = currentTime - arrivalTime[i] - burstTime[i];
if (waitingTime[i] < 0) waitingTime[i] = 0;
turnaroundTime[i] = waitingTime[i] + burstTime[i];
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
remainingTime[i] = 0;
completed++;}}}
if (allIdle) currentTime++;}
printf("Processes\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i], arrivalTime[i], burstTime[i],
waitingTime[i], turnaroundTime[i]);
}
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);}
int main() {
int n, quantum;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burstTime[n], arrivalTime[n];
printf("Enter the burst times for the processes: ");
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
scanf("%d", &burstTime[i]);
}
printf("Enter the arrival times for the processes: ");
for (int i = 0; i < n; i++) scanf("%d", &arrivalTime[i]);
printf("Enter the time quantum: ");
scanf("%d", &quantum);
roundRobin(processes, n, burstTime, arrivalTime, quantum);
return 0;
}
Parent child
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
printf("Fork failed!\n");
return 1;
} else if (pid == 0) {
printf("This is the child process. PID = %d, Parent PID = %d\n", getpid(), getppid());
} else {
printf("This is the parent process. PID = %d, Child PID = %d\n", getpid(), pid);
wait(NULL);
printf("Child process finished. Parent exiting.\n");
}
return 0;
}
Client server program:
CLIENT:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
#define SERVER_IP "127.0.0.1"
int main() {
int sock_fd;
struct sockaddr_in server_addr;
char buffer[1024];
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
if (connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
printf("Connected to server. Sending message...\n");
send(sock_fd, "Hello from client", 17, 0);
read(sock_fd, buffer, sizeof(buffer));
printf("Server: %s\n", buffer);
close(sock_fd);
return 0; }
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
char buffer[1024];
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server is waiting for connections...\n");
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len);
if (client_fd < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
printf("Client connected. Waiting for message...\n");
read(client_fd, buffer, sizeof(buffer));
printf("Client: %s\n", buffer);
send(client_fd, "Hello from server", 18, 0);
printf("Message sent to client.\n");
close(client_fd);
close(server_fd);
return 0;
}
Child process prarentPROGRAM X:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void process_Y() {
printf("Process Y (PID: %d): Please input a number: \n", getpid());
int input;
scanf("%d", &input);
printf("Process Y (PID: %d): You entered: %d\n", getpid(), input);}
void process_Z() {
printf("Process Z (PID: %d): Executing output operations...\n", getpid());
printf("Process Z (PID: %d): Greetings from Process Z!\n", getpid());}
int main() {
pid_t pid_Y, pid_Z;
pid_Y = fork();
if (pid_Y < 0) {
perror("Error creating process Y\n");
exit(EXIT_FAILURE);}
if (pid_Y == 0) {
process_Y();
exit(EXIT_SUCCESS);}
pid_Z = fork();
if (pid_Z < 0) {
perror("Error creating process Z\n");
exit(EXIT_FAILURE);}
if (pid_Z == 0) {
process_Z();
exit(EXIT_SUCCESS);}
printf("Main Process (PID: %d): Waiting for child processes to finish.\n", getpid());
return 0;}
PROGRAM A:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid_B;
pid_B = fork();
if (pid_B < 0) {
perror("Error creating process B\n");
exit(EXIT_FAILURE);}
if (pid_B == 0) {
execl("./program_X", "./program_X", (char *)NULL);
perror("Failed to execute program X");
exit(EXIT_FAILURE);}
printf("Main Process A (PID: %d): Process B completed.\n", getpid());
return 0;}
Hello World:
bash
Copy code
#!/bin/bash
echo "Hello, World!"
Even or Odd:
bash
Copy code
#!/bin/bash
echo "Enter a number:"
read number
if (( $number % 2 == 0 )); then
echo "$number is even."
else
echo "$number is odd."
fi
Operations:
bash
Copy code
#!/bin/bash
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
echo "Choose operation: +, -, *, /"
read op
case $op in
"+") echo "Result: $((num1 + num2))" ;;
"-") echo "Result: $((num1 - num2))" ;;
"*") echo "Result: $((num1 * num2))" ;;
"/") echo "Result: $((num1 / num2))" ;;
*) echo "Invalid operation" ;;
esac
Fibonacci Series:
bash
Copy code
#!/bin/bash
echo "Enter the number of terms:"
read n
a=0
b=1
echo "Fibonacci series up to $n terms:"
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo ""
Factorial:
bash
Copy code
#!/bin/bash
echo "Enter a number:"
read n
factorial=1
for (( i=1; i<=n; i++ ))
do
factorial=$((factorial * i))
done
echo "The factorial of $n is: $factorial"
Palindrome:
bash
Copy code
#!/bin/bash
echo "Enter a string or number:"
read input
reverse=""
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ "$input" == "$reverse" ]; then
echo "$input is a palindrome."
else
echo "$input is not a palindrome."
Fi
Sum of n numbers
#!/bin/sh
echo "Name: , Reg NO: "
echo "Enter Size (N):"
read N
i=1
sum=0
echo "Enter Numbers:"
while [ $i -le $N ]
do
read num
sum=$((sum + num))
i=$((i + 1))
done
echo "The sum of the entered numbers is: $sum"
Add array n elements
#!/bin/sh
echo "Name:, Reg NO: "
echo "Enter numbers separated by spaces:"
read input
sum=0
set -- $input
if [ $# -eq 0 ]; then
echo "No numbers entered."
exit 1
f
i
for num in "$@"; do
sum=$(expr $sum + $num)
done
echo "The sum of the array is: $sum"
Diagram question
#!/bin/bash
echo "Name"
num_rows=4
for ((i=1; i<=num_rows; i++)); do
for ((j=num_rows; j>i; j--)); do
echo -n " "
done
for ((k=1; k<=2*i-1; k++)); do
echo -n "*"
done
echo
done

num=1
for ((i=num_rows; i>0; i--)); do
for ((j=num_rows; j>i; j--)); do
echo -n " "
done
for ((k=1; k<=2*i-1; k++)); do
echo -n "$num "
done
echo
num=$((num + 1))
done
Execution:
bash
Copy code
chmod +x myscript.sh
./myscript.sh
gcc -o filename filename.c
./filename

You might also like