Last active
December 27, 2015 23:39
-
-
Save jvelezpo/7407140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "librerias.h" | |
typedef struct procesos{ | |
string id,path,fileName,lives; | |
}proceso; | |
// Prototipado de las funciones | |
void* funcion_control(void *arg); | |
void* readOut(void *ptr); | |
void* readError(void *ptr); | |
proceso InfoProcess(string s); | |
string intToStr(int i); | |
// Variables Globales | |
#define BUFFSIZE 4096 | |
int main (int argc, char *argv[]){ | |
ifstream open("../common/configLinux.txt"); // Abrir el archivo de configuracion | |
pthread_t *tablaDeHilos; // Información de los hilos | |
string linea[100]; // Cada linea del archivo de configuracion en este arreglo | |
proceso p[100]; | |
// Para separar el archivo de config por lineas | |
int i = 0; | |
while(!open.eof()){ | |
char archivo[100]; | |
open.getline(archivo, 100); | |
linea[i] = (string)archivo; | |
i++; | |
} | |
// Solicito memoria dinámica para la tabla | |
tablaDeHilos = (pthread_t *) malloc(sizeof(pthread_t) * (i-1)); | |
// Creando el hijo y pasandole el string | |
for (int j = 0; j < i-1; j++){ | |
p[j] = InfoProcess(linea[j]); | |
pthread_create((tablaDeHilos + j), NULL, &funcion_control,(void *) &p[j]); | |
} | |
// Inicio el hilo y le paso la variable de retorno | |
for (int j = 0; j < i-1; j++) { | |
pthread_join(*(tablaDeHilos +j),NULL); | |
} | |
return 0; | |
} | |
// Funcion que se ejecuta cada que se crea un hilo | |
void* funcion_control(void *arg) { | |
proceso *p; | |
p = (proceso *) arg; | |
int rv; | |
int readed; | |
char buffer[BUFFSIZE]; | |
pid_t pid; | |
//crear pipes | |
int pipeIn[2]; | |
int pipeOut[2]; | |
int pipeErr[2]; | |
pipe(pipeIn); | |
pipe(pipeOut); | |
pipe(pipeErr); | |
//creando el proceso controlador | |
pid = fork(); | |
if(pid == (pid_t)(-1)){ | |
fprintf(stderr, "Error: %s, fallo al hacer fork", strerror(errno)); | |
exit(13); | |
} | |
else if(pid == 0){ //proceso hijo | |
close(pipeErr[0]); | |
close(pipeOut[0]); | |
dup2(pipeOut[1], STDOUT_FILENO); | |
close(pipeOut[1]); | |
dup2(pipeErr[1], STDERR_FILENO); | |
close(pipeErr[1]); | |
execl("ctrlProcess", "ctrlProcess", p->id.c_str(), p->path.c_str(), p->fileName.c_str(), p->lives.c_str(), NULL); | |
} | |
fd_set read_fds; | |
FD_ZERO(&read_fds); | |
FD_SET(pipeOut[0], &read_fds); | |
FD_SET(pipeErr[0], &read_fds); | |
rv = select(FD_SETSIZE, &read_fds, NULL, NULL, NULL); | |
if(rv > 0) | |
{ | |
if(FD_ISSET(pipeOut[0], &read_fds)) | |
{ | |
while((readed = read(pipeOut[0], buffer, BUFFSIZE)) > 0) | |
{ | |
write(STDOUT_FILENO, buffer, readed); | |
} | |
} | |
if(FD_ISSET(pipeErr[0], &read_fds)) | |
{ | |
while((readed = read(pipeErr[0], buffer, BUFFSIZE)) > 0) | |
{ | |
write(STDOUT_FILENO, buffer, readed); | |
} | |
} | |
} | |
close(pipeOut[0]); | |
close(pipeErr[0]); | |
} | |
string intToStr(int i){ | |
string s; | |
stringstream out; | |
out << i; | |
s = out.str(); | |
return s; | |
} | |
/*void* readError(void *ptr){ | |
int *In = (int*)ptr; | |
int readed = 1; | |
char line[BUFFSIZE]; | |
while(readed>0){ | |
readed = read(*In, line, BUFFSIZE); | |
if(readed>0) | |
{ | |
write(2, line, BUFFSIZE); | |
//cout<<line; | |
} | |
} | |
} | |
void* readOut(void *ptr){ | |
int *In = (int*)ptr; | |
int readed = 1; | |
char line[BUFFSIZE]; | |
while(readed>0){ | |
readed = read(*In, line, BUFFSIZE); | |
if(readed>0){ | |
write(1, line, BUFFSIZE); | |
//cout<<line; | |
} | |
} | |
} | |
*/ | |
proceso InfoProcess(string s){ | |
proceso p; | |
char * str = strdup( s.c_str()); | |
char * token; | |
token = strtok(str," "); | |
int i = 0; | |
while(token != NULL){ | |
if (i == 1) | |
p.id = token; | |
else if (i == 3) | |
p.path = token; | |
else if(i == 5) | |
p.fileName = token; | |
else if(i == 6) | |
p.lives = token; | |
token = strtok(NULL, " "); | |
i++; | |
} | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment