Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Exercises/Response-Time Analysis/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Debug C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/real_time_analysis",
"program": "${workspaceFolder}/build/response_time_analysis",
"args": ["${workspaceFolder}/exercise-TC2.csv"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
4 changes: 2 additions & 2 deletions Exercises/Response-Time Analysis/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/src/real_time_analysis.cpp",
"${workspaceFolder}/src/response_time_analysis.cpp",
"-o",
"${workspaceFolder}/build/real_time_analysis"
"${workspaceFolder}/build/response_time_analysis"
],
"group": {
"kind": "build",
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ void readTasksCSV(const string& filename, vector<Task>& tasks)

getline(ss, token, ','); // Task ID
task.id = token;
getline(ss, token, ','); // WCET
task.WCET = stoi(token);
getline(ss, token, ','); // BCET
task.BCET = stoi(token);
getline(ss, token, ','); // WCET
task.WCET = stoi(token);
getline(ss, token, ','); // Period
task.period = stoi(token);
getline(ss, token, ','); // Deadline
Expand All @@ -56,43 +56,41 @@ void RTA_test(vector<Task>& tasks)
{
// Sort tasks by priority (lower number means higher priority)
sort(tasks.begin(), tasks.end(), [](const Task& a, const Task& b) {
return a.priority < b.priority; // Ensure lower priority number = higher priority
return a.priority < b.priority;
});

for (size_t task = 0; task < tasks.size(); task++)
for (size_t i = 0; i < tasks.size(); i++)
{
int R = tasks[task].WCET;
int R = tasks[i].WCET;
int last_R = -1;
bool schedulable = true;
int I = 0; // Reset interference for each iteration

while (R != last_R)
{
last_R = R;

// Compute interference from higher-priority tasks
for (size_t j = 0; j < task; j++)
int I = 0;
for (size_t j = 0; j < i; j++)
{
I += ceil((double)R / tasks[j].period) * tasks[j].WCET;
}
R = tasks[i].WCET + I;

R = I + tasks[task].WCET;

if (R > tasks[task].deadline)
if (R > tasks[i].deadline)
{
cout << "Task " << tasks[task].id << " is not schedulable." << endl;
cout << "Task " << tasks[i].id << " is not schedulable with WCRT." << endl;
schedulable = false;
break;
}
}

if (schedulable)
{
cout << "Task " << tasks[task].id << " is schedulable with WCRT = " << R << endl;
cout << "Task " << tasks[i].id << " is schedulable with WCRT = " << R << endl;
}
}
}


int main(int argc, char* argv[])
{
if (argc != 2) {
Expand All @@ -107,4 +105,4 @@ int main(int argc, char* argv[])
RTA_test(tasks);

return 0;
}
}
Binary file modified Exercises/Very Simple Simulator/build/very_simple_simulator
100644 → 100755
Binary file not shown.
118 changes: 64 additions & 54 deletions Exercises/Very Simple Simulator/src/very_simple_simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Task {
int WCET;
int period;
int deadline;
int priority; // Lower number => higher priority for RMS, if you wish
int priority; // Lower number => higher priority for RMS
};

// Define the Job structure
Expand Down Expand Up @@ -45,10 +45,10 @@ void readTasksCSV(const string& filename, vector<Task>& tasks)

getline(ss, token, ','); // Task ID
task.id = token;
getline(ss, token, ','); // WCET
task.WCET = stoi(token);
getline(ss, token, ','); // BCET
task.BCET = stoi(token);
getline(ss, token, ','); // WCET
task.WCET = stoi(token);
getline(ss, token, ','); // Period
task.period = stoi(token);
getline(ss, token, ','); // Deadline
Expand All @@ -59,22 +59,20 @@ void readTasksCSV(const string& filename, vector<Task>& tasks)
tasks.push_back(task);
}
file.close();
}

// Function to check if all jobs are completed
bool allJobsCompleted(const vector<Job>& jobs)
{
for (const auto& job : jobs) {
if (job.remainingTime > 0) {
return false;
}
}
return true;
sort(tasks.begin(), tasks.end(), [](const Task& a, const Task& b) {
return a.id < b.id;
});
}

// Function to find the job with the highest priority
Job *highest_priority(vector<Job *>& readyList)
{
if (readyList.empty())
{
return nullptr;
}

return *min_element(
readyList.begin(),
readyList.end(),
Expand All @@ -96,9 +94,15 @@ vector<Job*> get_ready(vector<Job>& jobs, int currentTime)
return readyList;
}

int advanceTime()
{
return 1;
}

int main(int argc, char* argv[])
{
if (argc != 2) {
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " <tasks.csv>" << endl;
return 1;
}
Expand All @@ -107,75 +111,81 @@ int main(int argc, char* argv[])
vector<Task> tasks;
readTasksCSV(filename, tasks);

// Simulation time (n cycles)
int n = 1000;
int currentTime = 0;

// List of all jobs that are in the system
vector<Job> jobs;
jobs.reserve(tasks.size());
for (const auto& t : tasks) {
jobs.push_back({t, 0, t.WCET, 0});
}

// Initialize next release time for each task (all tasks start at time 0)
vector<int> nextReleaseTimes(tasks.size(), 0);

// We also maintain worst-case response times (WCRT) for each task.
vector<int> worstCaseResponseTimes(tasks.size(), 0);

while (currentTime < n && !allJobsCompleted(jobs))
// Simulation loop
while (currentTime <= n)
{
// Release new jobs at the start of each period
for (const auto& t : tasks)
// For each task, if it is time to release a new job, create it.
for (size_t i = 0; i < tasks.size(); ++i)
{
if (currentTime != 0 && currentTime % t.period == 0) {
jobs.push_back({t, currentTime, t.WCET, 0});
if (currentTime >= nextReleaseTimes[i])
{
Job newJob { tasks[i], currentTime, tasks[i].WCET, 0 };
jobs.push_back(newJob);
nextReleaseTimes[i] += tasks[i].period;
}
}

// Get the list of ready jobs

vector<Job*> readyList = get_ready(jobs, currentTime);

// If no jobs are ready, increment the current time
if (readyList.empty())
{
currentTime++;
continue;
}

// Get the job with the highest priority
Job* currentJob = highest_priority(readyList);

if (currentJob != nullptr)
{
// Execute the job for one time unit
currentJob->remainingTime--;
currentTime++;
// If the job is completed, update its response time
int delta = advanceTime();
currentTime += delta;
currentJob->remainingTime -= delta;

// If the job has completed execution, calculate its response time and update WCRT
if (currentJob->remainingTime <= 0)
{
currentJob->responseTime = currentTime - currentJob->releaseTime;
int taskIndex = distance(
tasks.begin(),
find_if(tasks.begin(), tasks.end(), [&](const Task& tk){
return tk.id == currentJob->task.id;
})
);
worstCaseResponseTimes[taskIndex] = max(
worstCaseResponseTimes[taskIndex],
currentJob->responseTime
);

// Find the corresponding task index
auto it = find_if(tasks.begin(), tasks.end(), [&](const Task& t) {
return t.id == currentJob->task.id;
});

if (it != tasks.end())
{
size_t taskIndex = distance(tasks.begin(), it);
worstCaseResponseTimes[taskIndex] = max(
worstCaseResponseTimes[taskIndex],
currentJob->responseTime
);
}
}
}
else
{
currentJob->remainingTime--;
currentTime++;
// If no job is ready, just advance time
int delta = advanceTime();
currentTime += delta;
}
}

// Output the worst-case response times for each task
cout << "Task\tWCRT\tDeadline\tStatus" << endl;
cout << "---------------------------------" << endl;
for (size_t i = 0; i < tasks.size(); ++i)
{
cout << "Task " << tasks[i].id
<< " WCRT: " << worstCaseResponseTimes[i] << endl;
string status = (worstCaseResponseTimes[i] <= tasks[i].deadline) ? "✓" : "✗";
cout << " " << tasks[i].id << "\t"
<< worstCaseResponseTimes[i] << "\t"
<< tasks[i].deadline << "\t\t"
<< status << endl;
}

return 0;
}
}