2

I've problem with this code because this cycle not passing arguments correctly to son process and generate some error with fork.

nproc is numer process to create as 5 for example x is set to 1 value and i want to pass to my new process as 1,2,3 therefore every process as number and write it on file ...

I don't know can i do .. please help

but second parameter righeoperazione is 5 and is passed correct but because is always same father code where i put 2 type int and converted string to exec

/* father process*/
char param[0]="";
char param1[0]="";
for (i=0 ; i<NPROC ; i++) {
pid=fork();
 if (pid==-1) { // pid=-1; error process 
                    write(STDOUT,"fork error pid -1 ", 18);
                    }
 else if (pid==0) {
           sprintf(param1,"%d",x);
           sprintf(param,"%d",righeoperazioni); 
           execl("processore.x","processore.x",&param,&param1,(char *)NULL);
           write(STDOUT,"fork error ", 11);
           }
 else { write(STDOUT,"fork error else ", 15); }
 x++;           
} 

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

int nump=0;
int righe;
int oper=0;
char nome[10];
char temp[10];
    char temp1[10];

    nump=atoi(argv[2]);                  //
    oper=atoi(argv[1]);                  //


    righe=oper;
sprintf(nome,"%d",getpid());
int report = openFile(nome,O_CREAT | O_RDWR,S_IRUSR | S_IWUSR); 
sprintf(temp,"%d",nump);
sprintf(temp1,"%d",oper);
    stampa(report,"Number processo : ",18);
stampa(report,temp,strlen(temp));
stampa(report,"\Number comandi : ",18);
stampa(report,temp1,strlen(temp1));
stampa(report,"\n",1);

son code that capture 2 parameters (stampa is same as write but with -1 control)

1 Answers1

1

You are giving execl char** and not char* as you should give. Try

execl("processore.x","processore.x",param,param1,(char *)0);//EDIT!!!

Also declare param1 and param with some more size

char param1[64], param[64];

in order to hold your "sprinted" integers.

  • sorry this operation cause*** stack smashing detected ***: padre.x terminated – Alexandros Alékso Todeschini Jun 17 '13 at 14:02
  • @AlexandrosAléksoTodeschini your param variables have space for exactly 0 characters, but you're writing more than that into them. Apply V_Maenolis's suggestion of actually giving them some size because without that, you're going to overrun the stack (as your error indicates). – mah Jun 17 '13 at 14:05
  • My bad!! execl("processore.x","processore.x",param,param1,(char *)0); – Varvarigos Emmanouil Jun 17 '13 at 14:06
  • ok, so where is it crashing? What line does the error show / what happens when you examine the core file? – Useless Jun 17 '13 at 14:10