hello will see I have my code that counts the amount of files in a directory and then returns the name of that file next to your md5sum, what happens is that my task says the following:
The application should examine the contents of the "/ var / log" directory once a year to identify the changes that have occurred in the system log files since the last revision. Should omit the subdirectories and files with ".gz" extension. To verify the files you must use the shell command "Md5sum" through fork and exec system calls For each file to be processed, your application must create a subprocess which is responsible for executing the "md5sum" command. The result of the command must be returned to the parent process through an anonymous pipe created with the pipe system call. A pipe must be created for each subprocess.
Then I found a code here in stackoverflow on how to create multiple children processes: link , which I suppose which is how they ask in my statement since it says "for each file to be processed a subprocess is created" but I wanted to know if it is the correct way.
The second is with the pipes, I have my piepe function () and my inf [2] array and using the dup function. I redirect the output and input, but I have doubts if I have to create a kind of arrangement or arrangement arrangement, since you have to create N pipes according to my N read files.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include<dirent.h>
char buffer[100];
int s;
int path_is_dir(char *path) { //valido si son subdirectorios
struct stat st_info;
if (stat(path, &st_info) < 0) {
return 0;
}
return S_ISDIR(st_info.st_mode);
}
void validar_folder(){
int carp;
DIR *dirp_p;
struct dirent *entry;
dirp_p = opendir("/home/jose/Escritorio/carpetaPrueba") ; //va "/var/log/PROYECTO_SO_1"
if(dirp_p == NULL ){
carp= mkdir("carpetaPrueba",0777); //creo la carpeta
}
}
int main(){
int cont_file=0;
int inf[2];
validar_folder();
pipe(inf);
DIR *dirp;
struct dirent * entry;
dirp = opendir("/home/jose/Escritorio");
while ((entry = readdir(dirp) )!=NULL) { //cuanto la cant de archivos
if (entry->d_type == DT_REG) {
cont_file++;
}
}
closedir(dirp);
const char * dir_name = "/home/jose/Escritorio";
DIR * dir_ptr = NULL;
struct dirent * dirent_ptr;
dir_ptr = opendir( dir_name );
//int f = open("prueba.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO);
while ( (dirent_ptr = readdir( dir_ptr ) ) != NULL ){
if(!path_is_dir(dirent_ptr -> d_name)){
if(fork()==0){
close(1);
dup(inf[1]); // redirige la salida estandar
execl("/usr/bin/md5sum", "md5sum", dirent_ptr -> d_name, NULL);
}else{
read(inf[0], buffer, 100);
buffer[32] = '#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include<dirent.h>
char buffer[100];
int s;
int path_is_dir(char *path) { //valido si son subdirectorios
struct stat st_info;
if (stat(path, &st_info) < 0) {
return 0;
}
return S_ISDIR(st_info.st_mode);
}
void validar_folder(){
int carp;
DIR *dirp_p;
struct dirent *entry;
dirp_p = opendir("/home/jose/Escritorio/carpetaPrueba") ; //va "/var/log/PROYECTO_SO_1"
if(dirp_p == NULL ){
carp= mkdir("carpetaPrueba",0777); //creo la carpeta
}
}
int main(){
int cont_file=0;
int inf[2];
validar_folder();
pipe(inf);
DIR *dirp;
struct dirent * entry;
dirp = opendir("/home/jose/Escritorio");
while ((entry = readdir(dirp) )!=NULL) { //cuanto la cant de archivos
if (entry->d_type == DT_REG) {
cont_file++;
}
}
closedir(dirp);
const char * dir_name = "/home/jose/Escritorio";
DIR * dir_ptr = NULL;
struct dirent * dirent_ptr;
dir_ptr = opendir( dir_name );
//int f = open("prueba.txt", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO);
while ( (dirent_ptr = readdir( dir_ptr ) ) != NULL ){
if(!path_is_dir(dirent_ptr -> d_name)){
if(fork()==0){
close(1);
dup(inf[1]); // redirige la salida estandar
execl("/usr/bin/md5sum", "md5sum", dirent_ptr -> d_name, NULL);
}else{
read(inf[0], buffer, 100);
buffer[32] = '%pre%';
printf("%s :", dirent_ptr -> d_name);
printf("%s\n", buffer);
wait(&s);
}
}
}//while
return 0;
}
';
printf("%s :", dirent_ptr -> d_name);
printf("%s\n", buffer);
wait(&s);
}
}
}//while
return 0;
}