Exemplo n.º 1
0
int main(int argc, char **argv) {
  int n = 20, pid, i, j, h;
  int time[3]; // time[0] priority1, time[1] priority 2...
  time[0] = time [1] = time[2] = 0;
  int retime, rutime, stime;
  enum priority prio;

  if(argc > 1)
    n = atoi(argv[1]);
  for(i = 0; i < 3*n; ++i){
    pid = fork();
    if(pid == 0) { // Child
      set_prio((getpid() % 3)+1);
      for(j = 0; j < 100; ++j) 
        for(h = 0; h < 1000000; ++h){}
      exit();
    }
  }
  while( (pid = wait2(&retime, &rutime, &stime)) > 0 ) {
      prio = pid % 3; // To know which priority the child has
      printf(1, "process id: %d, priority: %s \n", pid, get_prio_name(prio));
      printf(1, "Time it took to complete: %d \n", rutime + retime + stime);
      time[prio] += rutime + retime + stime ;
  }
  turnaroundtime(PRIO_1, n, time[PRIO_1]);
  turnaroundtime(PRIO_2, n, time[PRIO_2]);
  turnaroundtime(PRIO_3, n, time[PRIO_3]);

  exit();
}
Exemplo n.º 2
0
int main()
{
	FILE *input=fopen("schedulinginput","r");
	FILE *waitoutput=fopen("waitoutput","w");
	FILE *turnoutput=fopen("turnoutput","w");
	FILE *startoutput=fopen("startoutput","w");
	int arrival[10002],service[10002],i,n,j,wait[10002],turnaround[10002],start[10002],flag[10002],currtime,minjob,minjobindex,x;
	for(i=0;;i++)
	{
		x=fscanf(input,"%d%d",&arrival[i],&service[i]);
		if(x==-1)
			break;
	}
	n=i;
	currtime=0;
	for(i=0;i<n;i++)
		flag[i]=0;
	for(i=0;i<n;i++)
	{
		minjob=1000000005;
		minjobindex=-1;
		for(j=0;j<n;j++)
		{
			if(flag[j]==-1)
				continue;
			if(arrival[j]>currtime)
				break;
			if(service[j]<minjob)
			{
				minjob=service[j];
				minjobindex=j;
			}
		}
		if(minjobindex!=-1)
		{
			start[minjobindex]=currtime;
			flag[minjobindex]=-1;
			currtime=currtime+service[minjobindex];
		}
		else
		{
			for(j;j<n;j++)
			{
				if(flag[j]==-1)
					continue;
				else
					break;
			}
			start[j]=arrival[j];
			flag[j]=-1;
			currtime=arrival[j]+service[j];
		}
	}
	waitingtime(arrival,start,n,wait);
	turnaroundtime(arrival,start,service,n,turnaround);
	print(wait,n,waitoutput);
	print(turnaround,n,turnoutput);
	print(start,n,startoutput);
	return 0;
}
Exemplo n.º 3
0
int
main(int argc, char *argv[]){
  int n, pid, i , j, h;
  int stime_cpu = 0,stime_scpu = 0, stime_IO = 0;
  int retime_cpu = 0, retime_scpu = 0, retime_IO  = 0;
  int tatime_cpu = 0, tatime_scpu = 0, tatime_IO = 0;
  int retime, rutime, stime;
  enum test_type test;

  if (argc != 2) 
    return -1;
  n = atoi(argv[1]);
  for(i = 0 ; i < 3 * n ; ++i) {
    pid = fork();
    if(pid == 0) {
      test = getpid() % 3;
      if(test == CPU) {
        for(j = 0 ; j < 100 ; ++j)
          for(h = 0 ; h < 1000000 ; ++h){}
      } 
      else if(test == S_CPU) {
        for(j = 0 ; j < 100 ; ++j) {
          for(h = 0 ; h < 1000000 ; ++h){}
          yield();
        }
      } 
      else { //IO
        for(j = 0 ; j < 100 ; ++j)
          sleep(1);
      }
      exit();
    }
    else if (pid < 0) {
      printf(1, "fork number %d, failed!!! \n", i);
    }
    else{} 
  }
  while((pid =wait2(&retime, &rutime, &stime)) > 0) { // pid != 0 (parent code)
    test = pid % 3;
    printf(1, "process id: %d, type: %s \n", pid, get_test_name(test));
    printf(1,"wait time: %d, run time: %d, IO time: %d \n",
        retime, rutime, stime);
    switch(test) {
      case CPU:
        stime_cpu += stime;
        retime_cpu += retime;
        tatime_cpu += stime + retime + rutime;
        break;
      case S_CPU:
        stime_scpu += stime;
        retime_scpu += retime;
        tatime_scpu += stime + retime + rutime;
        break; 
      case IO:
        stime_IO += stime;
        retime_IO += retime;
        tatime_IO += stime + retime + rutime;
        break;
    } 

  }

  sleeptime(CPU,n,stime_cpu);
  sleeptime(S_CPU,n,stime_scpu);
  sleeptime(IO,n,stime_IO);

  readytime(CPU,n,retime_cpu);
  readytime(S_CPU,n,retime_scpu);
  readytime(IO,n,retime_IO);

  turnaroundtime(CPU,n,tatime_cpu);
  turnaroundtime(S_CPU,n,tatime_scpu);
  turnaroundtime(IO,n,tatime_IO);

  exit();
}