Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-21-2009, 05:56 AM   #1
LQ Newbie
Registered: Aug 2005
Posts: 2

Rep: Reputation: 0
linux C exec fork

i have a linux pthread ...I am calling vfork and then exec another program .....But the child becomes zombie ...? please find the pseodo code inside pthread

//****************inside a pthread

exec ("/home/rat/print");

Old 03-21-2009, 08:48 AM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Not vfork() specifically, but a how-to for fork-excel you may find useful.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

int     main    (void)
        char    line [BUFSIZ];
        int     fifo [2], proc, n;

        (void) pipe (fifo);
        if ((proc = fork ()) == -1) {
                (void) fprintf (stderr, "can't fork\n");
                exit (errno);
        if (proc == 0) {        /* child        */
                 *      close the standard input and
                 *      dup write end of pipe onto it
                (void) close (1);
                (void) dup (fifo [1]);
                /*      run pwd with standard output attached to pipe   */
                (void) execl ("/bin/pwd", "pwd", NULL);
                /*      execl shouldn't return                          */
                (void) fprintf (stderr, "cannot execl pwd\n");
                exit (errno);
        /*      parent: read from pipe                                  */
        n = read (fifo [0], line, BUFSIZ);
        line [n] = '\0';
        (void) fprintf (stdout, "current directory = %s", line);
        exit (EXIT_SUCCESS);
Well, I thought about it a little more and here's a slightly simpler example; it prompts for a command, forks-and-exec's the command (until you just hit the carriage return at the prompt):
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>

int     main    (void)
        char    line [BUFSIZ];
        int     process;

        for ( ; ; ) {
                (void) fprintf (stderr, "cmd: ");
                if (gets (line) == (char *) NULL)
                        exit (EXIT_FAILURE);
                /*      create new process      */
                if ((process = fork ()) > 0)
                        (void) wait ((int *) NULL);
                else if (process == 0) {        /* child        */
                        /*      execute program                 */
                        (void) execlp (line, line, NULL);
                        /*      some problem if exec returns    */
                        (void) fprintf (stderr, "can't execute %s\n", line);
                        exit (errno);
                } else if (process == -1) {     /* can't create */
                        (void) fprintf (stderr, "can't fork\n");
                        exit (errno);
For, pretty much, any fork-and-exec this describes what you need to do so you don't wind up with zombies and the like. Check the man pages for the exec functions and adjust the arguments accordingly.

Hope this helps some.

Last edited by tronayne; 03-22-2009 at 09:40 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
fork() exec() or system() kalleanka Programming 4 09-18-2007 05:16 AM
fork/exec problem djgerbavore Programming 2 04-14-2006 12:13 AM
how to fork/ exec schneidz Programming 10 08-30-2005 12:50 PM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 03:31 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:50 AM.

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