bool timed_wait( const mutex_guard< Mutex > &g,
                  const T &ttl,
                  typename meta::enable_if< is_time_interval< T > >::type* = 0 ) const
 {
     return wait_( g.m_, static_cast< win32::dword >( ttl.msecs() ) );
 }
示例#2
0
int main(int argc, char *argv[]) {
    int error = 0;
    char command[20];
    char args[10][10];
    int pid = 0;
    int psw = 0;
    int page_table = 0;
    int reg1 = 0;
    int reg2 = 0;
    int reg3 = 0;
    int group = 0;
    int regs[3];
    FILE *file;
	int priority = 0;
    if (argc == 1){
        file = stdin;
    }
    else if (argc == 2){
        file = fopen(argv[1], "r");
    }
    else{
        printf("Usage:\n./processmanager_test.o (to read from stdin)\n");
        printf("./processmanager_test.o <filename> (to read from filename)\n");
        exit(-1);
    }

    init(GROUP);


    while(1) {
        /*printf("***reading the file***\n");*/
        if (file == stdin) {
            printf("> ");
        }
        error = fscanf(file," %s", command);
        if (error == 1){
            /* printf("%s\n", line); */
            /* printf("%s\n", command); */

            if (!strcmp(command, "INIT")) {
                error = fscanf(file, " %s", args[0]);
                if (error == 1){
                    if (!strcmp(args[0], "GROUP")){
                        printf("\n***INIT command issued (%s)***\n", args[0]);
                        init(GROUP);
                    }
                    else if(!strcmp(args[0], "PRIORITY")){
                        printf("\n***INIT command issued (%s)***\n", args[0]);
                        init(PRIORITY);
                    }
                    else{
                        printf("Usage: INIT <GROUP | PRIORITY>\n");
                    }
                }
                else{
                    printf("Usage: INIT <GROUP | PRIORITY>\n");
                }

            }
            else if (!strcmp(command, "LIST")) {
                error = fscanf(file, " %s", args[0]);
                if (error == 1){
                    printf("\n***LISTING command issued (%s)***\n", args[0]);
                    if (!strcmp(args[0], "NEW")){
                        list_Q(NEW);
                    }
                    else if (!strcmp(args[0], "WAITING")){
                        list_Q(WAITING);
                    }
                    else if (!strcmp(args[0], "READY")){
                        list_ready();
                    }
                    else if (!strcmp(args[0], "TERMINATED")){
                        list_Q(TERMINATED);
                    }
                    else if (!strcmp(args[0], "RUNNING")){
                        list_Q(RUNNING);
                    }
                    else if (!strcmp(args[0], "ALL")){
                        list_all();
                    }
                    else if (!strcmp(args[0], "SCHED")){
                        list_sched();
                    }
                    else{
                        printf("Usage: LIST <NEW | WAITING | READY | TERMINATED | RUNNING | ALL | SCHED>\n");
                    }
                }
                else{
                    printf("Usage: LIST <NEW | WAITING | READY | TERMINATED | RUNNING | ALL | SCHED>\n");
                }
            }
            else if (!strcmp(command, "GO")){
                printf("\n***GO command issued***\n");
                error = go();
                if (error == ERROR_NO_READY_PROCESS){
                    printf("Could not GO: %s\n", error_to_string(error));
                }
                else if (error == ERROR_QUEUE_EMPTY ||
						error == ERROR_QUEUE_FULL ||
						error == ERROR_SWITCH_DEFAULT ||
						error == ERROR_PROCESS_NOT_EXIST){
                    printf("FATAL ERROR: %s\n", error_to_string(error));
                    exit(-1);
                }
            }
            else if (!strcmp(command, "UNWAIT")) {
                error = fscanf(file, " %d", &pid);
                if (error == 1){
                    printf("\n***UNWAIT command issued (PID: %d)***\n", pid);
                    error = unwait(pid);
                    if (error == ERROR_QUEUE_EMPTY){
                        printf("Could not UNWAIT: No waiting processes\n");
                    }
                    else if (error == ERROR_PROCESS_NOT_EXIST){
                        printf("Could not UNWAIT: PID does not exist in waiting queue\n");
                    }
                    else if (error == ERROR_QUEUE_FULL){
                        printf("FATAL ERROR: %s\n", error_to_string(error));
                        exit(-1);
                    }
                }
                else{
                    printf("Usage: UNWAIT <pid>\n");
                }
            }
			else if (!strcmp(command, "SET_PRIORITY")) {

				error = fscanf(file, " %d %d", &pid, &priority);
				if (error == 2){
					printf("\n***SET_PRIORITY command issued (PID: %d)***\n", pid);
					error = set_priority(pid, priority);
					if (error == ERROR_WRONG_SCHEDULER){
						printf("Could not SET_PRIORITY: Command only avaliable in Priority Scheduler\n");
					}
					else if (error == ERROR_PROCESS_NOT_EXIST){
						printf("Could not SET_PRIORITY: PID does not exist in ready queue\n");
					}
					else if (error == ERROR_INVALID_PARAMETER){
						printf("Could not SET_PRIORITY: Priority must be between 1 and %d\n", MAX_PRIORITY);
					}
				}
				else{
					printf("Usage: SET_PRIORITY <pid> <priority>\n");
				}

            }
            else if (!strcmp(command, "EOLIFE")) {
                printf("\n***EOLIFE command issued***\n");
                error = eolife();
                if (error == ERROR_QUEUE_EMPTY){
                    printf("Could not EOLIFE: No running processes\n");
                }
                else if (error == ERROR_QUEUE_FULL){
                    printf("FATAL ERROR: %s\n", error_to_string(error));
                    exit(-1);
                }
            }
            else if (!strcmp(command, "WAIT")) {
                printf("\n***WAIT command issued***\n");
                error = wait_();
                if (error == ERROR_QUEUE_EMPTY){
                    printf("Could not WAIT: No running processes\n");
                }
                else if (error == ERROR_QUEUE_FULL){
                    printf("FATAL ERROR: %s\n", error_to_string(error));
                    exit(-1);
                }
            }
            else if (!strcmp(command, "CREATE")) {
                /*Group Scheduler*/
                if (scheduler == GROUP){
                    error = fscanf(file, " %d %d %d %d %d %d", &psw, &page_table, &reg1, &reg2, &reg3, &group);
                    if (error == 6){
                        /* printf("pid: %d, psw: %d, page_table: %d, reg1: %d, reg2: %d, reg3: %d\n", pid, psw, page_table, reg1, reg2, reg3); */
                        regs[0] = reg1;
                        regs[1] = reg2;
                        regs[2] = reg3;

						error = create(psw, page_table, regs, group);
						if (error == ERROR_MAX_PROCESSES){
							printf("Could not CREATE: Maximum allowed processes reached\n");
						}
						else if (error == ERROR_GROUP_NOT_EXIST){
							printf("Could not CREATE: Invalid group number\n");
						}
						else if (error == ERROR_PROCESS_NOT_UNIQUE ||
						error == ERROR_QUEUE_FULL ||
						error == ERROR_QUEUE_EMPTY ||
						error == ERROR_QUEUE_FULL) {
							printf("FATAL ERROR: %s\n", error_to_string(error));
							exit(-1);
						}
						else if (error == ERROR_SUCCESS){
							printf("\n***CREATE command issued (PID: %d)***\n", (pid_counter - 1));
						}
					}
					else{
						printf("Usage: CREATE <psw> <page_table> <reg1> <reg2> <reg3> <group>\n");
					}
				}
				/*Priority Scheduler*/
				else{
					error = fscanf(file, " %d %d %d %d %d", &psw, &page_table, &reg1, &reg2, &reg3);
					if (error == 5){
						/* printf("pid: %d, psw: %d, page_table: %d, reg1: %d, reg2: %d, reg3: %d\n", pid, psw, page_table, reg1, reg2, reg3); */
						regs[0] = reg1;
						regs[1] = reg2;
						regs[2] = reg3;

						printf("\n***CREATE command issued (PID: %d)***\n", pid_counter);
						/*Priority scheduler defaults to group 0*/
						error = create(psw, page_table, regs, 0);
						if (error == ERROR_MAX_PROCESSES){
							printf("Could not CREATE: Maximum allowed processes reached\n");
						}
						else if (error == ERROR_PROCESS_NOT_UNIQUE ||
						error == ERROR_QUEUE_FULL ||
						error == ERROR_QUEUE_EMPTY ||
						error == ERROR_QUEUE_FULL ||
						error == ERROR_GROUP_NOT_EXIST) {
							printf("FATAL ERROR: %s\n", error_to_string(error));
							exit(-1);
						}
						else if (error == ERROR_SUCCESS){
							printf("\n***CREATE command issued (PID: %d)***\n", (pid_counter - 1));
						}
					}
					else{
						printf("Usage: CREATE <psw> <page_table> <reg1> <reg2> <reg3>\n");
					}
				}
            }
            else if (!strcmp(command, "EMPTY_TERM")){
                printf("\n***EMPTY_TERM command issued***\n");
                error = empty_term();
                if (error == ERROR_QUEUE_EMPTY){
                    printf("Could not EMPTY_TERM: No process in terminated queue\n");
                }
            }
            else if (!strcmp(command, "#")) {
                printf("\n################################################################################\n");
                printf("#");
                textcolor(BRIGHT, WHITE, BLACK);
                printf("%s\n", fgetstring(file));
                textcolor(RESET, -1, -1);
                printf("################################################################################\n");
            }
            else {
                printf("Unrecognized command: %s\n", command);
            }
        }
        else
            if (error == EOF) {
                printf("\n");
                break;
            }
    }

    fclose(file);

    return(0);
}
 bool timed_wait( const mutex_tpl< MutexTag > &mutex,
                  const T &ttl,
                  typename meta::enable_if< is_time_interval< T > >::type* = 0 ) const
 {
     return wait_( mutex, static_cast< win32::dword >( ttl.msecs() ) );
 }
 bool timed_wait( const mutex_guard< Mutex > &g, int64_t ttl ) const
 {
     return wait_( g.m_, static_cast< win32::dword >( ttl ) );
 }
 bool timed_wait( const mutex_tpl< MutexTag > &mutex, int64_t ttl ) const
 {
     return wait_( mutex, static_cast< win32::dword >( ttl ) );
 }
 void wait( const mutex_guard< Mutex > &g ) const
 {
     (void) wait_( g.m_ );
 }
 void wait( const mutex_tpl< MutexTag > &mutex ) const
 {
     (void) wait_( mutex );
 }