LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-29-2009, 07:54 AM   #1
jimcftsai
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Rep: Reputation: 0
Get notified in Linux C program as Internet connection becomes available/unavailable


In a moving environment, the Internet connection is not stable. Is there a way to get notified in Linux C program as the connection state changes?
 
Old 08-30-2009, 12:53 AM   #2
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
Network manager have a dbus interface, it would be the right way to do it. You can also use

if (system("ping www.google.com"))
printf("failed\n");

as it will not return 0 (the right value if it work)
 
Old 08-30-2009, 09:03 AM   #3
jimcftsai
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
is there a lightweight way to know the state change?

Quote:
Originally Posted by Elv13 View Post
Network manager have a dbus interface, it would be the right way to do it. You can also use

if (system("ping www.google.com"))
printf("failed\n");

as it will not return 0 (the right value if it work)
Thanks a lot for the answer.

. Network Manager dbus interface
In addition to standard Linux, it seems that one should run another network manager to have the dbus interface. If so, I worry it will create much additional computing demand.

. system ping
This is a good idea. However, it also involves invocation of sh, fork, exec, and ping. It also demands much additional resources.

I am wondering if there is a simple way to register or probe Linux network.
 
Old 08-30-2009, 01:44 PM   #4
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
NetworkManager is used by all distributions by default (except Gentoo and Arch where, as everything, it is optional). It is the cleanest way to do it.

About ping, yes, it involve some delay (10 second if the network is not present). The fork itself is not that demanding, nor sh (if it is truly SH, if it is bash then, yes, it will slow things down). The problem is ping itself. You could of course make a unix socket and use the ping protocol by yourself, but it would be a waste of effort. NetworkManager is how every modern applications do it.
 
Old 08-31-2009, 07:03 AM   #5
jimcftsai
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
NetworkManager is computing demanding

After starting NetworkManager, I found that it is computing demanding.

NetworkManager takes about 20% computing power continuously
dbus-daemon takes about 10% computing power continuously

It seems that ping is the less-expensive way to know the network connection state.
 
Old 08-31-2009, 08:44 AM   #6
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
Unless you have a pentium 100mhz, you have a problem in your configuration, NetworkManager is even used in some cell phones to manage network.
 
Old 09-01-2009, 07:50 PM   #7
jimcftsai
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
My test was on a 3GHz Pentium 4. It is far beyond the performance of pentium 100MHz. It takes 30% of the Pentium 4 computing power. I am just wondering how can it be used in cellular phone platform where the computing power should be much lower. Maybe, there is some other way to start NetworkManager in lightweight mode.

In a GPRS mobile communication environment, the communication link may be broken due to lack of base station. The communication connection could be re-established if it moves to a area that has base stations coverage again.

As we know, Linux supports network. There should be somewhere inside Linux that it knows the network connection is down or up. Furthermore, maybe a program can register and then get notified as connection state changes.

In my test, ifconfig and netstat does not reflect the change immediately. How can an application know the connection state change in a realtime manner?
 
Old 09-01-2009, 08:10 PM   #8
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
I've experienced some "issues" with Network Manager but one thing I have never seen it do is use much cpu cycles. Usually when running top it is not even on the screen. In fact I just tested it now, running top while turning my wifi adapter off and back on, and NM and the user space nm-applet together never used more than 1.6% cpu. And this is on a 1.4Ghz Pentium M.

Something is wrong with your NM configuration if it using the cpu cycles you report.
 
Old 09-01-2009, 10:38 PM   #9
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
30% cpu is not normal, it is not configured correctly if it use that much power. And about network link, it is handled by network manager. It is how Linux does it. Before that, the connection was static and configured with text files (/etc/network/interfaces) and was activated or unactivated using the ifup/ifodwn command manually. The system itself used a script (/etc/init.d/networking) to start or stop all interfaces. Nothing was dynamic so there is not API. NetworkManager introduced these API and applications now use them to check network availability. KDE platform also have one called "Solid", but it rely on NetworkManager too on Linux (and native API on OSX and Windows).
 
Old 09-02-2009, 08:55 AM   #10
jimcftsai
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
My test did not consider any configuration issues. I just ran NetworkManager in background inside /usr/sbin and checked its cpu percentage by top. Could anyone point out a right way to run it?

Thanks for Elv's comments. Linux has static data and one needs Network Manager to have dynamic data. By saying so, there is no way to register pure Linux only system to be notified on network state change. By introducing NetworkManager, one can register via dbus to get notified as network state changes.

Could anyone provide more NetworkManager examples on cellular mobile platform?
 
Old 09-02-2009, 10:13 PM   #11
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
Some Nokia N*** use it, OpenMoko phone use it, some other too.

But NetworkManager is now part of Linux, every distribution (exept DIY distro, where nothing is installed by default) use it. An other exist called Conman, developped by Intel, but currently, nobody use it. NetworkManager should not be ressource heavy. Try running it correctly use /etc/init.d, but your distribution might enable it by default, check that before playing with it.
 
  


Reply

Tags
connection, internet, mobile, status


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
internet connection to newly installed linux, i have a dual up connection babu.knl Linux - Newbie 1 05-22-2008 10:36 PM
Secure connections on the intranet break when Internet is unavailable. Travis86 Linux - Networking 0 01-03-2007 09:42 PM
internet connection with linux sachitha Linux - Newbie 12 04-04-2005 10:35 AM
i can get internet connection in windows but in LINUX there is no connection ruzvay Linux - Networking 2 06-08-2004 07:02 PM
Graphic program for sharing internet connection? elkrammer Slackware 3 07-16-2002 10:31 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration