Thursday, December 22, 2016

Shell script to shut down your PC on power failure

Sometimes you may want to shut down your PC automatically, when there is a  power failure. And you don't have an intelligent UPS or other way to do it. Sometimes it can be like, you want to download a large file and want to reach office soon as possible (today I faced the same problem 😊).

If you are using Linux or Unix based PC with shell support, you can do it simply using the shell script and cron job.

Below is the shell script to shut down the PC, if the modem fails to respond to ping command. Update the IP address with your modem/router IP (get your router IP from Connection information; also update the root password as required and the ping count (4) too; if required.

Create the file pingnshut.sh and add the below code to it and set  permission to execute.
#!/bin/sh
ping -c 4 192.168.1.1
if [ "$?" = 0 ]; then

echo "Power On - `date`"
else
echo "Power Off - `date`"
echo "root_password" | sudo -S poweroff
fi
Then add a job (cron) using the command crontab -e and append the below line to end of the file and save it. This will execute the above script every 5 minutes. Update the shell script path to that of yours.
*/5 * * * * /home/mohammed/Desktop/pingnshut.sh
Change the time (5 minutes), close to your UPS backup time.

To work this fine, ensure your modem is connected to line power, not to any UPS or other power source with battery backup 😝.

Tuesday, November 30, 2010

Java and C++ differences

We can classify the object oriented languages as pure object oriented and not pure object oriented languages. For example java is purely object oriented and c++ is not, because java doesn't support procedure oriented programming paradigm and c++ not, so the difference starts there itself, and there is more here.

  1. c++ is not purely object oriented and java is purely object oriented
  2. c++ has pointer  facility so that, can directly access the memory location and java doesn't support pointers
  3. c++ char data type is 8 bit long and for java it is 16 bit
  4. c++ int type is system dependent, i.e. for 16 bit processor it is 16 bit and for 32 bit it is 32 bit and for java it is fixed 32 bit (4 byte)
  5. c++ output is executable machine code so that it can be directly executed by the processor, but java output is byte code which is then executed by the java frame works (i.e java frame work translates it into machine code during run time)
  6. c++ output is faster than java output, it is because the c++ doesn't need any translators during execution/run time, but java output(byte code) need java run time to translate the code to machine code.

Experience note:-
Asked the same question when I attended an interview for the position of java programmer/developer in a software company.

Sunday, November 28, 2010

Object based and Object oriented Languages

There are object oriented and object based languages differ in few properties, object oriented language is having support for all object oriented concepts like class, object, inheritance,polymorphism,dynamic binding,message passing etc, but object based languages doesn't support properties inheritance and dynamic binding (dynamic binding is closely related to inheritance), for example, C++, Simula, Small talk,Eiffel, Java all are object oriented languages, visual basic 6.0 can be considered as an object based language which support class and object but not inheritance and dynamic binding, but current versions of visual basic is object oriented because they support all the above mentioned features. Ada is another example for object based language. Some authors count C++ as not object oriented, instead it as extended conventional languages, similarly the same case for Objective C, Object pascal, Turbo pascal etc.

Experience Note:-
A similar question was asked on NIC (National Informatics Center), India recruitment test. The question was to find one in the 4 choices that is object based; choices were C++, Java, Visual Basic etc, like an odd man out question.

Friend of a friend and Parent's friend in C++

If A is a friend class of B and B is a friend of C then can we say A and C are friends?
No. If there will be no friend relation between those classes, if class A is a friend of B then A can access the data members(including private) of class B, and if class B is a friend of class C, similarly class C can have access to class B's data members, but can't access the member of class A which is already a friend of B. i.e can't access friend of a friend's member. It is the same concept that If Alice is bob's friend and if Bob is your friend then it will not be always true that Alice is your friend too(hint:- or even if Alice is your friend too, you will not have access to her private properties, that bob has access to).



class A
{
....
....
};
class B
{
.....
.....
friend class A;
};
class C
{
......
......
friend class B;
};

Friend of Parent

Similarly it is same in the case of inheritance too. If class A is a parent of class B and class F is friend of class A (Parent of B) then class F have access to class A's private attributes members, but not class B's. It is same concept that if your parent has a friend he have some right to access to your parent's properties, it doesn't mean that he has the same right to access your properties too.



class A
{
....
....
friend class F;
};
class B : public/private/protected A
{
};

Experience Note:-
A similar question was asked during NIC(National Informatics Center), India Recruitment Test. The NIC test was purely technical and there was questions from almost all areas of computer science including Data structure, Computer networks, Computer architecture, C Programming, C++ programming, Compiler design and Theory of computation (Automata and computer languages), Database systems, Software Engineering, Cryptography and Network Security, Digital logic and  basic electronics, awareness about current software systems available and few Linux commands based questions for example 

which is not a relational database in the given four choices (choices like Reflex, mysql, fourth dimension, oracle)
which of the following is an object based language
which of the following cant be used to make web pages etc (java services, jsp, asp etc)