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);

    }

}
user2501289
  • 49
  • 1
  • 8

2 Answers2

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.

Maurizio Benedetti
  • 3,557
  • 19
  • 26
  • 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 – user2501289 Apr 25 '14 at 08:57
  • 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. – Maurizio Benedetti Apr 25 '14 at 09:21
  • 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 – user2501289 Apr 25 '14 at 09:31
  • Sorry for the stupid question, have you tried to call it as execvp("./client",arguments) ? – Maurizio Benedetti Apr 25 '14 at 10:13
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);
    }

}
Vamshi
  • 437
  • 6
  • 14