Вы находитесь на странице: 1из 8

UNIVERSIDADE ESTADUAL DO SUDOESTE DA BAHIA CURSO DE CINCIA DA COMPUTAO RAISSA ARRUDA DE ARAJO VINCIUS BRITO CARDOSO

Programao Concorrente

VITRIA DA CONQUISTA BA 2013

RAISSA ARRUDA DE ARAJO VINCIUS BRITO CARDOSO

Programao Concorrente

Atividade apresentada a Universidade Estadual do Sudoeste da Bahia, Curso de Cincia da Computao, como avaliao da disciplina Programao Concorrente no perodo letivo 2013.1.

VITRIA DA CONQUISTA BA 2013

Questo 1) Elabore um programa java p/ obter o nmero total de linhas de N arquivos texto. import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; /** * Classe LinhasSemThread: classe que importar arquivos e contar a quantidade de * linhas usando * os respectivos mtodos: main e contarlinhas * @author Raissa Arruda de Arajo e Vinicius Brito Cardoso */ public class LinhasSemThread { /** * Mtodo principal da classe: * @param args */ public static void main(String[]args){ String dir = "C:\\Users\\Raissa\\Documents\\NetBeansProjects" + "\\TrabalhoConcorrente\\arquivos"; File diretorio = new File(dir); File[] listaArquivos= diretorio.listFiles(); int i= listaArquivos.length ; int linhasTotal=0; for(int j=0; j<listaArquivos.length; j++){ File arquivos = listaArquivos[j]; linhasTotal= linhasTotal + contarlinhas(arquivos); } System.out.println("O total de linhas nos "+listaArquivos.length+ " arquivos : "+linhasTotal); } /** * Mtodo contarLinhas * @param f do tipo arquivo * @return inteiro com o total de linhas de um arquivo */

public static int contarlinhas(File f) { int contador=0; try {

FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr);

//enquanto houver mais linhas while (br.ready()) { //l a proxima linha String linha = br.readLine(); //faz algo com a linha contador++; } br.close(); fr.close(); return contador; /** * @throws IOException: excees de entrada e saida * exemplo: arquivo no existe. */ } catch (IOException ex) { ex.printStackTrace(); } System.out.println(contador); return contador; } } 2) Elabore um programa Java multhreading utilizando semaforos p/ obter o nmero total de linhas de N arquivos de texto. Comparar o tempo de execuo de 1 e 2 e gerar um grfico.

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.concurrent.Semaphore; import java.util.logging.Level; import java.util.logging.Logger; /** * Classe: Leitor Linhas : Classe estendida de Thread que possui metodos para * ler e contar linhas de vrios arquivos * @author Raissa Arruda de Arajo e Vinicius Brito Cardoso

*/ public class LeitorLinhas extends Thread { public FileReader arquivoParaLer; public static int numLinhaGeral; public Semaphore semaforo = new Semaphore(1); String nome; /** * Construtor da classe * @param entrada * @param nome */ public LeitorLinhas(FileReader entrada, String nome) { arquivoParaLer = entrada; this.nome = nome; } /** * Mtodo gravarLinhas : adiciona uma linha ao contador numLinhaGeral * @param linhas */ public void gravarLinhas(int linhas) { numLinhaGeral += linhas; System.out.println("A " + nome + " leu " + linhas + " linhas"); } /** * Mtodo run(): sobrepe o mtodo, ler um arquivo para contar linhas */ @Override public void run() { String str; int numLinhas = 0; BufferedReader linhasDoArquivo = new BufferedReader(arquivoParaLer); try { while (linhasDoArquivo.ready()) { str = linhasDoArquivo.readLine(); numLinhas++; } arquivoParaLer.close(); } catch (IOException ex) { Logger.getLogger(LeitorLinhas.class.getName()).log(Level.SEVERE, null, ex); } try { semaforo.acquire(); } catch (InterruptedException ex) {

Logger.getLogger(LeitorLinhas.class.getName()).log(Level.SEVERE, null, ex); } gravarLinhas(numLinhas); semaforo.release(); }

public class Principal { /** * Mtodo principal * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { int numeroDeArquivos = 0; int saida = 0; String dir = "C:\\Users\\Raissa\\Documents\\NetBeansProjects" + "\\TrabalhoConcorrente\\arquivos"; File pasta = new File(dir); File[] listaArquivos = pasta.listFiles(); int quantArquivos = listaArquivos.length; /** * Instacia um objeto da classe LeitorLinhas */ LeitorLinhas[] leitores = new LeitorLinhas[quantArquivos]; for (int i = 0; i < listaArquivos.length; i++) { FileReader f = new FileReader(listaArquivos[i]); String nomeThread = "Thread " + i; leitores[i] = new LeitorLinhas(f, nomeThread); leitores[i].start(); } for (int i = 0; i < leitores.length; i++) { try { leitores[i].join(); } catch (InterruptedException ex) { Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println("O total de linhas nos " + listaArquivos.length + " arquivos : " + LeitorLinhas.numLinhaGeral);

Vrias Threads 200 180

Uma Thread

TEMPO(MILISSEGUNDOS)

160 140 120 100 80 60 40 20 0


2 5 10 20 40

QUANTIDADE DE ARQUIVOS LIDOS

3) Elaborar um programa C utilizando a biblioteca MPI p/ calcular a transposta de uma matriz NxN #include "mpi.h" #include <stdio.h>

int main (int argc, char *argv[]){ int tam =4; int rank, size; int matrizOrigem[tam][tam], matrizResultado[tam][tam] = {0}; int linha,coluna,k;

MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Comm_size(MPI_COMM_WORLD,&size);

/* Inicializando e imprimindo a matriz A */ printf("\n\nMatriz A:\n"); for(linha = 0; linha < tam-1; linha++){ for(coluna = 0; coluna < tam-1; coluna++){ printf("Hello from processor %d \n", rank); matrizOrigem[linha][coluna] = rand()%9; printf("%d ", matrizOrigem[linha][coluna]); } printf("\n"); }

/* Responsvel pelo calculo matriz transposta */ for(linha = 0; linha < tam-1; linha++){ for(coluna = 0; coluna < tam-1; coluna++){ matrizResultado[linha][coluna]=matrizOrigem[coluna][linha]; } } /* Imprimindo a matrizResultado */ printf("\nResultado de AxB:\n"); for(linha= 0; linha < tam-1; linha++){ for(coluna = 0; coluna < tam-1; coluna++) printf("Hello from processor %d n", rank); printf("%d ",matrizResultado[linha][coluna]); printf("\n"); } printf("\n\n");

MPI_Finalize(); /* Finaliza MPI */ return 0; /* Retorna */ }

Вам также может понравиться