Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to fork in this case ?

Status
Not open for further replies.

lcout

Programmer
Feb 6, 2003
9
0
0
FR
Hello

I have a problem to fork ...

In fact my command is like

"cd /pathy/; runny -arg1 AA -arg2 BB"

runny is an executable in the directory pathy.

Before, I made a system call like : system("cd /pathy/; runny -arg1 AA -arg2 BB")

system open a shell then run the line beetween the quotes.

But I need to fork now, And I have two problem.

the first one : How can I have two command, the cd first and the runny after
the second : My command is in a char*, it is not like in my exemple, so I use in my fork

execv(command, "");

but execv don't accept that and in fact prefer :

execv("runny", arguments)

with arguments :
arguments[0] = "-arg1"
arguments[1] = "AA"
arguments[2] = "-arg2"
arguments[3] = "BB"

The solution of my second question might be : spleeting the command with space:
But I have absolutly no idea for my first question.

under I wrote a small exemple to illustrate my problem with some different try,
thank you very much for your help, because I am lost

lcout

***************************

process.c
***********************************************

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
char * arguments[1];
char t[3];
//char * command = &quot;/bin/ls; ./test&quot;;
//char * command = &quot;/bin/ls&quot;;
char * command = &quot;./test 12&quot;;

FILE* fic=fopen(&quot;pidy&quot;, &quot;w&quot;);

for(int l=0; l<=2; l++)
{
pid_t pid = fork();
if (pid == 0)
{
printf(&quot;start of number %d\n&quot;, l);
sprintf(t, &quot;%d&quot;, l);
//arguments[0]=t;
//execv(command,arguments);
arguments[0]=&quot;&quot;;
execv(command,arguments);
}
else
{
int status;
fprintf(fic, &quot;%d\n&quot;, pid);
while( (status = wait(NULL)) && status!=-1)
;
printf(&quot; xxxxxxxxxxxxxxxxx > %d finish \n\n\n&quot;,pid);
}
}
fclose(fic);
return 0;
}


test.c
***************************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv)
{
printf(&quot;number : %s\n&quot;,argv[0]);
printf(&quot;**************************\n&quot;);

for (int i=1; i<=2; i++)
{
// sleep(1);
printf(&quot;%d hello word\n&quot;,i);
}
printf(&quot;\n&quot;);
return 0;
}
 
Try chdir(&quot;/pathy&quot;) instead of system (&quot;cd /pathy&quot;);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top