Exemple #1
0
tBTNode searchNodes(tBTNode N, tString *key){
    if(N==NULL) return NULL;
    else {
         int cmp=strCmp(key,N->key);
         if(cmp!=0){
             if(cmp<0)return searchNodes(N->left,key);
             else return searchNodes(N->right,key);
         }
         else return N;
    }
}
Exemple #2
0
//Set nodes in path to OPEN
void Graph::preservePath(std::list<Node>::iterator pbaseNode, std::list<Node>::iterator ptargetNode)
{
    //make sure iterators are valid
    std::list<Node>::iterator pnull = std::list<Node>::iterator(NULL);
    if (pbaseNode == pnull || ptargetNode == pnull)
    {
        //std::cout << "\nNULL ITERATORS PASSED TO preservePath()\n";
        return;
    }

    std::vector<short> baseWord = pbaseNode->getElemName();
    std::vector<short> targetWord = ptargetNode->getElemName();

    //target word must be longer than or equal length as base word
    if (targetWord.size() < baseWord.size())
    {
        //std::cout << "\nPRESERVE PATH BAD RETURN1\n";
        return;
    }
    //should never touch the identity; this is a special case which is handled in initial step
    if (baseWord[0] == 0 || targetWord[0] == 0)
    {
        //std::cout << "\nPRESERVE PATH BAD RETURN2\n";
        return;
    }
    //if words are identical there's nothing to do
    if (baseWord == targetWord)
    {
        //std::cout << "\nPRESERVE PATH BAD RETURN3\n";
        return;
    }
        //return;

    //At this point we know the nodes exist, their words have appropriate sizes, they're not the identity,
    //and they're not equal. But we could still have the problem that the base word is not contained
    //in the leftmost portion of the target word, so we check for that.
    bool baseContained = true;
    int i;
    for (i = 0; i < baseWord.size() - 1; i++)  //up until the last letter, each exponent identical
        if (baseWord[i] != targetWord[i])
            baseContained = false;
    if (baseWord[i] > targetWord[i])  //on the last letter, the exponent in base may be less or equal
        baseContained = false;
    if (baseContained == false)
    {
        std::cout << "\nPRESERVE PATH not contained:\n";
        pbaseNode->printWord();
        std::cout << "\n";
        ptargetNode->printWord();
        std::cout << "\n";
        return;
    }

    //nodes have been checked


    std::list<Node>::iterator psetNode = ptargetNode;
    while (targetWord.size() != baseWord.size()) //stop at last exponent of baseWord
    {
        int i = targetWord.size() - 1;  //always index of last exponent in targetWord
        int exp = targetWord[i];  //last exponent on targetWord
        while (exp > 0)
        {
            targetWord[i] = exp;
            psetNode = searchNodes(&targetWord);
            if (psetNode == std::list<Node>::iterator(NULL))
            {
                std::cout << "\nAttempt to set node via null iterator in preservePath()\n";
                return;
            }
            if (psetNode->getNodeType() != Node::TEMP && psetNode != ptargetNode)
            {
                //it's okay to hit a preserved node, this means the nodes before this one will also
                //be preserved
                return;
            }
            psetNode->setNodeType(Node::OPEN);
            exp--;
        }
        targetWord.pop_back(); //remove exponent just cleared
    }
    int d = targetWord.size() - 1;
    if (targetWord[d] == baseWord[d])
    {
        if(pbaseNode->isOpen())
            psetNode->setNodeType(Node::OPEN);
        else psetNode->setNodeType(Node::CLOSED);
        return;
    }
    int exp = targetWord[d];  //last exponent on targetWord
    while (exp > baseWord[d])
    {
        targetWord[d] = exp;
        psetNode = searchNodes(&targetWord);
        if (psetNode == std::list<Node>::iterator(NULL))
        {
            std::cout << "\nAttempt to set node via null iterator in preservePath()\n";
            return;
        }
        if (psetNode->getNodeType() != Node::TEMP)
        {
            //again this is not a problem
            return;
        }
        psetNode->setNodeType(Node::OPEN);
    }

    if(pbaseNode->isOpen())
        psetNode->setNodeType(Node::OPEN);
    else psetNode->setNodeType(Node::CLOSED);
    return;
}
Exemple #3
0
tBTNode btFind(tBTree *T,tString *key){
    return searchNodes(T->root,key);
}
Exemple #4
0
int main(int argc, char * argv[]) {
  
  killOldProcNannies();
  debugPrint("staring main\n");
  if (pipe(newProcessToChild) < 0) {
    fprintf(stderr,"pipe error");
    exit(0);
  }

  if (pipe(killCountPipe) < 0) {
    fprintf(stderr,"pipe error");
    exit(0);
  }

  fcntl(killCountPipe[0], F_SETFL, O_NONBLOCK);

  debugPrint("connecting to %s on port %d \n", argv[1], atoi(argv[2]));
  MY_PORT = atoi(argv[2]);
	
  /* Put here the name of the sun on which the server is executed */
  host = gethostbyname (argv[1]);


  int firstTime = 1;
  if (host == NULL) {
    perror ("Client: cannot get host description");
    exit (1);
  }

  s = socket (AF_INET, SOCK_STREAM, 0);

  if (s < 0) {
    perror ("Client: cannot open socket");
    exit (1);
  }

  bzero (&server, sizeof (server));
  bcopy (host->h_addr, & (server.sin_addr), host->h_length);
  server.sin_family = host->h_addrtype;
  server.sin_port = htons (MY_PORT);

  while (connect (s, (struct sockaddr*) & server, sizeof (server))) {
    perror ("Client: cannot connect to server");
    debugPrint("Tring again\n");
    //exit (1);
  }




  char * pipeBuffer = NULL;
  /* char * procInfoRequest = malloc(200); */
  /* snprintf(procInfoRequest,200, "%-200s", procInfoString); */
  /* char * a = "test\0"; */
  char a[200] = "test";
  char * processName = NULL;
  int len = 0;
  int numberofProcesses = 0;
  int processLifeSpan = 0;
  int PID = 0;
  char * pointerToProcessName = NULL;
  char * pointerToPipeBuffer = NULL;


  while (keepLooping == 1) {
    // use this loop to get process lists

    pipeBuffer = malloc(63);
    pointerToPipeBuffer = pipeBuffer;

    debugPrint("writing server\n");
    writeToServer(a);
    readFromServer(pipeBuffer);
    debugPrint("got %s\n", pipeBuffer);
    
    if (pipeBuffer[0] == '!') {
      keepLooping = 0;
      debugPrint("got exit signal!!!!!\n");
      continue;
    }

    //    char * tmpBuffer = pipeBuffer;
  
    //    char * processName;
    len = 0;
    numberofProcesses = 0;
    processLifeSpan = 0;
    PID = 0;

    len = strchr(pipeBuffer,'#') - pipeBuffer;
    processName = malloc(len+1);
    pointerToProcessName = processName;
    strncpy(processName, pipeBuffer, len);
    processName[len] = '\0'; 

    processName[strlen(processName) - 1] = '\0';

  
    pipeBuffer = strchr(pipeBuffer,'#');
    numberofProcesses = atoi(pipeBuffer+1);
    processLifeSpan = atoi(strchr(pipeBuffer,'!')+1);
    debugPrint("server: we have  %d different procs to monitor\n", numberofProcesses);

    if (processLifeSpan == 99) {
      // 99 is the code indicating that the 
      // server received a sighup
      // so flush our linked list record
      // of processes

      free(pointerToPipeBuffer);
      free(pointerToProcessName);
      pipeBuffer = NULL;
      processName = NULL;




      if (processList) {
	freeList(processList); // free it in case it is still set
	processList = NULL;
      }



      pipeBuffer = malloc(63);
      pointerToPipeBuffer = pipeBuffer;

      //      debugPrint("writing server\n");
      //      writeToServer(a); // we likely dont need to write test again cuz 
                                // our initial message should still be there
      readFromServer(pipeBuffer);
      debugPrint("got %s\n", pipeBuffer);
    
      if (pipeBuffer[0] == '!') {
	keepLooping = 0;
	debugPrint("got exit signal!!!!!\n");
	continue;
      }

      //    char * tmpBuffer = pipeBuffer;
  
      //    char * processName;
      len = 0;
      numberofProcesses = 0;
      processLifeSpan = 0;
      PID = 0;

      len = strchr(pipeBuffer,'#') - pipeBuffer;
      processName = malloc(len+1);
      pointerToProcessName = processName;
      strncpy(processName, pipeBuffer, len);
      processName[len] = '\0'; 

      processName[strlen(processName) - 1] = '\0';

  
      pipeBuffer = strchr(pipeBuffer,'#');
      numberofProcesses = atoi(pipeBuffer+1);
      processLifeSpan = atoi(strchr(pipeBuffer,'!')+1);
      debugPrint("server: we have  %d different procs to monitor\n", numberofProcesses);



    }

    free(pointerToPipeBuffer);
    free(pointerToProcessName);
    pipeBuffer = NULL;
    processName = NULL;
    //    free(processName);
    while(numberofProcesses > 0) {
      //      char * processName = NULL;
      len = 0;
      processLifeSpan = 0;
      PID = 0;
      numberofProcesses--;

      pipeBuffer = malloc(63);
      pointerToPipeBuffer = pipeBuffer;
      readFromServer(pipeBuffer);
      

      len = strchr(pipeBuffer,'#') - pipeBuffer;
      processName = malloc(len+1);
      pointerToProcessName = processName;
      //processName = (char *)realloc(processName, len+1);
      strncpy(processName, pipeBuffer, len);
      processName[len] = '\0'; 

      processName[strlen(processName) - 1] = '\0';

  
      pipeBuffer = strchr(pipeBuffer,'#');
      PID = atoi(pipeBuffer+1);
      processLifeSpan = atoi(strchr(pipeBuffer,'!')+1);


      char *newP = trimwhitespace(processName);

      debugPrint("for PID = %d\n", PID);
      debugPrint("name: %s \n", newP);
      debugPrint("life span: %d \n", processLifeSpan);
      debugPrint("pid: %d \n", PID);
      //      close (s);
      debugPrint("Process %d got %s\n", getpid (),
	       pipeBuffer);

      if (processList) { 
	if ( searchNodes(processList, newP, processLifeSpan) == -1) {
	  debugPrint("%d adding node \n", getpid());
	  addNode(processList, newP, processLifeSpan);	  
	}
      }
      else {
	debugPrint("creating process list... %d processListing node \n", getpid());
	processList = init(newP, processLifeSpan);
      }

      debugPrint("finished setting/looking thro processList\n");
      free(pointerToPipeBuffer);
      free(pointerToProcessName);
      pipeBuffer = NULL;
      processName = NULL;
    }


    sleep (5);
    updateKillCount();
    debugPrint("********************\n");

    debugPrint("number of freechildren is %d: \n", freeChildren);
    debugPrint("number of messages from children is %d: \n", messagesFromChildren);
    struct node * cursorNode;
    int i = 0;
    debugPrint("processes we monitored are: \n");
    cursorNode = monitoredPIDs;
    if (monitoredPIDs) {
      for (i = 0; i < getSize(monitoredPIDs); i++) {
	debugPrint("value: %s, key: %d\n", cursorNode->value, cursorNode->key);
	cursorNode = cursorNode->next;
      }
      
    }


    debugPrint("process we got are: \n");

    cursorNode = processList;

    for (i = 0; i < getSize(processList); i++) {
      debugPrint("for %s, %d \n",cursorNode->value, cursorNode->key);

      if (getNumberOfPIDsForProcess(cursorNode->value) > 0 || firstTime == 1) {
	debugPrint("calling monitorProcess \n");
	monitorProcess(cursorNode->value, cursorNode->key);   	
      }
      else {
	debugPrint("NOT calling monitorProcess \n");
	debugPrint("No %s proc found.\n", cursorNode->value);
      }

      cursorNode = cursorNode->next;
    }
    firstTime = 0;
    debugPrint("done listing processes in processList\n");


      
      
  }
  
  free(pipeBuffer);
  //  free(processName);
  
  freeList(processList);
  freeList(monitoredPIDs);
  freeList(childPIDs);
  return 0;
}
Exemple #5
0
void updateKillCount() {
  //close(killCountPipe[1]);
  char * pipeBuffer= malloc(63);
  debugPrint("updating kill...\n");

  if (read(killCountPipe[0], pipeBuffer, 63) <= 0) {
    debugPrint("found nothing on killCount pipe, returning\n");
    free(pipeBuffer);
    return;
  }

  char * tmpBuffer = pipeBuffer;
  
  char * inputBuffer = malloc(150);
  char * processName;
  int len = 0;
  int PID = 0;
  int processLifeSpan = 0;


  len = strchr(pipeBuffer,'#') - pipeBuffer;
  processName = malloc(len+1);
  strncpy(processName, pipeBuffer, len);
  processName[len] = '\0'; 

  processName[strlen(processName) - 1] = '\0';

  
  pipeBuffer = strchr(pipeBuffer,'#');
  PID = atoi(pipeBuffer+1);
  processLifeSpan = atoi(strchr(pipeBuffer,'!')+1);

  char *newP = trimwhitespace(processName);
  debugPrint("results from updateKillCount pipe read\n");
  debugPrint("name: %s|| \n", newP);
  debugPrint("life span: %d \n", processLifeSpan);
  debugPrint("pid: %d \n", PID);


  if (searchNodes(monitoredPIDs, newP, PID) != -1) {
    debugPrint("NOT REALLYremoving pid that was killed.\n");
    //removeNode(monitoredPIDs, newP, PID);    
  }

  totalProcsKilled++;
  
  debugPrint("PID (%d) (%s) killed after exceeding %d seconds. \n", PID, processName, processLifeSpan);
  snprintf(inputBuffer, 150, "PID (%d) (%s) killed after exceeding %d seconds ", PID, processName, processLifeSpan);
  writeToLogs("Action", inputBuffer);


  debugPrint("sending message to update total kill count in server\n");
  char * killCountMessage = malloc(200);
  snprintf(killCountMessage, 200, "k%-199d", 1);
  writeToServer(killCountMessage);
  debugPrint("sent kill count update!\n");

  free(tmpBuffer);
  free(killCountMessage);
  free(inputBuffer);
  free(processName);  
  freeChildren = freeChildren + 1;  
}
Exemple #6
0
void monitorProcess(char * processName, int processLifeSpan) { 
  // takes processName and lifeSpan
  // converts processName to PIDs
  // and monitors each PID by either
  // spawning a child for that process or
  // or reusing a "unbusy" child depending
  // on the global freeChildren variable.
  

  debugPrint("startin monitor Process for %s \n", processName);
  char * message;
  int * PIDs;
  int counter = 0;
  int pid = 0;
  int parentPID = getpid();
  
  int numberOfPIDs = 0;
  // check to make sure process "processName" is still active
  if ((numberOfPIDs = getNumberOfPIDsForProcess(processName))  < 1) {
    debugPrint("no processes %s found \n", processName);
    message = malloc(200);

    snprintf(message, 190, "No '%s' processes found ", processName);
    writeToLogs("Info", message);
      
    free(message);
    return;
  }
      
  debugPrint("we are parent: %d \n", getpid());
  debugPrint("for %d %s we have  the number of PIDs is equal to %d \n", getpid(), processName, numberOfPIDs);
  PIDs = malloc(numberOfPIDs + 100);
  message = malloc(200);

  getPIDs(processName, PIDs, numberOfPIDs);

  // print out messages to logs regarding which
  // processes are being monitored


  for (counter = 0; counter < numberOfPIDs; counter = counter + 1) {

    if (monitoredPIDs != NULL && searchNodes(monitoredPIDs, processName, PIDs[counter]) != -1) {
      // only call monitor process if
      // process has more than 0 pids active
      // and it is not in monitoredPIDs linked list
      // ie it is not being monitored already
      debugPrint("process %s with pid %d is already being monitored \n", processName, PIDs[counter]);
      continue;
    }

    snprintf(message, 200, "Initializing monitoring of process '%s' (PID %d)", processName, PIDs[counter]);
    writeToLogs("Info", message);

    debugPrint("adding pid %d to list \n", PIDs[counter]);
    if (monitoredPIDs) {
      debugPrint("ADDDING  pid %d to list! \n", PIDs[counter]);
      addNode(monitoredPIDs, processName, PIDs[counter]);
    }
    else {
      debugPrint("initialzing pid %d to list!\n", PIDs[counter]);
      monitoredPIDs = init(processName, PIDs[counter]);
    }
    if (freeChildren > 0) {
      // write to pipe newProcessToChild
      debugPrint("freeChildren is %d, preparing to reuse child for %s pid %d\n", freeChildren, processName, PIDs[counter]);
      //	  debugPrint("from parent: printing before writing to pipe \n");
      /* write message start */
      
      // close(newProcessToChild[0]);
      char tmpStr[63];
      snprintf(tmpStr, 63, "%-20s#%20d!%20d", processName, PIDs[counter], processLifeSpan);
      write(newProcessToChild[1], tmpStr, 63);


      debugPrint("using children\n");
      freeChildren = freeChildren - 1;
    }
	
    else {
      pid = fork();
      if (pid == 0) {
      
	// child portion
	debugPrint("Spawned A child %d for process: %s, pid %d \n",getpid(),  processName, PIDs[counter]);
	dispatch(parentPID, processName, PIDs[counter], processLifeSpan);
	    
	while(keepLooping == 1) {
	  debugPrint("%d child : starting dispatch \n", getpid());
	  dispatch(parentPID, processName, -1, processLifeSpan);
	}
	
	
	free(PIDs);
	free(message);
	debugPrint("child dying....\n");
	exit(0);
	
      }
      else if (pid < 0) {
	fprintf(stderr,"error in forking. \n");
	exit(0);
      }
      else {
	numberOfChildren++;
	if (childPIDs){
	  addNode(childPIDs, " ", pid);
	}
	else {
	  childPIDs = init(" ", pid);
	}

	//printf("parent process pid=%d \n", getpid());
      }
	  
    } // spawning child end bracket

  } // for loop end bracket
  
  free(PIDs);
  free(message);
}