Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 12.05.2011, 00:34   #1
vadim2992
Новичок
Джуниор
 
Регистрация: 14.04.2010
Сообщений: 2
По умолчанию Программа Linux

Программа сравнивает файлы в двух каталогах. Запуская каждое сравнение в отдельном потоке. Отлично работает с мелкими каталогами. Но если ввести каталог /etc, то виснит.





#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <wait.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

void * thread_func(void *buf44);
int count=1;


main(int argc, char ** argv)

{
char outfile3[256]={'\0'},outfile4[256]={'\0'},outfile5[256]={'\0'},*outfile6;
char outf[256]={'\0'};
int result,result1,num1;
int n;
int interray;
struct dirent *entry1;
DIR * dir1;
DIR * dir2;
pthread_t thread1;
char buff[256]={'\0'};
struct dirent *entry2;
if (argc < 4)
{
fprintf (stderr, "%s Too few arguments\n",argv[1]);
return 1;
}
dir1 = opendir (argv[1]);
if (dir1 == NULL)
{
fprintf (stderr, "%s opendir() error\n",argv[1]);
return 1;
}

n=atoi(argv[3]);

while ((entry1 = readdir (dir1)) != NULL)
{

dir2 = opendir (argv[2]);
if (dir2 == NULL)
{
printf ("%s opendir() error\n",argv[2]);
}
else
{

while ((entry2 = readdir (dir2))!=NULL)
{
outfile6=(char*)malloc(256);
sprintf(outf,"%s/%s",argv[1],entry1->d_name);
sprintf(outfile3,"%s/%c",outf,',');
sprintf(outfile4,"%s/%s",outfile3,argv[2]);

sprintf(outfile5,"%s/%s",outfile4,entry2->d_name);
sprintf(outfile6,"%s/%c",outfile5,',');

if(entry1->d_name[0]!='.')
while(1)
{
if(count<=n)
{
pthread_create(&thread1,NULL,thread _func,outfile6);
count++;

break;
}
}

}

rewinddir (dir2);

}

closedir(dir2);

}
closedir(dir1);

pthread_join(thread1,NULL);

return 0;
}
void * thread_func(void *buff44) // void func(char *outfile1,char *buff)
{
struct stat buf1;
struct stat buf2;
char buff1[100]={'\0'};
strcpy(buff1,buff44);
int j=0,i=0;
int fd1,fd2;
char outfile1[256]={'\0'};
char ch1[1],ch2[1];
long k;

char outfile2[256]={'\0'};

while(buff1[j+1]!=',')
{
outfile1[i]=buff1[j];
i++;
j++;

}
outfile1[i]='\0';
j++;
j++;
j++;
i=0;
while(buff1[j+1]!=',')
{
outfile2[i]=buff1[j];
i++;
j++;
}
outfile2[i]='\0';


fd1=open (outfile1, O_RDONLY);
fstat(fd1, &buf1);
if((long)buf1.st_mode>30000)
{
if (fd1 == -1)
{
printf ("Cannot open file1 %s\n",outfile1);
puts(outfile1);
}
fd2=open (outfile2, O_RDONLY);
if(fd2 == -1)
{
printf("Cannot open file %s\n",outfile2);

}
else
{

fstat( fd2, &buf2);
if((long)buf2.st_mode>30000)
{
k=1;
while ((read (fd1, ch1, 1)>0)&&(read(fd2,ch2,1)>0))
{
if(ch1[0]==ch2[0])
{
k++;
}
else {break;}
}
if((buf1.st_size==buf2.st_size)&&(( long int)buf1.st_size==(long int)(k-1)))
{
printf("ID: %u file1: %s file2: %s sravnili bait: %ld equal\n",(unsigned int)pthread_self(),outfile1,outfile 2,k-1);
}
else
{
printf("ID: %u file1: %s file2: %s sravnili bait: %ld not equal\n",(unsigned int)pthread_self(),outfile1,outfile 2,k);
}
lseek (fd1, 0, SEEK_SET);
lseek (fd2, 0, SEEK_SET);
}
close(fd2);

}
}


close(fd1);
count--;
return 0;
}
vadim2992 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Программа анализа содержимого каталогов (на ассемблере под Linux) fantom_13 Assembler - Ассемблер (FASM, MASM, WASM, NASM, GoASM, Gas, RosAsm, HLA) и не рекомендуем TASM 0 16.03.2011 00:29
Программа для снятия видео с экрана в Linux Яр|/||< (^_^) Софт 1 14.03.2010 15:42
c++ в Linux (Межпроцессорный обмен в Linux) RusLotus Помощь студентам 0 13.10.2009 16:59
Консольная программа под Linux amdbodia Общие вопросы C/C++ 8 14.06.2009 20:23