0% found this document useful (0 votes)
15 views2 pages

Programming C, C++, Thread Test

The document contains C code for a multithreaded program that processes data from a file using two threads: one for even numbers and another for odd numbers. It utilizes mutex locks to manage access to a shared variable, threadCount, and handles file operations to read and write data. There are several syntax errors and formatting issues present in the code that would need to be corrected for successful compilation and execution.

Uploaded by

mrvcarvalho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Programming C, C++, Thread Test

The document contains C code for a multithreaded program that processes data from a file using two threads: one for even numbers and another for odd numbers. It utilizes mutex locks to manage access to a shared variable, threadCount, and handles file operations to read and write data. There are several syntax errors and formatting issues present in the code that would need to be corrected for successful compilation and execution.

Uploaded by

mrvcarvalho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Thread TESTE

#include <stdio.h>
#include <stdlib . h>
#include <pthread . h>

volatile double threadCount = 0.0;

pthreadmutext pilock;

1/Função thread
void processPar (void *arg)
register int iproc = (*((char *) arg) - 'O');
FILE * fpArqivo;
char auxStr[120]='O";
int iNum;

pthreadmutexlock (& pi_lock);


threadCount++;
pthreadmutexunlock (& pilock);

//printf("\nAbertura do arquivo OK no processRead\n');

/*trava acesso a esta parte do código, altera pi, e destrava'/

while (1)

fpArquivo = fopen([Link]",a+");

if (!fpArquivo)
printf("\nErro na abertura do arquivo no procesoRead\n');
pthreadmutexlock (& pilock);
threadCount - -;
pthreadmutexunlock (& pilock);
return (NULL);

while( ! feot (fpArquivo) ) {


fgets (auxStr, 120 , fpArquivo);
//printf ( It [%s \nt auxStr)

iNum = (atoi(auxStr));
if (iNum%2 !=
fprintf (fpArquivo, "%03d-ThreadPar\n" , iNum+l);

fclose (fpArquivo);
if (iNum>lOO) break;

pthreadmtexlock (& pilock);


threadCount - -;
pthreadmutexunlock (& pilock) ;

return (NTJLL) ;

void * processlmpar (void *arg)


register int iproc = (*((char *) arg) - 'O');
FILE * fpArqivo;
char auxStr [l2O =0";
int iNum;

pthreadmutexlock (& pilock);


threadCount++;
pthreadmutexunlock (& pilock);

//printf("\nAbertura do arquivo OK no processRead\n') ;

/*trava acesso a esta parte do código, altera pi, e destrav


., Thread TESTE

while (1)

fpArquivo = fopen ( 'arq . dat , )

if (!foArquivo)
printt("\nErro na abertura do arquivo no processoRead\n");
pthreadmutexlock (& pilock);
threadCount - -;
pthreadmutexunlock (& pilock) ;
. : eturn (NULL);

while t•:Of (fpArquivo) )


taet (auxStr, 120, fpArquivo);
, //printf ( [%sJ \ntt , auxStr)

iNum - (atoi (auxStr));


i (iirr%2
fprintf (fpArquivo, "%03d-Threadlmpar\n" , iNum+1);

fclose(fpArquivo);
if (iL1m>100) break;

pthreadmutexlock (& pilock);


threadCount - -
pthread_mutex_unlock (& pilock);

return (NULL);

int main (int argc , char * argv [1 )


pthread_t tiadO , threadl;
void * retval;

1* Inicializ, a variável mutex*/

pthreadmutexiiit (& pilock , NULL);

//printf ("tLeadCount %d\n" , (int)threadCount);

/* Executa duas threads /


if ( pthread_create (& threadO , NULL , processPar , (void*) tiOlt) 1
pthread_create (& threadl , NULL , processlmpar , (void*) 'i")){

fprintf ( stderr , %s: cannot make thread\n' , argv[OJ);


exit (1

1* Join espera as threads terminarem, o retorno é armazenado em retval*/

//printf (thre3dCount %d\n" , (int) threadCount);

it ( pthread_join ( threadO , & retval) 11


pthread_join ( threadl , & retval) )

fprintt ( stderr , 1 %5: thread join failed\n argv[O]);


exit .1)

//printf (t.:eadCount %d\n , (int)threadCount) ;


return o;

//g++ -v [Link] -lpthread -o 2thread

You might also like