RE:PROBLEMA AL ELIMINAR UN PROCESO
hola Toma,
Nuevamente muchisimas gracias, adjunto todo el codigo, creo que el problema puede estar en el menú, lo voy a repasar detalladamente, lo pongo por si tú puedes tambien darle un vistazo. y de nuevo te estoy muy agradecido
#include "rutines.h"
/* Variables de parametres segon opcions de menu */
double R=0, S=0; /* Interval Integració*/
int T=0; /* Nombre Trapecis sub-interval Integració*/
int N=0; /* Nombre Processos Integradors */
int ID_E=0;
struct proceso_calculador p_calculador[MAXPROCS];
/* Prototips de les funcions desenvolupades */
int menu();
void inicialitzar(void);
int eliminar_proces_integrador(void);
int crear_processos_integradors(void);
void calcular_integral(void);
void acabar(void);
int menu()
{
int opcio;
char buffer[MAXSTR];
/* escriu menu */
escriure_string("\nMENU:","");
escriure_string(" 1) Crear processos calculadors ","");
escriure_string(" 2) Eliminar procès calculador ","");
escriure_string(" 3) Calcular ","");
escriure_string(" 4) Fi de totes les aplicacions ","");
llegir_string("OPCI� ", buffer);
opcio = atoi(buffer);
while (opcio < 1 || opcio > 4)
{
llegir_string("\n Indiqueu novament l'opci� ", buffer);
opcio=atoi(buffer);
}
/* llegeix el nombre de processos integradors */
if (opcio == 1)
{
do
{
llegir_string ("Introducir el nombre de processos: ", buffer);
N = atoi(buffer);
if ( (N > 5) || (N < 1) )
escriure_string("Nombre de processos incorrecte. ", "");
}
while ((N > 5) || (N < 1));
}
if(opcio== 2)
{
llegir_string("Identificador del procés a eliminar: ", buffer);
ID_E = atoi(buffer);
}
/* llegeix el interval dintegració [r, s] */
if (opcio == 3)
{
do
{
llegir_string("Introduir el inici del interval dintegraci� ", buffer);
R = atof(buffer);
llegir_string("Introduir el final del interval dintegraci� ", buffer);
S = atof(buffer);
if ( R>=S )
escriure_string("Interval d'integraci�incorrecte. ", "");
}
while ( R>=S );
do
{
llegir_string ("Introducir el nombre de trapezis: ", buffer);
T = atoi(buffer);
if ( (T > 100000) || (T < 1) )
escriure_string("Nombre de Trapezis Incorrecte. ", "");
}
while ( (T > 100000) || (T < 1) );
}
return opcio;
}
void inicialitzar(void)
{
int i=0;
for ( i=0; i<MAXPROCS; i++){
p_calculador[i].id= -1;
}
}
int crear_processos_integradors()
{
int i,proces,c;
int trobat;
char mensaje[80];
//Comprobamos que exista algun proceso libre
i = 0;
while (i<N) {
switch (proces = fork()) {
case -1 :
error("Error de creación proceso hijo");
//Proceso hijo
break;
case 0:
trobat =1;
execlp("./calculador","calculador",NULL);
error("Error ejecutando calculador");
break;
//Proceso padre
default:
p_calculador[i].id = proces;
sprintf(mensaje,"principal %d creando proceso hijo [%d] : %d",getpid(),i,p_calculador[i].id);
escriure_string(mensaje,"");
i++;
} // fin del switch
}
return trobat;
}
int eliminar_proces_integrador(){
int i,trobat,eliminar;
int status;
char mensaje[80];
trobat=0;
for ( i=0; i<MAXPROCS; i++){
if( ID_E==p_calculador[i].id){
kill (ID_E,SIGTERM) ;
wait(&status);
p_calculador[i].id=-1;
trobat=1;
}
}
return trobat;
}
/
/
int main(int argc, char* argv[])
{
char miss[80];
float x1,x2;
// Exemple utilitzaciò Macro Funcio Integració (FunInt)
x1=FunInt(1.0);
x2=FunInt(1.5);
sprintf(miss, "Valors funció integració: F(1.0)=%f F(1.5)=%f F(2)=%f.\n",x1,x2,FunInt(2) );
escriure_string(miss,"");
inicialitzar();
for (;;) {
switch (menu()) {
case 1:
if (!crear_processos_integradors())
if (write (2, "Error creando integrador o procesos ya creados\n", strlen("Error creando integrador o procesos ya creados\n")) < 0)
error("En crear Integrador");
break;
case 2:
/* Eliminar Proc� integrador (crida a funci�que ho faci) */
if (!eliminar_proces_integrador())
if (write (2, "Error eliminar integrador\n", strlen("Error eliminar integrador\n")) < 0)
error("En eliminar Integrador");
break;
case 3:
/* Calcular Integral (crida a funci�que ho faci) */
calcular_integral();
break;
case 4:
/* acabar, matant a tots els processos integradors (crida a funci�que ho faci)*/
acabar();
exit(0);
}
}
}