
15/12/2008, 16:39
|
| | Fecha de Ingreso: diciembre-2008
Mensajes: 4
Antigüedad: 16 años, 3 meses Puntos: 0 | |
Respuesta: creacion de procesos en linux usando hebras #include <stdio.h> //Librerías necesarias
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
//Creamos la estructura t_datos_hebra para pasar la ruta a la función
typedef struct mihebra{
char* ruta;
}t_datos_hebra;
//Declaramos lo siguiente para poder manipular los directorios
DIR *directorio;
struct dirent *siguiente;
//Estructuras que recogen la información del tipo de archivo
struct stat buff;
struct stat buff2;
//función entradas
void* entradas (void* ruta);
//main
int main (int argc, char* argv[]){
pid_t pid; //recoge el valor devuelto al hacer fork().
int pid_proc_padre; //variable para almacenar el pid del proceso padre
int pid_proc_hijo; //variable para almacenar el pid del proceso hijo
//Creamos la estructura argumentos
t_datos_hebra *argumentos;
//Creamos un array de hebras
pthread_t *ahebras;
//Reservamos memoria para la estructura y las hebras
argumentos=(t_datos_hebra*)malloc(1*sizeof(t_datos _hebra*));
ahebras=(pthread_t*)malloc(1*sizeof(pthread_t*));
argumentos[0].ruta=argv[1];
pid=fork();
if(pid==0){
pid_proc_padre=getppid();
pid_proc_hijo=getpid();
printf("\nEL IDENTIFICADOR DEL PROCESO PADRE ES: %d", pid_proc_padre);
printf("\nEL IDENTIFICADOR DEL PROCESO HIJO ES: %d\n", pid_proc_hijo);
execlp("file",argv[1],NULL);
}
//Lanzamos las hebras
pthread_create(&ahebras[0],NULL,(void*)entradas,(void*)&argumentos[0]);
//Esperamos a que termine
pthread_join(ahebras[0],NULL);
//Liberamos memoria
free(ahebras);
free(argumentos);
return 0;
}//fin main
//función entradas
void* entradas(void* ruta){
int num_dir=0; //variable que cuenta el número de directorios
//Hacemos un cast para poder utilizar aux
t_datos_hebra *aux;
aux=(t_datos_hebra*)ruta;
lstat(aux->ruta,&buff);
if((buff.st_mode & S_IFMT)==S_IFDIR){
printf("\nLA RUTA INTRODUCIDA ES UN DIRECTORIO.\n");
directorio=opendir(aux->ruta);
while ((siguiente=readdir(directorio))!=NULL){
lstat(siguiente->d_name,&buff2);
if ((buff2.st_mode & S_IFMT)==S_IFDIR){
num_dir++;
}
}//fin while
printf("\nY TIENE %d DIRECTORIOS DENTRO\n\n", num_dir);
}//fin if
}//fin funcion entradas |