본문 바로가기

IT

two word count 1 .c

// 값을 바꾸려는 애가 둘!

// 한 놈이 진입하면 다른 애가 접근 못하게 막아야됨.


// 더하는 과정이 사실 load add store 세단계인데, 그 중간에 다른 놈이 접근해서 값을 수정하려하면

// 원하는 결과가 안 나올 수 있다. = 크리티컬 섹션


#include <stdio.h>

#include <pthread.h>

#include <ctype.h>


int total_word;


main(int ac, char* av[])

{

        pthread_t t1,t2;

        void *count_word(void*);

        if(ac!=3)

        {

                printf("usage");

                exit(1);

        }

        total_word = 0;

        pthread_create(&t1,NULL,count_word,(void*)av[1]);

        pthread_create(&t2,NULL,count_word,(void*)av[2]);

        pthread_join(t1,NULL);

        pthread_join(t2,NULL);

        printf("%5d: total word\n", total_word);

}

void *count_word(void* f)

{

        char* filename = (char*) f;

        FILE *fp;

        int c, prevc = '\0';

        if ((fp=fopen(filename, "r"))!= NULL)

        {

                while((c=getc(fp))!=EOF)

                {

                        if(!isalnum(c)&&isalnum(prevc))

                                total_word++;

                        prevc = c;

                }

                fclose(fp);

        }

        else

                perror(filename);

        return NULL;

}


'IT' 카테고리의 다른 글

two word count 4  (0) 2014.12.09
two word 2  (0) 2014.12.09
print.c  (0) 2014.12.09
multi.c  (0) 2014.12.09
genericClient  (0) 2014.11.23