LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-08-2015, 09:21 PM   #1
ashokr
LQ Newbie
 
Registered: Nov 2015
Location: Keene,NH
Posts: 4

Rep: Reputation: Disabled
Can we use exec() before fork() call?


What would happen if we put exec() before fork() call?
 
Old 11-08-2015, 09:39 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You would execute the new executable and never call fork.
 
Old 11-08-2015, 10:10 PM   #3
ashokr
LQ Newbie
 
Registered: Nov 2015
Location: Keene,NH
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks berndbausch,

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?
 
Old 11-08-2015, 11:25 PM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by ashokr View Post
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.
 
Old 11-09-2015, 07:47 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by berndbausch View Post
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?

Or do you have a particular paradigm in mind?

Last edited by rtmistler; 11-09-2015 at 07:48 AM.
 
Old 11-09-2015, 07:59 AM   #6
ashokr
LQ Newbie
 
Registered: Nov 2015
Location: Keene,NH
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks berndbausch & rtmistler.

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


------Program------------------------

#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
pid_t parentpid = getpid();
pid_t childpid;

printf("Parent process, PID %i\n", parentpid);
execlp("/bin/ls","ls", NULL);
childpid = fork();

switch(childpid) {
case -1:
/* error forking */
printf("Error during fork() command\n");
break;

case 0:
/* child process */
printf("Child\n");
execlp("/bin/ls","ls", NULL);
printf("Child done\n");
break;

default:
/* parent process */
printf("Parent %i\n", childpid);
wait(NULL);
printf("Finishing parent...\n");
break;
}


return 0;
}
 
Old 11-09-2015, 08:05 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Couple of things:
  1. 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.
  2. 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.
  3. 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.

Last edited by rtmistler; 11-09-2015 at 08:09 AM.
 
Old 11-09-2015, 09:53 PM   #8
ashokr
LQ Newbie
 
Registered: Nov 2015
Location: Keene,NH
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks everyone, I now understood how exec() system call works.
 
Old 11-09-2015, 10:31 PM   #9
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
To finish this off:

Quote:
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Fork + Exec + ? Question! Blue-Knight Programming 2 04-10-2015 02:52 PM
linux C exec fork karana Linux - Newbie 1 03-21-2009 07:48 AM
Fork exec problem bhupeshchawda Programming 3 04-19-2007 04:58 AM
how to fork/ exec schneidz Programming 10 08-30-2005 11:50 AM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 02:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration