After 9/11, airline pilots requested and received the right to have a gun in the cockpit. I believe this is a serious mistake.
The issue is really one of military tactics. How best to defeat hijackers after they have taken over the plane?
It is not for the pilot to open the door to the cockpit.
The right answer is for the pilot to fly the plane, which is what he is good at, and land it as quickly as possible so that law-enforcement or military can handle the hijackers. Most importantly, after Sept 11, he must deny the hijackers control of the airplane, even if passengers and crew are held hostage or being murdered.
The right answer is for the cockpit door to be so well reinforced that a hijacker cannot force his way in, so the pilot has a chance to do what he needs to do.
A pilot with a gun is going to face a terrible temptation to do exactly the wrong thing: open that reinforced door and try to subdue the hijackers.
The hijackers may have trained for months or years in how to cause terror and death. The pilot has possibly trained for weeks in using the gun. He will be toast and they will have the plane.
Proponents of arming pilots love the vision of the pilot stepping triumphantly over the hijacker's body. But you have to think through all the possible outcomes, not just the one you want. Make the plan that is most likely to achieve the goal you want even when things go wrong, and don't put people into a situation where they are likely to do exactly the wrong thing.
The pilot's instructions must be: Land the plane, and under no circumstances open the door, no matter what is happening in the cabin. (If you are worried about the passengers and the crew, carry a sky marshall.) And if you want those instructions to be followed, by a human being under enormous stress, don't give the guy a gun.