LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-04-2012, 06:31 AM   #1
Anshu Tomar
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
Question Ping command hangs indefinitely i.e.system(ping -w 10 [ip address])


I tried to use system(ping -w 10 192.168.4.65).If I write a small program to do the same,things work fine.The status is returuned after the deadline.
But, if i want to use the same command in my project code, it hangs for indefinite time.It keeps running and does not return after the deadline gets over.
To run the command in my code, I used a new thread which call for a function, which further calls this command.Please help me out.
The code when the command is run in an individual program is given below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OUTFILE "out.txt"
using namespace std;
char *filepath = "/home/CardInfo.txt" ;
int main()
{
FILE *c_fp;
char temp[80];
char addr[80];
char ipAddr[80];
char floatIpAddr[80];
char broadcastAddr[80];
int i,ping_ret,result,nodeId,nodeType;
if((c_fp = fopen(filepath,"r")) == NULL)
{
printf("FILE COULD NOT BE OPENED\n");
exit(1);
}

while(fscanf(c_fp,"%d%d%s",&nodeId,&nodeType,ipAddr)!=EOF)
{
fscanf(c_fp,"%s",floatIpAddr);
fscanf(c_fp,"%s",broadcastAddr);
}
snprintf( temp ,80, "ping -w 10 %s >> %s" , floatIpAddr,OUTFILE);

do{
i = system( temp );
ping_ret = WEXITSTATUS(i);
printf("Ping i is :%d\n", i);
printf("Ping WEXITSTATUS ping_ret:%d\n", ping_ret);
}while(ping_ret!=0);

return(0);
}

This code works fine but if the same code is embedded in other code to use the ping command there,the command does not return.The little detail of the code where i have used this command is also given :

This function runs in new thread of the concernec code.
void MSHeartBeat::doSendServiceChangeToMS()
{
FILE *c_fp;
char pingString[80],ipAddr[20],floatIpAddr[20];
int status,ping_ret,nodeId,nodeType;

do
{
if((c_fp = fopen(c_file,"r")) == NULL)
{
exit(1);
}

fscanf(c_fp,"%d%d%s%s",&nodeId,&nodeType,ipAddr,floatIpAddr);
snprintf( temp ,80, "ping -w 10 %s >> %s" , floatIpAddr,OUTFILE);

do{
cout <<"PINGING..TRYING"<<endl;
cout<<"pingString is "<<pingString<<endl;
status = system( pingString );
cout<<"ping status is "<< status <<endl;
ping_ret = WEXITSTATUS(status);
cout<<"ping_ret is "<< ping_ret <<endl;
}while(ping_ret!=0);


I get the prints till "pingString is" and no print after that.I dont get "ping status is" print even once. The only difference between individually running the ping program and embedding it in some other program is that it later case a new thread has been started for it.
 
Old 07-04-2012, 12:23 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Something you may want to try is to use the count option (-c 1, say) to ping. ping will do one ping and exit. Here's an example of one ping to a good address but the device is turned off:
Code:
fubar: ping -c 1 pita
PING pita.com (192.168.1.30) 56(84) bytes of data.
From fubar.com (192.168.1.10) icmp_seq=1 Destination Host Unreachable

--- pita.com ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
# check the exit status
fubar: print $?
1
Here's an example of one ping to a good address where the device is turned on:
Code:
fubar: ping -c 1 InkJet
PING InkJet (192.168.1.15) 56(84) bytes of data.
64 bytes from InkJet (192.168.1.15): icmp_req=1 ttl=64 time=6.71 ms

--- InkJet ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 6.714/6.714/6.714/0.000 ms
# check the exit status
fubar: print $?
0
That is, the failure returns 1 and the success returns 0.

If you're using the system() function,
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void    main    (void)
{
        int     i;

        i = system ("ping -c 1 pita");
        if (WEXITSTATUS (i))
                exit (EXIT_FAILURE);
        exit (EXIT_SUCCESS);
}
you'd want to exit the program when the return status != 0:

Hope this helps some.

Last edited by tronayne; 07-04-2012 at 12:27 PM.
 
Old 07-05-2012, 12:05 AM   #3
Anshu Tomar
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Tronayne for the reply. But the problem is that I have already tried with -c 1 option too.Things does not work. Even when I stop the process under which this ping thread has been created and I do "ps -ef|grep ping" after that, it shows me ping command still running.The outcome of the "ps -ef|grep ping" is as follows:
When tried with -w 20 option:
otcaop 721 1 0 09:43 pts/10 00:00:00 sh -c ping -w 20 192.168.149.238 >>/tmp/pingOutput.txt
otcaop 723 721 0 09:43 pts/10 00:00:00 ping -w 20 192.168.149.238
root 8418 4709 0 09:59 pts/10 00:00:00 grep ping
root 16682 1 0 09:52 ? 00:00:00 gvim pingCheck.cc
root 17165 1 0 Jun15 ? 00:00:00 /usr/libexec/mapping-daemon

When tried with -c 1 option:
otcaop 721 1 0 09:43 pts/10 00:00:00 sh -c ping -c 1 192.168.149.238 >>/tmp/pingOutput.txt
otcaop 723 721 0 09:43 pts/10 00:00:00 ping -c 1 192.168.149.238
root 8418 4709 0 09:59 pts/10 00:00:00 grep ping
root 16682 1 0 09:52 ? 00:00:00 gvim pingCheck.cc
root 17165 1 0 Jun15 ? 00:00:00 /usr/libexec/mapping-daemon

Problem is that ping command starts running indefinitely irrespective of the -w or -c options given. It needs to be killed manually.
 
Old 07-05-2012, 12:07 AM   #4
Anshu Tomar
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Ping has to do anything with user?
Because the project in which I want to use the command has different processes running with different user. Some as root and some as other user.
 
Old 07-05-2012, 08:16 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Quote:
Originally Posted by Anshu Tomar View Post
Ping has to do anything with user?
Because the project in which I want to use the command has different processes running with different user. Some as root and some as other user.
Nope. Any user can execute ping.

Strange that ping doesn't die with -c 1 (that just should not happen).

Try removing the -w from the command line, use only the -c 1 option; the ping manual page says,
Quote:
-w deadline
Specify a timeout, in seconds, before ping exits regardless of how many
packets have been sent or received. In this case ping does not stop after
count packet are sent, it waits either for deadline expire or until count
probes are answered or for some error notification from network.
I'm looking at a couple of alternatives, get back to you if I can figure something out.

Last edited by tronayne; 07-05-2012 at 08:23 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
I cannot ping with command 'ping IP' address but can ping with 'ping IP -I eth0' sanketmlad Linux - Networking 2 07-15-2011 06:32 AM
[SOLVED] ping www.google.co.in gives unknown host error ,ping to LAN address works fine aspiring_stellar Linux - Newbie 10 05-24-2011 04:26 PM
can't ping local IP address but can ping remote hosts rob_xx17 Linux - Networking 4 12-02-2006 09:39 AM
Simple question about command syntax ping/log ping results ohalnet Linux - Networking 1 07-25-2006 05:46 AM
windows 98 m/c ping to ip address of red hat server but fails to ping hostname ravilohot Linux - Networking 2 09-07-2004 05:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 AM.

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