nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Help with execvp function in C program !

This is a discussion on Help with execvp function in C program ! within the Coding in General forums, part of the Development/Scripting category; I knew that execvp exec a child process with its argument list. Code: int main () { /* The argument ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Linux answers from nixCraft.


Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 16-06-2009, 04:58 PM
Junior Member
User
 
Join Date: Apr 2008
OS: Fedora 8
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
demuytree is on a distinguished road
Default Help with execvp function in C program !

I knew that execvp exec a child process with its argument list.
Code:
int main ()
{
  /* The argument list to pass to the “ls” command. */
  char* arg_list[] = {
     “ls”,     /* argv[0], the name of the program. */
     “-l”,
     “/”,
     NULL      /* The argument list must end with a NULL.  */
  };
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0)
  /* This is the parent process. */
  return child_pid;
else {
  /* Now execute PROGRAM, searching for it in the path. */
  execvp (ls, arg_list);

  fprintf (stderr, “an error occurred in execvp\n”);
  abort ();

  printf (“done with main program\n”);
  return 0;
}
But if I want to execute my shell or php script in exec
Code:
phpPATH=/usr/local/bin/php
scriptPATH=/usr/local/apache/htdocs/myweb/myscript.php
I've failed with
Code:
char* arg_list[] = {
     “phpPATH”,
     “scriptPATH”,
     “scriptArgument”,
     NULL      /* The argument list must end with a NULL.  */
  };
----------------
execvp(phpPATH,arg_list)
How can I use execvp with my own script ?
Thanks in advanced !
Reply With Quote
  #2 (permalink)  
Old 16-06-2009, 11:52 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Is it Linux or any other UNIX box? Also, do you get any specific error code or message? For further debugging try
PHP Code:
strace ./a.out
strace 
./yourapp 
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help

Last edited by nixcraft; 17-06-2009 at 12:08 AM.
Reply With Quote
  #3 (permalink)  
Old 17-06-2009, 12:12 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Here is my test.c
PHP Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
   
pid_t pid;
   
char *const parmList[] = 
   {
"/usr/bin/php""/tmp/test.php"NULL};

   if ((
pid fork()) == -1)
      
perror("fork() error");
   else if (
pid == 0) {
      
execvp("/usr/bin/php"parmList);
      
printf("Return not expected. Must be an execvp() error.\n");
   }

Here is my test.php
PHP Code:
#!/usr/bin/php
<?php
    
echo "Test\n";
?>
Output:
Code:
./test
Test
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #4 (permalink)  
Old 17-06-2009, 01:16 PM
Junior Member
User
 
Join Date: Apr 2008
OS: Fedora 8
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
demuytree is on a distinguished road
Default

thank you so much. IT's working !
Reply With Quote
Reply

Tags
c program , execvp , execvp replaces the calling process , gcc , linux , unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
fattach function which package sathiya Linux software 2 02-06-2008 05:41 PM
Adding storage function to a mail server satimis Databases servers 0 04-01-2008 01:15 PM
is any one good in c program ? oronno Shell scripting 0 19-09-2007 01:04 PM
start program schaapmansz Getting started tutorials 1 26-06-2007 09:49 PM
call function in bash mala_un Shell scripting 9 18-08-2006 01:36 AM


All times are GMT +5.5. The time now is 06:59 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38