Exemplo n.º 1
0
void Interpreter::close()
{
    m_localProgramRunning = false;
    m_console->m_mutexPrint.lock();
    m_console->m_waitPrint.wakeAll();
    m_console->m_mutexPrint.unlock();
    unwait(); // if we're waiting for input, unhang ourselves

    m_run = false;
}
Exemplo n.º 2
0
void Interpreter::execute(const QString &command)
{
    QStringList argv = command.split(QRegExp("[\\s(),\\t]"), QString::SkipEmptyParts);
    unwait(); // unhang ourselves if we're waiting for input
    m_mutexProg.lock();
    m_argv = argv;
    m_externalCommand = command; // save command so we can print.  This variable also indicates that we're not a human typing a command
    m_mutexProg.unlock();
    if (m_running==true)
        queueCommand(STOP);
}
Exemplo n.º 3
0
void Interpreter::runOrStopProgram()
{
    unwait(); // unhang ourselves if we're waiting
    if (m_localProgramRunning)
        m_localProgramRunning = false;
    else if (m_running==false)
        queueCommand(RUN);
    else if (m_running==true)
        queueCommand(STOP);
    // no case to run local program because this is sort of an undocumented feature for now
}
Exemplo n.º 4
0
int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, const struct timespec *ts)
{
    struct cm cm = { .c=c, .m=m };
    int r, e=0, seq;

    if (m->_m_type && (m->_m_lock&INT_MAX) != pthread_self()->tid)
        return EPERM;

    if (ts && ts->tv_nsec >= 1000000000UL)
        return EINVAL;

    pthread_testcancel();

    a_inc(&c->_c_waiters);

    if (c->_c_mutex != (void *)-1) {
        c->_c_mutex = m;
        while (a_swap(&c->_c_lock, 1))
            __wait(&c->_c_lock, &c->_c_lockwait, 1, 1);
        c->_c_waiters2++;
        a_store(&c->_c_lock, 0);
        if (c->_c_lockwait) __wake(&c->_c_lock, 1, 1);
    }

    seq = c->_c_seq;

    pthread_mutex_unlock(m);

    do e = __timedwait(&c->_c_seq, seq, c->_c_clock, ts, cleanup, &cm, 0);
    while (c->_c_seq == seq && (!e || e==EINTR));
    if (e == EINTR) e = 0;

    unwait(c, m);

    if ((r=pthread_mutex_lock(m))) return r;

    return e;
}
void Interpreter::runOrStopProgram(bool local)
{
    unwait(); // unhang ourselves if we're waiting
    if (local)
    {
        if (m_running)
            queueCommand(STOP);
        else if (m_localProgramRunning)
            queueCommand(STOP_LOCAL);
        else
            queueCommand(RUN_LOCAL);
    }
    else
    {
        if (m_localProgramRunning)
            queueCommand(STOP_LOCAL);
        else if (m_running==false)
            queueCommand(RUN);
        else if (m_running==true)
            queueCommand(STOP);
        // note m_running has 3 states...
    }
}
Exemplo n.º 6
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);
}
Exemplo n.º 7
0
static void cleanup(void *p)
{
    struct cm *cm = p;
    unwait(cm->c, cm->m);
    pthread_mutex_lock(cm->m);
}