Ejemplo n.º 1
0
// *** SEND MESSAGE ***
int send_message(int dest_id, msg_env *e) {
    
    // if the env is NULL
    if (!e)
        return 0;
    
    // if the PCB ID is not valid
    if (dest_id > (NUM_PROC - 1) )  { 
        //printf("Invalid 'Send To' ID, %i, ", dest_id);
        return 0;
    }
    
    // if the PCD ID is valid
    // set the target_id parameter of the env to dest_id
    e->target_id = dest_id;
    e->sender_id = curr_process->pid;

    // create a pointer to the target PCB using the target_id
    PCB *target = convert_PID(dest_id);

    // if the PID converts correctly to a pointer
    if (target){

        /*unblock the target process if necessary*/
        // if the target's receive message queue is empty or the target is not an i-process
        /*if (target->receive_msg_Q->head == NULL || target->priority != -1) { 

            // enqueue the PCB of the process on the appropriate ready queue
            PCB_ENQ(target, convert_priority(target->priority)); //*****not sure if need to put a '&' before convert_priority
            // set the target state to 'ready'
            strcpy(target->state, "READY"); //apparently strcpy is how you write into an array of chars
        }*/

        // enqueue the env on the target's receive queue
        env_ENQ(e, target->receive_msg_Q);

    return 1;
    
    }
    
    // if the PID doesn't convert successfully
    else
        return 0;
     
}
Ejemplo n.º 2
0
// *** PROCESS C *** PID 5
void ProcessC(){
    
    // init the local queue
    struct envQ* localQ = (struct envQ *) malloc (sizeof (struct envQ));
    if (localQ==NULL)
    {
        printf("localQ in Proc C not created properly");
        return;
    } // can we use the queue on the proc pcb instead?

    PCB* pC_pcb = convert_PID(5);
    
    msg_env* envC = request_msg_env();   
    // infinite loop of normal activity  
    while (1)
    {
        printf("A\n");
        int NUM =envC->msg_text[0];
        
        // if theres nothing the localQ, receive a message and enqueue it
        if (localQ->head == NULL)
        {
            envC = receive_message();    
            int H = env_ENQ(envC,localQ);
            if (H ==0)
               printf("Cannot enqueue on localQ");
        }
        
        // if there is something on the localQ, dequeue it
        else {
            envC = env_DEQ(localQ);
        }
            
        // if the message type is count report, and the value in msg_text is evenly divisible by 20, display "Process C"
        if (envC->msg_type == 2 && NUM % 20 == 0){
  
            // send the display message
            strcpy(envC->msg_text, "Process C\n\0");
            int W = send_console_chars(envC); // Returns confirmation of sending
            
            if (W==1)
            {
                // if it is the ack message request a delay of 10s, with wakeup code "wakeup10"
                int R = request_delay(10000, WAKEUP, envC); // request_delay returns an int
                if (R==0)
                   printf("Error with request_delay");
                   
                // wait for wakeup message
                envC = receive_message();
    
                // if its not the wakeup message put it on the local Q
                while (envC->msg_type != WAKEUP)
                {
                    envC = receive_message();
                    int XX = env_ENQ(envC,localQ);
                    if (XX==0)
                       printf("Error with putting message on local Q in Proc C");
                }   
            }
            else
               printf("Error sending 'Process C' to screen in Proc C");
            
        } 
        
        // deallocate envelopes
        int dun = release_msg_env(envC);
        if (dun==0)
           printf("ERROR IN DEALLOCATING ENVELOPE AT END OF Proc C");
        
        // release processor
        release_processor();
            
    }// end of infinite loop
}