示例#1
0
void NonNativeIrcEngine::relayReadyRead() {
	QString bulk;
	QString line;
	while (sock->canReadLine()) {
		/* On ajoute à la masse que si non PING/PONG :) */
		if (returnLine(line)) bulk += line;
	}
		emit lineReceived(bulk);
}
示例#2
0
文件: main.c 项目: rootulp/school
int main(int argc, char* argv[]) {
  int pipe_1[2];
  int pipe_2[2];
  int rc1;
  int rc2;
  int pid;
  int pid2;
  int status;
  char read_buffer_1[150];
  char read_buffer_2[150];
  char *line;
  FILE *fp;
  FILE *fpout;

  // Check for errors
  rc1 = pipe(pipe_1);
  if (rc1 == -1) {
    perror("Error: pipe_1");
    exit(1);
  }

  rc2 = pipe(pipe_2);
  if (rc2 == -1) {
    perror("Error: pipe_2");
    exit(1);
  }

  fp = fopen("mobydick.txt", "r");
  if (fp == NULL) {
    perror("Error: mobydick.txt");
    return(-1);
  }

  fpout = fopen("mobydicknew.txt", "wb");
  if (fpout == NULL) {
    perror("Error: mobydicknew.txt");
    return(-1);
  }

  line = returnLine(fp);

  while(line != NULL) {
    pid = fork();
    if (pid != 0) {
      /* printf("Process 1\n"); */
      /* printf("Reading from input file: '%s'\n", line); */
      /* printf("Writing to pipe 1: '%s'\n", line); */
      rc1 = write(pipe_1[1], line, strlen(line) + 1);
      if (rc1 == -1) {
        perror("Error: writing to pipe 1");
        close(pipe_1[1]);
        exit(1);
      }
      waitpid(pid, &status, 0);
      exit(1);
    } else {
      /* printf("Process 2\n"); */
      rc1 = read(pipe_1[0], read_buffer_1, sizeof(read_buffer_1));
      /* printf("Reading from pipe 1: '%s'\n", read_buffer_1); */

      for (int i = 0; i < strlen(read_buffer_1); i++) {
        int test1 = islower(read_buffer_1[i]);
        int test2 = isupper(read_buffer_1[i]);
        if ((test1 != 0) && (test2 == 0)) {
          read_buffer_1[i] = toupper(read_buffer_1[i]);
        } else if ((test1 == 0) && (test2 != 0)) {
          read_buffer_1[i] = tolower(read_buffer_1[i]);
        } else {
          read_buffer_1[i] = read_buffer_1[i];
        }
      }

      rc2 = write(pipe_2[1], read_buffer_1, sizeof(read_buffer_1));
      /* printf("Writing to pipe 2: '%s'\n", read_buffer_1); */

      pid2 = fork();
      if (pid2 != 0) {
        waitpid(pid2, &status, 0);
      } else {
        /* printf("Process 3\n"); */
        rc2 = read(pipe_2[0], read_buffer_2, sizeof(read_buffer_2));
        /* printf("Reading from pipe 2: '%s'\n", read_buffer_2); */
        /* printf("Writing to output file: '%s'\n\n", read_buffer_2); */

        strcat(read_buffer_2, "\n");
        fputs(read_buffer_2, fpout);
        exit(0);
      }
    }
    line = returnLine(fp);
  }

  return 0;
}
std::map<std::string,int> scottgs::bookCounter(const scottgs::book_type& book)
{
	// =========================
	// Master (thread 0) 
	// =========================
	int threadCount;
	MPI_Comm_size(MPI_COMM_WORLD, &threadCount);
	std::cout << "scottgs::bookCounter, " << (threadCount-1) << " workers are available to process " << book.size() << " verses from book" << std::endl;
	
	
	std::map<std::string,int> bookCounterResult;
	
	
	scottgs::book_type::const_iterator verse = book.begin();
	
	// Start with 1, because the master is =0
	for (int rank = 1; rank < threadCount && verse!=book.end(); ++rank, ++verse) 
	{
		// work tag / index
		int index = verse->first;
		
		const std::string line(verse->second);
		const size_t length = line.size();
		char msg[scottgs::LINE_MESSAGE_SIZE];
		line.copy(msg,length);
		msg[length] = '\0';

		MPI_Send(msg,           /* message buffer */
		     scottgs::LINE_MESSAGE_SIZE,            /* buffer size */
		     MPI_CHAR,          /* data item is an integer */
		     rank,              /* destination process rank */
		     index,  		/* user chosen message tag */
		     MPI_COMM_WORLD);   /* default communicator */
  	
  		
	}
	
	
	// Loop through verses until there is 
	// no more work to be done
	for ( ;verse!=book.end(); ++verse) 
	{

		// Receive results from a worker
	     	char resultMsg[scottgs::RESULT_MESSAGE_SIZE];
  		MPI_Status status;

		// Receive a message from the worker
		MPI_Recv(resultMsg, 			/* message buffer */
			scottgs::RESULT_MESSAGE_SIZE, 	/* buffer size */
			MPI_CHAR, 		/* data item is an integer */
			MPI_ANY_SOURCE,  	/* Recieve from thread */
			MPI_ANY_TAG,		/* tag */
		     	MPI_COMM_WORLD, 	/* default communicator */
		     	&status);
		     	
		const int incomingIndex = status.MPI_TAG;
		const int sourceCaught = status.MPI_SOURCE;
		// Convert the message into a string for parse work
		std::string returnLine(resultMsg);
#if GJS_DEBUG_PRINT			
		std::cout << "scottgs::bookCounter, " << (threadCount-1) << ", recieved (" << returnLine 
			<< "), status = " << incomingIndex << ", from " << sourceCaught << std::endl;
#endif		  

		// Merge the results from this thread to the global results
		scottgs::mergeBookCounts(bookCounterResult, returnLine);


		// work tag / index
		int outgoingIndex = verse->first;
		
		const std::string line(verse->second);
		const size_t length = line.size();
		char msg[scottgs::LINE_MESSAGE_SIZE];
		line.copy(msg,length);
		msg[length] = '\0';

		MPI_Send(msg,		/* message buffer */
		     scottgs::LINE_MESSAGE_SIZE,            /* buffer size */
		     MPI_CHAR,		/* data item is an integer */
		     sourceCaught,	/* destination process rank */
		     outgoingIndex,	/* user chosen message tag */
		     MPI_COMM_WORLD);	/* default communicator */
  	
	}
	
	
	
	// There's no more work to be done, so receive all the outstanding
	// results from the workers
	for (int rank = 1; rank < threadCount; ++rank) 
	{
	     		
	     	char resultMsg[scottgs::RESULT_MESSAGE_SIZE];
  		MPI_Status status;

		// Receive a message from the worker
		MPI_Recv(resultMsg, 			/* message buffer */
			scottgs::RESULT_MESSAGE_SIZE, 	/* buffer size */
			MPI_CHAR, 		/* data item is an integer */
			rank,  			/* Receive from master */
			MPI_ANY_TAG,		
		     	MPI_COMM_WORLD, 	/* default communicator */
		     	&status);
		     	
		const int index = status.MPI_TAG;
		// Convert the message into a string for parse work
		std::string line(resultMsg);
#if GJS_DEBUG_PRINT			
		std::cout << "scottgs::bookCounter, " << (threadCount-1) << ", recieved (" << line << "), status = " << index << std::endl;
#endif		  

		// Merge the results from this thread to the global results
		scottgs::mergeBookCounts(bookCounterResult, line);
	}


	// Tell all the workers to exit by sending an empty message with the
	// TERMINATE tag
	for (int rank = 1; rank < threadCount; ++rank) {
		MPI_Send(0, 0, MPI_INT, rank, scottgs::TERMINATE, MPI_COMM_WORLD);
	}
	
	std::cout << "scottgs::bookCounter, completed processing book" << std::endl;


	return bookCounterResult;
}