LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2007, 02:18 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
not getting the correct Process ID


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
the process list
Code:
root      5559  0.0  0.0   1408   268 pts/5    S+   18:09   0:00 ./a.out
root      5560  0.0  0.0      0     0 pts/5    Z+   18:09   0:00 [vsf] <defunct>
root      5570  0.0  0.2   4064  1076 ?        S    18:09   0:00 /usr/sbin/vsftp
root      5573  0.0  0.1   4488   924 pts/4    R+   18:09   0:00 ps -aux
THANKS....

Last edited by culin; 05-02-2007 at 02:20 AM.
 
Old 05-02-2007, 02:36 AM   #2
vazrik
LQ Newbie
 
Registered: Jan 2007
Location: Southern California
Distribution: debian and fedora
Posts: 5

Rep: Reputation: 0
To avoid the defunct (zombie) process, you have to wait for the child to finish. Lookup wait and waitpid man pages.

It seems that you're successfully executing the script to bring down ftpd and bring it up again, which it does.

Hope that helps.
 
Old 05-02-2007, 03:30 AM   #3
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
thanks for reply vazrik,
but as i know, execvp() is a blocking call it wont execute the next statement untill there is a failure in execvp()..
 
Old 05-03-2007, 04:28 AM   #4
vazrik
LQ Newbie
 
Registered: Jan 2007
Location: Southern California
Distribution: debian and fedora
Posts: 5

Rep: Reputation: 0
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.
 
Old 05-03-2007, 05:07 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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'.

--- rod.
 
Old 05-03-2007, 06:18 PM   #6
vazrik
LQ Newbie
 
Registered: Jan 2007
Location: Southern California
Distribution: debian and fedora
Posts: 5

Rep: Reputation: 0
Hi Rod,

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.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Process Table-Process Control Block sarahmencer Linux - General 1 01-09-2006 09:14 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
problem with binding process and than execute another process chapa Programming 0 08-27-2003 03:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:11 PM.

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