Re: Two threads reading from same file?

Tech-Archive recommends: Speed Up your PC by fixing your registry




"noleander" <noleander@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:680A58CB-46FD-4DDA-9703-0F56E7762CBC@xxxxxxxxxxxxxxxx

> You are correct: To test that the threads were reading the
> proper part of the disk file, I filled the disk file with 4-byte
> integers 0,1,2,3,.... When reading from disk index 999
> (at byte 4*999) the integer read should be 999.

Ok, then the verification logic shown in the code below
should be sufficient to prove it works fine. :-)

------------------ start: cut and paste ---------
// File: TestShareFile.cpp
//
// complile: CL testsharefile.cpp /W3 /GX /MD /D "_AFXDLL"
//
// usage:
//
// To create the test file, type:
//
// testsharefile /c
//
// To test the file reading with 20 multiple threads
//
// testsharefile


#include "stdafx.h"
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

#define NUM_THREADS 20

#define TEST_FILE_NAME "testsharefile.dat"
#define TOTAL_RECORDS 10000

void WINAPI PrintHello(DWORD)
{
DWORD tid = GetCurrentThreadId();
FILE* fv = fopen(TEST_FILE_NAME,"rb");
if (!fv) {
printf("%04d: Error %d fopen()\n",tid,GetLastError());
return;
}

int loop = 1000;
int amt = 10000;
int size = sizeof(int); // record size

int *pBuffer = (int *)malloc(amt*size);

printf("%04d: reading\n",tid);


for ( int i=0; i < loop; i ++ ) {

//-------------------------------------------------------
// Dance Baby! Dance!
//-------------------------------------------------------

int index = (rand() % loop)*size;

if (fseek ( fv, index, SEEK_SET )) {
printf("%04d: Error %d fseek()\n",tid,GetLastError());
break;
}

//-------------------------------------------------------
// read records (vector of integers)
//-------------------------------------------------------

int bpos = ftell(fv); // verification base index

int r = fread (pBuffer, size, amt, fv);
if (r != amt) {
// Remove this to increase verification
//printf("%04d: error fread() r = %d\n",tid,r);
//break;
}

//-------------------------------------------------------
// Verify buffer. Vector of integers correspond to the
// the file index.
//-------------------------------------------------------

for (int n=0; n < r; n++) {
int expected = (bpos+n*size)/size;
if (expected != pBuffer[n]) {
// Logic error!
printf("! %4d: bpos: %6d n: %4d buf[n]: %4d exp: %5d\n",
tid,bpos,n,pBuffer[n],expected);
break;
}
}

}
fclose(fv);
free(pBuffer);
printf("%04d: finish\n",tid);
return;
}

void CreateTestFile()
{
printf("- creating test file %s\n",TEST_FILE_NAME);
FILE* fv = fopen(TEST_FILE_NAME,"wb");
if (!fv) {
printf("Error %d CreateTestFile() fopen() \n",GetLastError());
return;
}
for (int i = 0; i < TOTAL_RECORDS; i++) {
if (!fwrite(&i,sizeof(i),1,fv)) {
printf("Error %d fwrite\n",GetLastError());
break;
}
}
fclose(fv);
}

int _tmain(int argc, _TCHAR* argv[])
{

if (argc > 1 && !stricmp(argv[1],"/c")) {
CreateTestFile();
return 0;
}

HANDLE hThreads[NUM_THREADS];
DWORD tid;
int t;

printf("* Starting threads\n");
for(t=0;t < NUM_THREADS;t++){
printf("- Creating thread %d\n", t);
hThreads[t] = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE) PrintHello,
0,
CREATE_SUSPENDED,
&tid);
}
printf("* Resuming threads\n");
for(t=0;t < NUM_THREADS;t++) ResumeThread(hThreads[t]);

while (WaitForMultipleObjects(NUM_THREADS,
hThreads, TRUE, 100) == WAIT_TIMEOUT) {
if (kbhit() && getch() == 27) {
break;
}
}
printf("* Done\n");
return 0;
}
------------------ end: cut and paste ---------

--
Hector Santos, Santronics Software, Inc.
http://www.santronics.com






.



Relevant Pages

  • Re: Two threads reading from same file?
    ... proper part of the disk file, I filled the disk file with 4-byte ... When reading from disk index 999 ... int loop = 1000; ... void CreateTestFile() ...
    (microsoft.public.win32.programmer.kernel)
  • Re: fread()
    ... > int i,istat,iaccum,intype; ... reading binary data from standard input? ... Use sizeof (float), or even better, sizeof *floatarray, instead. ... FLOAT, and set istat accordingly? ...
    (comp.lang.c)
  • Re: Opening a PPM file Image
    ... Just create a CFile object and open it in binary mode for reading, ... int height; ... int rowsize = width; ...
    (microsoft.public.vc.mfc)
  • Re: frustrated by fscanf and sscanf
    ... no threads talking about reading a series of numbers. ... The input files, somehow structured, is exemplified below: ... int SIZE; ... Reading 2 12X12 arrays of double. ...
    (comp.lang.c)
  • Re: Two threads reading from same file?
    ... Hector: Thank-you very much for taking the time to create an example! ... Unfortunately, when I ran your example, it also failed to read the disk file ... int buffer; ... > Other than that, if you did have a problem with independent streams, then I ...
    (microsoft.public.win32.programmer.kernel)