Пример #1
0
// *** PROCESS A *** PID 3
void ProcessA(){
    
    // initialize return int, count
    int num = 0;
    PCB* pA_pcb = convert_PID(3);
    // receive the message from the CCI
    msg_env* envA = receive_message();
    int R = 0;
    R = get_console_chars(envA);
    if (R==0)
        release_processor();

    // deallocate the message from Proc A's queue in its PCB
    envA= env_DEQ(pA_pcb->receive_msg_Q);

    while(1)
    {
        printf("A\n");
        // request an envelope
        msg_env* tempEnv  = request_msg_env();
        
        if(tempEnv)
        {
            // set the env type and text
            tempEnv->msg_type = 2;
            tempEnv->msg_text[0] = num;

            // send to ProcessB
            int result = send_message(4, tempEnv);

            num++;
        }

        release_processor();        
    }    
}
Пример #2
0
void cci_process()
{
	CCI_DISPLAY_ENV = request_msg_env();
	MsgEnv *cci_env = request_msg_env();
	MsgEnv*  a_env = request_msg_env(); // this wont be released as process A will release it later
	char formatted_msg[1000];
	int retVal;

	while(1)
	{
		const int twait = 500000;
		cci_print("CCI: ");
		//cci_env = request_msg_env();
		get_console_chars(cci_env);

		cci_env = receive_message();
		while(cci_env->msg_type != CONSOLE_INPUT)
		{
			release_message_env(cci_env);
			cci_env = receive_message();
		}

		//Obtained keyboard input
		char command [MAXCHAR];
		int offset = sprintf(command,  cci_env->data);
		// Check atleast 2 characters even if user entered 1.
		if (offset < 2)
			offset = 2;

		// Send a message to process A. This only happens once. If it has already been sent, then prompt user.
		if (strncasecmp(command, "s", offset) == 0)
		{
			if (a_env != NULL)
			{
				retVal = send_message(PROCA_ID, a_env);
				if (retVal != SUCCESS)
						cci_print("Failed to send message envlope and start A\n");
				a_env = NULL;
			}
			else
			{
				cci_print("A has already started.\n");
			}
		}
		else if(strncasecmp(command, "ps", offset ) == 0)
		{
			retVal = request_process_status(cci_env);
			if (retVal != SUCCESS)
				cci_print("Failed to request process status");
			sprintf(formatted_msg,  cci_env->data);
			cci_print(formatted_msg);
		}
		else if(strncasecmp(command, "cd", offset) == 0)
		{
			displayClock(1);
		}
		else if(strncasecmp(command, "ct", offset) == 0)
		{
			displayClock(0);
		}
		// Display Trace Buffers
		else if(strncasecmp(command, "b", offset) == 0)
		{
			retVal = get_trace_buffers(cci_env);
			if (retVal != SUCCESS)
				cci_print("Failed to get trace buffers");
			sprintf(formatted_msg, "%s", cci_env->data);
			cci_print(formatted_msg);
		}
		else if(strncasecmp(command, "c", 2) == 0)
		{
			if(command[4] != ':' || command[7] != ':' || strlen(command) != 10)
			{
				sprintf(formatted_msg, "Invalid format for command %s. It should be: c <hh>:<mm>:<ss>\n", "c");
				cci_print(formatted_msg);
			}
			else
			{
				const char * rawTimeString = command+2;
			    char hourString [3] = { '0', '0', '\0'};
			    char minString [3] = { '0', '0', '\0'};
			    char secString [3] = { '0', '0', '\0'};
			    int i, hr, min, sec;
			    for (i=0;i<2;i++)
			    {
			        hourString[i] =rawTimeString[i];
			        minString[i]=rawTimeString[3+i];
			        secString[i]=rawTimeString[6+i];
			    }

			    hour = atoi(hourString);
			    min = atoi(minString);
			    sec = atoi(secString);

			    if (hour>23 || min>59 || sec > 59)
			    {
			    	sprintf(formatted_msg, "Invalid input."
			    			" Ensure hh not greater than 23, mm not greater than 59, ss not greater than 59.\n");
			    	cci_print(formatted_msg);
			    }
			    else
			    	setClock(hour, min, sec);   // offset the "c " (c-space)
			}
		}
		else if(strncasecmp(command, "n", 1) == 0)
		{

			int priority, pid;
			// extract priority and pid from the command
			if (sscanf(command, "%*s %i %i", &priority, &pid)!=2)
			{
				sprintf(formatted_msg, "Invalid format for command %s. It should be: n <priority> <process id>\n", "n");
				cci_print(formatted_msg);
			}
			else
			{
				retVal = change_priority(priority, pid);
				sprintf(formatted_msg, "Priority: %i, Pid: %i\n", priority, pid);
				cci_print(formatted_msg);
				if (retVal == ILLEGAL_ARGUMENT)
				{
					sprintf(formatted_msg, "Invalid arguments. Ensure that the priority is between [0-3], and the process ID is a valid"
							"process ID other than the NULL process ID\n");
					cci_print(formatted_msg);
				}
				else if (retVal != SUCCESS)
				{
					cci_print("Priority of specified process could not be changed\n");
				}
			}
		}
		else if(strncasecmp(command, "t", offset) == 0)
		{
			// no need to release envelopes as terminate will free all memory from MSG_LIST
			terminate();
		}
		// debugging function. find where all the envelopes are.
		else if(strncasecmp(command, "en", offset) == 0)
		{
			int offset = sprintf(formatted_msg, "\nEnvelope Num\tHeld By\n");
			int i;
			for (i  = 0; i < MSG_ENV_COUNT; ++i)
			{
				const char* pcb_name;
				switch(MSG_LIST[i]->dest_pid)
				{
					case(-1):
						pcb_name = "NONE\0";
						break;
					default:
						pcb_name = PCB_LIST[MSG_LIST[i]->dest_pid]->name;
				}
				offset += sprintf(formatted_msg+offset, "%i\t\t%s\n", i+1, pcb_name);
			}
			cci_print(formatted_msg);
		}
		// debugging, check all queues
		else if(strncasecmp(command, "cq", offset) == 0)
		{
			int offset = 0;
			offset += sprintf(formatted_msg, "\nREADY QUEUE");
			int rdy_queues = proc_pq_get_num_prorities(RDY_PROC_QUEUE);
			int i;
			for (i = 0; i < rdy_queues; ++i)
			{

				offset+= sprintf(formatted_msg+offset, "\nPriority %i: ", i);
				pcb* head = RDY_PROC_QUEUE->priority_queues[i]->head;
				while (head != NULL)
				{
					offset += sprintf(formatted_msg+offset, "%s->", head->name);
					head = head->next;
				}
			}

			offset += sprintf(formatted_msg+offset, "\n\nBLOCKED QUEUE: ");
			pcb* head = BLOCKED_QUEUE->head;
			while(head!=NULL)
			{
				offset += sprintf(formatted_msg+offset, "%s->", head->name);
				head = head->next;
			}
			offset += sprintf(formatted_msg + offset, "\n\n");

			cci_print(formatted_msg);
		}
		// One space and enter results in CCI: being printed again
		else if(strncasecmp(command, " ", offset) == 0)
		{
		}
		// If enter is directly pressed, display a different error
		else if(strcmp(command, "") == 0)
		{
			cci_print("Enter a command!\n");
		}
		// Default error message
		else
		{
			sprintf(formatted_msg, "The command %s is not supported\n", command);
			cci_print(formatted_msg);
		}
		//printf("releasing %p\n", cci_env);
		//release_message_env(cci_env);
	}
	fflush(stdout);
	printf("=====================WTH! CCI came out of while loop!");
	fflush(stdout);
	terminate();
}
Пример #3
0
void CCI() 
{   
    
        //printf("You're in the CCI\n");
        int HH=0;
        int MM=0;
        int SS=0;
        int pri=0;
        int id=0;
        int U = 0;
        int dun = 0;
        
        msg_env* env = request_msg_env();
        msg_env* first = request_msg_env();
    
        while(1)
        {   
            // send the welcome message
/*
            if (first == NULL){
                terminate(1);
            }
            strcpy(first->msg_text, "Enter input for the CCI [s, ps, c [##:##:##], cd, ct, b, n [new_prio] [pid], e... , t]:\0");

            int F = 0;
            F = send_console_chars(first);

            if (F == 0){
                terminate(1);
            }
          
*/
         
            U = get_console_chars(env);   //keyboard input 
            //sleep(5);
            
            // only do this if there was input
            if (U==1)
            {
                env = receive_message();
                while (env->msg_type != CONSOLE_INPUT)
                {                
                        env = receive_message(); //***STOPS HERE TO WAIT FOR INPUT
                }
              
                char* input_txt = env->msg_text;
                
                // send envelope to Process A
                if (strcmp(input_txt,"s")==0) 
                { 
                    // printf("input was s\n");
                     //if the text in the field is "s"
                     env = request_msg_env(); //request an envelope
                     int Z =0;
                     Z = send_message(3,env); // send message to Process A
                }
                
                // display process status of all processes
                else if (strcmp(input_txt,"ps")==0) 
                { 
                    // printf("input was ps\n");
                     env = request_msg_env(); //request an envelope
                     int J=request_process_status(env);
                     if (J==0)
                        printf("Error with getting Process Status\n");                  
                }
                
                // allows time to be displayed on console and halts if getting ct
                else if (strcmp(input_txt,"cd")==0) 
                { 
                    // printf("input was cd\n");
                     
                    wallClockOut = 1;
                }
                else if (strcmp(input_txt,"ct")==0)
                {
                   // printf("input was ct\n");
                     wallClockOut = 0;
                }
    
                // set clock to any valid 24hr time
                else if (strncmp(input_txt,"c", 1)==0) 
                { 
                    // printf("input was c\n");
                     int HH = atoi(input_txt+2);
                     int MM = atoi(input_txt+5);
                     int SS = atoi(input_txt+8);
                     int R = clock_set(wallclock, HH, MM, SS);
                     
                     if (R==0)
                        printf("Error with setting clock in CCI\n");
                }
                
                // b displays past instances of send and receive messages
                else if (strcmp(input_txt,"b")==0) 
                { 
                   // printf("input was b\n");
                    env = request_msg_env(); //request an envelope
                    int U=get_trace_buffers(env);
                    
                    if (U==0)
                       printf("Error performing call on Trace Buffers\n");
               }
                
                // terminates RTX
                else if (strcmp(input_txt,"t")==0)
                { 
                    terminate(0);
                }
                
                // changes priority
                else if (strncmp(input_txt,"n",1)==0) 
                { 
                   //  printf("input was n\n");
                     int new_pri = atoi(input_txt+2);
                     int ID = atoi(input_txt+4);
                     int R = change_priority(new_pri, ID);
                     
                     if (R==0)
                            printf("Error with changing priority in CCI\n");
                }
                
                // echo the input back

                else if (strncmp(input_txt,"e", 1)==0) 
                { 
                   // printf("input was e\n");
                    env = request_msg_env(); //request an envelope
                    input_txt[0] = ' ';
                    strcat(input_txt, "\n\n");
                    strcpy(env->msg_text, "echo is: ");
                    strcpy(env->msg_text, input_txt);
                    int R = send_console_chars(env);
                    
                    if (R==0)
                            printf("Error with echoing to screen\n");
                }
                
                else 
                {
                     env = request_msg_env(); //request an envelope
                     char* temp = (char*) malloc(sizeof(SIZE));
                     sprintf(temp, "'%s' is not valid CCI input. Please try again.\n\n", input_txt);
                     strcpy(env->msg_text, temp);
                     int R = send_console_chars(env);
                    
                     if (R==0)
                         printf("Error with printing invalid input message\n");
                     
                    // printf("'%s' is not valid CCI input. Please try again.\n\n", input_txt);
                }

                
                dun = release_msg_env(env);

                if (dun == 0)
                {
                    env = request_msg_env(); //request an envelope
                    strcpy(env->msg_text, "ERROR IN DEALLOCATING ENVELOPE AT END OF CCI\n");
                    int R = send_console_chars(env);

                    if (R==0)
                          printf("Error with printing error message\n");
                }
                //else
                 //  printf("CCI finished, I think\n");
               
            }
         
        env = request_msg_env();
        
        // if there was no input
        release_processor();
        }
}