System call failed with 127 .. after 500 times when called in loop


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users System call failed with 127 .. after 500 times when called in loop
# 1  
Old 01-09-2012
System call failed with 127 .. after 500 times when called in loop

Hi Experts,

I have a code like this.

=====
Code:
#include.... 

int main()
{ 
  int count = 0;
  while(1){
    
    printf("\n Interation number is: %d \n ",count);
    rv = system(" test.sh > log.txt " );

    if (-1 == rv)
    {
        printf("Could not generate static log: error calling system()");
    }
    else if (WEXITSTATUS(rv) != 0)
    {
        printf("\nProblem generating static log: shell script returned %d \n", WEXITSTATUS(rv) );
        
    }
     count ++;
    .
    .
    .
    //some other code comes here
    .
    .

  } //while ends
}

Content of the test.sh is.
Code:
#!/bin/sh

echo "HIIIII"
ls -lrt
ps -ef

Problem is that, after this code is looped for 500+ times, the error happens
Problem generating static log: shell script returned 127

In the meantime when the error is happening, if you execure this manually on the shell, i.e. test.pl > log.txt .. it works fine..
ONLY WHEN IT HAPPENS THROUGH SYSTEM CALL, it fails

after failing for 30 times, it resumes again successfully.

I have tried to replace system() with popen(), but that also gives same behavior.

What would be the reason? Is it that the fork() ed child processes by these two functions (internally) did not free the resources?
did /bin/sh fail to launch? Did the system have no memory to fork?(if so, why did it resume after 30 continuous failures?

I am looking for your suggestions and comments.

Thanks for the help and your time,
Binny Jeshan,
Senior Software Designer,
Aricent.

Last edited by binnyjeshan; 01-20-2012 at 03:04 AM.. Reason: typing error rectified; please use code tags
# 2  
Old 01-09-2012
Quote:
while{
What is the while condition here? Is it while(true)?
If so, the problem may be the delay caused by execution of the script.
You call the script 500 times, but it cannot catch up; I would think the commands are being fork()-ed, which takes some time, and resources need to be allocated. The forked processes don't have the time to resolve, and more and more forks are requested. Then, as the system cannot do any more, execution fails. After some 30 loops, the system catches up executing the script and resources are freed to continue the mad race.
There is a limit to how many forks can your program do at one time. This may be the culprit.
This is just my vague idea of what's happening. I am looking forward to read some more accurate answers Smilie
# 3  
Old 01-09-2012
I don't think the number of processes would limit it. system() waits for the process it creates to complete, and nothing inside that script ought to be leaving anything behind.

That doesn't mean fork isn't failing -- I just can't imagine why it would be.

Unless you actually want it to buffer, everything you print with printf() should end with \n, not begin with it, since an \n on the end will cause it to print immediately and not buffer.

You might try perror("error code was "); before you print the exit status, to see if errno contains anything illuminating after system() fails.

---------- Post updated at 10:58 AM ---------- Previous update was at 10:54 AM ----------

A process dying with code 127 usually means it was killed by a signal of some sort. Something from the outside may be killing them, or they may be exceeding some sort of resource limit and being killed.
# 4  
Old 01-10-2012
Tools

Hi all,

The question is only WHY it fails after say 500 times.. During this time i found some references regarding fork: resource unavailable, but i dont think these will solve the problem that i face.

@ MIRNI: You could be right..But for every one cycle, the system call after it returns back, it ensures that it checks the exit status of the child, and frees up the resources it takes.


In the man page i see this statement like this..
As mentioned, system() ignores SIGINT and SIGQUIT. This may make programs that call it from a loop
uninterruptible, unless they take care themselves to check the exit status of the child. E.g.

while(something) {
int ret = system("foo");

if (WIFSIGNALED(ret) &&
(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
break;
}

But i feel the above statement should now be same as popen. SO i think the issue should be with fork since both these utilities have it as common.

Supposedly as told in man page (and even i call it in loop), if the exit status was not fine, and after continuous calls it failed, if we find it in between, what action to take? instead of the "break" above, should i manually set the return status and clean something? I am not sure.

Please share your ideas now.

And i dont think the "\n"s added here impacts the functionality of the system (). Tested it.

Regards,
Binny.
# 5  
Old 01-10-2012
Did you try Corona's suggestion with perror()? Does it run successfully if you slow down the loop with usleep(1000) or something of the sort?
# 6  
Old 01-11-2012
Is it exactly every 500 times, or does it just happen "occasionally"? It may be your system is low on resources in general...
# 7  
Old 01-13-2012
For the first 500 times, its fine.. after that its not working. unless u reboot and try again to enter the condition.

The number is not exactly 500.. .it sometimes is 487, 510 .. 505 etc..

Yes i also suspect that its resource unavailable... But Why? If system call cleaned up all child process resources that it internally created to do the shell command, then when it returns back, we expect no memory leaks or 'defunct'-ed processes.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk call in bash function called with arugments not working, something lost in translation?

Hello, I have this awk code in a bash script to perform a find and replace task. This finds one unique line in a file and substitutes the found line with a replacement. #! /bin/bash # value determined elsewhere total_outputs_p1=100 # file being modified... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Bash script from makefile - it is called each time i call make

I've created a tag in the makefile: mytag: $(shell ${PWD}/script.sh) When i do: make clean - the script is executed When i perform make or make mytag the script is again executed with the output: make: Nothing to be done for mytag What i want ? I want script.sh to be executed only... (0 Replies)
Discussion started by: Pufo
0 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. UNIX for Dummies Questions & Answers

shell program- how many times a function is called

We have a program source C and is required to indicate how many times each function is called from the C program. also print the line number where there is a call. I've tried something like this: #!/bin/sh for i in $*;do if ! then echo $i is not a C file. else echo $i... (0 Replies)
Discussion started by: oana06
0 Replies

5. UNIX Desktop Questions & Answers

for loop (001 to 500)

hey, how do i create a for loop that runs for i from 001 to 500 ? i need that the zero prefix will remain so when i print "i" it will look like so: 001 002 . . 008 009 . . 058 059 . . 500 please advise. (2 Replies)
Discussion started by: boaz733
2 Replies

6. Programming

parent called more times - fork - C language

Hi gurus can you explain following lines of code ? #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv; switch(pid = fork()) { case -1: ... (7 Replies)
Discussion started by: wakatana
7 Replies

7. Shell Programming and Scripting

To call a Oracle sql file 1000 times

Hi Guys, Even though, i have called db2 sql file earlier, this is my first time to call a oracle sql file. I need to make a database(oracle) connection and then call the sql file in a loop. Can you please help me out. Thanks for your help and time. Regards, Magesh (4 Replies)
Discussion started by: mac4rfree
4 Replies

8. Shell Programming and Scripting

call a passwd file to a script multiple times

Hello everybody, I have a requirement in my script.. When i'am executing a script, it'll ask a passwd of some service account.. I need to pass it to the script through a zipped file when it asks for it. The script can be executed by more people many number times. So for securty purpose, it... (1 Reply)
Discussion started by: raghu.iv85
1 Replies

9. BSD

Deny logon for x hours if login failed x times

Hello, I have a small inquiry. Sometimes, my good friend, Charlie Root, sends me security notifications that a possible breakin attempt has occured. It looks like this: Oct 29 06:58:17 cigva sshd: reverse mapping checking getaddrinfo for 180.144.164.220.broad.sm.yn.dynamic.163data.com.cn ... (2 Replies)
Discussion started by: brightstorm
2 Replies

10. UNIX for Dummies Questions & Answers

Session request failed (Called name not present)

I have the following shell script to map a drive to a windows machine: echo Enter password: stty -echo read passwd stty echo mount -t smbfs -ousername=myusername,password=$passwd //192.168.2.5/sharename /mnt/mountname It works fine and the share mounts, but for some reason I get this... (4 Replies)
Discussion started by: Spetnik
4 Replies
Login or Register to Ask a Question