#include #include #include #include #include #include #include #include #define MAX_PHILOSOPHERS 3 #define ZERO 48 #define DELAY rand()%25 struct PCB //待测试的哲学家数 //数字0的ASCII码 { char p_name[20]; int p_priority; int p_needTime; int p_runTime; char p_state; char deadlock(); struct PCB* next; }; void HighPriority(); void deadlock(); void Information();//形参的改变映射给实参 说白了就是实参传过去不用return 返回就可以把实参改变 char Choice(); struct PCB* SortList(PCB* HL); int main(int argc,char *argv[]) { Information(); char choice = Choice(); switch(choice) { case '1': system(\"cls\"); HighPriority(); break; case '2': system(\"cls\"); void deadlock(); break; default: break; } system(\"pause\"); return 0; } void Information() { printf(\"\\n\\n\"); printf(\" \\n\"); printf(\" ********************************************* 模拟进程调度算法\\n\"); printf(\" ********************************************* \\n\\n\\n\"); printf(\" printf(\" printf(\" getchar(); system(\"cls\"); system(\"cls\"); } char Choice() { printf(\"\\n\\n\"); printf(\" \\n\"); 静态优先级调度算法\"); 死锁问题\"); 按回车键进入演示程序\"); ********************************************* printf(\" 进程调度演示\\n\"); printf(\" ********************************************* \\n\\n\\n\"); printf(\" 1.演示最高优先数优先算法。\\n\"); printf(\" 2.演示死锁问题。\\n\"); printf(\" 3.退出程序。\\n\\n\\n\\n\"); printf(\" 选择进程调度方法:\"); printf(\"select a function(1~3):\"); char ch = getchar(); return ch; system(\"cls\"); } void HighPriority() { struct PCB *processes, *pt; //pt作为临时节点来创建链表 processes = pt = (struct PCB*)malloc(sizeof(struct PCB)); for (int i = 1; i != 4; ++i) { struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB)); printf(\"进程号No.%d:\\n\ printf(\"输入进程名:\"); scanf(\"%s\ printf(\"输入进程优先数:\"); scanf(\"%d\ printf(\"输入进程运行时间:\"); scanf(\"%d\ p->p_runTime = 0; p->p_state = 'W'; p->next = NULL; pt->next = p; pt = p; printf(\"\\n\\n\"); } getchar(); //接受回车 //processes作为头结点来存储链表 processes = processes->next; int cases = 0; struct PCB *psorted = processes; while (1) { ++cases; pt = processes; //对链表按照优先数排序 //psorted用来存放排序后的链表 psorted = SortList(psorted); printf(\"The execute number: %d\\n\\n\ printf(\"**** 当前正在运行的进程是:%s\\n\ psorted->p_state = 'R'; printf(\"qname state super ndtime runtime\\n\"); printf(\"%s\%c\%d\%d\%d\\\n\\n\psorted->p_priority, psorted->p_needTime, psorted->p_runTime); pt->p_state = 'W'; psorted->p_runTime++; psorted->p_priority--; printf(\"**** 当前就绪状态的队列为:\\n\\n\"); //pt指向已经排序的队列 pt = psorted->next; while (pt != NULL) { printf(\"qname state super ndtime runtime\\n\"); printf(\"%s\%c\%d\%d\%d\\\n\\n\pt->p_priority, pt->p_needTime, pt->p_runTime); pt->p_name, pt->p_state, pt = pt->next; } //pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的 pt = psorted; struct PCB *ap; ap = NULL; //ap指向pt的前一个节点 while (pt != NULL) { if (pt->p_needTime == pt->p_runTime) { if (ap == NULL) { pt = psorted->next; psorted = pt; } else ap->next = pt->next; } ap = pt; pt = pt->next; } if (psorted->next == NULL) break; getchar(); } } struct PCB* SortList(PCB* HL) { struct PCB* SL; SL = (struct PCB*)malloc(sizeof(struct PCB)); SL = NULL; struct PCB* r = HL; while (r != NULL) { struct PCB* t = r->next; struct PCB* cp = SL; struct PCB* ap = NULL; while (cp != NULL) { if (r->p_priority > cp->p_priority) break; else { ap = cp; cp = cp->next; } } if (ap == NULL) { r->next = SL; SL = r; } else { r->next = cp; ap->next = r; } r = t; } return SL; } // HANDLE h_mutex_chopsticks[MAX_PHILOSOPHERS]; //互斥体数组,每根筷子需要一个互斥体 int thread_number[MAX_PHILOSOPHERS]={1,2,3};//定义死锁的个数 //会产生死锁的哲学家线程 int deadlock_philosopher(LPVOID data){ int philosopher_number=*(int *)(data); //哲学家编号 for(;;) { srand( (unsigned)time( NULL ) * ( philosopher_number+ 1) ); Sleep(DELAY); printf(\"%s%c%s%c\\n\\is waiting chopstick \ WaitForSingleObject(h_mutex_chopsticks[philosopher_number], INFINITE); printf(\"%s%c%s%c\\n\chopstick \ \got Sleep(DELAY/4); printf(\"%s%c%s%c\\n\\is waiting chopstick \ WaitForSingleObject(h_mutex_chopsticks[((1+philosopher_number)%MAX_PHILOSOPHERS)], INFINITE); printf(\"%s%c%s%c\\n\\got chopstick \ printf(\"%s%c%s\\n\ Sleep(DELAY); ReleaseMutex(h_mutex_chopsticks[philosopher_number]); printf(\"%s%c%s%c\\n\\released chopstick \ ReleaseMutex(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS]); printf(\"%s%c%s%c\\n\\released chopstick \ Sleep(DELAY); } // end for return 0; } //死锁时的初始化程序 void deadlock() { char choice; int i=0; HANDLE h_thread[MAX_PHILOSOPHERS]; printf(\"可能出现死锁的哲学家就餐问题\\n\"); for(i=0;i h_mutex_chopsticks[i]=CreateMutex(NULL,FALSE,NULL); }; for(i=0;i h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(deadlock_philosopher),&thread_number[i],0,NULL); }; WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1); do { choice=(char)getch(); }while( choice!='2'); system(\"cls\"); deadlock(); printf(\"\\nPress any key to return to main menu.\"); getch(); system(\"cls\"); } //通过按序分配资源预防死锁的哲学家线程 int ordered_allocation_philosopher(LPVOID data) { int philosopher_number=*(int *)(data); for(;;) { srand( (unsigned)time( NULL ) * ( philosopher_number+ 1) ); Sleep(DELAY); if(philosopher_number==MAX_PHILOSOPHERS-1){ printf(\"%s%c%s%c\\n\chopstick \ WaitForSingleObject(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS], INFINITE); Sleep(DELAY/4); printf(\"%s%c%s%c\\n\ chopstick \ WaitForSingleObject(h_mutex_chopsticks[philosopher_number], INFINITE); } else{ printf(\"%s%c%s%c\\n\\is waiting chopstick \ WaitForSingleObject(h_mutex_chopsticks[philosopher_number], INFINITE); Sleep(DELAY/4); printf(\"%s%c%s%c\\n\\is waiting chopstick \ WaitForSingleObject(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS], INFINITE); } printf(\"%s%c%s\\n\eating.\"); \is Sleep(DELAY); printf(\"%s%c%s%c\\n\\is releasing chopstick \ ReleaseMutex(h_mutex_chopsticks[philosopher_number]); printf(\"%s%c%s%c\\n\chopstick \ ReleaseMutex(h_mutex_chopsticks[(1+philosopher_number)%MAX_PHILOSOPHERS]); Sleep(DELAY); } // end for return 0; } //通过按序分配资源预防死锁的初始化程序 void ordered_allocation() { char choice; int i=0; HANDLE h_thread[MAX_PHILOSOPHERS]; printf(\"可能出现死锁的哲学家就餐问题\\n\"); for(i=0;i }; for(i=0;i }; WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1); do { choice=(char)getch(); }while( choice!='2'); system(\"cls\"); deadlock(); printf(\"\\nPress any key to return to main menu.\"); getch(); system(\"cls\"); } 因篇幅问题不能全部显示,请点此查看更多更全内容