1

synchronize with semaphore

This commit is contained in:
2022-07-11 15:37:45 +02:00
parent 66b3b914d0
commit 597b823ac3
3 changed files with 15 additions and 7 deletions

View File

@ -6,12 +6,12 @@ void PreemptiveLoopThread::run() {
int cnt = 0;
while (true) {
// Basic synchronization by disabling PIT interrupts
cpu.disable_int();
// Basic synchronization by semaphore
sem->p();
kout.setpos(55, this->id);
kout << this->id << ": " << dec << cnt++ << endl;
cpu.enable_int();
sem->v();
}
}

View File

@ -3,17 +3,19 @@
#include "kernel/Globals.h"
#include "kernel/threads/Thread.h"
#include "lib/Semaphore.h"
class PreemptiveLoopThread : public Thread {
private:
int id;
Semaphore* sem;
PreemptiveLoopThread(const PreemptiveLoopThread& copy) = delete; // Verhindere Kopieren
public:
// Gibt der Loop einen Stack und eine Id.
PreemptiveLoopThread(int i) : id(i) {}
PreemptiveLoopThread(int i, Semaphore* sem) : id(i), sem(sem) {}
// Zaehlt einen Zaehler hoch und gibt ihn auf dem Bildschirm aus.
void run() override;

View File

@ -1,12 +1,18 @@
#include "user/PreemptiveThreadDemo.h"
#include "lib/Semaphore.h"
void PreemptiveThreadDemo::run() {
kout << "PreemptiveThreadDemo ====================================" << endl;
kout << "Initializing Semaphore" << endl;
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
// so stack allocated objects will be lost
kout << "Allocating LoopThread A" << endl;
Thread* cntA = new PreemptiveLoopThread(0);
Thread* cntA = new PreemptiveLoopThread(0, sem);
kout << "Allocating LoopThread B" << endl;
Thread* cntB = new PreemptiveLoopThread(1);
Thread* cntB = new PreemptiveLoopThread(1, sem);
kout << "Allocating LoopThread C" << endl;
Thread* cntC = new PreemptiveLoopThread(2);
Thread* cntC = new PreemptiveLoopThread(2, sem);
kout << "Adding threads to ready queue" << endl;
scheduler.ready(cntA);