Foros del Web » Programación para mayores de 30 ;) » C/C++ »

creacion de procesos en linux usando hebras

Estas en el tema de creacion de procesos en linux usando hebras en el foro de C/C++ en Foros del Web. //Librerias necesarias. #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> //Estructura para la hebra. typedef struct datos_hebra{ char* ruta; ...
  #1 (permalink)  
Antiguo 15/12/2008, 16:35
 
Fecha de Ingreso: diciembre-2008
Mensajes: 4
Antigüedad: 15 años, 4 meses
Puntos: 0
creacion de procesos en linux usando hebras

//Librerias necesarias.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

//Estructura para la hebra.
typedef struct datos_hebra{
char* ruta;
}t_datos_hebra;

void* mostrarDatos(void* parametros);

void* mostrarDatos(void* parametros){
int cte;
//Definimos un puntero a la estructura que vamos a utilizar.
t_datos_hebra* aux;
aux=(t_datos_hebra*) parametros;
//Estructura donde se va a guardar toda la informacion del archivo o directorio.
struct stat buff;

cte=lstat(aux->ruta,&buff);

if((buff.st_mode & S_IFMT)==S_IFDIR){
printf("\nLa entrada es un directorio.\n.");
}
//Ahora si no es un directorio vemos si es un dispositivo de e/s por bloques.
else if((buff.st_mode & S_IFMT)==S_IFBLK)
{
printf("\nEl archivo es de tipo desconocido.\n");
}
//¿Es un dispositivo de e/s por caracteres?.
else if((buff.st_mode & S_IFMT)==S_IFCHR)
{
printf("\nEl archivo es de tipo desconocido.\n");
}
//¿Es un enlace simbólico?.
else if((buff.st_mode & S_IFMT)==S_IFLNK)
{
printf("\nEl archivo es de tipo desconocido.\n");
}
//¿Es una tuberia?.
else if((buff.st_mode & S_IFMT)==S_IFIFO)
{
printf("\nEl archivo es de tipo desconocido.\n");
}
//Si no es nada de lo anterior,entonces será un archivo regular.
else{
printf("\nEl archivo es regular.\n");
}
}

int main(int argc,char* argv[]){

int result;
int pid_proc_padre;
int pid_proc_hijo;
//Creamos los parámetros para la hebra.
t_datos_hebra *parametros;
//Creamos una hebra
pthread_t *ahebra;
//Reservo memoria para la hebra y para sus parámetros.
ahebra=(pthread_t*)malloc(1*sizeof(pthread_t));
parametros=(t_datos_hebra*)malloc(1*sizeof(t_datos _hebra));
//Metemos la ruta en los parametros de la hebra, para utilizarla en mostrarDatos.
parametros[0].ruta=argv[1];
//Ahora creamos el proceso.
result=fork();
if(result==0){
//Ahora mostramos el pid del padre y del hijo.
pid_proc_padre=getppid();
pid_proc_hijo=getpid();
printf("Pid del proceso padre:%d\n",pid_proc_padre);
printf("Pid del proceso hijo:%d\n",pid_proc_hijo);
//Ahora ejecutamos el comando ls.
execlp("ls","",NULL);
}
//Lanzamos la hebra.
pthread_create(&ahebra[0],NULL,(void*)mostrarDatos,(void*)&(parametros[0]));
//Esperamos a que termine la hebra.
pthread_join(ahebra[0],NULL);
//Liberamos recursos.
free(parametros);
free(ahebra);

return 0;
}
  #2 (permalink)  
Antiguo 15/12/2008, 16:36
 
Fecha de Ingreso: diciembre-2008
Mensajes: 4
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: creacion de procesos en linux usando hebras

#include <unistd.h> //Librerias a utilizar.
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
DIR *direccion;
struct stat buff;//Estructura donde se va a guardar toda la informacion del archivo.
struct dirent *dirr;
int cte;
int resul;
int contando=0;
typedef struct datos_hebra{
char* entra;
}t_datos_hebra;
void* contar(void* datos_hebra);

void* contar(void* datos_hebra){

t_datos_hebra* aux;
aux=(t_datos_hebra*) datos_hebra;

cte=lstat(aux->entra,&buff);
if((buff.st_mode & S_IFMT)==S_IFDIR)
{
printf("\nEl fichero es un DIRECTORIO\n");
direccion=opendir(aux->entra);//Abrimos el documento.
printf("\n**********************************\n");
printf("* Contenido del directorio. *\n");
printf("**********************************\n");
dirr=readdir(direccion);//Nos preparamos para leerlo.
while(dirr!=NULL)//Vamos leyendo el contenido del fichero.
{

printf("%s\n",dirr->d_name);

dirr=readdir(direccion);
contando++;
}
printf("\n");

resul=closedir(direccion);//Cerramos el fichero.

}
}
int main (int argc,char* argv[]){

int i;//Constante para for.
int res;//Constante.

t_datos_hebra* parametros;
int pid_proc_padre;//Pid del proceso padre.
int pid_proc_hijo; //Pid del proceso hijo.
pthread_t *ahebras;
parametros=(t_datos_hebra*)malloc(4*sizeof(t_datos _hebra));
ahebras=(pthread_t*)malloc((4)*sizeof(pthread_t));
res=fork();
parametros[0].entra=argv[1];
if(res==0){

pthread_create(&ahebras[0],NULL,(void*)contar,(void*)&(parametros[0]));
pid_proc_padre=getppid();//Nos devuelve el pid del proceso padre.
pid_proc_hijo=getpid();//Nos devuelve el pid del proceso hijo creado.
pthread_join(ahebras[0],NULL);
printf("Pid del proceso padre:%d\n",pid_proc_padre);
printf("Pid del proceso hijo:%d\n\n",pid_proc_hijo);
printf("Numero de entradas del directorio:%d\n\n",contando);
}



return 2;
}
  #3 (permalink)  
Antiguo 15/12/2008, 16:39
 
Fecha de Ingreso: diciembre-2008
Mensajes: 4
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: creacion de procesos en linux usando hebras

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <pthread.h>


typedef struct datos_hebra{
char* ruta;
}t_datos_hebra;

int num_entradas=0;
struct stat buff;
DIR* direccion;
struct dirent* dir;

void* contar(void* parametro);

int main(int argc, char* argv[]){
int res;

int pid_proc_padre;
int pid_proc_hijo;

t_datos_hebra* parametros;
pthread_t* ahebra;
parametros=(t_datos_hebra*)malloc(1*sizeof(t_datos _hebra));
ahebra=(pthread_t*)malloc(1*sizeof(pthread_t));

parametros[0].ruta=argv[1];

res=fork();

if(res==0){
pthread_create(&ahebra[0],NULL,(void*)contar,(void*)&parametros[0]);
pthread_join(ahebra[0],NULL);
printf("\nEl numero de entradas del directorio es: %d\n",num_entradas);
}

else{
pid_proc_padre=getppid();
pid_proc_hijo=getpid();
printf("\nEl PID del proceso padre es: %d",pid_proc_padre);
printf("\nEl PID del proceso hijo es: %d\n",pid_proc_hijo);
}

free(parametros);
free(ahebra);

return 1;
}

void* contar(void* parametro){
int cte;
int cerrar;

t_datos_hebra* aux;
aux=(t_datos_hebra*)parametro;

cte=lstat(aux->ruta,&buff);

if((buff.st_mode & S_IFMT)==S_IFDIR){
printf("\nEs un DIRECTORIO");
direccion=opendir(aux->ruta);
dir=readdir(direccion);
while(dir!=NULL){
num_entradas++;
dir=readdir(direccion);
}
cerrar=closedir(direccion);
}
}
  #4 (permalink)  
Antiguo 15/12/2008, 16:39
 
Fecha de Ingreso: diciembre-2008
Mensajes: 4
Antigüedad: 15 años, 4 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
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:17.