Skip to content

Commit a6b74ac

Browse files
committed
Added Two More Scheduling Algorithms and Context Switch time with some bug fixes
1 parent 202d159 commit a6b74ac

20 files changed

Lines changed: 5014 additions & 6345 deletions

CPP_SETUP.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Setting Up C++ Development in VS Code
2+
3+
## Step 1: Install Visual Studio Build Tools
4+
5+
Since you want to use C++, you need the Visual Studio Build Tools compiler. Follow these steps:
6+
7+
### On Windows:
8+
9+
1. **Download Visual Studio Build Tools**
10+
- Go to: https://visualstudio.microsoft.com/visual-cpp-build-tools/
11+
- Click "Download Build Tools"
12+
13+
2. **Run the Installer**
14+
- Open the downloaded `.exe` file
15+
- When prompted for workloads, select: **"Desktop development with C++"**
16+
- Include these components:
17+
- MSVC v143 or later
18+
- Windows 10/11 SDK
19+
- CMake tools
20+
21+
3. **Verify Installation**
22+
```bash
23+
where cl.exe
24+
```
25+
You should see a path to the compiler.
26+
27+
4. **Restart Your Terminal**
28+
- Close and reopen your terminal/VS Code
29+
30+
### On macOS:
31+
```bash
32+
xcode-select --install
33+
```
34+
35+
### On Linux (Ubuntu/Debian):
36+
```bash
37+
sudo apt-get install build-essential python3
38+
```
39+
40+
## Step 2: Install Dependencies
41+
42+
After installing the C++ tools, run:
43+
```bash
44+
npm install
45+
```
46+
47+
This will:
48+
- Install all Node.js dependencies
49+
- Build the C++ addon automatically (via `postinstall`)
50+
51+
## Step 3: Verify C++ IntelliSense in VS Code
52+
53+
1. **Install the C++ Extension**
54+
- Open VS Code
55+
- Go to Extensions (Ctrl+Shift+X)
56+
- Search for "C++"
57+
- Install "C/C++" by Microsoft
58+
59+
2. **Select Configuration**
60+
- Open any `.cpp` file in the `cpp/` folder
61+
- You'll see a popup to select a configuration (Win32/Linux/Mac)
62+
- Choose based on your OS
63+
64+
3. **Verify IntelliSense**
65+
- Open `cpp/include/Process.h`
66+
- Hover over types - you should see tooltips
67+
- Ctrl+Click on includes - they should resolve
68+
69+
## Step 4: Build and Run
70+
71+
### Build the C++ Module:
72+
```bash
73+
npm run build-native
74+
```
75+
76+
### Run Development Server:
77+
```bash
78+
npm run dev
79+
```
80+
81+
### Production Build:
82+
```bash
83+
npm run build
84+
```
85+
86+
## Troubleshooting
87+
88+
### "cl.exe not found"
89+
- Make sure Visual Studio Build Tools is installed
90+
- Restart Terminal/VS Code after installation
91+
- Try: `npm config set msvs_version 2022`
92+
93+
### "IntelliSense not working"
94+
- Reload VS Code: Ctrl+R or Cmd+R
95+
- Select the correct configuration in the C++ extension
96+
- Delete `.vscode/settings.json` IntelliSense cache
97+
98+
### Build still fails
99+
- Run: `npm run clean-native && npm install`
100+
- Check build output: `npm run build-native -- --verbose`
101+
102+
## File Structure
103+
104+
Your C++ code is organized as:
105+
- `cpp/include/` - Header files (Process.h, Algorithms.h)
106+
- `cpp/src/` - Implementation files
107+
- `binding.cc` - Node.js bindings
108+
- `*FirstServe.cpp` - Algorithm implementations
109+
110+
The compiled addon will be available at:
111+
- `build/Release/scheduling_algorithms.node` (Windows/Linux/Mac)
112+
113+
## Next Steps
114+
115+
Once everything is set up:
116+
1. Edit C++ files in `cpp/src/`
117+
2. Run `npm run dev` to rebuild and test
118+
3. Use VS Code debugging with the "Debug Development Server" configuration
119+
4. Changes will be hot-reloaded when you modify files
120+
121+
Happy coding with C++! 🚀

app/api/schedule/route.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { NextResponse } from "next/server";
2+
import {
3+
firstComeFirstServe,
4+
roundRobin,
5+
shortestJobFirst,
6+
shortestRemainingTimeFirst,
7+
priorityNonPreemptive,
8+
priorityPreemptive,
9+
type Process,
10+
} from "@/lib/scheduling-native";
11+
12+
export const runtime = "nodejs";
13+
14+
type ScheduleRequest = {
15+
algorithm: "FCFS" | "SJF" | "RR" | "SRTF" | "PNP" | "PP";
16+
quantum?: number;
17+
contextSwitch?: number;
18+
processes: Process[];
19+
};
20+
21+
function applyContextSwitch(sequence: Process[], contextSwitch: number): Process[] {
22+
if (contextSwitch <= 0 || sequence.length <= 1) {
23+
return sequence;
24+
}
25+
26+
const withContext: Process[] = [];
27+
28+
for (let i = 0; i < sequence.length; i++) {
29+
const current = sequence[i];
30+
withContext.push(current);
31+
32+
if (i === sequence.length - 1) {
33+
continue;
34+
}
35+
36+
const next = sequence[i + 1];
37+
const isProcessSwitch =
38+
current.process_id > 0 &&
39+
next.process_id > 0 &&
40+
current.process_id !== next.process_id;
41+
42+
if (isProcessSwitch) {
43+
withContext.push({
44+
process_id: -2,
45+
arrival_time: -1,
46+
burst_time: contextSwitch,
47+
priority: 0,
48+
background: "#6b7280",
49+
});
50+
}
51+
}
52+
53+
return withContext;
54+
}
55+
56+
export async function POST(req: Request) {
57+
try {
58+
const body = (await req.json()) as ScheduleRequest;
59+
const { algorithm, quantum, contextSwitch = 0, processes } = body;
60+
61+
if (!Array.isArray(processes) || processes.length === 0) {
62+
return NextResponse.json(
63+
{ error: "No processes provided." },
64+
{ status: 400 }
65+
);
66+
}
67+
68+
if (contextSwitch < 0) {
69+
return NextResponse.json(
70+
{ error: "Context switch time cannot be negative." },
71+
{ status: 400 }
72+
);
73+
}
74+
75+
let sequence: Process[] = [];
76+
switch (algorithm) {
77+
case "FCFS":
78+
sequence = firstComeFirstServe(processes);
79+
break;
80+
case "SJF":
81+
sequence = shortestJobFirst(processes);
82+
break;
83+
case "RR":
84+
if (!quantum || quantum <= 0) {
85+
return NextResponse.json(
86+
{ error: "Quantum must be greater than 0 for Round Robin." },
87+
{ status: 400 }
88+
);
89+
}
90+
sequence = roundRobin(processes, quantum);
91+
break;
92+
case "SRTF":
93+
sequence = shortestRemainingTimeFirst(processes);
94+
break;
95+
case "PNP":
96+
sequence = priorityNonPreemptive(processes);
97+
break;
98+
case "PP":
99+
sequence = priorityPreemptive(processes);
100+
break;
101+
default:
102+
return NextResponse.json(
103+
{ error: "Unsupported algorithm." },
104+
{ status: 400 }
105+
);
106+
}
107+
108+
const finalSequence = applyContextSwitch(sequence, contextSwitch);
109+
110+
return NextResponse.json({ sequence: finalSequence });
111+
} catch {
112+
return NextResponse.json(
113+
{ error: "Failed to run scheduling algorithm." },
114+
{ status: 500 }
115+
);
116+
}
117+
}

binding.gyp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
'cpp/src/FirstComeFirstServe.cpp',
88
'cpp/src/RoundRobin.cpp',
99
'cpp/src/ShortestJobFirst.cpp',
10-
'cpp/src/ShortestRemainingTimeFirst.cpp'
10+
'cpp/src/ShortestRemainingTimeFirst.cpp',
11+
'cpp/src/PriorityNonPreemptive.cpp',
12+
'cpp/src/PriorityPreemptive.cpp'
1113
],
1214
'include_dirs': [
1315
'cpp/include',

components/GanttChart.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ const GanttChart: React.FC<GanttChartProps> = ({ processes }) => {
8585
variants={itemVariants}
8686
>
8787
<span>
88-
{process.arrival_time === -1
89-
? `Idle`
90-
: `P${process.process_id}`}
88+
{process.process_id === -2
89+
? `CS`
90+
: process.arrival_time === -1
91+
? `Idle`
92+
: `P${process.process_id}`}
9193
</span>
9294
</motion.div>
9395
);

0 commit comments

Comments
 (0)