进程调度与同步
系统:Linux Ubuntu 编译器:gcc
过程:
1.在主进程中,创建20个子线程,分别模拟进程调度算法FCFS、RR、SJF的实现,并分析子线程调度顺序是否正确。
2.以哲学家就餐问题为依托,利用信号量等方式解决同步问题。
3.通过创建5个子线程以模拟哲学家,然后设置5个互斥区,以模拟筷子
4.利用哲学家就餐问题的解决算法,模拟哲学家就餐问题的求解过程。
5.最后对程序运行结果进行分析,判断是否解决问题;并总结分析实验过程遇到的问题以及解决方法。
代码
#include<stdio.h>
#include<sys/types.h> //defind pid t
#include<unistd.h>
#include<sys/wait.h>
#include<pthread.h>
#include<stdlib.h>
struct VirtualPCB
{
int id;
int priority;
int arrivetime;
int runtime;
int rtime;
//int waitingtime;
//VirtualPCB *next;
}PCBs[20];
void sort(int a[20]){
int i,j,temp;
for(i=0;i<20;i++){
for(j=0;j<20-1-i;j++){
if(a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int k=0; k<20; k++){
printf("%d ",a[k]);
}
}
void handleFCFS(VirtualPCB PCBs[20]){
printf("FCFS Scheduling--------------------------------------------\n");
struct VirtualPCB temp;
for(int i=1;i<=20;i++){
for(int j=1;j<=20-i;j++){
if(PCBs[j].arrivetime > PCBs[j+1]