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 = "/bin/ls; ./test";
//char * command = "/bin/ls";
char * command = "./test 12";
FILE* fic=fopen("pidy", "w"
;
for(int l=0; l<=2; l++)
{
pid_t pid = fork();
if (pid == 0)
{
printf("start of number %d\n", l);
sprintf(t, "%d", l);
//arguments[0]=t;
//execv(command,arguments);
arguments[0]="";
execv(command,arguments);
}
else
{
int status;
fprintf(fic, "%d\n", pid);
while( (status = wait(NULL)) && status!=-1)
;
printf(" xxxxxxxxxxxxxxxxx > %d finish \n\n\n",pid);
}
}
fclose(fic);
return 0;
}
test.c
***************************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
printf("number : %s\n",argv[0]);
printf("**************************\n"
;
for (int i=1; i<=2; i++)
{
// sleep(1);
printf("%d hello word\n",i);
}
printf("\n"
;
return 0;
}
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 = "/bin/ls; ./test";
//char * command = "/bin/ls";
char * command = "./test 12";
FILE* fic=fopen("pidy", "w"
for(int l=0; l<=2; l++)
{
pid_t pid = fork();
if (pid == 0)
{
printf("start of number %d\n", l);
sprintf(t, "%d", l);
//arguments[0]=t;
//execv(command,arguments);
arguments[0]="";
execv(command,arguments);
}
else
{
int status;
fprintf(fic, "%d\n", pid);
while( (status = wait(NULL)) && status!=-1)
;
printf(" xxxxxxxxxxxxxxxxx > %d finish \n\n\n",pid);
}
}
fclose(fic);
return 0;
}
test.c
***************************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char** argv)
{
printf("number : %s\n",argv[0]);
printf("**************************\n"
for (int i=1; i<=2; i++)
{
// sleep(1);
printf("%d hello word\n",i);
}
printf("\n"
return 0;
}