1

I am trying to execute program (let's call that program2) that I wrote from another program.

I compile program2 and have executable file call client.

I want to run program2 and for that I create process fork for trying to execute program2 by using the exec function.

The program needs two arguments run.

I use:

execvp("client",arguments)

Where arguments is a NULL-terminated char*-array, and arguments[0] is "client". The program fails in the exec operation with error No Such File Or Directory.

In other words, how do I run my code from another program using the exec function?

code:

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

struct Integrals* shm_ptr; 
key_t key;      
int i,status;
int shm_id;
pid_t pid;  
int child_pid[PROCESS];
char  curPid[10];
int curpid;
double calcSegment[SIZE];
int from,to;
double segment;

curpid=getpid();
sprintf(curPid,"%d",curpid);

char* arguments[]={"client",argv[1],curPid,NULL};

signal(SIGUSR1, sig_handler);//"install" the 'sig_handler' func in case ^C signl.

printf("---------->%s",arguments[0]);
key=ftok("\tmp",(char)argv[1][0]);

if( (shm_id=shmget(key,sizeof(Integrals),IPC_CREAT|0600))==-1)
{
    perror("Fail To Allocate Shared Memory");
    exit(1);
}


if((shm_ptr=(Integrals*)shmat(shm_id,NULL,0))<0)
{
    perror("Fail To Attach Memory");
    exit(1);

}


pid=fork();

if(pid<0)
{
    perror("The Fork Failed");
    exit(1);    
}

else if(pid==0)
{
    if(execvp("workspace/hafala/EX2/client",arguments)==-1)
    {
        perror("Faile To Execute 'client.c' Program");
        exit(1);

    }

}
10
  • Why does the program fail? Check errno. Commented Apr 25, 2014 at 7:23
  • the program fail in "No Such File Or Directory" Commented Apr 25, 2014 at 7:32
  • give us a strace -s 200 -f -o ./program1 output... Commented Apr 25, 2014 at 7:33
  • Do you have a program called client in your PATH? Commented Apr 25, 2014 at 7:33
  • The client is an exectubale file that located in the same directory of the current program(the program that trying to run the program) what path i need to add ? and how? Commented Apr 25, 2014 at 7:37

2 Answers 2

0

Try to call the external program with the full path instead of just the exec name.

It looks like a very simple problem of relocation of the executable file.

At least I would check there. Another solution is to move the client application in the same directory of the caller of change the current directory context.

Sign up to request clarification or add additional context in comments.

4 Comments

The client program is in the same directory. because of that i use execvp(".",arguments) now the problem is "Permission Denied" I tried do on program 2 "chmod +x client" but still same erroe of permission
Permission denied could depend on the fact that the user running the process you are developing is not able to "read" the called (client) program. Eventually try with a chmod o+r or make sure your user is the owner of part of the group owner.
This is The permissiond that i have by run ls - l: prog 1:(from her i trying to run prog 2): -rwxrwxr-x 1 prog 2:(the client) -rwxr-xr-x 1
Sorry for the stupid question, have you tried to call it as execvp("./client",arguments) ?
0

I think the error is in the 1st parameter which is not matched with the program you specified

Try copying the client executable file to the same directory and change the code to this.

char* arguments[]={"./client",argv[1],curPid,NULL};

else if(pid==0)
{
    if(execvp("./client",arguments)==-1)
    {
        perror("Failed To Execute 'client.c' Program");
        exit(1);
    }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.