您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 操作系统课程设计——假脱机打印程序与虚拟设备
//该文件所含代码是课设需要学生自己写的代码和补充的代码,包含部分需要修改的课程设计指导书中的代码,不包含不需修改的代码//1.主程序intmain(intargc,char*argv[]){charselect;//按如下顺序创建互斥体、信号和线程h_print=CreateSemaphore(NULL,0,1,NULL);h_sendthread_to_mainthread=CreateSemaphore(NULL,0,1,NULL);h_spoolthread_to_mainthread=CreateSemaphore(NULL,0,1,NULL);h_semaphore_spool=CreateSemaphore(NULL,0,1,NULL);h_send=CreateThread(NULL,0,sendthread,NULL,0,NULL);h_spool_thread=CreateThread(NULL,0,spool_thread,NULL,0,NULL);h_spool_mutex=CreateMutex(NULL,FALSE,NULL);h_screen_mutex=CreateMutex(NULL,FALSE,NULL);h_spool_full=CreateSemaphore(NULL,0,MAX_SPOOL,NULL);h_spool_empty=CreateSemaphore(NULL,MAX_SPOOL,MAX_SPOOL,NULL);WaitForSingleObject(h_screen_mutex,INFINITE);while(1){printf(|-----------------------------------|\n);printf(|1:sendaprintrequest|\n);printf(|2:listspoolqueue|\n);printf(|3:printafileinspool|\n);printf(|4:exit|\n);printf(|-----------------------------------|\n);printf(selectafunction(1~4):);do{select=(char)getch();}while(select!='1'&&select!='2'&&select!='3'&&select!='4');system(cls);ReleaseMutex(h_screen_mutex);switch(select){case'1':ReleaseSemaphore(h_print,1,NULL);WaitForSingleObject(h_sendthread_to_mainthread,INFINITE);break;case'2':list_spool_queue();break;case'3':ReleaseSemaphore(h_semaphore_spool,1,NULL);WaitForSingleObject(h_spoolthread_to_mainthread,INFINITE);break;case'4':return0;}WaitForSingleObject(h_screen_mutex,INFINITE);printf(\nPressanykeytoreturntomainmenu.);getch();system(cls);ReleaseMutex(h_screen_mutex);}return0;}//2.打印线程函数DWORDWINAPIspool_thread(LPVOIDlpParameter){while(1){WaitForSingleObject(h_semaphore_spool,INFINITE);WaitForSingleObject(h_spool_mutex,INFINITE);WaitForSingleObject(h_screen_mutex,INFINITE);if(WaitForSingleObject(h_spool_full,10)==WAIT_TIMEOUT){printf(打印队列空!);}else{spool_buffer.spool_count--;printf(printafileinspool:\nfilename:%s\n,spool_buffer.spool_queue[spool_buffer.spool_out].file_name);strcpy(spool_buffer.spool_queue[spool_buffer.spool_out].file_name,);spool_buffer.spool_queue[spool_buffer.spool_out].file_size=0;spool_buffer.spool_out=(spool_buffer.spool_out+1)%MAX_SPOOL;}ReleaseMutex(h_screen_mutex);ReleaseMutex(h_spool_mutex);ReleaseSemaphore(h_spool_empty,1,NULL);ReleaseSemaphore(h_spoolthread_to_mainthread,1,NULL);}return0;}//主界面//执行1命令后//执行2命令后//执行3命令后//再次执行3命令后
本文标题:操作系统课程设计——假脱机打印程序与虚拟设备
链接地址:https://www.777doc.com/doc-2002741 .html