Click to See Complete Forum and Search --> : How to make threads!!! URGENT


May 25th, 1999, 11:31 AM
Hi,
i did a programm in c and i want to run it many times that means to run it for many threads, how can i do it.

here is my programm:
#include <stdio.h>
#include <direct.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <time.h>

#define ArrayNum 128
#define ArraySize 65536
#define BufferSize 64*1024

int main ()
{
short int *Hist[ArrayNum]; /* Array for storing the counts */
int pulseheight;
int i;
int j;
int sum=0; /* total number of counts */
int fd; /* file descriptor */
FILE *fp; /* input file pointer */
int time1, time2, etime;
int *ip, *ia, *ib;
int index;
float temp1, temp2;
short int *ic, *id;
float A=3.1415, B=4.56778;

char buffer[_MAX_PATH]; /* Get the current working directory: */
if( _getcwd( buffer, _MAX_PATH ) == NULL )
perror( "_getcwd error" );
else
printf( "%s\n", buffer );

/*if ((fp = fopen ("data.dat" , "r")) == NULL) {
fprintf (stderr, "File Access Denied!\n");
exit (1);
} */


for (i=0; i<ArrayNum; i++) { /* initialize the array */
Hist[i] = (short int *) malloc (sizeof(short int)*ArraySize);
for (j=0; j< ArraySize ; j++) {
*(Hist[i]+j) = 0;
}
}

ip = (int*) malloc (sizeof(int) * BufferSize);
ia = (int *) malloc (sizeof(int) * BufferSize);
ib = (int *) malloc (sizeof(int) * BufferSize);
ic = (short int *) malloc (sizeof(short int) * BufferSize);
id = (short int *) malloc (sizeof(short int) * BufferSize);

for (i=0; i<BufferSize; i++) {
*(ia+i) = i;
*(ip+i) = i;
*(ib+i) = BufferSize-i;
*(ic+i) = 100;
*(id+i) = 121;
}

time1 = time(NULL);

for (j=0; j<1024 ; j++) {
for (i=0 ; i<BufferSize ; i++) {
temp1 = (*(ia+i)-*(ic+i))*A;
temp2 = (*(ib+i)+*(id+i))*B;
index = ((temp1-temp2)*64)/(temp1+temp2)+64;
pulseheight = *(ip+i);
(*(Hist[index]+pulseheight)) ++;
}
}


/* accumulating the counts */


// ... perform time-consuming task ...

/* while (fscanf(fp,"%d",&pulseheight) != EOF) {
if (pulseheight >=0 && pulseheight < BinSize)
Hist[pulseheight]++;
} */

time2 = time(NULL);

etime = time2 - time1;
printf ("%d %d \n", time1, time2);
printf ("Elapsed Time = %d\n", etime);

/*fclose (fp);*/

if ((fd = _open ("hist.out", _O_WRONLY | _O_CREAT | _O_TRUNC, _S_IWRITE)) == -1) {
fprintf (stderr, "Open file failed!\n");
exit (1);
}

if ((fp = fdopen (fd , "w+")) == NULL) {
fprintf (stderr, "Open file failed!\n");
exit (1);
}

for (i=0; i<ArrayNum; i++) {
for (j=0; j<ArraySize; j++) {
/*printf ("%d %d\n", i*ArraySize+j, *(Hist[i]+j));*/
sum += *(Hist[i]+j);
}
}

printf ("Total number of counts = %d\n", sum);

fclose (fp);

return 0;
}