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.
Hi all,
below is the piece of code i am working on...i have some doubts regarding this....what i wanted from this code is, it has to print the process ID of the vsftpd that is running ( which is started by me by doing execvp(); )
Code:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
int main()
{
char *arg[]={ "VSFTPD",
"restart",
NULL };
int child_pid,*status;
child_pid=fork();
if (child_pid == 0)
{
//sleep(5);
execvp("/etc/init.d/vsftpd", arg);
fprintf (stderr, "\n an error occurred in execvp\n\n\n");
}
else
{
sleep(5);
printf("the child_pid is %d\n",child_pid);
}
return 0;
}
but the problem what is have is when i do an execvp() inside the child it is starting 2 processes named vsftpd one is /usr/sbin/vsftpf(the pid of which i am expecting to get printed ) and the other is <defunct vsftpd>... but it is printing the pid of the <defunct vsftpd> which will not be in the process list after 5 seconds... so how do i get the pid of the /usr/sbin/vsfptd by using this piece of code...??? please gimme ur suggestions.. and please tell me why this <defunct vsftpd> is coming.....???
output of my program..
Code:
[root@localhost ~]# ./a.out
Shutting down vsftpd: [ OK ]
Starting vsftpd for vsftpd: [ OK ]
the child_pid is 5560
I hope you've solved your problem by now, but just in a remote case ...
Your exec() (and variations of it) creates a child process, and initiates it, then it returns. From that point on, the child process and the parent process run asynchroneusly (sp?) with respect to each other. Therefore, your parent process (just before the return) must wait for the child process to finish in order to not create a zombie. I am a little foggy on these, since I have not done these for some years, but that is basically the jist of it. I am foggy in particular about the initiation part. The best I remember (from Solaris), I think it sets it to runnable and then returns. At which point the parent continues executing to the end of its time-slice or
At any rate, if the parent exits (terminates) before the child, then the child process becomes zombie.
Actually, exec() family calls do not return. They simply exit. You are correct that you must call wait() on those child processes to prevent that nasty 'zombie buildup'.
Thank you for "making" me read and refresh my memory :-)
You are absolutely right about exec() not returning. It overrides/replaces the running process memory image with that of the new code.
However, since you do fork() and then exec() in the child process, the parent process continues to run as well. You execute the exec() in the child process after forking.
I wonder, if you don't fork at all, would you still get the zombies? According to my understanding, you should not.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.