Question

I am writing a C program in linux.

I have created a process and forking it to create a child. I want to run another program in this child so I am using execlp. But, this program must run in an independent window.

if ( (execlp("xterm","xterm","-e","./Child1", "127.0.0.1", (char *) 0)) < 0)  {
  printf("Failed to Start the Echo Client. Exiting application.");
  return 1;
}

Child1.c is a simple program which is in the same directory as my current file.

On execution the code runs fine with the xterm window coming up but I get an error "xterm: Can't execvp: No file or directory"

Can you please suggest me a resolution?

Was it helpful?

Solution

Your system might not have an xterm installed (or the user has a wrong PATH). You could test the existence of /usr/bin/xterm (with e.g. the access syscall), or use something else. For example, many Debian or Ubuntu distributions have a x-terminal-emulator (which is usually a symlink to some precise program) which you could use instead of xterm.

If xterm fires up, then your program ./Child1 does not exist, you should test its existence (with access) before. If you only have a source code ./Child1.c you should have compiled it before (perhaps by running a system("gcc -Wall Child1.c -o Child1") before and testing that it has compiled successfully, i.e. test that system returns 0 and that the Child1 file exists and is executable).

You should take time to read the xterm(1) man page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top