Write a program to count words from a text file. The text file is partitioned into N segments. Each segment is processed by a separate thread that outputs the intermediate count for its segment. The main process waits until all the threads complete; then it computes the consolidated count data based on the individual threads’ output.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

Write a program to count words from a text file. The text file is partitioned into N segments. Each segment is processed by a separate thread that outputs the intermediate count for its segment. The main process waits until all the threads complete; then it computes the consolidated count data based on the individual threads’ output.

 

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

//THE PAGE FREQUENCY PER 1000 MEMORY REFERENCE DATA IS STORED IN A SEPARATE FILE CALLED DATA.TXT.
//DATA POINTS CAN BE USED TO PLOT THE GRAPH

void AgeIncrease (int frames);
int OldestIndex (int frames);

struct PageTable {
int frame_number;
bool valid;
}*PageTable;

struct FrameTable {
int page_number;
int age;
}*FrameTable;

int main (void){
printf("Number of pages:\n");
int pages;
scanf("%d", &pages);

printf("Number of frames:\n");
int frames;
scanf("%d", &frames);

PageTable=malloc(sizeof(PageTable)*pages);
FrameTable=malloc(sizeof(FrameTable)*frames);

int i;
for (i=0;i<pages; i++){
PageTable[i].valid=false;
}

for (i=0;i<frames;i++){
FrameTable[i].age=0;
}

FILE *fp=fopen("file.txt", "r");

int request;
int frame_table_counter=0;
int page_fault_counter=0;
int request_counter=0;
int memory_ref_counter=0;
//add frame size to page faults later
while (!feof(fp)){
fscanf(fp, "%d", &request);
request_counter++;
if (PageTable[request].valid==false){
FrameTable[frame_table_counter].page_number=request;
PageTable[request].valid=true;
PageTable[request].frame_number=frame_table_counter;
frame_table_counter++;
page_fault_counter++;
memory_ref_counter++;
}

if (request_counter==1000){
FILE *fp=fopen("data.txt", "a");
fprintf (fp, "%d\n", memory_ref_counter);
memory_ref_counter=0;
request_counter=0;
fclose(fp);
}
if (frame_table_counter==frames){
break;
}
}


while (!feof(fp)){
fscanf(fp, "%d", &request);
request_counter++;
AgeIncrease(frames);
if (PageTable[request].valid==false){
int oldest_page=OldestIndex(frames);
PageTable[FrameTable[oldest_page].page_number].valid=false;
FrameTable[oldest_page].page_number=request;
FrameTable[oldest_page].age=0;
PageTable[request].frame_number=oldest_page;
PageTable[request].valid=true;
page_fault_counter++;
memory_ref_counter++;
}

if (request_counter==1000){
FILE *fp=fopen("data.txt", "a");
fprintf (fp, "%d\n", memory_ref_counter);
memory_ref_counter=0;
request_counter=0;
fclose(fp);
}
}

printf("Total page faults %d\n", page_fault_counter);

fclose(fp);
free(PageTable);
free(FrameTable);

return 0;
}

void AgeIncrease (int frames){
int i;
for (i=0;i<frames;i++){
FrameTable[i].age++;
}
}

int OldestIndex (int frames){
int oldest=-1;
int i;
int index;
for (i=0;i<frames;i++){
if (oldest<FrameTable[i].age){
oldest=FrameTable[i].age;
index=i;
}
}
return index;
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Race Condition
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education