0

i have 2 executable files. From one of them i'm calling the other one. it is also executed correctly, but does not return 0.

why?

backup.c file :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <mysql/mysql.h>


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


   if(!execl("/home/user1/exec_work/binary","/home/user1/exec_work/binary","serdar","ilter",NULL))
   {
      /* This line is not printed to the screen. */
      printf("0\n");
   }
   else
   {
      /* This line is not printed to the screen either. */
      printf("1\n");
   }

   return 1;

}

The other source file:

#include <stdio.h>
int main(int argc, char * argv[])
{

/* I see that this line is printed to the screen */
   printf("%s %s\n",argv[1],argv[2]);

   return 0;

}
4
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Duoas
0

You are using the wrong function family. exec() isn't supposed to return.

Use one of the spawn() functions instead.

Hope this helps.

I used spawnl instead of execl, but it gives compile time error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <mysql/mysql.h>

.....
spawnl(P_WAIT,"/home/user1/exec_work/binary","/home/user1/exec_work/binary","serdar","ilter",NULL);

it says:
`P_WAIT' undeclared (first use in this function)

what header file am i missing? If I put 1 instead of P_WAIT, this time it says that spawnl undeclared.

0

If you're using a POSIX system (like Linux), then you also need to read up on fork() and wait().

In pseudocode

if ( fork() == 0 ) {
  execl()
} else {
  wait()
}
0
int status;
int options;
...
switch (pid = fork()) {
        case -1: /* fork failed */
                printf("forked failed\n");
                exit(-1);
        case 0: /* child starts executing here */
                execl(...);
        default: /* parent starts executing here */
                waitpid(pid, &status, options);
                ...
}

You can then examine status, ans see how the child process has terminated,
and set options to wait for the child or not...

man waitpid for more details

0

Why type all the verbiage when you can just use spawnl()?

You need to #include <process.h> Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.