ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.