Database and OS
Database and OS
SJF
#include <stdio.h>
#include <stdlib.h>
struct Process {
int id; // Process ID
int arrivalTime; // Arrival time
int burstTime; // Burst time
};
void FCFS(struct Process processes[], int n) {
int waitingTime = 0, turnaroundTime = 0;
printf("\nFCFS Scheduling\n");
printf("---------------------------------------------
------\n");
printf("Process\t| Arrival Time\t| Burst Time\t|
Waiting Time\t| Turnaround Time\n");
printf("---------------------------------------------
------\n");
[1]
}
}
void RR(struct Process processes[], int n, int
timeQuantum) {
int remainingTime[n];
int waitingTime = 0, turnaroundTime = 0;
for (int i = 0; i < n; i++) {
remainingTime[i] = processes[i].burstTime;
}
printf("---------------------------------------------
------\n");
printf("Process\t| Arrival Time\t| Burst Time\t|
Waiting Time\t| Turnaround Time\n");
printf("---------------------------------------------
------\n");
[2]
remainingTime[i] -= executeTime;
waitingTime += time -
processes[i].arrivalTime;
turnaroundTime += time -
processes[i].arrivalTime + executeTime;
printf("P%d\t|\t%d\t|\t%d\t|\t%d\t|\t
%d\n", processes[i].id, processes[i].arrivalTime,
executeTime, waitingTime,
turnaroundTime);
time += executeTime;
}
}
} while (flag);
}
void SJF(struct Process processes[], int n) {
int remainingTime[n];
int waitingTime = 0, turnaroundTime = 0;
for (int i = 0; i < n; i++) {
remainingTime[i] = processes[i].burstTime;
}
printf("---------------------------------------------
------\n");
printf("Process\t| Arrival Time\t| Burst Time\t|
Waiting Time\t| Turnaround Time\n");
[3]
printf("---------------------------------------------
------\n");
return 0;
}
Output:
Enter the number of processes: Enter time quantum for Round Robin
scheduling:
FCFS Scheduling
[5]
---------------------------------------------------
Process | Arrival Time | Burst Time | Waiting Time |
Turnaround Time
---------------------------------------------------
#define MAX_PROCESSES 5
#define MAX_RESOURCES 3
int available[MAX_RESOURCES];
int maximum[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
[6]
int isSafe(int process, int request[]) {
for (int i = 0; i < MAX_RESOURCES; i++) {
if (request[i] > need[process][i] ||
request[i] > available[i]) {
return 0;
}
}
for (int i = 0; i < MAX_RESOURCES; i++) {
available[i] -= request[i];
allocation[process][i] += request[i];
need[process][i] -= request[i];
}
int work[MAX_RESOURCES];
for (int i = 0; i < MAX_RESOURCES; i++) {
work[i] = available[i];
}
[7]
int j;
for (j = 0; j < MAX_RESOURCES; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == MAX_RESOURCES) {
for (int k = 0; k <
MAX_RESOURCES; k++) {
work[k] += allocation[i][k];
}
safeSequence[count++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (found == 0) {
// No safe sequence found
return 0;
}
}
for (int i = 0; i < MAX_RESOURCES; i++) {
available[i] += request[i];
allocation[process][i] -= request[i];
need[process][i] += request[i];
}
return 1; // Request can be granted
[8]
}
void displayState() {
printf("Available resources: ");
for (int i = 0; i < MAX_RESOURCES; i++) {
printf("%d ", available[i]);
}
printf("\n");
printf("Maximum resources:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", maximum[i][j]);
}
printf("\n");
}
printf("Allocated resources:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}
printf("Remaining needs:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}
[9]
}
int main() {
// Initialize available resources
printf("Enter the available resources for each
resource type:\n");
for (int i = 0; i < MAX_RESOURCES; i++) {
scanf("%d", &available[i]);
}
printf("Enter the maximum resources required for
each process and resource type:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
scanf("%d", &maximum[i][j]);
need[i][j] = maximum[i][j];
}
}
printf("Enter the allocated resources for each
process and resource type:\n");
for (int i = 0; i < MAX_PROCESSES; i++) {
for (int j = 0; j < MAX_RESOURCES; j++) {
scanf("%d", &allocation[i][j]);
need[i][j] -= allocation[i][j];
}
}
int process;
printf("Enter the process number (0 to %d)
requesting resources: ", MAX_PROCESSES - 1);
scanf("%d", &process);
[10]
int request[MAX_RESOURCES];
printf("Enter the requested resources for each
resource type:\n");
for (int i = 0; i < MAX_RESOURCES; i++) {
scanf("%d", &request[i]);
}
if (isSafe(process, request)) {
printf("Request can be granted. System is in
a safe state.\n");
printf("Safe sequence: ");
for (int i = 0; i < MAX_PROCESSES; i++) {
printf("%d ", i);
}
printf("\n");
displayState();
} else {
printf("Request cannot be granted. System is
not in a safe state.\n");
}
return 0;
}
Output:
[11]
Assignment 3: To drop a table, alter the schema of a table, and
insert/update/delete records using tables created in previous
Assignments. (Use simple forms of insert/update/delete
statements)
1. Drop a Table:
-- Drop the EmployeeTable if it exists
IF OBJECT_ID('EmployeeTable', 'U') IS NOT NULL
DROP TABLE EmployeeTable;
3. Insert Records:
-- Insert a new record into the EmployeeTable
INSERT INTO EmployeeTable (EmployeeID, EmployeeName,
Salary, Department)
VALUES (4, 'Alice Johnson', 70000, 'Marketing');
[12]
4. Update Records:
-- Update the salary for a specific employee in the
EmployeeTable
UPDATE EmployeeTable
SET Salary = 75000
WHERE EmployeeID = 1;
5. Delete Records:
-- Delete a record from the EmployeeTable
DELETE FROM EmployeeTable
WHERE EmployeeID = 2;
Output:
[13]
Assignment 4: To create simple tables with only the primary key
constraint (as a table-level constraint & and as a field level
constraint) (include all data types),
CREATE TABLE TableWithTableLevelPK (
ID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT
);
CREATE TABLE TableWithFieldLevelPK (
ID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
PRIMARY KEY (ID)
);
INSERT INTO TableWithTableLevelPK (ID, FirstName,
LastName, Age) VALUES
(1, 'John', 'Doe', 25),
(2, 'Jane', 'Smith', 30);
[14]
INSERT INTO TableWithFieldLevelPK (ID, FirstName,
LastName, Age) VALUES
(101, 'Alice', 'Johnson', 28),
(102, 'Bob', 'Williams', 35);
Output:
[15]
Assignment 5: To create more than one table, with referential
integrity constraint, PK constrain, Check constraint, Unique
constraint, not null constraint
[16]
CONSTRAINT CHK_Books_Title_Length CHECK
(LENGTH(Title) <= 100)
);
INSERT INTO Authors (AuthorID, AuthorName, BirthDate)
VALUES
(1, 'John Doe', '1980-01-01'),
(2, 'Jane Smith', '1990-05-15');
Output:
[17]