Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help... I am trying to get this program to read from a file using 16 diff

ID: 643409 • Letter: I

Question

I need help... I am trying to get this program to read from a file using 16 different threads, count the words from the threads individually, then combine the thread totals for a total count. I can't use getchar() with a buffer apparently and I'm out of ideas. This must be done in C for the Fedora Linux environment, so MS-DOS headers (conio.h) and thus getch() won't work. Please help!

# include
# include

int size, i, j, wcarr[16], total_words=0;

char buff[3000]; /* declaring variable to hold string read from the file */

char rdchar;

void wordcount(fp,i)

{

rdchar = getchar(buff);

while (rdchar >= NULL)

{

if (rdchar == ' ') /* when ever a space occurs it means end of word - so check if end of word has come */

wcarr[i]++; /* add one to the word count of this particular portion of the file */

rdchar = getchar(buff); /* read each character of the text stored in the buffer after the read statement */

} /* end of loop */

} /* end of function */

void findfilesize(fp,i)

{

if (fp == NULL)
        printf(" File unable to open ");
    else

        printf(" File opened ");

    fseek(fp, 0, 2);    /* file pointer at the end of file */
    size = ftell(fp);   /* take a position of file pointer un size variable */
    fclose(fp);

}

int main() {

FILE *fp;                  /* declaring file pointer   */

fp = fopen("assign4.txt", "r");         /* open the file */
findfilesize();                         /* call the function to find file size */
fp = fopen("assign4.txt", "r");         /* open file again because the previously called function will close the file       */


while (!feof(fp)) /* loop till end of file is reached */
{
for(int i=0; i<16; i++) /* dividing program into 16 chunks */
fscanf (fp , "%s" ,buff ); /* read the contents into the variable buff*/

fork(); /* create a new process */

wordcount(fp,i); /* that will call the function wordcount */

} /* end of while loop */

for (int j=0;j<16;j++) /* loop to calculate the total of the word count performed by each fork */

{total_words = total_words + wcarr[i];}

printf ("The total words in the file were %d ", total_words); /* print the total calculated */

} /* end of main */

Explanation / Answer

#include
#include

void* say_hello(void* data)
{
char *str;
str = (char*)data;
while(1)
{
printf("%s ",str);
usleep(1);
}
}

void* open_file(void* data)
{
char *str;
str = (char*)data;
printf("Opening File ");
FILE* f = fopen(str,"w");
fclose(f);
printf("Closing File ");
}

void* countChar()
{
FILE *p;
char ch;
int w=1;
p=fopen("source","r");
ch=fgetc(p);
while(ch!=EOF)
{
printf("%c",ch);
if(ch==' '||ch==' ')
{
w++;
}
ch=fgetc(p);
}
printf(" Words in a file are=%d",w);
}
fclose(p);
usleep(1);
}

void main()
{
pthread_t t1,t2,t3;
pthread_create(&t1,NULL,open_file,"hello.txt");
pthread_create(&t2,NULL,say_hello,"The CPU is mine now :D");
pthread_create(&t3,NULL,countChar,"");
pthread_join(t1,NULL);
}