Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
It means if we call exec() before fork(), in the calling process, this system call(exec()) simply replaces the current process with a new program and the control is not passed back to the calling process(current process will terminate). Is it right?
It means if we call exec() before fork(), in the calling process, this system call(exec()) simply replaces the current process with a new program
More or less correct, I'd say. The process will remain the same process, that is, its process ID and other properties won't change. However (quoted from the execve man page; my emphasis):
Quote:
execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded.
Quote:
and the control is not passed back to the calling process(current process will terminate)
This is not correct. The process doesn't terminate, but code and data will be thrown away and replaced by code and data from the exec'd file.
More or less correct, I'd say. The process will remain the same process, that is, its process ID and other properties won't change. However (quoted from the execve man page; my emphasis):
This is not correct. The process doesn't terminate, but code and data will be thrown away and replaced by code and data from the exec'd file.
I agree with this. Exec will not return on success. When a child which has called exec() completes the process, it does so either successfully or not and then a signal is sent to the parent. Thus whatever process you are running, it does have a parent, perhaps that would be a command shell if you ran it from there, and so a signal indicating that the child process was complete is sent to that parent.
Are you just asking to get a better understanding of this?
In the below program, I used execlp() call before fork() and this is what i seen in the output.
Here the process control exits the program after executing without creating the child process. Is my understanding correct?
------Output of the below program-------
Parent process, PID 1913
Desktop Downloads fork-exec1.c homework.c Pictures Templates
Documents fork-exec1 homework Music Public Videos
In the future, and you can also edit above, use [code][/code] tags when you post code. It retains the spacings and makes it easier for someone to review you code. There are links in the LQ FAQ as well as a link in my signature regarding this.
No one has said that a call to exec() would not work, and that result is highly expected, hence why the fork() never occurs, because your sort of branch away from it to never return.
Is there a question? I do recognize that this appears to be an assignment, which is not a problem. Given the information you've been told to this point, why not change the program by way of removing that first call to execlp(), it doesn't really need to be there anyways, you have one in the child case and you're processing the signal from the child appropriately in the parent case.
In the below program, I used execlp() call before fork() and this is what i seen in the output.
Here the process control exits the program after executing without creating the child process. Is my understanding correct?
------Output of the below program-------
Parent process, PID 1913
Desktop Downloads fork-exec1.c homework.c Pictures Templates
Documents fork-exec1 homework Music Public Videos
The execlp call replaces your code by /bin/ls, which duly prints the content of the current directory. The code after the execlp, from the fork to the return including the switch, is thrown away and never executed.
Yes, the process exits after executing /bin/ls and never creates a child.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.