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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-04-2012, 05:31 AM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Rep:
|
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.
|
|
|
07-04-2012, 11:23 AM
|
#2
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
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 11:27 AM.
|
|
|
07-04-2012, 11:05 PM
|
#3
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Original Poster
Rep:
|
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.
|
|
|
07-04-2012, 11:07 PM
|
#4
|
LQ Newbie
Registered: Jul 2012
Posts: 3
Original Poster
Rep:
|
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.
|
|
|
07-05-2012, 07:16 AM
|
#5
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
Quote:
Originally Posted by Anshu Tomar
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 07:23 AM.
|
|
|
All times are GMT -5. The time now is 02:00 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|