//Run the processes set
void MemoryManager::runMemoryManager(vector<Process> set){
   int setCounter = 0; //Index for the set
   //Loop until no more processes are avalible
   while(setCounter < set.size() || waitQueue.size() > 0){
       //Find expired processes that should be removed
       map<int,LoadedProcesses>::iterator expiredProcess = findExpiredProcess();

       //If expired process was found remove it
       if(expiredProcess != runningQueue.end()){
          LoadedProcesses p = expiredProcess->second;
          runningQueue.erase(expiredProcess);
          my_free(p);
       }

       //Add new process from wait queue
       if(setCounter >= set.size() || waitQueue.size() > 0){
          Process p = waitQueue[0];
          int index = findEmptySlot(int(p.mem));
          if(index != -1){
             LoadedProcesses l = createLoadedProcess(p,index);
             my_malloc(l);
             waitQueue.erase(waitQueue.begin()); //Remove from waitqueue
          }
       } else {
          //Add new process from the original set of processes
          Process p = set[setCounter++];
          int index = findEmptySlot(int(p.mem));
          //If empty slot load it else put in wait queue
          if(index != -1){
             LoadedProcesses l = createLoadedProcess(p,index);
             my_malloc(l);
          } else {
             waitQueue.push_back(p);
          }
       }       
       currCycles += SPAWNPROCESS; //Increment the current cycle by 50
   }
}
Exemple #2
0
void* clientHandler(void* arg)
{
    int clt_sock = *(int*)arg;
    char buf[MAX_MESSAGE]; bzero(buf, sizeof(buf));
    long bytes_read;
    char client_name[MAX_MESSAGE]; bzero(client_name, sizeof(buf));
    
    /* Insert new client into clients array */
    int idx = findEmptySlot(clients);
    pthread_mutex_lock(&array_lock);
    clients[idx] = clt_sock;
    pthread_mutex_unlock(&array_lock);
    
    /* Read client name */
    bytes_read = read(clt_sock, buf, sizeof(buf));
    char greeting[] = " connected to the server";
    strcpy(client_name, buf);
    strcat(buf, greeting);
    printf("%s\n", buf);
    
    /* Inform all clients of the connected user */
    int i;
    pthread_mutex_lock(&array_lock);
    for(i = 0; i < MAX_CLIENTS; i++)
    {
        if(clients[i] == -1) continue;
    
        write(clients[i], buf, strlen(buf));
    }
    pthread_mutex_unlock(&array_lock);
    
    bzero(buf, sizeof(buf));
    
    /* Read messages from the client and emit to all other connect clients */
    while( (bytes_read = read(clt_sock, buf, sizeof(buf))) != 0)
    {
        if(strcmp(buf,"/exit")==0 || strcmp(buf,"/quit")==0 || strcmp(buf,"/part")==0)
        {
            break;
        }
        
        printf("%s: %s\n", client_name, buf);
        
        char message[MAX_MESSAGE]; bzero(message, sizeof(message));
        strcpy(message, client_name);
        strcat(message, ": ");
        strcat(message, buf);
        
        emitMessage(message, strlen(message), clt_sock, clients);
        bzero(buf, sizeof(buf));
    }
    
    /* Update client state variables and close socket */
    pthread_mutex_lock(&array_lock);
    clients[idx] = -1;
    pthread_mutex_unlock(&array_lock);
    
    pthread_mutex_lock(&count_lock);
    client_count--;
    pthread_mutex_unlock(&count_lock);
    
    close(clt_sock);
    
    /* Inform users that a user is exiting */
    char exit_message[MAX_MESSAGE]; bzero(exit_message, sizeof(exit_message));
    strcpy(exit_message, client_name);
    strcat(exit_message, " disconnected from the server");
    printf("%s\n", exit_message);
    
    if(bytes_read > 0)
    {
        emitMessage(exit_message, strlen(exit_message), clt_sock, clients);
    }
    
    return NULL;
}