Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <pthread.h>
- #include <regex.h>
- int match(const char *string, const char *pattern)
- {
- regex_t re;
- if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) return 0;
- int status = regexec(&re, string, 0, NULL, 0);
- regfree(&re);
- if (status != 0) return 0;
- return 1;
- }
- int wykonaj1(char *filename)
- {
- // count the number of lines in the file called filename
- FILE *fp = fopen(filename,"r");
- int ch=0;
- int lines=0;
- if (fp == NULL){
- printf("CO DO KURWY");
- return 0;
- }
- while ((ch = fgetc(fp)) != EOF)
- {
- if (ch == '\n')
- lines++;
- }
- fclose(fp);
- printf("%d\n", lines);
- }
- int wykonaj2(char *filename){
- FILE *fp = fopen(filename,"r");
- int ch=0;
- int words=0;
- if (fp == NULL){
- printf("CO DO KURWY");
- return 0;
- }
- char cur_str[10000];
- int counter = 0;
- while ((ch = fgetc(fp)) != EOF)
- {
- if (ch == '\n'){
- counter = 0;
- words += match(cur_str, "pipe");
- }
- cur_str[counter] = ch;
- counter++;
- }
- printf("%d\n", words);
- }
- void error(char *msg)
- {
- printf("%s", msg);
- }
- int main(){
- pthread_t t0;
- pthread_t t1;
- if(pthread_create(&t0,NULL,wykonaj1,"dictionary.txt")==-1)
- error("Nie mozna utworzyc watku t0");
- if(pthread_create(&t1,NULL,wykonaj2,"dictionary.txt")==-1)
- error("Nie mozna utworzyc watku t1");
- void* result;
- if(pthread_join(t0,&result)==-1)
- error("Blad oczekiwania na zakonczenie watku t0");
- if(pthread_join(t1,&result)==-1)
- error("Blad oczekiwania na zakonczenie watku t0");
- //printf("licznk na koniec ma wartosc = %i ", licznik);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement