int main(void) {
    Job in[N];
    Job jobs[N];
    int cnt = 0;
    while (scanf("%s%d%d", in[cnt].name, &in[cnt].arrive_time, &in[cnt].serve_time) == 3) cnt++;

    copy(jobs, in, cnt);
    printf("FCFS\n");
    FCFS(jobs, cnt);
    display(jobs, cnt);

    copy(jobs, in, cnt);
    printf("RR-1\n");
    RR(jobs, cnt, 1);
    display(jobs, cnt);

    copy(jobs, in, cnt);
    printf("RR-4\n");
    RR(jobs, cnt, 4);
    display(jobs, cnt);

    copy(jobs, in, cnt);
    printf("SJF\n");
    SJF(jobs, cnt);
    display(jobs, cnt);

    copy(jobs, in, cnt);
    printf("HRN\n");
    HRN(jobs, cnt);
    display(jobs, cnt);

    return 0;
}
int main()
{
	int ch, size, burstTime[MAX], flag = 1,i; 
	printf("Enter the number of processes : \n");
	scanf("%d",&size);

	printf("Enter the burst time for each process P\n");
	for (i = 0; i < size; ++i)
		scanf("%d", &burstTime[i]);

	printf("Enter the choice of implementation of CPU Scheduling Algorithm :\n");
	menu();
	
	while(flag)
	{
		scanf("%d",&ch);

		switch(ch)
		{
			case 1 : FCFS(burstTime, size);
			break;

			// case 2 : SJF(burstTime, size);
			// break;

			case 3 : RR(burstTime, size);
			break;

			default : flag = 0;
			continue;
		}
	}
	printf("Thank You for using the program.\n");
}
Beispiel #3
0
/***************************************************
    function: PassProcessesToCPU

    purpose:  Schedule tasks to be processed by CPU
              according to chosen method.

    input:    current clock time
***************************************************/
void Scheduler::PassProcessesToCPU(int time)
{   current_time = time;

    if(p_table.AllTasksComplete())
    {
        if(output_scheduler_info)
                cout << "No remaining tasks to be processed.\n";
        return;
    }

    /// Identify running states
    switch(schedule_method)
    {   case fcfs:  FCFS(); break;
        case rr:    RR();   break;
        case srt:   SRT();  break;
        case hrrn:  HRRN(); break;
    }


    if(output_scheduler_info == TRUE){
        cout << "Clock Time = "<<current_time<<",  \tTasks: ";
    }
    /// Proces running states
    for(int i = 0; i < p_table.tasks.size(); i++)
        if(p_table.tasks[i].state == running)
        {
            if(p_table.tasks[i].process.response_time == 0)
                p_table.tasks[i].process.response_time = current_time - p_table.tasks[i].process.arrival_time;

            if(output_scheduler_info == TRUE)
                cout << p_table.tasks[i].process.pid << ",\t";
            p_table.tasks[i].process.time_serviced += 1;    // Spend 1 clock cycle processing task
        }
    if(output_scheduler_info == TRUE)
        cout << endl;


    /// Update state of each task
    UpdateWaitStates();
    for(int i = 0; i < p_table.tasks.size(); i++)   // Update each running state
    {   if(p_table.tasks[i].state == running)
        {   p_table.tasks[i].UpdateState();         // Must come after 'UpdateWaitStates()'
            if(p_table.tasks[i].state == complete)
                p_table.tasks[i].completion_time = current_time+1;   // Store time at which task was completed
            if(current_method == rr){
                if(p_table.tasks[i].state == blocked || p_table.tasks[i].state == waiting)
                    p_table.tasks[i].process.blocked_time = current_time+1;
            }

        }
    }

}
Beispiel #4
0
int main(int arg,char* argv[])
{
  /******check input argment********/
  if(arg < 3 || arg > 5){
      printf("Please check parameter and run it again\n");
      return 0;
  }

  char* Filename = argv[1];
  char* Policy = argv[2];
  int quota;
  if(strcmp(Policy,"RR") == 0){
    if(arg == 4)
      quota = atoi(argv[3]);
    else{
      printf("Lossing quota time for RR scheduler.Please run it again\n");
      return 0;
    }
  }
  /**************************/

  //get the data from file and store into Jobs
  FILE* pFile;
  Jobs* all_jobs = (Jobs*)malloc(sizeof(Jobs));
  pFile = fopen (Filename , "r");
  if (pFile == NULL) perror ("Opening file error");
  else  all_jobs = __Parse(pFile);

  //calculate which time should be do first (using its arrival time)
  __Sort_jobs(all_jobs);

  printf("Start\n");

  /***********Do scheduler *******************/
  if(strcmp(Policy,"FCFS") == 0){
    FCFS(all_jobs);
  }
  else if(strcmp(Policy,"RR") == 0){
    RR(all_jobs,quota);
  
  }
  printf("Finish\n");
  return 0;
}
Beispiel #5
0
int main(){
    int sch = 99;
    //input();
    while( sch != 0 ){
        printf( "\nPlease select an algorithm:\n" );
        printf( "(1)FCFS: First come first serve\n" );
        printf( "(2)SJF : Short job first serve\n" );
        printf( "(3)HRF : High reflection first serve\n" );
        printf( "(4)Timeslice: Time slice serve\n" );
        printf( "(5)MRLA: Multi relation xxxx queue serve\n" );
        printf( "(0)Quit\n" );
        scanf( "%d", &sch );

        switch( sch ){
            case 1:
                FCFS();
                break;
            case 2:
                SJF();
                break;
            case 3:
                HRF();
                break;
            case 4:
                Timeslice();
                break;
            case 5:
                MRLA();
                break;
            case 0:
                printf( "Quited program\n" );
                break;
        }
    }
    _keygo();
    return 0;
}
Beispiel #6
0
int main(int argc,char *argv[]){
	int d = 0;
	int v = 0;
	int r = 0;
	FILE * input = stdin;
	sch list;
	list.head_proc = NULL;
	printf("starting program\n");
	if(check_arg(argc, argv, &d,&v, &r) != -1){
		if(read_File(input,&list) != -1){
			if(r == 0){
				FCFS(&list,v,d);
			}else{
				RR(&list,v,d,r);
			}	
			destroy_list(&list);
		}else{
			printf("ERROR: no file provided!\n");	
		}
	}else{
		printf("ERROR: wrong quantum number!\n");
	}
	return 0;
}
Beispiel #7
0
int main(int argc, char *argv[]) {
    //declare the time period
    extern int monthDay[12];
    extern int periodStartYear, periodStartMonth, periodStartDay, periodStartHour, periodStartMinute;
    extern int periodEndYear, periodEndMonth, periodEndDay, periodEndHour, periodEndMinute;
    FILE *fp, *tmpfp, *dest;
    int intbuf,qtm;
    qtm = 0;
    //type of scheduler
    char* scheduleType = (char*)malloc(10);
    //current number of array slot
    int eventSize = 80;
    //declare the event array, can be expanded its size twice to contain all events
    Event *event = (Event*)calloc(eventSize, sizeof(*event));
    //printf("allocate calloc space + %d",sizeof(*event));
    int i, j,k,l,m,havevalidday;
    int pid = 0;
    // initialize all the data in event array (may not needed)
    for (i=0; i<eventSize; i++) {
        strcpy(event[i].type, "");
        strcpy(event[i].name, "");
        event[i].startYear = 0;
        event[i].startMonth = 0;
        event[i].startDay = 0;
        event[i].startHour = 0;
        event[i].dueYear = 0;
        event[i].dueMonth = 0;
        event[i].dueDay = 0;
        event[i].dueHour = 0;
        event[i].duration = 0;
        event[i].priority = 0;
        event[i].handled = 0;
        event[i].startTimeValue = 0;
        event[i].dueTimeValue = 0;
    }

    printf("   ~~WELCOME TO ASKS~~\n\n");
    //declare the buffer to contain the input string
    char input[100];
    printf("Please enter:\n");
    fgets(input, 100, stdin);
    //initialize the period of schedule
    char* tmp;
    char* tmpforrr;
    tmp = strtok(input, " ");
    tmp = strtok(NULL, "-");
    periodStartYear = atoi(tmp);
    tmp = strtok(NULL, "-");
    periodStartMonth = atoi(tmp);
    tmp = strtok(NULL, " ");
    periodStartDay = atoi(tmp);
    tmp = strtok(NULL, ":");
    periodStartHour = atoi(tmp);
    tmp = strtok(NULL, " ");
    periodStartMinute = atoi(tmp);
    tmp = strtok(NULL, "-");
    periodEndYear = atoi(tmp);
    tmp = strtok(NULL, "-");
    periodEndMonth = atoi(tmp);
    tmp = strtok(NULL, " ");
    periodEndDay = atoi(tmp);
    tmp = strtok(NULL, ":");
    periodEndHour = atoi(tmp);
    tmp = strtok(NULL, " ");
    periodEndMinute = atoi(tmp);

    // time table array
    int duryear = periodEndYear-periodStartYear+1;
    int timetable [duryear][12][31][24];

    int max = (periodEndYear-periodStartYear)*1000000+periodEndMonth*10000+periodEndDay*100+periodEndHour;
    int min = periodStartMonth*10000+periodStartDay*100+periodStartHour;
    int dayAmount = 0;
    // initialization
    int tmptime;
    for(i=0; i<duryear; i++) {
        for(j=0; j<12; j++) {
            for(k=0; k<31; k++) {
                havevalidday = 0;
                for(l=0; l<24; l++) {
                    tmptime = i*1000000+(j+1)*10000+(k+1)*100+l;
                    if(tmptime<min||tmptime>max)
                        timetable[i][j][k][l] = -2; //day do not need, disable
                    else
                        timetable[i][j][k][l] = -1; //valid day, give -1
                    if(k>monthDay[j]-1)
                        timetable[i][j][k][l] = -2; //day do not exist, disable
                    if(timetable[i][j][k][l] == -1)
                        havevalidday = 1;
                }
                if(havevalidday == 1)
                    dayAmount++;
            }
        }
    }
    int timetablebyday[dayAmount][24];
// for counting the number of used array slot to decide whether there is need to expand the array
    int eventCounter = 0;
    //initialize the formal input event and put them into correct Event data type.
    while(1) {
        printf("Please enter:\n");
        char input[100];
        fgets(input, 100, stdin);

        //decide whether this is the end of input
        if (input[0]=='a'&&input[1]=='d'&&input[2]=='d') {
            //initialize the type of event
            tmp = strtok(input, " ");
            for (i=3; i<strlen(tmp)+1; i++) {
                *(tmp+i-3) = *(tmp+i);
            }
            strcpy(event[eventCounter].type, tmp);
            //printf("%s\n",event[eventCounter].type);

            //initialize the name of event
            tmp = strtok(NULL, " ");
            strcpy(event[eventCounter].name, tmp);
            //printf("%s\n",event[eventCounter].name);

            if (strcmp(event[eventCounter].type, "Project") != 0) {
                //initialize the year of start time of this event
                tmp = strtok(NULL, "-");
                event[eventCounter].startYear = atoi(tmp);
                //printf("%d\n",event[eventCounter].startYear);

                //initialize the month of start time of this event
                tmp = strtok(NULL, "-");
                event[eventCounter].startMonth = atoi(tmp);
                //printf("%d\n",event[eventCounter].startMonth);

                //initialize the day of start time of this event
                tmp = strtok(NULL, " ");
                event[eventCounter].startDay = atoi(tmp);
                //printf("%d\n",event[eventCounter].startDay);

                //initialize the hour of start time of this event
                tmp = strtok(NULL, ":");
                event[eventCounter].startHour = atoi(tmp);
                //printf("%d\n",event[eventCounter].startHour);
                tmp = strtok(NULL, " ");
            }

            //test whether the event is project or assignment
            if (strcmp(event[eventCounter].type, "Project") == 0 || strcmp(event[eventCounter].type, "Assignment") == 0) {
                //initialize the year of due time of this event
                tmp = strtok(NULL, "-");
                event[eventCounter].dueYear = atoi(tmp);

                //initialize the month of due time of this event
                tmp = strtok(NULL, "-");
                event[eventCounter].dueMonth = atoi(tmp);

                //initialize the day of due time of this event
                tmp = strtok(NULL, " ");
                event[eventCounter].dueDay = atoi(tmp);

                //initialize the hour of due time of this event
                tmp = strtok(NULL, ":");
                event[eventCounter].dueHour = atoi(tmp);
                tmp = strtok(NULL, " ");
                int minute = atoi(tmp);
                if (minute > 0) {
                    event[eventCounter].dueHour++;
                }
            }

            //initialize the duration
            tmp = strtok(NULL, " ");
            event[eventCounter].duration = atoi(tmp);
            //printf("%d\n",event[eventCounter].duration);

            tmp = strtok(NULL, " ");
            event[eventCounter].priority = atoi(tmp);
            //printf("%d\n",event[eventCounter].priority);

            //expand dynamic array to contain more event (may need more tests)
            eventCounter++;
            if (eventCounter == eventSize) {
                Event *tmpEvent = (Event*)calloc(eventSize, 80);
                memcpy(tmpEvent, event, eventSize*80);
                eventSize *= 2;
                event = (Event*)calloc(eventSize, 80);
                memcpy(event, tmpEvent, eventSize*40);
                free(tmpEvent);
            }
        } else if(strcmp(strtok(input, "-"), "runASKS ") == 0) {
            tmp = strtok(NULL, "|");
            strcpy(scheduleType, tmp);
            //	printf("---%s---\n",scheduleType);
            tmp = strtok(NULL, " ");
            tmp = strtok(NULL, " ");
            tmp = strtok(NULL, " ");
            tmp[strlen(tmp)-2] = '\0';
            fp = fopen("report.dat", "wt");
            tmpfp = fopen("analysis.dat","wt");
            break;
        }
        //printf("%d\n", eventCounter); //testing code
    }//end of input module

    // put all class and sleep into the timetable
    int theYear;
    int theMonth;
    int theDay;
    int theHour;
    int timetablevalue;
    int periodStartTimeValue = periodStartMonth*10000+periodStartDay*100+periodStartHour;
    int periodEndTimeValue = duryear*1000000+periodEndMonth*10000+periodEndDay*100+periodEndHour;
    for(m=0; m<eventCounter; m++) {
        theYear = event[m].startYear - periodStartYear;
        theMonth = event[m].startMonth;
        theDay = event[m].startDay;
        theHour = event[m].startHour;
        if((event[m].startYear+event[m].startMonth+event[m].startDay+event[m].startHour)!=0) {
            // if start time has been input, calculate the starttimevalue
            event[m].startTimeValue = theYear*1000000+theMonth*10000+theDay*100+theHour;
        }
        else { // if start time not input, set it to the start of the period and calculate
            event[m].startYear = periodStartYear;
            event[m].startMonth = periodStartMonth;
            event[m].startDay = periodStartDay;
            event[m].startHour = periodStartHour;
            theYear = event[m].startYear - periodStartYear;
            theMonth = event[m].startMonth;
            theDay = event[m].startDay;
            theHour = event[m].startHour;
            event[m].startTimeValue = theYear*1000000+theMonth*10000+theDay*100+theHour;
        }
        if((event[m].dueYear+event[m].dueMonth+event[m].dueDay+event[m].dueHour)!=0)
            // calculate duetimevalue
            event[m].dueTimeValue = (event[m].dueYear-periodStartYear)*1000000+event[m].dueMonth*10000+event[m].dueDay*100+event[m].dueHour;
        //	printf("%d %d %d %d\n",event[m].startYear,event[m].startMonth,event[m].startDay,event[m].startHour);
        //	printf("%d %d\n ",event[m].startTimeValue,event[m].dueTimeValue);
        if(strcmp(event[m].type,"Class")==0||strcmp(event[m].name,"Sleep")==0) { // for class and sleep
            if(event[m].startTimeValue<periodStartTimeValue) {
                //printf("%s %d %d\n",event[m].name,event[m].startTimeValue,periodStartTimeValue);
                continue;
            }
            //printf("%s %s \n",event[m].type,event[m].name);
            for(i=theYear; i<duryear; i++) { //year
                for(j=0; j<12; j++) { //month
                    for(k=0; k<31; k++) { //day
                        for(l=0; l<24; l++) { //hour
                            timetablevalue = (i)*1000000+(j+1)*10000+(k+1)*100+l+1;//time in this round of loop
                            if(timetablevalue > event[m].startTimeValue &&timetable[i][j][k][l] == -1) {
                                event[m].handled++;
// 啊啊啊腰疼疼疼疼						//printf("handled\n");
                                timetable[i][j][k][l] = m;
                                if(event[m].handled == event[m].duration) {
                                    //	printf("%d %d %d %d %s\n",i+1,j+1,k+1,l+1,event[m].type);
                                    break;
                                }
                            }//if
                            if(event[m].handled == event[m].duration) {
                                break;
                            }
                        }//l
                        if(event[m].handled == event[m].duration) {
                            break;
                        }
                    }//k
                    if(event[m].handled == event[m].duration) {
                        break;
                    }
                }//j
                if(event[m].handled == event[m].duration) {
                    break;
                }
            }//i
        }
    }
    //printf("%s\n",scheduleType);
    if(strcmp(scheduleType, "FCFS ") == 0) {
        //	printf("fcfs\n");
        FCFS(event,eventCounter,duryear,timetable);
        scheduleType = "First Come First Serve";
    }
    else if(strcmp(scheduleType, "SJF ") == 0) {
        //	printf("sjf\n");
        SJF(event,eventCounter,duryear,timetable);
        scheduleType = "Short Job First";
    }
    else if(strcmp(scheduleType, "SRT ") == 0) {
        //	printf("srt\n");
        SRT(event,eventCounter,duryear,timetable);
        scheduleType = "Shortest Remaining Time";
    }
    else if(strcmp(scheduleType, "PR p ") == 0) {
        //	printf("pr p\n");
        PRP(event,eventCounter,duryear,timetable);
        scheduleType = "priority with preemption";
    }
    else if(strcmp(scheduleType, "PR n ") == 0) {
        //	printf("pr n\n");
        PRN(event,eventCounter,duryear,timetable);
        scheduleType = "priority without preemption";
    }
    else {
        tmpforrr = strtok(scheduleType, " ");
        if(strcmp(tmpforrr,"RR ")) {
            qtm = atoi(strtok(NULL," "));
            RRX(event,eventCounter,duryear,timetable,qtm);
            scheduleType = "Round Robin with quantum of ";
        }
    }

    /*for(i=0;i<duryear;i++){
    	for(j=0;j<12;j++){
    //	printf("month %d\n",j+1);
    		for(k=0;k<31;k++){
    	//	printf("day %d\n",k+1);
    			for(l=0;l<24;l++){
    					if(timetable[i][j][k][l] != -2)
    					printf("%d ",timetable[i][j][k][l]);
    			}
    	//	printf("\n");
    		}
    	}
    }
    printf("\n-----\n");
    */

    //prepare for multi-process
    int fd[2];
    if(pipe(fd)<0) {
        printf("pipe creation error!\n");
        exit(1);
    }
    pid = fork();
    if(pid == 0) { //child for output...
        //printf("outer child %d %d\n",getpid(),getppid());
        close(fd[1]);
        for(i=0; i<dayAmount; i++) {
            for(j=0; j<24; j++) {
                if(i==0&&j<periodStartHour)
                    timetablebyday[i][j] = -2;
                else if(i==dayAmount-1&&j>periodEndHour)
                    timetablebyday[i][j] = -2;

                else {
                    read(fd[0],&intbuf,sizeof(int));
                    timetablebyday[i][j] = intbuf;
                    //printf(" %d",timetablebyday[i][j]);
                }
            }
        }
        pid = fork();
        if(pid == 0) { //child of child for output
            //printf("inner child %d %d\n",getpid(),getppid());
            fprintf(fp, "Alex Timetable\n");
            fprintf(fp, "%d-%02d-%02d %02d:%02d to %d-%02d-%02d %02d:%02d\n", periodStartYear, periodStartMonth, periodStartDay,periodStartHour, periodStartMinute, periodEndYear, periodEndMonth, periodEndDay, periodEndHour, periodEndMinute);
            fprintf(fp, "time\t");
            for(i=0; i<duryear; i++) {
                for(j=0; j<12; j++) {
                    for(k=0; k<31; k++) {
                        havevalidday = 0;
                        for(l=0; l<24; l++) {
                            if(timetable[i][j][k][l] != -2)
                                havevalidday = 1;
                        }

                        if(havevalidday == 1)
                            fprintf(fp,"%d-%02d-%02d\t",i+periodStartYear,j+1,k+1);
                    }//k
                }//j
            }//i
            fprintf(fp,"\n");
            for(i=0; i<24; i++) {
                fprintf(fp,"%02d:00\t",i);
                for(j=0; j<dayAmount; j++) {
                    if(timetablebyday[j][i] == -1)
                        fprintf(fp,"N/A\t\t");
                    else if(timetablebyday[j][i] > -1)
                        fprintf(fp,"%s  \t",event[timetablebyday[j][i]].name);
                    else
                        fprintf(fp,"invalid time\t");
                }
                fprintf(fp,"\n");
            }
            fclose(fp);
            exit(0);
        }
        else { //parent of child for analysis
            //printf("child parent %d %d\n",getpid(),getppid());
            fprintf(tmpfp,"\n\n**Summary of assignments allocation**\n\n");
            fprintf(tmpfp,"Algorithms used: %s",scheduleType);
            if(qtm != 0)
                fprintf(tmpfp,"%d",qtm);
            fprintf(tmpfp,"\n");
            int cnt=0;
            for(i=0; i<eventCounter; i++) {
                if(event[i].duration == event[i].handled)
                    cnt++;
            }
            int turntime = 0;
            int tmpturntime = 0;
            int tmpwaittime = 0;
            int waittime = 0;
            fprintf(tmpfp,"There are %d assignments scheduled. Details are as follows\n",cnt);
            fprintf(tmpfp,"ASSIGNMENT\tSTART\t\t\tEND\t\t\tTURNAROUND TIME\n");
            fprintf(tmpfp,"===========================================================================\n");
            //finished calculate turnaround time and wait time
            for(m=0; m<eventCounter; m++) {
                tmpturntime = 0;
                tmpwaittime = 0;
                if(event[m].duration != event[m].handled)// not finished
                    continue;//pass
                event[m].handled = 0;
                for(i=0; i<duryear; i++) { //year
                    for(j=0; j<12; j++) { //month
                        for(k=0; k<31; k++) { //day
                            for(l=0; l<24; l++) { //hour
                                if(timetable[i][j][k][l]==-2)
                                    continue;
                                timetablevalue = (i)*1000000+(j+1)*10000+(k+1)*100+l;//time in this round of loop
                                if(event[m].startTimeValue<timetablevalue+1) {
                                    tmpwaittime++;
                                    tmpturntime++;
                                }
                                if(timetable[i][j][k][l] == m) {
                                    if(event[m].handled==0) {
                                        event[m].startYear = i+periodStartYear;
                                        event[m].startMonth = j+1;
                                        event[m].startDay = k+1;
                                        event[m].startHour = l;
                                    }
                                    event[m].handled++;
                                    tmpwaittime--;
                                    if(event[m].handled == event[m].duration) {
                                        fprintf(tmpfp,"%s\t%d-%02d-%02d %02d:00\t%d-%02d-%02d %02d:00\t%dHrs\t\n",event[m].name,event[m].startYear,event[m].startMonth,event[m].startDay,event[m].startHour,i+periodStartYear,j+1,k+1,l,tmpturntime);
                                        turntime = turntime + tmpturntime;
                                        waittime = waittime + tmpwaittime;
                                        break;
                                    }
                                }
                            }//l
                            if(event[m].handled == event[m].duration) {
                                break;
                            }
                        }//k
                        if(event[m].handled == event[m].duration) {
                            break;
                        }
                    }//j
                    if(event[m].handled == event[m].duration) {
                        break;
                    }
                }//i
            }
            fprintf(tmpfp,"  -end-\n");
            fprintf(tmpfp,"\n===========================================================================\n");
            fprintf(tmpfp,"**PERFORMANCE\n\n");
            fprintf(tmpfp,"AVERAGE TURNAROUND TIME FOR ASSIGNMENTS: %d HRS\n",turntime/cnt);
            fprintf(tmpfp,"AVERAGE WAITINGTIME FOR ASSIGNMENTS: %d HRS\n",waittime/cnt);
            fprintf(tmpfp,"\n**Outstanding/Rejected List**\n\n");
            m=0;
            for(i=0; i<eventCounter; i++)
                if(event[i].duration!=event[i].handled) {
                    m++;
                    fprintf(tmpfp,"%s %s\n",event[i].type,event[i].name);
                }

            fprintf(tmpfp,"There are %d events rejected.\n\n\n",m);
            fclose(tmpfp);
            //remove("analysis.dat");
            waitpid();
            exit(0);
        }
    }
    else { //parent
        close(fd[0]);
        //printf("parent %d %d\n",getpid(),getppid());
        for(i=0; i<duryear; i++) {
            for(j=0; j<12; j++) {
                for(k=0; k<31; k++) {
                    for(l=0; l<24; l++) {
                        if(timetable[i][j][k][l] != -2) {
                            intbuf = timetable[i][j][k][l];
                            //	printf(" %d \n ",intbuf);
                            write(fd[1],&intbuf,sizeof(int));
                        }
                    }
                }
            }
        }
        waitpid();
        sleep(1);
        fp = fopen("report.dat", "r");
        tmpfp = fopen("analysis.dat","r");
        dest = fopen(tmp,"a+");
        char byte;

        while (!feof(fp)) {
            fread(&byte, sizeof(char), 1, fp);
            fwrite(&byte, sizeof(char), 1, dest);
        }
        close(fp);
        while (!feof(tmpfp)) {
            fread(&byte, sizeof(char), 1, tmpfp);
            fwrite(&byte, sizeof(char), 1, dest);
        }
        close(tmpfp);
        close(dest);
        remove("report.dat");
        remove("analysis.dat");
        exit(0);
    }
}
Beispiel #8
0
int main(void)
{
    int preemtive(int line_number,struct proc process[MAXPROC]);
    int nonpreemtive(int line_number,struct proc process[MAXPROC]);
    int FCFS(int line_number,struct proc process[MAXPROC]);
    int RoundRobin(int line_number,struct proc process[MAXPROC]);
    int choice;
    int loop=4;
    while(1)
    {
        int i;
        for(i=0; i<MAXSIZE; i++)
            q[i]=-2;
        char *inname = "test.txt";
        FILE *infile;
        char line_buffer[50][BUFSIZ]; /* BUFSIZ is defined if you include stdio.h */
        int length;

        infile = fopen(inname, "r");
        if (!infile)
        {
            printf("Couldn't open file %s for reading.\n", inname);
            return 0;
        }
        char space=' ';
        printf("Opened file %s for reading.\n", inname);

        line_number = 0;
        struct proc process[MAXPROC];
        while (fgets(line_buffer[line_number], sizeof(line_buffer[line_number]), infile))
        {


            /* note that the newline is in the buffer */
            length = strlen(line_buffer[line_number]);
            if (line_buffer[line_number][length - 1] == '\n')
                line_buffer[line_number][length - 1] = '\0';
            printf("%4d: %s\n", line_number, line_buffer[line_number]);
            int i=0;
            int c=0;

            {
                /* data */
            };
            int ar=0;
            while(1)
            {
                if(i==length)
                    break;
                if(line_buffer[line_number][i]==space && ar==1)
                {
                    process[line_number].cpu[c]=atoi(&line_buffer[line_number][i+1]);
                    c++;
                }
                if(line_buffer[line_number][i]==space && ar==0)
                {
                    process[line_number].arrival=atoi(&line_buffer[line_number][i+1]);
                    ar=1;
                }
                i++;
            }
            process[line_number].max=c;
            process[line_number].pointer=0;
            process[line_number].number=atoi(&line_buffer[line_number][0]);
            process[line_number].cpuf=true;

            ++line_number;
        }
        fclose(infile);
        printf("1. Pre-emptive\n");
        printf("2. Non-emptive\n");
        printf("3. FCFS\n");
        printf("4. Round Robin\n");
        printf("5. To exit\n");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
            preemtive(line_number,process);
            break;
        case 2:
            nonpreemtive(line_number,process);
            break;
        case 3:
            FCFS(line_number,process);
            break;
        case 4:
            RoundRobin(line_number,process);
            break;
        case 5:
            return 0;
        default:
            printf("Wrong Choice\n");
        }
    }
    printf("\nTotal number of lines = %d\n", line_number);

    return 0;
}
Beispiel #9
0
int main(int argc, char *argv[])
{
    //set the variables
    int Select = 0;
    bool INTERACTIVE = false;
    bool test = false;
    SIMULATION* SIMINFO;
    FILE *DATA;
    if(argc !=5)
    {
        printf("+===================================+\n");
        printf("|              ProcSim              |\n");
        printf("|===================================|\n");
        printf("|  What type of simulation?         |\n");
        printf("|----+------------------------------|\n");
        printf("|  1 |  First Come First Server     |\n");
        printf("|  2 |  Shortest Job First          |\n");
        printf("|  3 |  Shortest Job Remaining      |\n");
        printf("|  4 |  Round-Robin                 |\n");
        printf("|  5 |  Priority                    |\n");
        printf("|----|--Multi-level Feedback Queue  |\n");
        printf("+----+------------------------------+\n\n\n\n");
        printf("ProcSim <Input File> <Num of Processes> <Snapshot Time Interval> <Select>\n");
        return -1;
    }

    if ((DATA=fopen(argv[1],"rt")) == NULL)
    {
        printf("Sorry Could not find the File\nIt might have ran away from you!\nmake sure it is in the same directory as me(Program)\n");
        printf("I am going to skip the inputing from a file");
        exit(0);
    }

    Select = atoi(argv[4]);

//setup the Data
    SIMINFO= (SIMULATION*) malloc (sizeof (SIMULATION));
    SIMINFO->IOProc = 0;
    SIMINFO->RQProc = 0;
    SIMINFO->Time = 0;
    SIMINFO->CPU_Idle = 0;
    SIMINFO->IOJFinished = -1;//indicate no jobs finished
    SIMINFO->TotalProc = atoi(argv[2]);
    SIMINFO->TimeInterval = atoi(argv[3]);
    SIMINFO->PCheck = false;

    sprintf(SIMINFO->SeqOfProc, ":");
    PROCESS Proc[SIMINFO->TotalProc];
    SIMINFO->RQProc = InputFromFile(Proc,DATA);
    SIMINFO->IOProc = 0;
    //ListProcess(Proc,SIMINFO);

//just uncomment what function you need

if(Select == 1)
{
    FCFS(Proc, SIMINFO);
}
if(Select == 2)
{
    SJF(Proc, SIMINFO);
}
if(Select == 3)
{
    SJR(Proc, SIMINFO);
}
if(Select == 4)
{
    RR(Proc, SIMINFO);
}
if(Select == 5)
{
    PS(Proc,SIMINFO);
}
if(Select == 6)
{
    //Not Implemented
}


    if(test == true)
    {
        //Array_test(DATA,total);
    }

    //Interactive Mode
    if(INTERACTIVE == true)
    {
        //main menu for the ProSim functions
        printf("+===================================+\n");
        printf("|              ProcSim              |\n");
        printf("|===================================|\n");
        printf("|  What type of simulation?         |\n");
        printf("|----+------------------------------|\n");
        printf("|  1 |  First Come First Server     |\n");
        printf("|  2 |  Shortest Job First          |\n");
        printf("|  3 |  Shortest Job Remaining      |\n");
        printf("|  4 |  Round-Robin                 |\n");
        printf("|  5 |  Priority                    |\n");
        printf("|  6 |  Multi-level Feedback Queue  |\n");
        printf("|  x |  Exit the program            |\n");
        printf("+----+------------------------------+\n");

    }

    return 0;
}
Beispiel #10
0
int main(){
	Queue *ready_queue;
	Queue *FCFS_queue, *RR_queue, *MRR_queue, *MHRR_queue;
	Queue *FCFS_pool, *RR_pool, *MRR_pool, *MHRR_pool;
	ready_queue = createQueue(MAX_SIZE_QUEUE);

	createJobPool();
	int i = 0;
	FILE *f;
	f = fopen("job_pool.txt", "r");

	// Copy the job pool data into another queue for each scheduling
    	// method and copy the queue of elements into another queue for
    	// each scheduling method
	printf("%s\n", "Generating jobs.");
    	FCFS_pool = createQueue(JOB_POOL_SIZE);
    	RR_pool = createQueue(JOB_POOL_SIZE);
    	MRR_pool = createQueue(JOB_POOL_SIZE);
    	MHRR_pool = createQueue(JOB_POOL_SIZE);

    	FCFS_queue = createQueue(MAX_SIZE_QUEUE);
    	RR_queue = createQueue(MAX_SIZE_QUEUE);
    	MRR_queue = createQueue(MAX_SIZE_QUEUE);
    	MHRR_queue = createQueue(MAX_SIZE_QUEUE);

    	for (i = 0; i < JOB_POOL_SIZE; i++){
    	    int quanta;
    	    fscanf(f, "%i", &quanta);
    	    enqueue(FCFS_pool, quanta);
    	    enqueue(RR_pool, quanta);
    	    enqueue(MRR_pool, quanta);
    	    enqueue(MHRR_pool, quanta);
    	}
	
    	// Transfer jobs to reach the steady state.
	printf("%s\n", "Transferring elements to reach the steady state.");
    	transfer(FCFS_queue, FCFS_pool, STEADY_STATE);
    	transfer(RR_queue, RR_pool, STEADY_STATE);
    	transfer(MRR_queue, MRR_pool, STEADY_STATE);
    	transfer(MHRR_queue, MHRR_pool, STEADY_STATE);
	
	srand(time(NULL));
	
    	// Now run each of the processes on the same exact data
	printf("%s\n", "Starting FCFS.");
    	FCFS(FCFS_queue, FCFS_pool);
	printf("%s\n", "Starting Round Robin");
    	RoundRobin(RR_queue, RR_pool);
	printf("%s\n", "Starting Modified Round Robin");
    	ModifiedRoundRobin(MRR_queue, MRR_pool);
	printf("%s\n", "Modified Half Round Robin");
    	ModifiedHalfedRoundRobin(MHRR_queue, MHRR_pool);

	printf("%s\n", "Freeing allocated memory.");
	// Free all allocated memory
	free(ready_queue);
	free(FCFS_queue);
	free(RR_queue);
	free(MRR_queue);
	free(MHRR_queue);
	free(FCFS_pool);
	free(RR_pool);
	free(MRR_pool);
	free(MHRR_pool);
	
	return 0;
}