Linux - NewbieThis 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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
#define OUTFILE "out.txt"
using namespace std;
char *filepath = "/home/CardInfo.txt" ;
if((c_fp = fopen(filepath,"r")) == NULL)
printf("FILE COULD NOT BE OPENED\n");
i = system( temp );
ping_ret = WEXITSTATUS(i);
printf("Ping i is :%d\n", i);
printf("Ping WEXITSTATUS ping_ret:%d\n", ping_ret);
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.
if((c_fp = fopen(c_file,"r")) == NULL)
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;
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.
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
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
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,
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.