LinuxQuestions.org
Visit Jeremy's Blog.
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 09-18-2013, 12:04 AM   #1
Reima Cram
LQ Newbie
 
Registered: Sep 2013
Location: Manila, Philippines
Posts: 2

Rep: Reputation: Disabled
Smile Pro*C Infinite loop at WIFEXITED


Hello,

This might be noobish but here it goes.

Problem:
In a pro*C program we have it somehow do an infinite loop on the following piece of code.

Code:
int exec_cmd(cmd)
    char      *cmd;
{
    int         nPid;
    int         nStatus;
    char       *argv[10];
    int         argc = 0;

    nPid = fork();
    if (nPid > 0) {
        waitpid(nPid, &nStatus, 0);
/*problem in the while below*/
        while ( WIFEXITED(nStatus) == 0 )  
            waitpid(nPid, &nStatus, 0);
        return ((WEXITSTATUS(nStatus) > 0) ? 1 : 0);   
    } 
    else if (nPid == 0) {
        nPid = getpid();
        argv[argc++] = "/bin/sh";
        argv[argc++] = "-c";
        argv[argc++] = cmd;
        argv[argc++] = NULL;
        /*sigignore(SIGCHLD);*/
        if (execv(argv[0],argv) < 0) {
            printf("error\n");
            return 0;
        }
        return 1; 
    }
}
Initial Check:
The code above is part of a Proc*C program that basically uses sqlldr to load interface files to DB.
We were using SuSe 9 before on one server and it works just fine. This test server has Suse 11 Sp2. Upon compilation and other related configs like dos2unix was done, it somehow stops on this piece of command.
Can you guys give me an idea if internal functions like fork(), etc was changed or something? I'm really sorry that I ask this type of question. Any ideas would be appreciated.
 
Old 09-18-2013, 06:42 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I don't anything about Pro*C, but if the execv() call is similar to that in the Unix/Linux "standard" library, then if the call is successful, then calling program will never return. Thus your forked child process will never hit the "return 1" statement.

Sorry, I hit the 'save' button to quickly... I wanted to add that otherwise, your program seems fine. Perhaps you could diagnose the problem you are witnessing with debug statements or even using the debugger.

Last edited by dwhitney67; 09-18-2013 at 06:46 PM.
 
Old 09-18-2013, 09:29 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Code:
        waitpid(nPid, &nStatus, 0);
/*problem in the while below*/
        while ( WIFEXITED(nStatus) == 0 )  
            waitpid(nPid, &nStatus, 0);
        return ((WEXITSTATUS(nStatus) > 0) ? 1 : 0);
It makes no sense to me to call waitpid() in a loop: waitpid() will only return after the child process is finished, there is no use calling it again. If you're getting stuck in the loop, that indicates the child process didn't exit cleanly. I think you want something like this:
Code:
waitpid(nPid, &nStatus, 0);
if (WIFEXITED(nStatus)) {
    return WEXITSTATUS(nStatus);
} else if (WIFSIGNALED(nStatus)) {
    fprintf(stderr, "child died by signal: %d\n", WTERMSIG(nStatus));
#   ifdef WCOREDUMP
        if (WCOREDUMP(nStatus))
           fprintf(stderr, "core dumped\n");
#   endif
    return 128;
} else {
    fprintf(stderr, "this should never happen...\n");
    return 255;
}
 
Old 09-19-2013, 06:35 AM   #4
Reima Cram
LQ Newbie
 
Registered: Sep 2013
Location: Manila, Philippines
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks for your replies. will apply it to my code and try your suggestions. I will get back to you as soon as i can.

Cheers!
 
Old 09-19-2013, 10:13 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Note: I may be blind, but cannot see where any Oracle-precompiler related part is in this code.
 
  


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
[SOLVED] infinite nested while loop wolverene13 Programming 3 11-14-2012 08:32 PM
[SOLVED] infinite loop that isnt infinite? frieza Programming 2 10-27-2010 02:16 PM
infinite loop on fclose()? joe2748 Programming 10 03-05-2010 08:09 PM
Infinite Loop ewt3y Programming 3 08-16-2005 09:48 AM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM

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

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