LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Get notified in Linux C program as Internet connection becomes available/unavailable (https://www.linuxquestions.org/questions/linux-software-2/get-notified-in-linux-c-program-as-internet-connection-becomes-available-unavailable-751059/)

jimcftsai 08-29-2009 07:54 AM

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?

Elv13 08-30-2009 12:53 AM

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)

jimcftsai 08-30-2009 09:03 AM

is there a lightweight way to know the state change?
 
Quote:

Originally Posted by Elv13 (Post 3662561)
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.

Elv13 08-30-2009 01:44 PM

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.

jimcftsai 08-31-2009 07:03 AM

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.

Elv13 08-31-2009 08:44 AM

Unless you have a pentium 100mhz, you have a problem in your configuration, NetworkManager is even used in some cell phones to manage network.

jimcftsai 09-01-2009 07:50 PM

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?

lwasserm 09-01-2009 08:10 PM

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.

Elv13 09-01-2009 10:38 PM

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).

jimcftsai 09-02-2009 08:55 AM

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?

Elv13 09-02-2009 10:13 PM

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.


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