#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
#include<time.h>
//#include<iostream>
#define Thread_Num 5
//int threadnum;
//using namespace std;
pthread_mutex_t Device_mutex ;
//Virtual PCB of threads
struct VirtualPCB
{
int tid;
int priority;
int finishtime;
int runtime;
int arrivetime;
int visited;
int tempruntime;
}TCB[Thread_Num];
//Function to initial virtual PCB
void t_init()
{
int n;
srand(time(NULL));
for(n =0;n<Thread_Num;n++)
{
TCB[n].tid = n + 1;//用线程创建序号作为虚拟进程id
//用随机数随机产生虚拟PCB的值
// TCB[n].priority = 1 + rand()%19;
//TCB[n].priority = n+1;
printf("input arrivetime runtime priority\n");
// TCB[n].runtime = 1 + rand()%19;
//TCB[n].runtime = n+1;
scanf("%d%d%d",&TCB[n].arrivetime,&TCB[n].runtime,&TCB[n].priority);
//scanf("%d",&TCB[n].arrivetime);
// TCB[n].arrivetime = 0;//模拟时,默认进程按创建顺序依次在0时刻到达
TCB[n].finishtime = 0;
TCB[n].visited =0;
TCB[n].tempruntime = TCB[n].runtime;
}
}
//Threads run function
void *t_print(void *arg)
{
int n = *(int *)arg;//get argument
while(1)
{
pthread_mutex_lock(&Device_mutex);
printf("Thread_%-2d: ",n);
printf("tid:%-2d arrivetime:%-2d runtime:%-2d priority:%-2d \n",TCB[n-1].tid,TCB[n-1].arrivetime,TCB[n-1].runtime,TCB[n-1].priority);
pthread_mutex_unlock(&Device_mutex);
sleep(1);
break;
}
//printf("Error %d\n",n);
pthread_exit(0);
}
//First come first service schedule function
void FCFS()
{
printf("-----------FCFS:\n");
int i,j;
//int start = 0;
float nowtime = 0;
float avwait = 0;
float awwait=0;
for(j=0;j<Thread_Num;j++)
{
if(TCB[j].visited==0){
TCB[j].finishtime = nowtime + TCB[j].runtime;
printf("Thread: %-2d Arrivetime: %-3d finishtime: %-2d\n",TCB[j].tid,TCB[j].arrivetime,TCB[j].finishtime);
nowtime=nowtime + TCB[j].runtime;
TCB[j].visited=1;
}
avwait = avwait+(TCB[j].finishtime-TCB[j].arrivetime);
awwait=awwait+(TCB[j].finishtime-TCB[j].arrivetime)/(float)TCB[j].runtime;
}
avwait = avwait / (float)Thread_Num;
awwait = awwait / (float)Thread_Num;
printf("Average waitting time : %f\n",avwait);
printf("Average waitting time with weight : %f\n",awwait);
}
//Shortest job first schedule function
void SJF()
{
}
void Priority()
{
}
void HRRN()
{
}
//Main thread execute function to create 20 children threads
void *Children()
{
int ret[Thread_Num];
// t_init();
pthread_t tid[Thread_Num];
pthread_mutex_init(&Device_mutex,NULL);
int i,j;
for(i=0;i<Thread_Num;i++)
{
int k =i+1;
ret[i] = pthread_create(&tid[i],NULL,&t_print, &k);
if(ret[i] == 0) {
sleep(1);
}
else{
printf("Thread_%-2d failed!\n",i+1);
}
}
for(j=0;j<Thread_Num;j++)
pthread_join (tid[i], NULL);
pthread_mutex_destroy(&Device_mutex);
pthread_exit(0);
}
int main()
{
int ret1; int c;
t_init();
// printf("please input the thread number\n");
// scanf("%d",&Thread_Num);
pthread_t tid1;//Declare main thread
ret1 = pthread_create(&tid1,NULL,&Children,NULL);//Create main thread
if(ret1 == 0)
{
printf("Main Thread ok!\n");
sleep(Thread_Num);
}
else{
printf("Thread failed!\n");
}
FCFS();
SJF();
Priority();
HRRN();
return 0;
}