LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-21-2009, 05:56 AM   #1
karana
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

if(vfork()==0)
{
exec ("/home/rat/print");
_exit(0);
}

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

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
Not vfork() specifically, but a how-to for fork-excel you may find useful.
Code:
#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);
}
[EDIT]
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):
Code:
#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.
 
  


Reply


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


All times are GMT -5. The time now is 12:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration