4

I have this code;

pid_t process;
process = fork();

if (process < 0){
   //fork error
   perror("fork");
   exit(EXIT_FAILURE);
}
if (process == 0){
   //i try here the execl
   execl ("process.c", "process" , n, NULL);
}
else {
   wait(NULL);
}

I don't know if this use of fork() and exec() combined is correct. When I try to run the program from the bash I do not receive any result, so I thought it could be a problem in this part of code.
Thanks.

Sicioldr
  • 543
  • 3
  • 7
  • 13

2 Answers2

16

One problem is that

if (process = 0){

should read

if (process == 0){

Otherwise you're assigning zero to process and only calling execl if result is non-zero (i.e. never).

Also, you're trying to exec something called process.c. There's no doubt that one could have an executable called process.c. However, conventionally names ending in .c are given to C source code files. If process.c is indeed a C file, you need to compile and link it first.

Once you've built the executable, you need to either place it somewhere on $PATH or specify its full path to execle(). In many Unix environments placing it in the current directory won't be enough.

Finally, it's unclear what n is in the execle() call, but the name hints at a numeric variable. You need to make sure that it's a string and not, for example, an integer.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @Sorcipuppolo: You don't need to link it to your code, you just need to produce an executable. The compiler often calls the linker by default. E.g. with gcc you simply run `gcc -o process process.c` and you'll get an executable called `process`. – NPE Dec 03 '11 at 22:09
  • I made a makefile in which i compile all my functions, i have an executable called 'process'. But i don't know if it is enough. Is it possibile to pass numeric arguments in the ececl without a cast? – Sicioldr Dec 03 '11 at 22:16
  • @Sorcipuppolo: A cast won't do you any good as it won't allocate memory for the string... you have to *convert* everything to strings. – NPE Dec 03 '11 at 22:22
  • execl ("/path/process", "/path/process", "5", NULL); – Duck Dec 03 '11 at 22:23
  • @aix: But what if i need to pass a numeric argument? (Thanks a lot anyway for the help) – Sicioldr Dec 03 '11 at 22:25
  • Just sprintf() your numeric to a string and use a pointer to the string. You are setting up the command line for the exec-ed program. All program arguments are strings. – Duck Dec 03 '11 at 22:28
  • @Duck: that's true, so i'll try to pass strings instead of integers and the path of my executable file, thanks – Sicioldr Dec 03 '11 at 22:33
-1

Well as per the answers and comments above your code should look somewhat like this

pid_t process;
process = vfork(); //if your sole aim lies in creating a child that will ultimately call exec family functions then its advisable to use vfork

if (process < 0)
{
  //fork error
  perror("fork");
  exit(EXIT_FAILURE);
}
if (process == 0)
{
  //i try here the execl
  char N[MAX_DIGITS];//A correction here
  itoa(n,N);//write this function yourself
  execl ("process", "process" , N, NULL);// Here process is the name of the executable N is your original argument
  fprintf(stderr,"execl failed\n");//check for error in execl

}
else
{
  wait(NULL);
}

Notice the use of vfork instead of fork.Its because it would be much more efficient.The reason could be found here

Community
  • 1
  • 1
bashrc
  • 4,725
  • 1
  • 22
  • 49
  • As the child writes to `n` after `vfork()` the process runs into undefined behaviour. Use `fork()` if modify any variables before `exec*()`ing. – alk Sep 30 '13 at 17:11