效果图:
#include<stdio.h>
#include<iostream>
#include<queue>
#include<Windows.h>
#include<string>
#include<time.h>
using namespace std;
int error = 0;
int cnt = 0;//用来记录总任务数
int n;//用来记录当次输入的任务数量
int cur = 0;
int pre = 0;
CRITICAL_SECTION Printer; //定义Printer临界区资源变量
#define MAXSIZE 2 //打印队列的最大长度
struct Job {int id;//任务的序号int level;//任务的优先级int capacity;//打印内容的大小int amount = 0;string print_work;
};//任务结构体的创建
priority_queue<Job> work, q;
Job job[100000];
int done[100000];
DWORD thread_1, thread_2;
HANDLE hThread_1, hThread_2;
bool operator<(const Job& s1, const Job& s2) {if (s1.level == s2.level)//优先度相同返回打印内容少的return s1.capacity> s2.capacity;return s1.level < s2.level;//返回优先度高的任务
}
void lock()
{EnterCriticalSection(&Printer);
}
void unlock()
{LeaveCriticalSection(&Printer);
}
DWORD CALLBACK work_add(LPVOID param)
{while (!error){lock();//work_add线程占用临界区资源{cout << "请输入需要添加的打印任务个数(输入0则打印完剩下任务)";cin >> n;cur =cur+n-pre;if (n == 0) //不再有新的打印任务{unlock();//停止占用临界区资源while (!pty())Sleep(0);//如果打印队列中还有剩余的任务,则令work_print线程打印完所有剩余的任务error = 1;//跳出循环break;}for (int i = cnt; i < n + cnt; i++){string st;job[i].id = i + 1;cout << "请输入第" << i + 1 << "个任务的优先级、打印份数、打印内容:";cin >> job[i].level >> job[i].amount;if (job[i].level > 7)job[i].level = 7;getline(cin, st);//预防打印内容带有空格cin无法接收的情况job[i].print_work = st;job[i].capacity = job[i].print_work.size();//打印内容的大小}for (int i = cnt; i < n + cnt; i++){cur+= job[i].amount-1;for (int j = 1; j <= job[i].amount; j++){q.push(job[i]);//将job的任务插入临时队列}}if (work.size()>0){while (!pty()){q.p());work.pop();}}for (int i = 0; i < cur; i++) {if (work.size() == MAXSIZE) {//如果work打印队列满了unlock();//停止占用临界区资源while (work.size() == MAXSIZE)Sleep(0);//让其他线程运行lock();//再次占用临界区资源}work.p());//打印任务入队int x = q.top().id;q.pop();cout << "作业序号" << x << "进入打印队列准备打印" << endl;}cnt += n;//记录总任务数}unlock();//停止占用临界区资源}return 0;
}
DWORD CALLBACK work_print(LPVOID param)
{while (!error) {lock();//work_print线程占用临界区资源{cout <<endl<< "开始打印........" << endl;while (!pty()) {//将当前work打印任务队列中所有任务打印完成auto t1 = clock();cout << "任务序号" << p().id << " 正在打印...";cout << '[';for (int i = 0; i < 100; i += 10){p().capacity * 25);cout << (char)0xa8 << (char)0x80;}auto t2 = clock();cout << "] 100%" << " 打印耗时:"<<(t2 - t1)/1000<<"s"<< endl;cout << "第"<<p().id]+1<< "份 "<< "任务序号" << p().id << " 打印完成:" << p().print_work << endl;p().id]++;work.pop();pre++;}}cout << "打印结束........" << endl << endl;unlock();while (pty())Sleep(0);}return 0;
}
void CreatePrinterThread()
{hThread_1 = CreateThread(NULL, 0, work_add, NULL, 0, &thread_1);if (hThread_1 == NULL){cout << "线程创建失败..." << endl;error = 1;exit(error);}hThread_2 = CreateThread(NULL, 0, work_print, NULL, 0, &thread_2);if (hThread_2 == NULL){cout << "线程创建失败..." << endl;error = 1;exit(error);}
}
int main()
{cout << "--------------------------欢迎使用打印机--------------------------" << endl;cout << " 注意事项:打印队列最大容量为:" << MAXSIZE << " 任务优先级为:1-7(数字越大越紧急)" << endl << endl;InitializeCriticalSection(&Printer);//初始化打印机临界区资源CreatePrinterThread();//开始创建添加任务、打印任务两条线程while (!error)Sleep(0);system("pause");cout << "--------------------------打印机已退出--------------------------" << endl;DeleteCriticalSection(&Printer);//摧毁打印机临界区资源return 0;
}
本文发布于:2024-02-04 11:59:29,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170706683555363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |