SlideShare a Scribd company logo
1 of 11
Download to read offline
this is my code to count the frequency of words in a text file
#define _GNU_SOURCE
#include
#include
#include
#include
/******************************
here is the Structure for storing words
received from each thread
******************************/
struct return_vall{
char wordlist[100][100];
int count[100];
} *arr;
pthread_t *threads;
int lines;
/*****************************
here are Aggregates Frequency Sum of
different threads
*****************************/
int aggregate(char *a)
{
int sum = 0;
int i=0;
while(1)
{
if(i==lines) break;
int count = 0;
while(1)
{
if(arr[i].count[count] == -1)
{
break;
}
if(strcmp(a,arr[i].wordlist[count]) == 0)
{
sum += arr[i].count[count];
arr[i].count[count] = 0;
}
count++;
}
i++;
}
return sum;
}
/**************************
Function thta are executed by each
thread on seperate line.
**************************/
void *word_count(void* num)
{
int *ln = num;
unsigned int line_number = *ln;
line_number++;
//printf("Thread %d ",line_number);
char cmd_p1[9] = "sed -n '0";
char cmd_p2[2];
sprintf(cmd_p2,"%d",line_number);
char cmd_p3[14] = "p' 'file.txt'0";
char command[100];
command[0] = '0';
strcat(command,cmd_p1);
strcat(command,cmd_p2);
strcat(command,cmd_p3);
//usleep(line_number);
char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0";
strcat(command,cmd);
//printf("%s ",command);
//fflush(stdout);
FILE *in;
in= popen(command, "r");
rewind(in);
char buff[50];
int counter = 0;
while(fgets(buff,sizeof(buff),in))
{
char c=' ';
int i = 0;
int cnt = atoi(buff);
arr[line_number-1].count[counter] = cnt;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+6];
i++;
}
int cnnt = 0;
while(c!=' ')
{
c = buff[cnnt];
cnnt++;
}
i=0;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+cnnt];
i++;
}
sprintf(arr[line_number-1].wordlist[counter],"%s",buff);
//printf("%d %s",arr[line_number-1].count[counter],arr[line_number-
1].wordlist[counter]);
counter++;
}
arr[line_number-1].count[counter] = -1;
fclose(in);
return NULL;
}
int main(void)
{
FILE *fp;
fp = fopen("file.txt","r");
char a[200];
lines = 0;
void *status = NULL;
while(fgets(a,sizeof(a),fp) !=NULL)
{
lines++;
}
fclose(fp);
arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines);
threads = malloc(sizeof(pthread_t)*lines);
int i;
for(i=0;i= 0)
{
if(arr[i].count[ctr] > 0)
{
strcpy(final[final_ctr],arr[i].wordlist[ctr]);
final_cnt[final_ctr] = arr[i].count[ctr];
arr[i].count[ctr] = 0;
int sum = aggregate(arr[i].wordlist[ctr]);
final_cnt[final_ctr] += sum;
printf("%d %s",final_cnt[final_ctr],final[final_ctr]);
}
ctr++; final_ctr++;
}
i++;
if(i==lines)
{
break;
}
}
free(arr);
return 0;
}
Solution
this is my code to count the frequency of words in a text file
#define _GNU_SOURCE
#include
#include
#include
#include
/******************************
here is the Structure for storing words
received from each thread
******************************/
struct return_vall{
char wordlist[100][100];
int count[100];
} *arr;
pthread_t *threads;
int lines;
/*****************************
here are Aggregates Frequency Sum of
different threads
*****************************/
int aggregate(char *a)
{
int sum = 0;
int i=0;
while(1)
{
if(i==lines) break;
int count = 0;
while(1)
{
if(arr[i].count[count] == -1)
{
break;
}
if(strcmp(a,arr[i].wordlist[count]) == 0)
{
sum += arr[i].count[count];
arr[i].count[count] = 0;
}
count++;
}
i++;
}
return sum;
}
/**************************
Function thta are executed by each
thread on seperate line.
**************************/
void *word_count(void* num)
{
int *ln = num;
unsigned int line_number = *ln;
line_number++;
//printf("Thread %d ",line_number);
char cmd_p1[9] = "sed -n '0";
char cmd_p2[2];
sprintf(cmd_p2,"%d",line_number);
char cmd_p3[14] = "p' 'file.txt'0";
char command[100];
command[0] = '0';
strcat(command,cmd_p1);
strcat(command,cmd_p2);
strcat(command,cmd_p3);
//usleep(line_number);
char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0";
strcat(command,cmd);
//printf("%s ",command);
//fflush(stdout);
FILE *in;
in= popen(command, "r");
rewind(in);
char buff[50];
int counter = 0;
while(fgets(buff,sizeof(buff),in))
{
char c=' ';
int i = 0;
int cnt = atoi(buff);
arr[line_number-1].count[counter] = cnt;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+6];
i++;
}
int cnnt = 0;
while(c!=' ')
{
c = buff[cnnt];
cnnt++;
}
i=0;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+cnnt];
i++;
}
sprintf(arr[line_number-1].wordlist[counter],"%s",buff);
//printf("%d %s",arr[line_number-1].count[counter],arr[line_number-
1].wordlist[counter]);
counter++;
}
arr[line_number-1].count[counter] = -1;
fclose(in);
return NULL;
}
int main(void)
{
FILE *fp;
fp = fopen("file.txt","r");
char a[200];
lines = 0;
void *status = NULL;
while(fgets(a,sizeof(a),fp) !=NULL)
{
lines++;
}
fclose(fp);
arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines);
threads = malloc(sizeof(pthread_t)*lines);
int i;
for(i=0;i= 0)
{
if(arr[i].count[ctr] > 0)
{
strcpy(final[final_ctr],arr[i].wordlist[ctr]);
final_cnt[final_ctr] = arr[i].count[ctr];
arr[i].count[ctr] = 0;
int sum = aggregate(arr[i].wordlist[ctr]);
final_cnt[final_ctr] += sum;
printf("%d %s",final_cnt[final_ctr],final[final_ctr]);
}
ctr++; final_ctr++;
}
i++;
if(i==lines)
{
break;
}
}
free(arr);
return 0;
}

More Related Content

Similar to this is my code to count the frequency of words in a text file#.pdf

Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxajoy21
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxAbdulrahman890100
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docxShiraPrater50
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
Library Managment System - C++ Program
Library Managment System - C++ ProgramLibrary Managment System - C++ Program
Library Managment System - C++ ProgramMuhammad Danish Badar
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.pptLokeshK66
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfarakalamkah11
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxkrishna50blogging
 
Multithreaded sockets c++11
Multithreaded sockets c++11Multithreaded sockets c++11
Multithreaded sockets c++11Russell Childs
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTEr. Ganesh Ram Suwal
 
Regular Expression (RegExp)
Regular Expression (RegExp)Regular Expression (RegExp)
Regular Expression (RegExp)Davide Dell'Erba
 

Similar to this is my code to count the frequency of words in a text file#.pdf (20)

Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Advanced Shell Scripting
Advanced Shell ScriptingAdvanced Shell Scripting
Advanced Shell Scripting
 
Library Managment System - C++ Program
Library Managment System - C++ ProgramLibrary Managment System - C++ Program
Library Managment System - C++ Program
 
Data type in c
Data type in cData type in c
Data type in c
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptx
 
slides3_077.ppt
slides3_077.pptslides3_077.ppt
slides3_077.ppt
 
Multithreaded sockets c++11
Multithreaded sockets c++11Multithreaded sockets c++11
Multithreaded sockets c++11
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LIST
 
Regular Expression (RegExp)
Regular Expression (RegExp)Regular Expression (RegExp)
Regular Expression (RegExp)
 

More from aquacareser

1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdfaquacareser
 
1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdfaquacareser
 
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdfaquacareser
 
The ice allows the refluxed materials to crystall.pdf
                     The ice allows the refluxed materials to crystall.pdf                     The ice allows the refluxed materials to crystall.pdf
The ice allows the refluxed materials to crystall.pdfaquacareser
 
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdfaquacareser
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquacareser
 
Web sever environmentA Web server is a program that uses HTTP (Hy.pdf
Web sever environmentA Web server is a program that uses HTTP (Hy.pdfWeb sever environmentA Web server is a program that uses HTTP (Hy.pdf
Web sever environmentA Web server is a program that uses HTTP (Hy.pdfaquacareser
 
We ca use HashSet data structure to remove duplicats from an array i.pdf
We ca use HashSet data structure to remove duplicats from an array i.pdfWe ca use HashSet data structure to remove duplicats from an array i.pdf
We ca use HashSet data structure to remove duplicats from an array i.pdfaquacareser
 
this eample is of t-test for comparing two meanshere mean1 = m.pdf
this eample is of t-test for comparing two meanshere mean1 = m.pdfthis eample is of t-test for comparing two meanshere mean1 = m.pdf
this eample is of t-test for comparing two meanshere mean1 = m.pdfaquacareser
 
The middle ear bones in mammals are derived from bones in the dentar.pdf
The middle ear bones in mammals are derived from bones in the dentar.pdfThe middle ear bones in mammals are derived from bones in the dentar.pdf
The middle ear bones in mammals are derived from bones in the dentar.pdfaquacareser
 
There is an old saying “Do not put all eggs in one basket”WhyBe.pdf
There is an old saying “Do not put all eggs in one basket”WhyBe.pdfThere is an old saying “Do not put all eggs in one basket”WhyBe.pdf
There is an old saying “Do not put all eggs in one basket”WhyBe.pdfaquacareser
 
The ER Model is focussed to be a description of real-world entities..pdf
The ER Model is focussed to be a description of real-world entities..pdfThe ER Model is focussed to be a description of real-world entities..pdf
The ER Model is focussed to be a description of real-world entities..pdfaquacareser
 
Please find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdf
Please find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdfPlease find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdf
Please find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdfaquacareser
 
Question 1 answer is A Global trade allows wealthy countries to use .pdf
Question 1 answer is A Global trade allows wealthy countries to use .pdfQuestion 1 answer is A Global trade allows wealthy countries to use .pdf
Question 1 answer is A Global trade allows wealthy countries to use .pdfaquacareser
 
Precipitation hardening, or age hardening, provides one of the most .pdf
Precipitation hardening, or age hardening, provides one of the most .pdfPrecipitation hardening, or age hardening, provides one of the most .pdf
Precipitation hardening, or age hardening, provides one of the most .pdfaquacareser
 
Protists are beneficial to the ecosystem in generating oxygen as the.pdf
Protists are beneficial to the ecosystem in generating oxygen as the.pdfProtists are beneficial to the ecosystem in generating oxygen as the.pdf
Protists are beneficial to the ecosystem in generating oxygen as the.pdfaquacareser
 
Now a normal O2 molecule has 12 electrons in the valence shell so, O.pdf
Now a normal O2 molecule has 12 electrons in the valence shell so, O.pdfNow a normal O2 molecule has 12 electrons in the valence shell so, O.pdf
Now a normal O2 molecule has 12 electrons in the valence shell so, O.pdfaquacareser
 
P1.javaimport java.util.Scanner;keyboard inputting package pub.pdf
P1.javaimport java.util.Scanner;keyboard inputting package pub.pdfP1.javaimport java.util.Scanner;keyboard inputting package pub.pdf
P1.javaimport java.util.Scanner;keyboard inputting package pub.pdfaquacareser
 
operating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfoperating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfaquacareser
 
Network Protocol1) A Protocol is used for the communication betwe.pdf
Network Protocol1) A Protocol is used for the communication betwe.pdfNetwork Protocol1) A Protocol is used for the communication betwe.pdf
Network Protocol1) A Protocol is used for the communication betwe.pdfaquacareser
 

More from aquacareser (20)

1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
 
1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf
 
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
 
The ice allows the refluxed materials to crystall.pdf
                     The ice allows the refluxed materials to crystall.pdf                     The ice allows the refluxed materials to crystall.pdf
The ice allows the refluxed materials to crystall.pdf
 
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
Web sever environmentA Web server is a program that uses HTTP (Hy.pdf
Web sever environmentA Web server is a program that uses HTTP (Hy.pdfWeb sever environmentA Web server is a program that uses HTTP (Hy.pdf
Web sever environmentA Web server is a program that uses HTTP (Hy.pdf
 
We ca use HashSet data structure to remove duplicats from an array i.pdf
We ca use HashSet data structure to remove duplicats from an array i.pdfWe ca use HashSet data structure to remove duplicats from an array i.pdf
We ca use HashSet data structure to remove duplicats from an array i.pdf
 
this eample is of t-test for comparing two meanshere mean1 = m.pdf
this eample is of t-test for comparing two meanshere mean1 = m.pdfthis eample is of t-test for comparing two meanshere mean1 = m.pdf
this eample is of t-test for comparing two meanshere mean1 = m.pdf
 
The middle ear bones in mammals are derived from bones in the dentar.pdf
The middle ear bones in mammals are derived from bones in the dentar.pdfThe middle ear bones in mammals are derived from bones in the dentar.pdf
The middle ear bones in mammals are derived from bones in the dentar.pdf
 
There is an old saying “Do not put all eggs in one basket”WhyBe.pdf
There is an old saying “Do not put all eggs in one basket”WhyBe.pdfThere is an old saying “Do not put all eggs in one basket”WhyBe.pdf
There is an old saying “Do not put all eggs in one basket”WhyBe.pdf
 
The ER Model is focussed to be a description of real-world entities..pdf
The ER Model is focussed to be a description of real-world entities..pdfThe ER Model is focussed to be a description of real-world entities..pdf
The ER Model is focussed to be a description of real-world entities..pdf
 
Please find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdf
Please find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdfPlease find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdf
Please find the answers belowAnswer 16 Option A (IEP). (IEP or i.pdf
 
Question 1 answer is A Global trade allows wealthy countries to use .pdf
Question 1 answer is A Global trade allows wealthy countries to use .pdfQuestion 1 answer is A Global trade allows wealthy countries to use .pdf
Question 1 answer is A Global trade allows wealthy countries to use .pdf
 
Precipitation hardening, or age hardening, provides one of the most .pdf
Precipitation hardening, or age hardening, provides one of the most .pdfPrecipitation hardening, or age hardening, provides one of the most .pdf
Precipitation hardening, or age hardening, provides one of the most .pdf
 
Protists are beneficial to the ecosystem in generating oxygen as the.pdf
Protists are beneficial to the ecosystem in generating oxygen as the.pdfProtists are beneficial to the ecosystem in generating oxygen as the.pdf
Protists are beneficial to the ecosystem in generating oxygen as the.pdf
 
Now a normal O2 molecule has 12 electrons in the valence shell so, O.pdf
Now a normal O2 molecule has 12 electrons in the valence shell so, O.pdfNow a normal O2 molecule has 12 electrons in the valence shell so, O.pdf
Now a normal O2 molecule has 12 electrons in the valence shell so, O.pdf
 
P1.javaimport java.util.Scanner;keyboard inputting package pub.pdf
P1.javaimport java.util.Scanner;keyboard inputting package pub.pdfP1.javaimport java.util.Scanner;keyboard inputting package pub.pdf
P1.javaimport java.util.Scanner;keyboard inputting package pub.pdf
 
operating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdfoperating system linux,ubuntu,Mac#include iostream #include .pdf
operating system linux,ubuntu,Mac#include iostream #include .pdf
 
Network Protocol1) A Protocol is used for the communication betwe.pdf
Network Protocol1) A Protocol is used for the communication betwe.pdfNetwork Protocol1) A Protocol is used for the communication betwe.pdf
Network Protocol1) A Protocol is used for the communication betwe.pdf
 

Recently uploaded

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

this is my code to count the frequency of words in a text file#.pdf

  • 1. this is my code to count the frequency of words in a text file #define _GNU_SOURCE #include #include #include #include /****************************** here is the Structure for storing words received from each thread ******************************/ struct return_vall{ char wordlist[100][100]; int count[100]; } *arr; pthread_t *threads; int lines; /***************************** here are Aggregates Frequency Sum of different threads *****************************/ int aggregate(char *a) { int sum = 0; int i=0; while(1) { if(i==lines) break; int count = 0; while(1) { if(arr[i].count[count] == -1) { break; }
  • 2. if(strcmp(a,arr[i].wordlist[count]) == 0) { sum += arr[i].count[count]; arr[i].count[count] = 0; } count++; } i++; } return sum; } /************************** Function thta are executed by each thread on seperate line. **************************/ void *word_count(void* num) { int *ln = num; unsigned int line_number = *ln; line_number++; //printf("Thread %d ",line_number); char cmd_p1[9] = "sed -n '0"; char cmd_p2[2]; sprintf(cmd_p2,"%d",line_number); char cmd_p3[14] = "p' 'file.txt'0"; char command[100];
  • 3. command[0] = '0'; strcat(command,cmd_p1); strcat(command,cmd_p2); strcat(command,cmd_p3); //usleep(line_number); char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0"; strcat(command,cmd); //printf("%s ",command); //fflush(stdout); FILE *in; in= popen(command, "r"); rewind(in); char buff[50]; int counter = 0; while(fgets(buff,sizeof(buff),in)) { char c=' '; int i = 0; int cnt = atoi(buff); arr[line_number-1].count[counter] = cnt; while(c!='0') { c=buff[i]; buff[i]=buff[i+6]; i++; } int cnnt = 0; while(c!=' ') {
  • 4. c = buff[cnnt]; cnnt++; } i=0; while(c!='0') { c=buff[i]; buff[i]=buff[i+cnnt]; i++; } sprintf(arr[line_number-1].wordlist[counter],"%s",buff); //printf("%d %s",arr[line_number-1].count[counter],arr[line_number- 1].wordlist[counter]); counter++; } arr[line_number-1].count[counter] = -1; fclose(in); return NULL; } int main(void) { FILE *fp; fp = fopen("file.txt","r"); char a[200]; lines = 0; void *status = NULL; while(fgets(a,sizeof(a),fp) !=NULL) {
  • 5. lines++; } fclose(fp); arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines); threads = malloc(sizeof(pthread_t)*lines); int i; for(i=0;i= 0) { if(arr[i].count[ctr] > 0) { strcpy(final[final_ctr],arr[i].wordlist[ctr]); final_cnt[final_ctr] = arr[i].count[ctr]; arr[i].count[ctr] = 0; int sum = aggregate(arr[i].wordlist[ctr]); final_cnt[final_ctr] += sum; printf("%d %s",final_cnt[final_ctr],final[final_ctr]); } ctr++; final_ctr++; } i++; if(i==lines) { break; } } free(arr); return 0;
  • 6. } Solution this is my code to count the frequency of words in a text file #define _GNU_SOURCE #include #include #include #include /****************************** here is the Structure for storing words received from each thread ******************************/ struct return_vall{ char wordlist[100][100]; int count[100]; } *arr; pthread_t *threads; int lines; /***************************** here are Aggregates Frequency Sum of different threads *****************************/ int aggregate(char *a) { int sum = 0; int i=0; while(1) { if(i==lines) break; int count = 0; while(1) { if(arr[i].count[count] == -1)
  • 7. { break; } if(strcmp(a,arr[i].wordlist[count]) == 0) { sum += arr[i].count[count]; arr[i].count[count] = 0; } count++; } i++; } return sum; } /************************** Function thta are executed by each thread on seperate line. **************************/ void *word_count(void* num) { int *ln = num; unsigned int line_number = *ln; line_number++; //printf("Thread %d ",line_number); char cmd_p1[9] = "sed -n '0"; char cmd_p2[2];
  • 8. sprintf(cmd_p2,"%d",line_number); char cmd_p3[14] = "p' 'file.txt'0"; char command[100]; command[0] = '0'; strcat(command,cmd_p1); strcat(command,cmd_p2); strcat(command,cmd_p3); //usleep(line_number); char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0"; strcat(command,cmd); //printf("%s ",command); //fflush(stdout); FILE *in; in= popen(command, "r"); rewind(in); char buff[50]; int counter = 0; while(fgets(buff,sizeof(buff),in)) { char c=' '; int i = 0; int cnt = atoi(buff); arr[line_number-1].count[counter] = cnt; while(c!='0') { c=buff[i]; buff[i]=buff[i+6]; i++; }
  • 9. int cnnt = 0; while(c!=' ') { c = buff[cnnt]; cnnt++; } i=0; while(c!='0') { c=buff[i]; buff[i]=buff[i+cnnt]; i++; } sprintf(arr[line_number-1].wordlist[counter],"%s",buff); //printf("%d %s",arr[line_number-1].count[counter],arr[line_number- 1].wordlist[counter]); counter++; } arr[line_number-1].count[counter] = -1; fclose(in); return NULL; } int main(void) { FILE *fp; fp = fopen("file.txt","r"); char a[200]; lines = 0; void *status = NULL;
  • 10. while(fgets(a,sizeof(a),fp) !=NULL) { lines++; } fclose(fp); arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines); threads = malloc(sizeof(pthread_t)*lines); int i; for(i=0;i= 0) { if(arr[i].count[ctr] > 0) { strcpy(final[final_ctr],arr[i].wordlist[ctr]); final_cnt[final_ctr] = arr[i].count[ctr]; arr[i].count[ctr] = 0; int sum = aggregate(arr[i].wordlist[ctr]); final_cnt[final_ctr] += sum; printf("%d %s",final_cnt[final_ctr],final[final_ctr]); } ctr++; final_ctr++; } i++; if(i==lines) { break; } }