ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
execlp() is being called in the parent and the child. I have no idea what f_fork() is, but normally you'd do something like if(!fork()) { execlp(whatever); } else { printf("\tworld \n"); }
That way execlp() is only called in the parent. fork() returns the PID of the child process to the parent, but always returns 0 to the child.
Originally posted by schneidz
how to make exec return and continue running my program.
You cannot make execlp() "return". It only returns if there was an error executing the new program.
execlp() replaces the current process with the new program to execute. The program that the process was running is removed from memory, and does not come back.
The only way around this is to first create another process using fork(). Then call execlp(), but only in the child process. This child process is then running "ls" instead of your program. The "ls" program will of course not print "World", so you'll need to do that in the parent process, which should still be running the original program.
fork() duplicates the current program in a new process. So after fork() 2 processes are running concurrently. This means the last message ("World") printed by the parent process may appear in the middle of the output of "ls -a". If you do not want this to happen, you can wait for the child to finish, and then print "World". You can use wait() for a child process to finish. See "man 2 wait" for more info.
This "fork() a child proces, exec() another program, and wait for it to finish" is needed quite often. Therefore there is a library function that does all of this at once: system()
Here's an example how use system() to do what you are trying:
Actually it's probably similar to the old vfork which would not copy the parent's pages and page tables to the child (since you're just going to overlay them with an exec call anyhow). Modern systems that implement proper copy on write and don't need to copy pages and page table entries don't need such hints.
the aix man page says to use f_fork if an exec is called directly after. i assumed it was to help return to the calling program.
take care,
schneidz
Just some advice since I had to deal with this issue a few months ago. system() is generally inefficient and can be dependant on the specific installation. system() actually invokes a new shell to execute the given command. For something like ls -a this is overkill. It is better to use an exec() variant. The last time I used system() was to execute something like ls *.mp3 and I couldn't figure out how to invoke regexp expansion using exec, so I used system instead.
Originally posted by mvan83
The last time I used system() was to execute something like ls *.mp3 and I couldn't figure out how to invoke regexp expansion using exec, so I used system instead.
FYI: You can find more about wildcard expansion in "man 3 glob" and "man 3 wordexp".
Originally posted by Hko FYI: You can find more about wildcard expansion in "man 3 glob" and "man 3 wordexp".
Thank you. I did not know of those functions (although I guessed something like them existed). This seems to be a problem with a lot of people (including myself obviously). We need a function that does something common, but don't know what it's called or how to find it easily.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.