From 1514e4d9728c8a2340da20e9f6641ff82f09b3ff Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 18 Feb 2025 14:55:12 +0530 Subject: [PATCH 01/10] Update DBMS/SQL/Week7/writeup.md --- DBMS/SQL/Week7/writeup.md | 176 +++++++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 1 deletion(-) diff --git a/DBMS/SQL/Week7/writeup.md b/DBMS/SQL/Week7/writeup.md index 235f665..ae8d1a9 100644 --- a/DBMS/SQL/Week7/writeup.md +++ b/DBMS/SQL/Week7/writeup.md @@ -1 +1,175 @@ -## Week 7 - DBMS Lab - PL SQL & Triggers \ No newline at end of file +## Week 7 - DBMS Lab - PL SQL & Triggers + +### DB + +```sql +SQL> select table_name from user_tables; + +TABLE_NAME +-------------------------------------------------------------------------------- +ACCIDENT +CAR +OWNS +PARTICIPATED +PERSON + +SQL> select * from accident; + +REPORT_NUMBER ACCD_DATE LOCATION +------------- --------- -------------------------------------------------- + 1 01-JAN-24 Delhi India + 3 29-FEB-24 India + 5 31-MAR-00 Gujrat India + 12 17-JUL-00 karnataka India + +SQL> select * from car; + +REGNO MODEL YEAR +-------------------- ------------------------------ ---------- +ABCD0001 Celtos 3 +EFGH2001 Ferrari 32 +DSDS0001 Urus 5 +ABCD4001 Honda City 1 +HFSP5601 mini cooper 7 + +SQL> select * from owns; + +DRIVER_ID# REGNO +------------------------------ -------------------- +1234 ABCD0001 +1235 EFGH2001 +1236 DSDS0001 +1237 ABCD4001 +1238 HFSP5601 + +SQL> select * from participated; + +DRIVER_ID# REGNO REPORT_NUMBER DAMAGE_AMOUNT +------------------------------ -------------------- ------------- ------------- +1234 ABCD0001 1 10000 +1236 DSDS0001 3 150 +1238 HFSP5601 5 1500 +1238 HFSP5601 12 25000 + +SQL> select * from person; + +DRIV NAME ADDRESS +---- ---------- ------------------------------ +1234 Rohan Delhi India +1237 Saarthak Manipal India +1236 Ramesh Maharashtra India +1235 Rohit Banglalore India +1238 Amogh Muzaffarnagar +``` + +### Q1. Generate a trigger displaying driver information, on participating in an accident. + +Trigger: +```sql +CREATE OR REPLACE TRIGGER trg_display_driver_info +AFTER INSERT ON participated +FOR EACH ROW +DECLARE + v_driver_name VARCHAR2(100); + v_driver_address VARCHAR2(255); +BEGIN + -- Fetch driver information based on DRIVER_ID# + SELECT NAME, ADDRESS + INTO v_driver_name, v_driver_address + FROM person + WHERE DRIVER_ID# = :NEW.DRIVER_ID#; + + -- Display the driver information + DBMS_OUTPUT.PUT_LINE('Driver ID: ' || :NEW.DRIVER_ID#); + DBMS_OUTPUT.PUT_LINE('Driver Name: ' || v_driver_name); + DBMS_OUTPUT.PUT_LINE('Driver Address: ' || v_driver_address); + DBMS_OUTPUT.PUT_LINE('Car Registration Number: ' || :NEW.REGNO); + DBMS_OUTPUT.PUT_LINE('Report Number: ' || :NEW.REPORT_NUMBER); + DBMS_OUTPUT.PUT_LINE('Damage Amount: ' || :NEW.DAMAGE_AMOUNT); +END; +/ +``` + +Demo: +```sql +SQL> INSERT INTO participated (DRIVER_ID#, REGNO, REPORT_NUMBER, DAMAGE_AMOUNT) + 2 VALUES (1235, 'EFGH2001', 3, 5000); +Driver ID: 1235 +Driver Name: Rohit +Driver Address: Banglalore India +Car Registration Number: EFGH2001 +Report Number: 3 +Damage Amount: 5000 +Driver_id: 1235 Name: Rohit Address: Banglalore India + +1 row created. +``` + +### Q2. + + + +### Q3. List cars involved in accidents with cumulative damage exceeding a specific amount. + +Checking for damage amount > 10000, we have +```SQL +SQL> SELECT + p.driver_id#, + o.regno, + SUM(pa.damage_amount) AS total_damage + FROM + PARTICIPATED pa + JOIN + OWNS o ON pa.driver_id# = o.driver_id# + JOIN + PERSON p ON o.driver_id# = p.driver_id# + GROUP BY + p.driver_id#, o.regno + HAVING + SUM(pa.damage_amount) > 10000; + +DRIVER_ID# REGNO TOTAL_DAMAGE +------------------------------ -------------------- ------------ +1238 HFSP5601 26500 +``` + +### Q4. Identify cars that have been involved in more than one accident and calculate the total damage for each car. + +```SQL +SQL> SELECT + o.regno, + COUNT(DISTINCT pa.report_number) AS accident_count, + SUM(pa.damage_amount) AS total_damage +FROM + PARTICIPATED pa +JOIN + OWNS o ON pa.driver_id# = o.driver_id# +GROUP BY + o.regno +HAVING + COUNT(DISTINCT pa.report_number) > 1; + +REGNO ACCIDENT_COUNT TOTAL_DAMAGE +-------------------- -------------- ------------ +HFSP5601 2 26500 +``` + +### Q5. Calculate the average damage amount for accidents at each location. +```sql +SQL> SELECT + a.location, + AVG(pa.damage_amount) AS average_damage + FROM + ACCIDENT a + JOIN + PARTICIPATED pa ON a.report_number = pa.report_number + GROUP BY + a.location; + +LOCATION AVERAGE_DAMAGE +-------------------------------------------------- -------------- + karnataka India 25000 +Delhi India 10000 + India 2575 + Gujrat India 1500 + ``` \ No newline at end of file From f50567be0d15c8571559ee1031c7971501ef4020 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 18 Feb 2025 14:55:37 +0530 Subject: [PATCH 02/10] Update DBMS/SQL/Week7/writeup.md --- DBMS/SQL/Week7/writeup.md | 62 --------------------------------------- 1 file changed, 62 deletions(-) diff --git a/DBMS/SQL/Week7/writeup.md b/DBMS/SQL/Week7/writeup.md index ae8d1a9..f113eb0 100644 --- a/DBMS/SQL/Week7/writeup.md +++ b/DBMS/SQL/Week7/writeup.md @@ -1,67 +1,5 @@ ## Week 7 - DBMS Lab - PL SQL & Triggers -### DB - -```sql -SQL> select table_name from user_tables; - -TABLE_NAME --------------------------------------------------------------------------------- -ACCIDENT -CAR -OWNS -PARTICIPATED -PERSON - -SQL> select * from accident; - -REPORT_NUMBER ACCD_DATE LOCATION -------------- --------- -------------------------------------------------- - 1 01-JAN-24 Delhi India - 3 29-FEB-24 India - 5 31-MAR-00 Gujrat India - 12 17-JUL-00 karnataka India - -SQL> select * from car; - -REGNO MODEL YEAR --------------------- ------------------------------ ---------- -ABCD0001 Celtos 3 -EFGH2001 Ferrari 32 -DSDS0001 Urus 5 -ABCD4001 Honda City 1 -HFSP5601 mini cooper 7 - -SQL> select * from owns; - -DRIVER_ID# REGNO ------------------------------- -------------------- -1234 ABCD0001 -1235 EFGH2001 -1236 DSDS0001 -1237 ABCD4001 -1238 HFSP5601 - -SQL> select * from participated; - -DRIVER_ID# REGNO REPORT_NUMBER DAMAGE_AMOUNT ------------------------------- -------------------- ------------- ------------- -1234 ABCD0001 1 10000 -1236 DSDS0001 3 150 -1238 HFSP5601 5 1500 -1238 HFSP5601 12 25000 - -SQL> select * from person; - -DRIV NAME ADDRESS ----- ---------- ------------------------------ -1234 Rohan Delhi India -1237 Saarthak Manipal India -1236 Ramesh Maharashtra India -1235 Rohit Banglalore India -1238 Amogh Muzaffarnagar -``` - ### Q1. Generate a trigger displaying driver information, on participating in an accident. Trigger: From 4c49bd8d7d0ba5d35b05345b9c70854fd1632714 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 18 Feb 2025 14:55:55 +0530 Subject: [PATCH 03/10] Update DBMS/SQL/Week7/writeup.md --- DBMS/SQL/Week7/writeup.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/DBMS/SQL/Week7/writeup.md b/DBMS/SQL/Week7/writeup.md index f113eb0..c5445fd 100644 --- a/DBMS/SQL/Week7/writeup.md +++ b/DBMS/SQL/Week7/writeup.md @@ -11,13 +11,11 @@ DECLARE v_driver_name VARCHAR2(100); v_driver_address VARCHAR2(255); BEGIN - -- Fetch driver information based on DRIVER_ID# SELECT NAME, ADDRESS INTO v_driver_name, v_driver_address FROM person WHERE DRIVER_ID# = :NEW.DRIVER_ID#; - -- Display the driver information DBMS_OUTPUT.PUT_LINE('Driver ID: ' || :NEW.DRIVER_ID#); DBMS_OUTPUT.PUT_LINE('Driver Name: ' || v_driver_name); DBMS_OUTPUT.PUT_LINE('Driver Address: ' || v_driver_address); From 2bd895d3bf8d9b86390e5355ec6ca0bbc729a158 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 18 Feb 2025 15:06:12 +0530 Subject: [PATCH 04/10] Update DBMS/SQL/Week7/writeup.md --- DBMS/SQL/Week7/writeup.md | 63 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/DBMS/SQL/Week7/writeup.md b/DBMS/SQL/Week7/writeup.md index c5445fd..88132aa 100644 --- a/DBMS/SQL/Week7/writeup.md +++ b/DBMS/SQL/Week7/writeup.md @@ -41,8 +41,69 @@ Driver_id: 1235 Name: Rohit Address: Banglalore India 1 row created. ``` -### Q2. +### Q2. Create a trigger that updates a `total_damage` column in the `accident` table whenever a new entry is added to or removed from the participated field.` +```SQL +SQL> CREATE OR REPLACE TRIGGER update_total_damage + 2 AFTER + 3 INSERT OR DELETE ON PARTICIPATED + 4 BEGIN + 5 UPDATE ACCIDENT + 6 SET + 7 total_damage = ( + 8 SELECT + 9 SUM(damage_amount) + 10 FROM PARTICIPATED p + 11 WHERE + 12 p.report_number = ACCIDENT.report_number + 13 ); + 14 END; + 15 / + +Trigger created. +``` +```sql +SQL> ALTER TABLE ACCIDENT ADD total_damage NUMBER; +``` +```sql +SQL> INSERT INTO + PARTICIPATED (driver_id#, regno, report_number, damage_amount) + VALUES + ('1235', 'EFGH2001', 1, 5000); +Driver_id: 1235 Name: Rohit Address: Banglalore India + +1 row created. + +SQL> SELECT + report_number, + total_damage + FROM ACCIDENT + WHERE + report_number = 1; + +REPORT_NUMBER TOTAL_DAMAGE +------------- ------------ + 1 15000 + +SQL> DELETE FROM PARTICIPATED + WHERE + driver_id# = '1235' + AND regno = 'EFGH2001' + AND report_number = 1; + +1 row deleted. + +SQL> SELECT + report_number, + total_damage + FROM ACCIDENT + WHERE + report_number = 1; + +REPORT_NUMBER TOTAL_DAMAGE +------------- ------------ + 1 10000 +``` ### Q3. List cars involved in accidents with cumulative damage exceeding a specific amount. From 5a67380e0c6a00ac8c41b20e368d8f0d01d9d8e3 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 18 Feb 2025 15:07:22 +0530 Subject: [PATCH 05/10] Update DBMS/SQL/Week7/writeup.md --- DBMS/SQL/Week7/writeup.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/DBMS/SQL/Week7/writeup.md b/DBMS/SQL/Week7/writeup.md index 88132aa..1b4f589 100644 --- a/DBMS/SQL/Week7/writeup.md +++ b/DBMS/SQL/Week7/writeup.md @@ -42,7 +42,7 @@ Driver_id: 1235 Name: Rohit Address: Banglalore India ``` ### Q2. Create a trigger that updates a `total_damage` column in the `accident` table whenever a new entry is added to or removed from the participated field.` - +Trigger: ```SQL SQL> CREATE OR REPLACE TRIGGER update_total_damage 2 AFTER @@ -62,9 +62,11 @@ SQL> CREATE OR REPLACE TRIGGER update_total_damage Trigger created. ``` +Adding an identifier `total_damage`: ```sql SQL> ALTER TABLE ACCIDENT ADD total_damage NUMBER; ``` +Test case with Insertion: ```sql SQL> INSERT INTO PARTICIPATED (driver_id#, regno, report_number, damage_amount) @@ -84,7 +86,9 @@ SQL> SELECT REPORT_NUMBER TOTAL_DAMAGE ------------- ------------ 1 15000 - +``` +Test case with Deletion: +```sql SQL> DELETE FROM PARTICIPATED WHERE driver_id# = '1235' From 8eead06307f4469a32b378cb6385a05e6329c93e Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 18 Feb 2025 15:26:31 +0530 Subject: [PATCH 06/10] Add Tinkering/client.ps1 --- Tinkering/client.ps1 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Tinkering/client.ps1 diff --git a/Tinkering/client.ps1 b/Tinkering/client.ps1 new file mode 100644 index 0000000..81c177d --- /dev/null +++ b/Tinkering/client.ps1 @@ -0,0 +1,15 @@ +$client = New-Object System.Net.Sockets.TcpClient -ArgumentList @("172.16.54.31", 6969) +$stream = $client.GetStream() +$reader = New-Object System.IO.StreamReader -ArgumentList $stream +$writer = New-Object System.IO.StreamWriter -ArgumentList $stream +$writer.AutoFlush = $true + +while ($true) { + $message = Read-Host "You" + $writer.WriteLine($message) + + $received = $reader.ReadLine() + Write-Host "Server: $received" +} + +$client.Close() From 31ebd76c78688ae5e6b8a3be68ed6c4626fdebfb Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 20 Feb 2025 10:03:33 +0530 Subject: [PATCH 07/10] deleted tinkering --- Tinkering/client.ps1 | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 Tinkering/client.ps1 diff --git a/Tinkering/client.ps1 b/Tinkering/client.ps1 deleted file mode 100644 index 81c177d..0000000 --- a/Tinkering/client.ps1 +++ /dev/null @@ -1,15 +0,0 @@ -$client = New-Object System.Net.Sockets.TcpClient -ArgumentList @("172.16.54.31", 6969) -$stream = $client.GetStream() -$reader = New-Object System.IO.StreamReader -ArgumentList $stream -$writer = New-Object System.IO.StreamWriter -ArgumentList $stream -$writer.AutoFlush = $true - -while ($true) { - $message = Read-Host "You" - $writer.WriteLine($message) - - $received = $reader.ReadLine() - Write-Host "Server: $received" -} - -$client.Close() From d53e55ce7c989767555ed1fd40ecd7e86c876b68 Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 20 Feb 2025 10:16:27 +0530 Subject: [PATCH 08/10] updated week7, week8 --- OS/C/Week6/menu | Bin 0 -> 34248 bytes OS/C/Week6/menudriven.c | 522 ++++++++++++++++++++-------------------- OS/C/Week7/aq1.c | 1 + OS/C/Week8/q1.c | 134 +++++++++++ 4 files changed, 395 insertions(+), 262 deletions(-) create mode 100755 OS/C/Week6/menu create mode 100644 OS/C/Week8/q1.c diff --git a/OS/C/Week6/menu b/OS/C/Week6/menu new file mode 100755 index 0000000000000000000000000000000000000000..a8eb922789c7a954c0390834a35aa764f6557623 GIT binary patch literal 34248 zcmeHQeQ;CPmA~?{gDl&z`3PUe_)%c7Ol${CYeK>zN00zb0x?dYN#G%D*;Xw}jwFY8 zfvysfEh%Zough-Ir3r>)Cz8pw88(~9J56EN+b-#*ld_wT>^4cwN4hc)rrBv{q!!2f zJNLaOKglxPNoW7qo%?34-aY4jo%6froOh)0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qjiCjsBKSuc+1N!#YjpRM7aND7aIVUnlTs zrg2VG2K_HtG|ukLX#e*t`dk)0l0|=-MHhoELBaVztQ3Win~E|Gh0fn7Lb+Za8Fo5x z_m%P8PurP^r&*k0n1$Ke(oD_jVCCcrlNpTJipXGKDmbu4HyDK*%>R~O0T;S{`%&>J z>)mEv(_1ZFZeI_(HM#%Y zg7P0Py+{4K8DBo}&UY8Ziyz{Es&du7B$0m-c(QlUI}P#a}wt_@CRa_T9ViyZM7Z+I`=~1D~z#yLR@c z4{W{qeCXMWzpRS({l!0c|F-h((of3%?89rzOB 0 && processes[i].at <= current_time) { - if (processes[i].rt == -1) { - processes[i].rt = current_time - processes[i].at; - } - - int execute_time = (processes[i].remaining_bt > quantum) ? quantum : processes[i].remaining_bt; processes[i].remaining_bt -= execute_time; current_time += execute_time; - - if (processes[i].remaining_bt == 0) { - completed++; - processes[i].ct = current_time; - processes[i].is_completed = 1; - } - - timeline[timeline_index++] = current_time; - - } else if (processes[i].at > current_time) { - current_time++; // if process hasn't arrived, time is incremented (to prevent a stall) - } - - - i = (i + 1) % n; - if (current_time > 1000) break; - } - - - calculate_times(processes, n); - - - float avg_ct, avg_tat, avg_wt, avg_rt; - calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt); - - - printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum); - display_table(processes, n); - - - printf("\nAverage Completion Time: %.2f\n", avg_ct); - printf("Average Turnaround Time: %.2f\n", avg_tat); + printf("\nAverage Completion Time: %.2f\n", avg_ct); + printf("Average Turnaround Time: %.2f\n", avg_tat); printf("Average Waiting Time: %.2f\n", avg_wt); printf("Average Response Time: %.2f\n", avg_rt); - display_gantt_chart(processes, n, timeline); - free(timeline); // Free memory - } + display_gantt_chart(processes, n, timeline, timeline_index); + free(timeline); +} + + void round_robin(Process processes[], int n, int quantum) { + + for (int i = 0; i < n; i++) { + processes[i].remaining_bt = processes[i].bt; + processes[i].rt = -1; + processes[i].is_completed = 0; + } - void non_preemptive_priority(Process processes[], int n) { - for (int i = 0; i < n - 1; i++) { - for (int j = 0; j < n - i - 1; j++) { - if (processes[j].at > processes[j + 1].at) { - swap(&processes[j], &processes[j + 1]); - } - } - } - - int current_time = 0; - int completed = 0; - int *timeline = (int *)malloc((n * 2) * sizeof(int)); + int current_time = 0; + int completed = 0; + int i = 0; + int *timeline = (int *)malloc((n * 100) * sizeof(int)); if (timeline == NULL) { - perror("Failed to allocate memory for timeline"); - return; - } + perror("Failed to allocate memory for timeline"); + return; + } int timeline_index = 0; + int last_process = -1; - while (completed != n) { - int highest_priority = -1; - int min_priority = 9999; // Large value + while (completed != n) { + if (processes[i].remaining_bt > 0 && processes[i].at <= current_time) { + if (processes[i].rt == -1) { + processes[i].rt = current_time - processes[i].at; + } + if(i != last_process) { + timeline[timeline_index++] = i; + last_process = i; + } - for (int j = 0; j < n; j++) { - if (processes[j].at <= current_time && processes[j].bt > 0 && processes[j].priority < min_priority) { - min_priority = processes[j].priority; - highest_priority = j; - } - } + int execute_time = (processes[i].remaining_bt > quantum) ? quantum : processes[i].remaining_bt; + processes[i].remaining_bt -= execute_time; + current_time += execute_time; - if (highest_priority == -1) { - current_time++; - continue; - } + if (processes[i].remaining_bt == 0) { + completed++; + processes[i].ct = current_time; + processes[i].is_completed = 1; + } - if (processes[highest_priority].rt == -1) { - processes[highest_priority].rt = - current_time - processes[highest_priority].at; - } + } else if (processes[i].at > current_time) { + current_time++; + } - current_time += processes[highest_priority].bt; + i = (i + 1) % n; + if (current_time > 1000) break; + } - processes[highest_priority].ct = current_time; - processes[highest_priority].bt = 0; // Mark completed + calculate_times(processes, n); - completed++; - timeline[timeline_index++] = current_time; - } + float avg_ct, avg_tat, avg_wt, avg_rt; + calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt); - calculate_times(processes, n); + printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum); + display_table(processes, n); - float avg_ct, avg_tat, avg_wt, avg_rt; - calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt); + printf("\nAverage Completion Time: %.2f\n", avg_ct); + printf("Average Turnaround Time: %.2f\n", avg_tat); + printf("Average Waiting Time: %.2f\n", avg_wt); + printf("Average Response Time: %.2f\n", avg_rt); - printf("\nNon-Preemptive Priority Scheduling:\n"); - display_table(processes, n); + display_gantt_chart(processes, n, timeline, timeline_index); + free(timeline); + } - printf("\nAverage Completion Time: %.2f\n", avg_ct); - printf("Average Turnaround Time: %.2f\n", avg_tat); - printf("Average Waiting Time: %.2f\n", avg_wt); - printf("Average Response Time: %.2f\n", avg_rt); + void non_preemptive_priority(Process processes[], int n) { + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (processes[j].at > processes[j + 1].at) { + swap(&processes[j], &processes[j + 1]); + } + } + } - display_gantt_chart(processes, n, timeline); - free(timeline); // Free memory - } + int current_time = 0; + int completed = 0; + int *timeline = (int *)malloc((n * 100) * sizeof(int)); + if (timeline == NULL) { + perror("Failed to allocate memory for timeline"); + return; + } - int main() { - int n, choice, quantum; + int timeline_index = 0; + int last_process = -1; - printf("Enter the number of processes: "); - scanf("%d", &n); + while (completed != n) { + int highest_priority = -1; + int min_priority = 9999; - Process processes[n]; + for (int j = 0; j < n; j++) { + if (processes[j].at <= current_time && processes[j].bt > 0 && processes[j].priority < min_priority) { + min_priority = processes[j].priority; + highest_priority = j; + } + } - // Input process details - for (int i = 0; i < n; i++) { + if (highest_priority == -1) { + current_time++; + continue; + } + + if (processes[highest_priority].rt == -1) { + processes[highest_priority].rt = current_time - processes[highest_priority].at; + } + + if(highest_priority != last_process) { + timeline[timeline_index++] = highest_priority; + last_process = highest_priority; + } + + current_time += processes[highest_priority].bt; + processes[highest_priority].ct = current_time; + processes[highest_priority].bt = 0; + completed++; + } + + calculate_times(processes, n); + + float avg_ct, avg_tat, avg_wt, avg_rt; + calculate_averages(processes, n, &avg_ct, &avg_tat, &avg_wt, &avg_rt); + + printf("\nNon-Preemptive Priority Scheduling:\n"); + display_table(processes, n); + + printf("\nAverage Completion Time: %.2f\n", avg_ct); + printf("Average Turnaround Time: %.2f\n", avg_tat); + printf("Average Waiting Time: %.2f\n", avg_wt); + printf("Average Response Time: %.2f\n", avg_rt); + + display_gantt_chart(processes, n, timeline, timeline_index); + free(timeline); + } + + int main() { + int n, choice, quantum; + + printf("Enter the number of processes: "); + scanf("%d", &n); + + Process processes[n]; + + // Input process details + for (int i = 0; i < n; i++) { printf("\nEnter details for process %d:\n", i + 1); printf("PID: "); scanf("%s", processes[i].pid); printf("Arrival Time: "); - scanf("%d", &processes[i].at); + scanf("%d", &processes[i].at); printf("Burst Time: "); - scanf("%d", &processes[i].bt); + scanf("%d", &processes[i].bt); printf("Priority (lower value = higher priority): "); - scanf("%d", &processes[i].priority); + scanf("%d", &processes[i].priority); processes[i].rt = 0; // Initialize response time - processes[i].is_completed = 0; // Initialize completion flag - } + processes[i].is_completed = 0; // Initialize completion flag + } - // Display initial table - printf("\nInitial Process Table:\n"); - printf("-----------------------\n"); - printf("| PID | AT | BT |\n"); - printf("-----------------------\n"); + // Display initial table + printf("\nInitial Process Table:\n"); + printf("-----------------------\n"); + printf("| PID | AT | BT |\n"); + printf("-----------------------\n"); - for (int i = 0; i < n; i++) { - printf("| %-5s | %-3d | %-3d |\n", processes[i].pid, processes[i].at, processes[i].bt); - } + for (int i = 0; i < n; i++) { + printf("| %-5s | %-3d | %-3d |\n", processes[i].pid, processes[i].at, processes[i].bt); + } printf("-----------------------\n"); - // Algorithm Selection Menu with Loop and Exit - while (1) { - printf("\nChoose a scheduling algorithm:\n"); - printf("1. Preemptive SJF\n"); - printf("2. Round Robin\n"); - printf("3. Non-Preemptive Priority\n"); - printf("4. Exit\n"); - printf("Enter your choice: "); + // Algorithm Selection Menu with Loop and Exit + while (1) { + printf("\nChoose a scheduling algorithm:\n"); + printf("1. Preemptive SJF\n"); + printf("2. Round Robin\n"); + printf("3. Non-Preemptive Priority\n"); + printf("4. Exit\n"); + printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { - case 1: - preemptive_sjf(processes, n); - break; - case 2: - printf("Enter the time quantum: "); - scanf("%d", &quantum); - round_robin(processes, n, quantum); - break; - case 3: - non_preemptive_priority(processes, n); - break; - case 4: - printf("Exiting program.\n"); - exit(0); - default: - printf("Invalid choice. Please try again.\n"); - } - } + case 1: + preemptive_sjf(processes, n); + break; + case 2: + printf("Enter the time quantum: "); + scanf("%d", &quantum); + round_robin(processes, n, quantum); + break; + case 3: + non_preemptive_priority(processes, n); + break; + case 4: + printf("Exiting program.\n"); + exit(0); + default: + printf("Invalid choice. Please try again.\n"); + } + } - return 0; - } + return 0; + } diff --git a/OS/C/Week7/aq1.c b/OS/C/Week7/aq1.c index e69de29..4cce698 100644 --- a/OS/C/Week7/aq1.c +++ b/OS/C/Week7/aq1.c @@ -0,0 +1 @@ +// Write a C program to solve the Dining-Philosophers problem. diff --git a/OS/C/Week8/q1.c b/OS/C/Week8/q1.c new file mode 100644 index 0000000..783cfe8 --- /dev/null +++ b/OS/C/Week8/q1.c @@ -0,0 +1,134 @@ +// Develop a program to simulate banker’s algorithm. (Consider safety and resource-request algorithms) + +#include + +#define MAX_PROCESSES 10 +#define MAX_RESOURCES 10 + +int processes, resources; +int available[MAX_RESOURCES]; +int maximum[MAX_PROCESSES][MAX_RESOURCES]; +int allocation[MAX_PROCESSES][MAX_RESOURCES]; +int need[MAX_PROCESSES][MAX_RESOURCES]; + +int safeSequence[MAX_PROCESSES]; + +void initialize() { + printf("Enter number of processes: "); + scanf("%d", &processes); + + printf("Enter number of resources: "); + scanf("%d", &resources); + + printf("\nEnter available resources:\n"); + for(int i=0; i work[j]) + break; + } + if(j == resources) { + for(int k=0; k need[process][i]) { + printf("Error: Request exceeds maximum claim\n"); + return; + } + if(request[i] > available[i]) { + printf("Error: Resources not available\n"); + return; + } + } + + // Try to allocate + for(int i=0; i Date: Thu, 20 Feb 2025 10:16:44 +0530 Subject: [PATCH 09/10] updated week7, week8 --- OS/C/Week8/aq1.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 OS/C/Week8/aq1.c diff --git a/OS/C/Week8/aq1.c b/OS/C/Week8/aq1.c new file mode 100644 index 0000000..e69de29 From 7847547bafc850068ee7973eecef148e83e0948f Mon Sep 17 00:00:00 2001 From: hello Date: Tue, 25 Feb 2025 13:36:10 +0530 Subject: [PATCH 10/10] added gitignore --- .gitignore | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41641c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,65 @@ +### MacOS System Files ### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db +.AppleDouble +.LSOverride +Icon +.DocumentRevisions-V100 +.fseventsd +.TemporaryItems +.VolumeIcon.icns + +### Obsidian ### +.obsidian/ + +### IDEs and Editors ### +.idea/ +.vscode/ +*.swp +*.swo +*~ + +### Node ### +node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +### Python ### +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +### Java ### +*.class +*.log +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar