Execl (passing parameters) in c

10,905

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.

Share:
10,905
Alexandros Alékso Todeschini
Author by

Alexandros Alékso Todeschini

Studying Informatics Science at University of Verona, Informatics Department key sector : c / ARM / Cortex M3 / Embedded System / java / RTOS

Updated on June 04, 2022

Comments

  • Alexandros Alékso Todeschini
    Alexandros Alékso Todeschini almost 2 years

    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)

    • unwind
      unwind almost 11 years
      Please fix the indentation, it's obviously very broken.
    • Terry
      Terry almost 11 years
      It's very hard to make something of this question.
    • mah
      mah almost 11 years
      write(STDOUT,"fork error else ", 15); -- don't you like printf("fork error else "); so much better? In addition to not having to count your output, you could provide dynamic output as well :)
    • Alexandros Alékso Todeschini
      Alexandros Alékso Todeschini almost 11 years
      i must use syscall unfortunately ... this is project for O.S. for my exam
  • Alexandros Alékso Todeschini
    Alexandros Alékso Todeschini almost 11 years
    sorry this operation cause*** stack smashing detected ***: padre.x terminated
  • mah
    mah almost 11 years
    @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).
  • Varvarigos Emmanouil
    Varvarigos Emmanouil almost 11 years
    My bad!! execl("processore.x","processore.x",param,param1,(char *)0);
  • Useless
    Useless almost 11 years
    ok, so where is it crashing? What line does the error show / what happens when you examine the core file?