Modified Lab 1 IS
This commit is contained in:
parent
fdc634b589
commit
933a52b3a8
8 changed files with 166 additions and 384 deletions
|
@ -1,6 +1,7 @@
|
||||||
## ptext = plaintext
|
## ptext = plaintext
|
||||||
|
## ctext = ciphertext
|
||||||
|
## mk = multiplicative key
|
||||||
## ak = additive key
|
## ak = additive key
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
def add_cipher_en(ptext, ak):
|
def add_cipher_en(ptext, ak):
|
||||||
result = ""
|
result = ""
|
||||||
|
@ -76,6 +77,10 @@ def affine_de(ctext, ak, mk):
|
||||||
result += chr((((ord(ch) - 97 - ak) * inverse) % 26) + 97)
|
result += chr((((ord(ch) - 97 - ak) * inverse) % 26) + 97)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def mult_inverse(mk):
|
||||||
|
inverse = pow(mk, -1, 26)
|
||||||
|
return inverse
|
||||||
|
|
||||||
def operator(argument,ptext,ak,mk):
|
def operator(argument,ptext,ak,mk):
|
||||||
match argument:
|
match argument:
|
||||||
case '1':
|
case '1':
|
||||||
|
@ -91,6 +96,7 @@ def operator(argument,ptext,ak,mk):
|
||||||
print("Multiplicative Key: ", mk)
|
print("Multiplicative Key: ", mk)
|
||||||
ctext = mult_cipher_en(ptext, mk)
|
ctext = mult_cipher_en(ptext, mk)
|
||||||
print("Ciphertext: ", ctext)
|
print("Ciphertext: ", ctext)
|
||||||
|
print("Multiplicative Inverse: ", mult_inverse(mk))
|
||||||
print("Decrypted Text: ", mult_cipher_de(ctext, mk))
|
print("Decrypted Text: ", mult_cipher_de(ctext, mk))
|
||||||
case '3':
|
case '3':
|
||||||
print("Affine Cipher")
|
print("Affine Cipher")
|
||||||
|
@ -99,6 +105,7 @@ def operator(argument,ptext,ak,mk):
|
||||||
print("Multiplicative Key: ", mk)
|
print("Multiplicative Key: ", mk)
|
||||||
ctext = affine_en(ptext, ak, mk)
|
ctext = affine_en(ptext, ak, mk)
|
||||||
print("Ciphertext: ", ctext)
|
print("Ciphertext: ", ctext)
|
||||||
|
print("Affine Inverse: ", mult_inverse(mk))
|
||||||
print("Decrypted Text: ", affine_de(ctext, ak, mk))
|
print("Decrypted Text: ", affine_de(ctext, ak, mk))
|
||||||
case '4':
|
case '4':
|
||||||
print("Goodbye")
|
print("Goodbye")
|
||||||
|
@ -122,6 +129,5 @@ def main():
|
||||||
op = input("Enter your choice of operation: ")
|
op = input("Enter your choice of operation: ")
|
||||||
operator(op, ptext, ak, mk)
|
operator(op, ptext, ak, mk)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,18 +1,50 @@
|
||||||
def hill_en(ptext,hk):
|
import numpy as np
|
||||||
|
|
||||||
def hill_de(ptext,hk):
|
def hill_en(ptext, hk):
|
||||||
|
# all letters to uppercase
|
||||||
|
ptext = ''.join(c.upper() for c in ptext if c.isalpha())
|
||||||
|
|
||||||
|
# matrix size
|
||||||
|
n = int(len(hk)**0.5)
|
||||||
|
|
||||||
|
# key matrix
|
||||||
|
key = np.array([ord(c) - 65 for c in hk]).reshape(n, n)
|
||||||
|
|
||||||
|
# padding
|
||||||
|
ptext += 'X' * (-len(ptext) % n)
|
||||||
|
|
||||||
|
# block operation
|
||||||
|
result = ""
|
||||||
|
for i in range(0, len(ptext), n):
|
||||||
|
block = np.array([ord(c) - 65 for c in ptext[i:i+n]])
|
||||||
|
encrypted_block = (key @ block) % 26
|
||||||
|
result += ''.join(chr(val + 65) for val in encrypted_block)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def hill_de(ctext, hk):
|
||||||
|
# matrix size
|
||||||
|
n = int(len(hk)**0.5)
|
||||||
|
|
||||||
|
# key matrix and its inverse
|
||||||
|
key = np.array([ord(c) - 65 for c in hk]).reshape(n, n)
|
||||||
|
inv_key = np.linalg.inv(key).astype(int) % 26
|
||||||
|
|
||||||
|
# block operation
|
||||||
|
result = ""
|
||||||
|
for i in range(0, len(ctext), n):
|
||||||
|
block = np.array([ord(c) - 65 for c in ctext[i:i+n]])
|
||||||
|
decrypted_block = (inv_key @ block) % 26
|
||||||
|
result += ''.join(chr(val + 65) for val in decrypted_block)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
ptext = input("Kindly enter your desired plaintext: ")
|
ptext = input("Plaintext: ")
|
||||||
hk = input("Kindly enter the Hill Key: ")
|
hk = input("Hill Key: ")
|
||||||
|
|
||||||
print("Welcome to the Hill cipher.")
|
|
||||||
print("Plaintext: ", ptext)
|
|
||||||
print("Hill Key: ", hk)
|
|
||||||
ctext = hill_en(ptext, hk)
|
ctext = hill_en(ptext, hk)
|
||||||
print("Ciphertext: ", ctext)
|
print(f"Ciphertext: {ctext}")
|
||||||
decrypted_text = hill_de(ctext, hk)
|
print(f"Decrypted: {hill_de(ctext, hk)}")
|
||||||
print("Decrypted Text: ", decrypted_text)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
def main():
|
||||||
|
shift = (ord('C') - ord('y')) % 26
|
||||||
|
|
||||||
|
ctext = "XVIEWYWI"
|
||||||
|
plaintext = ""
|
||||||
|
|
||||||
|
for char in ctext:
|
||||||
|
if char.isalpha():
|
||||||
|
shifted = ord(char.lower()) - shift
|
||||||
|
if shifted < ord('a'):
|
||||||
|
shifted += 26
|
||||||
|
plaintext += chr(shifted)
|
||||||
|
else:
|
||||||
|
plaintext += char
|
||||||
|
|
||||||
|
print(f"Attack type: Known plaintext attack")
|
||||||
|
print(f"Ciphertext: {ctext}")
|
||||||
|
print(f"Decrypted: {plaintext}")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -0,0 +1,47 @@
|
||||||
|
def main():
|
||||||
|
# Affine cipher: E(x) = (ax + b) mod 26
|
||||||
|
# Given: "ab" -> "GL"
|
||||||
|
# a=0, b=1 -> G=6, L=11
|
||||||
|
# So: 6 = (a*0 + b) mod 26 -> b = 6
|
||||||
|
# And: 11 = (a*1 + b) mod 26 -> 11 = (a + 6) mod 26 -> a = 5
|
||||||
|
|
||||||
|
ciphertext = "XPALASXYFGFUKPXUSOGEUTKCDGEXANMGNVS"
|
||||||
|
|
||||||
|
# Try all possible values of a and b for affine cipher
|
||||||
|
for a in range(1, 26):
|
||||||
|
# a must be coprime to 26
|
||||||
|
if gcd(a, 26) != 1:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for b in range(26):
|
||||||
|
# Check if this key produces "ab" -> "GL"
|
||||||
|
if (a * 0 + b) % 26 == 6 and (a * 1 + b) % 26 == 11:
|
||||||
|
# Found the key, now decrypt the message
|
||||||
|
a_inv = mod_inverse(a, 26)
|
||||||
|
decrypted = ""
|
||||||
|
for char in ciphertext:
|
||||||
|
if char.isalpha():
|
||||||
|
y = ord(char.upper()) - ord('A')
|
||||||
|
x = (a_inv * (y - b)) % 26
|
||||||
|
decrypted += chr(x + ord('A'))
|
||||||
|
else:
|
||||||
|
decrypted += char
|
||||||
|
|
||||||
|
print(f"Key found: a={a}, b={b}")
|
||||||
|
print(f"Ciphertext: {ciphertext}")
|
||||||
|
print(f"Decrypted: {decrypted}")
|
||||||
|
return
|
||||||
|
|
||||||
|
def gcd(a, b):
|
||||||
|
while b:
|
||||||
|
a, b = b, a % b
|
||||||
|
return a
|
||||||
|
|
||||||
|
def mod_inverse(a, m):
|
||||||
|
for i in range(1, m):
|
||||||
|
if (a * i) % m == 1:
|
||||||
|
return i
|
||||||
|
return None
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
BIN
OS/C/Week11/qq1
Executable file
BIN
OS/C/Week11/qq1
Executable file
Binary file not shown.
|
@ -1,234 +1,11 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <limits.h>
|
#include <math.h>
|
||||||
|
#define MAX 100
|
||||||
void sstf(int requests[], int n, int head) {
|
void sstf(int a[],int n,int h){int v[MAX]={0},t=0,c=h;printf("\nSSTF:\n%d ",c);for(int i=0;i<n;i++){int x=-1,m=1e9;for(int j=0;j<n;j++){if(!v[j]){int d=abs(a[j]-c);if(d<m){m=d;x=j;}}}v[x]=1;t+=m;c=a[x];printf("->%d ",c);}printf("\nTotal:%d\n",t);}
|
||||||
int total_seek = 0;
|
void sortAsc(int a[],int n){for(int i=0;i<n-1;i++)for(int j=i+1;j<n;j++)if(a[i]>a[j]){int t=a[i];a[i]=a[j];a[j]=t;}}
|
||||||
int completed = 0;
|
void sortDesc(int a[],int n){for(int i=0;i<n-1;i++)for(int j=i+1;j<n;j++)if(a[i]<a[j]){int t=a[i];a[i]=a[j];a[j]=t;}}
|
||||||
int visited[100] = {0};
|
void scan(int a[],int n,int h,int d){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i<n;i++)if(a[i]<h)l[x++]=a[i];else r[y++]=a[i];sortDesc(l,x);sortAsc(r,y);int t=0,c=h;printf("\nSCAN:\n%d ",c);for(int i=0;i<x;i++){t+=abs(c-l[i]);c=l[i];printf("->%d ",c);}if(c){t+=c;c=0;printf("->%d ",c);}for(int i=0;i<y;i++){t+=abs(r[i]-c);c=r[i];printf("->%d ",c);}printf("\nTotal:%d\n",t);}
|
||||||
int current = head;
|
void cscan(int a[],int n,int h,int d){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i<n;i++)if(a[i]<h)l[x++]=a[i];else r[y++]=a[i];sortAsc(l,x);sortAsc(r,y);int t=0,c=h;printf("\nCSCAN:\n%d ",c);for(int i=0;i<y;i++){t+=abs(r[i]-c);c=r[i];printf("->%d ",c);}if(c!=d-1){t+=abs(d-1-c);c=d-1;printf("->%d ",c);}t+=d-1;c=0;printf("->%d ",c);for(int i=0;i<x;i++){t+=abs(l[i]-c);c=l[i];printf("->%d ",c);}printf("\nTotal:%d\n",t);}
|
||||||
|
void clook(int a[],int n,int h){int l[MAX],r[MAX],x=0,y=0;for(int i=0;i<n;i++)if(a[i]<h)l[x++]=a[i];else r[y++]=a[i];sortAsc(l,x);sortAsc(r,y);int t=0,c=h;printf("\nCLOOK:\n%d ",c);for(int i=0;i<y;i++){t+=abs(r[i]-c);c=r[i];printf("->%d ",c);}if(x){t+=abs(c-l[0]);c=l[0];printf("->%d ",c);for(int i=1;i<x;i++){t+=abs(l[i]-c);c=l[i];printf("->%d ",c);}}printf("\nTotal:%d\n",t);}
|
||||||
printf("\nSSTF Disk Scheduling\n");
|
int main(){int c,n,h,d,a[MAX];printf("1.SSTF\n2.SCAN\n3.CSCAN\n4.CLOOK\nChoice:");scanf("%d",&c);printf("Requests:");scanf("%d",&n);printf("Queue:");for(int i=0;i<n;i++)scanf("%d",&a[i]);printf("Head:");scanf("%d",&h);if(c==2||c==3){printf("Size:");scanf("%d",&d);}switch(c){case 1:sstf(a,n,h);break;case 2:scan(a,n,h,d);break;case 3:cscan(a,n,h,d);break;case 4:clook(a,n,h);break;default:printf("Invalid\n");}return 0;}
|
||||||
printf("Seek Sequence: %d", head);
|
|
||||||
|
|
||||||
while (completed < n) {
|
|
||||||
int min_distance = INT_MAX;
|
|
||||||
int min_index = -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
if (!visited[i]) {
|
|
||||||
int distance = abs(requests[i] - current);
|
|
||||||
if (distance < min_distance) {
|
|
||||||
min_distance = distance;
|
|
||||||
min_index = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
visited[min_index] = 1;
|
|
||||||
current = requests[min_index];
|
|
||||||
total_seek += min_distance;
|
|
||||||
completed++;
|
|
||||||
printf(" -> %d", current);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
|
||||||
}
|
|
||||||
|
|
||||||
void scan(int requests[], int n, int head, int disk_size) {
|
|
||||||
int total_seek = 0;
|
|
||||||
int direction = 1; // 1 for moving right, 0 for moving left
|
|
||||||
int current = head;
|
|
||||||
|
|
||||||
printf("\nSCAN Disk Scheduling\n");
|
|
||||||
printf("Seek Sequence: %d", head);
|
|
||||||
|
|
||||||
// Sort requests
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
for (int j = 0; j < n - i - 1; j++) {
|
|
||||||
if (requests[j] > requests[j + 1]) {
|
|
||||||
int temp = requests[j];
|
|
||||||
requests[j] = requests[j + 1];
|
|
||||||
requests[j + 1] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find position of head in sorted array
|
|
||||||
int index;
|
|
||||||
for (index = 0; index < n; index++) {
|
|
||||||
if (requests[index] >= head)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move right
|
|
||||||
for (int i = index; i < n; i++) {
|
|
||||||
current = requests[i];
|
|
||||||
printf(" -> %d", current);
|
|
||||||
total_seek += abs(current - head);
|
|
||||||
head = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move to the end of disk
|
|
||||||
printf(" -> %d", disk_size - 1);
|
|
||||||
total_seek += abs(disk_size - 1 - head);
|
|
||||||
head = disk_size - 1;
|
|
||||||
|
|
||||||
// Move left
|
|
||||||
for (int i = index - 1; i >= 0; i--) {
|
|
||||||
current = requests[i];
|
|
||||||
printf(" -> %d", current);
|
|
||||||
total_seek += abs(current - head);
|
|
||||||
head = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
|
||||||
}
|
|
||||||
|
|
||||||
void cscan(int requests[], int n, int head, int disk_size) {
|
|
||||||
int total_seek = 0;
|
|
||||||
int current = head;
|
|
||||||
|
|
||||||
printf("\nC-SCAN Disk Scheduling\n");
|
|
||||||
printf("Seek Sequence: %d", head);
|
|
||||||
|
|
||||||
// Sort requests
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
for (int j = 0; j < n - i - 1; j++) {
|
|
||||||
if (requests[j] > requests[j + 1]) {
|
|
||||||
int temp = requests[j];
|
|
||||||
requests[j] = requests[j + 1];
|
|
||||||
requests[j + 1] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find position of head in sorted array
|
|
||||||
int index;
|
|
||||||
for (index = 0; index < n; index++) {
|
|
||||||
if (requests[index] >= head)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move right
|
|
||||||
for (int i = index; i < n; i++) {
|
|
||||||
current = requests[i];
|
|
||||||
printf(" -> %d", current);
|
|
||||||
total_seek += abs(current - head);
|
|
||||||
head = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move to the end of disk
|
|
||||||
printf(" -> %d", disk_size - 1);
|
|
||||||
total_seek += abs(disk_size - 1 - head);
|
|
||||||
|
|
||||||
// Move to the beginning
|
|
||||||
printf(" -> 0");
|
|
||||||
total_seek += disk_size - 1;
|
|
||||||
head = 0;
|
|
||||||
|
|
||||||
// Move right again
|
|
||||||
for (int i = 0; i < index; i++) {
|
|
||||||
current = requests[i];
|
|
||||||
printf(" -> %d", current);
|
|
||||||
total_seek += abs(current - head);
|
|
||||||
head = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clook(int requests[], int n, int head) {
|
|
||||||
int total_seek = 0;
|
|
||||||
int current = head;
|
|
||||||
|
|
||||||
printf("\nC-LOOK Disk Scheduling\n");
|
|
||||||
printf("Seek Sequence: %d", head);
|
|
||||||
|
|
||||||
// Sort requests
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
for (int j = 0; j < n - i - 1; j++) {
|
|
||||||
if (requests[j] > requests[j + 1]) {
|
|
||||||
int temp = requests[j];
|
|
||||||
requests[j] = requests[j + 1];
|
|
||||||
requests[j + 1] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find position of head in sorted array
|
|
||||||
int index;
|
|
||||||
for (index = 0; index < n; index++) {
|
|
||||||
if (requests[index] >= head)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move right
|
|
||||||
for (int i = index; i < n; i++) {
|
|
||||||
current = requests[i];
|
|
||||||
printf(" -> %d", current);
|
|
||||||
total_seek += abs(current - head);
|
|
||||||
head = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move to first request
|
|
||||||
for (int i = 0; i < index; i++) {
|
|
||||||
current = requests[i];
|
|
||||||
printf(" -> %d", current);
|
|
||||||
total_seek += abs(current - head);
|
|
||||||
head = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\nTotal Seek Time: %d\n", total_seek);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
int requests[100], n, head, disk_size, choice;
|
|
||||||
|
|
||||||
printf("Enter the number of disk requests: ");
|
|
||||||
scanf("%d", &n);
|
|
||||||
|
|
||||||
printf("Enter the disk requests: ");
|
|
||||||
for (int i = 0; i < n; i++) {
|
|
||||||
scanf("%d", &requests[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Enter the initial head position: ");
|
|
||||||
scanf("%d", &head);
|
|
||||||
|
|
||||||
printf("Enter the disk size (0 to size-1): ");
|
|
||||||
scanf("%d", &disk_size);
|
|
||||||
|
|
||||||
do {
|
|
||||||
printf("\n\nDisk Scheduling Algorithms\n");
|
|
||||||
printf("1. SSTF (Shortest Seek Time First)\n");
|
|
||||||
printf("2. SCAN\n");
|
|
||||||
printf("3. C-SCAN\n");
|
|
||||||
printf("4. C-LOOK\n");
|
|
||||||
printf("5. Exit\n");
|
|
||||||
printf("Enter your choice: ");
|
|
||||||
scanf("%d", &choice);
|
|
||||||
|
|
||||||
switch (choice) {
|
|
||||||
case 1:
|
|
||||||
sstf(requests, n, head);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
scan(requests, n, head, disk_size);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
cscan(requests, n, head, disk_size);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
clook(requests, n, head);
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
printf("Exiting program...\n");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("Invalid choice!\n");
|
|
||||||
}
|
|
||||||
} while (choice != 5);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
BIN
OS/C/Week12/rtos
Executable file
BIN
OS/C/Week12/rtos
Executable file
Binary file not shown.
|
@ -1,153 +1,52 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
// Define Task structure (simplified for memorization)
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int id; // Task ID
|
int id,p,et,re,ad,ta;
|
||||||
int period; // Period (also deadline for simplicity)
|
|
||||||
int execution_time; // Worst-case execution time (WCET)
|
|
||||||
// --- Simulation State ---
|
|
||||||
int remaining_execution; // Remaining execution time for current instance
|
|
||||||
int absolute_deadline; // Absolute deadline for current instance
|
|
||||||
int time_to_arrival; // Time until the next instance arrives/is released
|
|
||||||
} Task;
|
} Task;
|
||||||
|
|
||||||
// --- Global Variables ---
|
Task tasks[]={{1,5,2,0,0,0},{2,8,3,0,0,0}};
|
||||||
// Define the tasks for the simulation (Example Set)
|
int nt=sizeof(tasks)/sizeof(Task);
|
||||||
// Format: {id, Period, ExecutionTime, 0, 0, 0} <-- Initial state values
|
int st=40;
|
||||||
Task tasks[] = {
|
|
||||||
{1, 5, 2, 0, 0, 0}, // Task 1: Period=5, Exec Time=2
|
|
||||||
{2, 8, 3, 0, 0, 0} // Task 2: Period=8, Exec Time=3
|
|
||||||
// Add more tasks here if needed
|
|
||||||
};
|
|
||||||
// Calculate number of tasks automatically
|
|
||||||
int num_tasks = sizeof(tasks) / sizeof(Task);
|
|
||||||
// Set simulation duration (e.g., Hyperperiod or a fixed time)
|
|
||||||
// LCM(5, 8) = 40
|
|
||||||
int simulation_time = 40;
|
|
||||||
|
|
||||||
// --- Rate Monotonic (RM) Simulation ---
|
void simulate_rm(){
|
||||||
void simulate_rm() {
|
printf("--- RM ---\n");
|
||||||
printf("--- Rate Monotonic Scheduling ---\n");
|
for(int i=0;i<nt;i++){tasks[i].re=tasks[i].ad=tasks[i].ta=0;}
|
||||||
// Reset task states for the simulation run
|
for(int t=0;t<st;t++){
|
||||||
for (int i = 0; i < num_tasks; i++) {
|
for(int i=0;i<nt;i++){
|
||||||
tasks[i].remaining_execution = 0;
|
if(!tasks[i].ta){
|
||||||
tasks[i].absolute_deadline = 0;
|
if(tasks[i].re>0)printf("!T%d:Task%d missed!\n",t,tasks[i].id);
|
||||||
tasks[i].time_to_arrival = 0; // All tasks start at time 0
|
tasks[i].re=tasks[i].et;
|
||||||
}
|
tasks[i].ad=t+tasks[i].p;
|
||||||
|
tasks[i].ta=tasks[i].p;
|
||||||
// Main simulation loop
|
|
||||||
for (int time = 0; time < simulation_time; time++) {
|
|
||||||
// 1. Check for task arrivals (release time)
|
|
||||||
for (int i = 0; i < num_tasks; i++) {
|
|
||||||
if (tasks[i].time_to_arrival == 0) {
|
|
||||||
// Check if the previous instance of this task missed its deadline
|
|
||||||
if (tasks[i].remaining_execution > 0) {
|
|
||||||
printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id);
|
|
||||||
// Simple handling: Continue with the new instance, old one is lost
|
|
||||||
}
|
|
||||||
// Release new instance of the task
|
|
||||||
tasks[i].remaining_execution = tasks[i].execution_time;
|
|
||||||
tasks[i].absolute_deadline = time + tasks[i].period; // Deadline = Period
|
|
||||||
tasks[i].time_to_arrival = tasks[i].period; // Set timer for the *next* arrival
|
|
||||||
}
|
}
|
||||||
tasks[i].time_to_arrival--; // Decrement time until the next arrival for all tasks
|
tasks[i].ta--;
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Select highest priority task to run (RM: Shortest Period has highest priority)
|
|
||||||
int task_to_run = -1; // -1 indicates CPU Idle
|
|
||||||
int highest_priority = 10000; // Initialize with a low priority (large period)
|
|
||||||
|
|
||||||
for (int i = 0; i < num_tasks; i++) {
|
|
||||||
// Check if task is ready (has arrived and needs execution)
|
|
||||||
if (tasks[i].remaining_execution > 0) {
|
|
||||||
// RM priority check: Lower period value means higher priority
|
|
||||||
if (tasks[i].period < highest_priority) {
|
|
||||||
highest_priority = tasks[i].period;
|
|
||||||
task_to_run = i; // Select this task
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Execute the selected task (or remain idle)
|
|
||||||
if (task_to_run != -1) {
|
|
||||||
// Task selected to run
|
|
||||||
printf("Time %d: Task %d running\n", time, tasks[task_to_run].id);
|
|
||||||
tasks[task_to_run].remaining_execution--; // Execute for one time unit
|
|
||||||
|
|
||||||
// Optional: Check if task just finished
|
|
||||||
// if (tasks[task_to_run].remaining_execution == 0) {
|
|
||||||
// printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id);
|
|
||||||
// }
|
|
||||||
} else {
|
|
||||||
// No task ready to run
|
|
||||||
printf("Time %d: CPU Idle\n", time);
|
|
||||||
}
|
}
|
||||||
|
int r=-1,hp=10000;
|
||||||
|
for(int i=0;i<nt;i++)
|
||||||
|
if(tasks[i].re>0&&tasks[i].p<hp){hp=tasks[i].p;r=i;}
|
||||||
|
if(r>=0){printf("T%d:Task%d run\n",t,tasks[r].id);tasks[r].re--;}
|
||||||
|
else printf("T%d:Idle\n",t);
|
||||||
}
|
}
|
||||||
printf("--- RM Simulation Complete ---\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Earliest Deadline First (EDF) Simulation ---
|
void simulate_edf(){
|
||||||
void simulate_edf() {
|
printf("\n--- EDF ---\n");
|
||||||
printf("\n--- Earliest Deadline First Scheduling ---\n");
|
for(int i=0;i<nt;i++){tasks[i].re=tasks[i].ad=tasks[i].ta=0;}
|
||||||
// Reset task states
|
for(int t=0;t<st;t++){
|
||||||
for (int i = 0; i < num_tasks; i++) {
|
for(int i=0;i<nt;i++){
|
||||||
tasks[i].remaining_execution = 0;
|
if(!tasks[i].ta){
|
||||||
tasks[i].absolute_deadline = 0;
|
if(tasks[i].re>0)printf("!T%d:Task%d missed!\n",t,tasks[i].id);
|
||||||
tasks[i].time_to_arrival = 0;
|
tasks[i].re=tasks[i].et;
|
||||||
}
|
tasks[i].ad=t+tasks[i].p;
|
||||||
|
tasks[i].ta=tasks[i].p;
|
||||||
// Main simulation loop
|
|
||||||
for (int time = 0; time < simulation_time; time++) {
|
|
||||||
// 1. Check for task arrivals (same as RM)
|
|
||||||
for (int i = 0; i < num_tasks; i++) {
|
|
||||||
if (tasks[i].time_to_arrival == 0) {
|
|
||||||
if (tasks[i].remaining_execution > 0) {
|
|
||||||
printf("!!! Time %d: Task %d MISSED DEADLINE !!!\n", time, tasks[i].id);
|
|
||||||
}
|
|
||||||
tasks[i].remaining_execution = tasks[i].execution_time;
|
|
||||||
tasks[i].absolute_deadline = time + tasks[i].period;
|
|
||||||
tasks[i].time_to_arrival = tasks[i].period;
|
|
||||||
}
|
}
|
||||||
tasks[i].time_to_arrival--;
|
tasks[i].ta--;
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Select highest priority task to run (EDF: Earliest Absolute Deadline has highest priority)
|
|
||||||
int task_to_run = -1;
|
|
||||||
int earliest_deadline = 10000; // Initialize with a late deadline
|
|
||||||
|
|
||||||
for (int i = 0; i < num_tasks; i++) {
|
|
||||||
// Check if task is ready
|
|
||||||
if (tasks[i].remaining_execution > 0) {
|
|
||||||
// EDF priority check: Lower deadline value means higher priority (earlier deadline)
|
|
||||||
if (tasks[i].absolute_deadline < earliest_deadline) {
|
|
||||||
earliest_deadline = tasks[i].absolute_deadline;
|
|
||||||
task_to_run = i; // Select this task
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Execute the selected task (same as RM)
|
|
||||||
if (task_to_run != -1) {
|
|
||||||
printf("Time %d: Task %d running\n", time, tasks[task_to_run].id);
|
|
||||||
tasks[task_to_run].remaining_execution--;
|
|
||||||
// Optional: Check finish
|
|
||||||
// if (tasks[task_to_run].remaining_execution == 0) {
|
|
||||||
// printf("Time %d: Task %d finished\n", time + 1, tasks[task_to_run].id);
|
|
||||||
// }
|
|
||||||
} else {
|
|
||||||
printf("Time %d: CPU Idle\n", time);
|
|
||||||
}
|
}
|
||||||
|
int r=-1,ed=10000;
|
||||||
|
for(int i=0;i<nt;i++)
|
||||||
|
if(tasks[i].re>0&&tasks[i].ad<ed){ed=tasks[i].ad;r=i;}
|
||||||
|
if(r>=0){printf("T%d:Task%d run\n",t,tasks[r].id);tasks[r].re--;}
|
||||||
|
else printf("T%d:Idle\n",t);
|
||||||
}
|
}
|
||||||
printf("--- EDF Simulation Complete ---\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Main Function ---
|
int main(){simulate_rm();simulate_edf();return 0;}
|
||||||
int main() {
|
|
||||||
// Run Rate Monotonic simulation
|
|
||||||
simulate_rm();
|
|
||||||
|
|
||||||
// Run Earliest Deadline First simulation
|
|
||||||
simulate_edf();
|
|
||||||
|
|
||||||
return 0; // Indicate successful execution
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue