Assignment 1: To implement scheduling algorithms like FCFS, RR,
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");
for (int i = 0; i < n; i++) {
// Calculate waiting time and turnaround time
for each process
turnaroundTime = turnaroundTime +
processes[i].burstTime;
printf("P%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n",
processes[i].id, processes[i].arrivalTime,
processes[i].burstTime, waitingTime,
turnaroundTime);
waitingTime = turnaroundTime;
[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("\nRound Robin Scheduling (Time Quantum:
%d)\n", timeQuantum);
printf("---------------------------------------------
------\n");
printf("Process\t| Arrival Time\t| Burst Time\t|
Waiting Time\t| Turnaround Time\n");
printf("---------------------------------------------
------\n");
int time = 0; // Current time
int flag; // Flag to check if any process is
remaining
do {
flag = 0;
for (int i = 0; i < n; i++) {
if (remainingTime[i] > 0) {
flag = 1;
int executeTime = (remainingTime[i] <
timeQuantum) ? remainingTime[i] : timeQuantum;
[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("\nShortest Job First (SJF) Scheduling\
n");
printf("---------------------------------------------
------\n");
printf("Process\t| Arrival Time\t| Burst Time\t|
Waiting Time\t| Turnaround Time\n");
[3]
printf("---------------------------------------------
------\n");
int time = 0; // Current time
for (int i = 0; i < n; i++) {
int shortest = i; // Assume the current
process has the shortest burst time
for (int j = i + 1; j < n; j++) {
if (processes[j].burstTime <
processes[shortest].burstTime &&
processes[j].arrivalTime <= time) {
shortest = j;
}
}
// Swap processes[i] and processes[shortest]
struct Process temp = processes[i];
processes[i] = processes[shortest];
processes[shortest] = temp;
int executeTime = processes[i].burstTime;
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;
}
}
[4]
int main() {
int n, timeQuantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter arrival time for process P%d:
", i + 1);
scanf("%d", &processes[i].arrivalTime);
printf("Enter burst time for process P%d: ",
i + 1);
scanf("%d", &processes[i].burstTime);
}
printf("Enter time quantum for Round Robin
scheduling: ");
scanf("%d", &timeQuantum);
FCFS(processes, n);
RR(processes, n, timeQuantum);
SJF(processes, 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
---------------------------------------------------
Round Robin Scheduling (Time Quantum: 0)
---------------------------------------------------
Process | Arrival Time | Burst Time | Waiting Time |
Turnaround Time
---------------------------------------------------
Shortest Job First (SJF) Scheduling
---------------------------------------------------
Process | Arrival Time | Burst Time | Waiting Time |
Turnaround Time
---------------------------------------------------
2: To implement the banker's algorithm
#include <stdio.h>
#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];
}
int finish[MAX_PROCESSES] = {0}; // Array to
track finished processes
int safeSequence[MAX_PROCESSES];
int count = 0;
while (count < MAX_PROCESSES) {
int found = 0;
for (int i = 0; i < MAX_PROCESSES; i++) {
if (finish[i] == 0) {
[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;
2. Alter the Schema of a Table:
-- Add a new column to the EmployeeTable
ALTER TABLE EmployeeTable
ADD Email NVARCHAR(255);
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
CREATE TABLE Authors (
AuthorID INT PRIMARY KEY,
AuthorName VARCHAR(50) NOT NULL,
BirthDate DATE,
UNIQUE (AuthorName), -- Unique constraint on
AuthorName
CONSTRAINT CHK_Authors_BirthDate CHECK (BirthDate
<= CURRENT_DATE())
);
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
ISBN VARCHAR(13) UNIQUE,
PublishedYear INT CHECK (PublishedYear >= 0),
AuthorID INT,
CONSTRAINT FK_Books_Authors FOREIGN KEY
(AuthorID) REFERENCES Authors(AuthorID),
CONSTRAINT CHK_Books_PublishedYear CHECK
(PublishedYear <= YEAR(CURRENT_DATE())),
[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');
INSERT INTO Books (BookID, Title, ISBN,
PublishedYear, AuthorID) VALUES
(101, 'Introduction to SQL', '1234567890123',
2020, 1),
(102, 'Database Design Basics', '9876543210987',
2018, 2);
Output:
[17]