DISCLAIMER: I get all my facts about stuff like this from TV cop shows, so keep that in mind.
That being said, i was watching a cop show where they put a bunch of regular patrol officers in plainclothes to catch a guy that planned on killing cops. Before they hit the street, they got briefed a "color of the day." If they got into a situation where they were challenged by another cop, they were supposed to display the color of the day. It's not perfect, but it would hopefully lower the tensions so they could then sort it out without anything unfortunate happening.
That obviously wouldn't help a concealed carry permit holder (or well-meaning citizen carrying without a permit), though. In any case, I'd recommend NOT having the gun in your hands when the police show up. If I were sure the threat was over, I'd clear it and place it in plain sight, and also keep my hands in plain sight. If I thought I might still need it, I'd reholster, but still keep my hands in plain sight and immediately tell the responding officers that I was armed. Either way, if you're carrying without a permit, I think you're still screwed... You may be able to reduce your chances of getting shot, but I'd bet you're still headed to jail.