Programme de recherche d'un mot
Salut a tous
J'ai un repertoire qui contient n fichier text. Et un processus pere doit creer pour chaque fichier un processus fils et chaque processus fils fait la recherche dans son fichier .et des qu'un processus fils trouve un mot M donné dans un fichier la recherche s'arrete et le pere met fin aux autre fils qui cherchent le mot .et si aucun fils ne trouve le mot on retoune qu'on a pas trouver .
J'ai ecrit les code de recherche mais je n'arrive pas faire la relation entre eux pour construire un executable qui marche
Premier code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| #include <stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include<string.h>
#include <sys/wait.h>
#include "tp.h"
int chercher (char * buffer, char * word)
{
char S[50]; int i =0, j; char c;
while ((c != '\t') && (c != '\r') && (c != '\n') && (i < strlen(buffer)) && (c != '\0'))
{
j = 0;
strcpy(S,"");
while ((c != '\t') && (c != '\r') && (c != ' ') && (c != '\n') && (i < strlen(buffer)) && (c != '\0'))
{
c = buffer[i];
S[j] = c;
j++;
i++;
}
if(j > 0)
{
S[j-1]='\0';
if(!strcmp(S,word)) return 0;
while ((c != '\t') && (c != '\r') && (c == ' ') && (c != '\n') && (i < strlen(buffer)) && (c != '\0'))
{
c = buffer[i];
i++;
}
i--;
}
else return -1 ;
}
return -1;
}
void reader ( FILE * stream, char * word )
{
char buffer [1024]; int ligne = 0;
while (! feof ( stream ) && ! ferror ( stream ) && fgets ( buffer , sizeof ( buffer ) , stream ) != NULL )
{
fputs ( buffer , stdout) ;
ligne ++;
if(!chercher (buffer,word))
{
printf("je suis le processus %d et j'ai trouvé le mot %s à la ligne %d \n",getpid(),word,ligne);
exit(ligne);
}
}
exit(0);
}
int main (int argc, char** argv)
{
if (argc == 3)
{
FILE * stream = fopen(argv[1],"r");
reader (stream, argv[2]);
}
else printf ("Le nombre de paramètres est insuffisantes \n");
return(0);
} |
Deuxieme code
Code:
1 2 3 4 5 6 7 8 9 10 11
| #define MAX 50
typedef struct Elem
{
int pid;
char file [40];
} Elem;
int cpt = 0;
Elem TElem [MAX]; |
Troisieme code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include<string.h>
#include <sys/wait.h>
#include "tp.h"
int main (int argc, char** argv)
{
char c;
if (argc < 3 ) {
printf("Le nombre de paramètre est insuffisante \n");
return (-1);
}
list_dir(argv[1],argv[2]);
return(0);
} |
Quatrieme code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #define MAX 50
typedef struct Elem
{
int pid;
char file [40];
} Elem;
int cpt = 0;
Elem TElem [MAX];
int Exist_Elem ()
{
int i;
for(i = 0; i < cpt ; i++) if (TElem[i].pid != 0) return 1;
return 0;
}
int Cher_Elem (pid_t y)
{ int i;
for(i = 0; i < cpt ; i++)
if (TElem[i].pid == y )
{
TElem[i].pid = 0;
return i ;
}
return -1;
}
int Add_Elem (char * x,pid_t y)
{
if (cpt < MAX)
{
strcpy(TElem[cpt].file,x);
TElem[cpt].pid = y;
cpt++;
return(0);
}
else return (-1);
}
Aff_Elem ()
{
int i = 0;
for(; i < cpt; i++) printf("### Le fils %d il a traité le fichier %s ### \n", TElem[i].pid, TElem[i].file);
}
int child(char* dir,char* file, char* word)
{
pid_t fils = fork();
if(fils == 0)
{
char path [50];
fprintf(stdout, "je suis le fils %d et j'ai traité le fichier :%s/%s \n", getpid(), dir,file);
exit(0);
}
else {
Add_Elem (file, fils);
}
}
int list_dir(char* argv1, char* argv2)
{
DIR *dir;
struct dirent *p;
char *fic = NULL; int nb = 0;
dir=opendir(argv1);
while((p = readdir(dir))!=NULL)
{
fic = p->d_name;
if (strcmp(fic,"..") && strcmp(fic,".") )
child(argv1, fic, argv2);
}
closedir(dir);
return 0;
} |
Merci.