LinuxQuestions.org
Review your favorite Linux distribution.
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 01-12-2005, 12:09 AM   #1
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Rep: Reputation: 15
about "Create Process", how to get the return status?


with "system(COMMAND)", we can get the return status of COMMAND.

Then, what about fork, execvp, can they get the return status?

///////////////////////////////////////////////////////////////////////////
the follow is some code, but it doesn't get the return value:
///////////////////////////////////////////////////////////////////////////
int spawn (char* program, char** arg_list)
{
pid_t child_pid;

/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0)
/* This is the parent process. */
return child_pid;
else {
/* Now execute PROGRAM, searching for it in the path. */
execvp (program, arg_list);
/* The execvp function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execvp\n");
abort ();
}
}
 
Old 01-12-2005, 12:50 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Code:
//Unix version
//returns 1 on success, 0 on failure
//
int fork_exec_wait(char *cmd,       //duplicate this in argv[0]
                   char *argv[],    //last argv[] must be NULL
                   const char *type,//"", "p" = use path env. variable
                   int *result,     //can be NULL
                   int *fd_to_close_in_child,        //[in], array of fd
                   int fd_to_close_in_child_count)   //number of valid fd
{
    pid_t pid;
    int j, status;


    assert(cmd);
    assert(argv);
    assert(type);
    assert(!fd_to_close_in_child_count || fd_to_close_in_child);
    if (!cmd || !argv || !type) return 0;
    if ((pid=fork())<0) return 0;
    if (!pid) {
        //child continues
        for (j=0; j<fd_to_close_in_child_count; j++)
            if (fd_to_close_in_child[j]) close(fd_to_close_in_child[j]);
        if (strstr(type,"p")) execvp(cmd, argv);
        else execv(cmd, argv);
        _exit(127); //execv() failed
    }
    //parent continues
    while (waitpid(pid,&status,0)<0) if (EINTR!=errno) return 0;
    if (result) {
        if (WIFEXITED(status)) *result=WEXITSTATUS(status);
        else *result=255;
    }
    return 1;
}    //fork_exec_wait()
 
Old 01-12-2005, 08:02 PM   #3
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
3x
 
  


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
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
"cannot exec `as': ..." AND "C compiler cannot create ..." pr3st0 Linux - Software 0 10-15-2004 12:42 PM
kppp mdk10 ppp exit status 10 "CONNECT 44000/LAPM/" geek745 Linux - Networking 2 09-09-2004 11:04 PM
"wine: failed to create the process heap", in Fedora Core 2 crimsonmoon Linux - Software 3 06-16-2004 04:59 AM
problem with "rpm -bb" , 1 out of 1 hunk ignored ...error: Bad exit status from /v wohaolouis Linux - Software 0 03-07-2004 07:12 AM

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

All times are GMT -5. The time now is 05:21 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