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