Back to Projects
C++ · Unix · Operating Systems

Process Dispatcher

A basic implementation of a process dispatcher for managing multiple processes in a multitasking environment. Demonstrates process scheduling, resource management, and UNIX signal handling.

Process Dispatcher
Process Management
Learned how to create and manage Process Control Blocks (PCBs), tracking process information such as PID, arguments, arrival time, remaining CPU time, priority, and resource needs.
Managed the full lifecycle of a process — starting, suspending, continuing, and terminating.
Resource Allocation & Management
Implemented logic to check for resource availability and allocate/deallocate resources (printers, scanners, modems, CD drives) to processes.
Priority Scheduling & Feedback Queues
Implemented a priority-based scheduling algorithm using multiple feedback queues.
Managed and manipulated processes within feedback queues so that higher-priority processes get more CPU time, and lower-priority processes are demoted when they consume too much.
Interprocess Communication & Signals
Used UNIX signals — SIGINT, SIGTSTP, SIGCONT — to control process states (start, suspend, continue, terminate).
Created new processes using fork() and replaced their image using execvp().
Concurrency & Synchronization
Managed multiple processes concurrently and understood how to synchronize their execution using scheduling algorithms.

Source Code

#include <iostream>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <queue>
#include "memory.h"

#define MAXARGS 10

using namespace std;

// Define the structure for process control blocks (PCBs)
struct pcb {
    pid_t pid;
    char *args[MAXARGS];
    int arrivaltime;
    int remainingcputime;
    int priority;
    struct pcb *next;
    bool started;
    int memory_needed;
    MabPtr memory_block;
    int printers;
    int scanners;
    int modems;
    int cds;
};

typedef struct pcb Pcb;
typedef Pcb* PcbPtr;

const int NUM_FEEDBACK_QUEUES = 4;
queue<PcbPtr> input_queue, user_job_queue;
queue<PcbPtr> feedback_queues[NUM_FEEDBACK_QUEUES];
PcbPtr current_process = NULL;

int available_printers = 2;
int available_scanners = 1;
int available_modems   = 1;
int available_cds      = 2;

void startPcb(PcbPtr process) {
    process->pid = fork();
    if (process->pid == 0) {
        execvp(process->args[0], process->args);
        exit(1);
    } else if (process->pid < 0) {
        cerr << "Fork failed" << endl;
        exit(1);
    }
    process->started = true;
    cout << "Process " << process->pid << " started with priority "
         << process->priority << " and remaining CPU time "
         << process->remainingcputime << endl;
}

void terminatePcb(PcbPtr process) {
    if (kill(process->pid, SIGINT))
        cerr << "Terminate of process " << process->pid << " failed" << endl;
    memDealloc(process->memory_block);
    cout << "Process " << process->pid << " terminated and memory deallocated" << endl;
    delete process;
}

void suspendPcb(PcbPtr process) {
    if (kill(process->pid, SIGTSTP))
        cerr << "Suspend of process " << process->pid << " failed" << endl;
    cout << "Process " << process->pid << " suspended" << endl;
}

void continuePcb(PcbPtr process) {
    if (kill(process->pid, SIGCONT))
        cerr << "Continue of process " << process->pid << " failed" << endl;
    cout << "Process " << process->pid << " continued" << endl;
}

void enqueuePcb(PcbPtr process) {
    feedback_queues[process->priority].push(process);
    cout << "Process " << process->pid << " enqueued in priority " << process->priority << endl;
}

PcbPtr dequeuePcb() {
    for (int i = 0; i < NUM_FEEDBACK_QUEUES; ++i) {
        if (!feedback_queues[i].empty()) {
            PcbPtr process = feedback_queues[i].front();
            feedback_queues[i].pop();
            cout << "Process " << process->pid << " dequeued from priority " << i << endl;
            return process;
        }
    }
    return NULL;
}

bool resourcesAvailable(PcbPtr process) {
    return (available_printers >= process->printers &&
            available_scanners >= process->scanners &&
            available_modems   >= process->modems   &&
            available_cds      >= process->cds);
}

void allocateResources(PcbPtr process) {
    available_printers -= process->printers;
    available_scanners -= process->scanners;
    available_modems   -= process->modems;
    available_cds      -= process->cds;
}

void deallocateResources(PcbPtr process) {
    available_printers += process->printers;
    available_scanners += process->scanners;
    available_modems   += process->modems;
    available_cds      += process->cds;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        cerr << "Usage: " << argv[0] << " <dispatchlist>" << endl;
        return 1;
    }

    int current_time = 0;
    while (!input_queue.empty() || !user_job_queue.empty() || current_process != NULL) {
        while (!input_queue.empty() && input_queue.front()->arrivaltime <= current_time) {
            user_job_queue.push(input_queue.front());
            input_queue.pop();
        }
        if (current_process == NULL && !user_job_queue.empty()) {
            current_process = user_job_queue.front();
            user_job_queue.pop();
            if (resourcesAvailable(current_process)) {
                allocateResources(current_process);
                startPcb(current_process);
            } else {
                user_job_queue.push(current_process);
                current_process = NULL;
            }
        }
        if (current_process != NULL) {
            current_process->remainingcputime--;
            if (current_process->remainingcputime == 0) {
                terminatePcb(current_process);
                deallocateResources(current_process);
                current_process = NULL;
            }
        }
        current_time++;
    }
    return 0;
}