You are on page 1of 4

Mini Project on Operating Systems

Printing Center Job Scheduling Simulator

Students submit print jobs to be handled by shared printers at VIT printing facility. The
operating system is in charge of controlling the print queue and making sure that every
student has an equal opportunity to print their Digital Assignments. The intention is to put in
place a simulator that shows how resource management and job scheduling are done in the
printing facility.

> Defining a class to represent each user with attributes like user ID and submitted print
jobs.

> Defining a class to represent each printer with attributes like printer ID, status (idle or
busy), and the current print job.
Print Queue:

>Simulating the job scheduling system where users submit print jobs, and the operating
system manages the print queue.

Code:

#include <iostream>
#include <vector>
#include <queue>
#include <thread>
#include <mutex>

using namespace std;

//User representation
class User {
public:
int userID;
vector<string> printJobs;

User(int userID) : userID(userID) {}


};

//Printer representation
class Printer {
public:
int printerID;
bool isBusy;
string currentJob;
Printer(int printerID) : printerID(printerID), isBusy(false), currentJob("") {}
};

queue<string> printQueue;

mutex printMutex;

//Function to simulate print job processing


void processPrintJob(Printer& printer) {
while (true) {

unique_lock<mutex> lock(printMutex);

if (!printQueue.empty()) {
string job = printQueue.front();
printQueue.pop();

//Simulate printing time


this_thread::sleep_for(chrono::seconds(2));

cout << "Printer " << printer.printerID << " finished printing: " << job << endl;

printer.isBusy = false;
printer.currentJob = "";
}

lock.unlock();
}
}

int main() {

vector<User> users = {User(1), User(2), User(3)};

vector<Printer> printers = {Printer(1), Printer(2)};

thread printerThread1(processPrintJob, ref(printers[0]));


thread printerThread2(processPrintJob, ref(printers[1]));

//Simulate users submitting print jobs


for (int i = 0; i < 10; ++i) {

int userIndex = i % users.size();


User& currentUser = users[userIndex];
string job = "PrintJob-" + to_string(i);
lock_guard<mutex> lock(printMutex);

printQueue.push(job);

//Display the print job submitted


cout << "User " << currentUser.userID << " submitted print job: " << job << endl;
}

printerThread1.join();
printerThread2.join();

return 0;
}

Output:

You might also like