Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <dlfcn.h>
- #include <sys/sem.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <aio.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <signal.h>
- #include <pthread.h>
- #include <assert.h>
- #include <string.h>
- #include <stdlib.h>
- #define BUF_S 1024
- void MakeFileList();
- void* ReadTh(void*);
- void* WriteTh(void*);
- void ErrorPrint(char* string,int num);
- void clear_buf();
- int (*AReadF)(struct aiocb*);
- int (*AWriteF)(struct aiocb*);
- pthread_mutex_t r_mutex, w_mutex;
- char FileList[100][256];
- int sem_id=0;
- int file_count;
- char buf[BUF_S];
- int flag=0;
- struct aiocb read_cb,write_cb;
- int read_len=0,write_len=0;
- void main()
- {
- int i=0,j;
- pthread_t tid[2];
- void *lid;
- lid=dlopen("./55dll.so",RTLD_LAZY);
- if(lid==NULL)
- ErrorPrint("Library",0);
- AReadF=dlsym(lid,"AReadF");
- AWriteF=dlsym(lid,"AWriteF");
- int res = pthread_mutex_init(&r_mutex, NULL);
- if(res!=0)
- ErrorPrint("Read Mutex",0);
- res = pthread_mutex_init(&r_mutex, NULL);
- if(res!=0)
- ErrorPrint("Write Mutex",0);
- MakeFileList();
- printf("\nThere are next files in dir: \n");
- while(FileList[i][0])
- {
- printf("%s\n",FileList[i]);
- i++;
- }
- printf("\n");
- i=pthread_create(&tid[0],NULL,&ReadTh,NULL);
- if(i!=0)
- ErrorPrint("Thread",0);
- i=pthread_create(&tid[1],NULL,&WriteTh,NULL);
- if(i!=0)
- ErrorPrint("Thread",0);
- pthread_join (tid[0], 0);
- pthread_join (tid[1], 0);
- printf("END\n");
- pthread_mutex_destroy(&r_mutex);
- pthread_mutex_destroy(&w_mutex);
- dlclose(lid);
- }
- void MakeFileList()
- {
- struct dirent **namelist;
- int k;
- int i,j,len;
- k = scandir("./5", &namelist, 0, alphasort);
- file_count=k;
- if (k < 0)
- ErrorPrint("Scandir",0);
- i=0;
- while(k--)
- {
- len=strlen(namelist[k]->d_name);
- if(namelist[k]->d_name[0]!='.' && namelist[k]->d_name[0]!=' ')
- {
- for(j=0;j<len;j++)
- {
- FileList[i][j]=namelist[k]->d_name[j];
- }
- }
- free(namelist[k]);
- i++;
- }
- free(namelist);
- }
- void* ReadTh(void *pp)
- {
- int fid;
- char temp[256];
- int i;
- printf("\n-------------------work-----------------\n");
- for(i=0;i<file_count-2;i++)
- {
- strcpy(temp,"5");
- strcat(temp,FileList[i]);
- fid=open(temp,O_RDONLY);
- if(fid==-1)
- {
- ErrorPrint("Open File Read",1);
- continue;
- }
- usleep(100);
- clear_buf();
- read_cb.aio_fildes=fid;
- read_cb.aio_offset=0;
- read_cb.aio_buf=&buf;
- read_cb.aio_nbytes=BUF_S;
- read_cb.aio_sigevent.sigev_notify=SIGEV_NONE;
- while(1)
- {
- printf("\n%s read...\n",temp);
- pthread_mutex_lock(&r_mutex);
- read_len=AReadF(&read_cb);
- pthread_mutex_unlock(&w_mutex);
- read_cb.aio_offset+=read_len;
- printf("%s",(char*)read_cb.aio_buf);
- if(read_len<BUF_S)
- break;
- }
- close(fid);
- }
- flag=1;
- return;
- }
- void* WriteTh(void* pp)
- {
- int fid;
- char temp[256];
- int val;
- strcpy(temp,"./res.txt");
- fid=open(temp,O_WRONLY | O_CREAT | O_APPEND | O_TRUNC);
- printf("%d",fid);
- if(fid==-1)
- ErrorPrint("Open Write File",0);
- write_cb.aio_fildes=fid;
- write_cb.aio_offset=0;
- write_cb.aio_buf=&buf;
- write_cb.aio_sigevent.sigev_notify=SIGEV_NONE;
- while(!flag)
- {
- printf("\t\t%s write...\n",temp);
- pthread_mutex_lock(&w_mutex);
- write_cb.aio_nbytes = read_len;
- write_len=AWriteF(&write_cb);
- pthread_mutex_unlock(&r_mutex);
- }
- close(fid);
- return;
- }
- void ErrorPrint(char* string, int num)
- {
- printf("\n%s ERROR\n",string);
- if(num==0)
- exit(0);
- }
- void clear_buf()
- {
- int e;
- for(e=0;e<BUF_S;e++)
- buf[e]=0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement