(CNN) -- Most of us wake up from nightmares relieved that we're safe in bed. But a small fraction uncontrollably act out their dreams as they unfold, endangering themselves or others.
The recent movie "Sleepwalk With Me," about a struggling stand-up comedian and his issues with sleep, brings to light a rare condition called REM behavior disorder.
This film is not a documentary, but lead actor and director Mike Birbiglia has said that many events, including the dangerous behaviors while asleep, are based on his real life.
Matt Pandamiglio, Birbiglia's character, drives hundreds of miles to any comedy gig he can possibly get to, away from the girlfriend he isn't sure he wants to marry. In the midst of all this, Pandamiglio experiences dramatic episodes of acting out his dreams.
For instance, while staying at a hotel alone, he jumps through the window of his room because, in his dream, he's being pursued by a missile targeting him.
Although the film is called "Sleepwalk With Me," Pandamiglio isn't sleepwalking per se -- he's kicking, running, climbing on top of things and falling off them.
After repeatedly delaying a doctor's visit, he finally gets the diagnosis of REM behavior disorder, which affects less than 1% of the population, according to the American Sleep Association.
Unusual patterns of behavior that people exhibit while sleeping are called parasomnias. Most of them are most likely to happen when a person is sleep-deprived and under stress, said Philip Gehrman, assistant professor of psychiatry at the University of Pennsylvania.
Sleepwalking and REM behavior disorder are two examples of parasomnias. Sleepwalking happens during non-REM sleep; it usually involves walking around and perhaps mumbling.
REM is a stage of deep sleep during which certain parts of the body are normally paralyzed temporarily.
"The brain sends out the signals to move, but at the level of the spinal cord, the motor neurons are inhibited," said Dr. William C. Dement, a pioneer in the field of sleep research and professor at Stanford University School of Medicine.
But that inhibition fails in REM behavior disorder, allowing the person to move in the way that he or she is envisioning in a dream. "It can be very dangerous," Dement said.
REM behavior disorder and sleepwalking are defined as separate disorders, but in reality there may be some overlap, Dement said.
Dement and Dr. Rafael Pelayo, associate professor at Stanford medical school, said they have never seen patients who jumped out of windows, but some have punched windows.
Dement appears as himself in "Sleepwalk With Me," sitting in the passenger seat beside the struggling comedian while he's dreaming/listening to Dement's audio book in the car. (It's a little reminiscent of Cornel West's cameo in "The Matrix Reloaded," another widely known academic in surreal circumstances.)
"In the simplistic way of thinking, the brain is either awake, sleeping or dreaming," Pelayo said. "But we know that the brain is complicated enough that one part can be awake while another part can be sleeping at the same time."
In sleepwalking, the rational part of the brain is offline, while the emotional part is activated, he said.
Most people with REM behavior disorder are men, and they tend to be older, Pelayo said. The disorder can mark the start of Parkinson's disease.
It's unusual for someone as young as Birbiglia, 34, to exhibit REM behavior disorder, Pelayo said. But it's possible that he has a combination of sleepwalking and REM behavior disorder, he said.
The film portrayed Birbiglia's character exhibiting several risk factors: drinking alcohol, staying up late, sleeping in different environments and being under stress.
Sleepwalking by itself is more common, especially among children. Research suggests that as many as 30% of children have experienced sleepwalking. Most people stop once they hit puberty, Gehrman said. Sleepwalking has occurred among nearly one in three Americans at some point in their lives, a study in Neurology found in May.
As for whether someone should awaken people who are in one of these altered sleep states, it's more dangerous for others than for the sleepers, experts say. The sleepers' brains will not be impaired by waking up, but they may react violently.
Sleepwalking among adults is not necessarily a problem. For mild cases, education about the phenomenon and reassurance are enough, Gehrman said.
But if the person goes outside and gets into dangerous situations, he or she may need to sleep more and undergo stress management interventions.
If that doesn't work, especially among people who have been sleepwalking for their whole lives, medications may be necessary.
The drug that sleepwalkers tend to start with is the anti-anxiety medication clonazepam, Gehrman said. It's not clear why it works, but it may help people relax and sleep more deeply.
Clonazepam is also prescribed for REM behavior disorder, although it is sometimes not appropriate in older patients because it may aggravate sleep apnea, Pelayo said. There's evidence that the hormone melatonin can be effective.
Some people don't respond to any medications for sleepwalking, Gehrman said. It may help to put a bell on the door so that if they try to leave the bedroom, they will wake up -- or at least their bed partner will, he said.
In extreme cases, people need restraints so that they can't get out of bed during the night, Gehrman said -- something that attaches them to the bed in such a way that only someone who is awake could undo it.
Birbiglia has said in interviews that, as with his alter ego in the film, he takes medication and sleeps in a sleeping bag, and he used to wear mittens so that he couldn't open the bag.
Gehrman hasn't had any patients who have tried the bag-and-mittens method but noted, "You have to be creative sometimes."
Experts say there isn't a lot of research going on into sleepwalking or REM behavior disorder treatments since the available resources are sufficient in most people.
The next step in REM behavior disorder is exploring the connection to Parkinson's disease, Pelayo said. It may be possible to identify patients for whom the sleep disorder is a risk factor for Parkinson's and treat them before the disease gets worse.
Have you ever had an episode of sleepwalking or other sleep disorder? Tell us about it in the comments.