Mogadishu residents waiting at a food distribution center. After 17 years of war, simple survival seems to be driving the violence. (Jehad Nga for The New York Times)

MOGADISHU, Somalia: The trouble started when government soldiers went to the market and, at gunpoint, began helping themselves to sacks of grain.

Islamist insurgents poured into the streets to defend the merchants. The government troops got hammered, taking heavy casualties and retreating all the way back to the presidential palace, supposedly the most secure place in the city. It, too, came under fire.

Mohamed Abdirizak, a top government official, crouched on a balcony at the palace, with bullets whizzing over his head. He had just given up a cushy life as a development consultant in Springfield, Virginia. His wife thought he was crazy. Sweat beaded on his forehead.

"I feel this slipping away," he said.

By its own admission, the Transitional Federal Government of Somalia is on life support. When it came here to the capital 15 months ago, backed by thousands of Ethiopian troops, it was widely hailed as the best chance in years to end Somalia's ceaseless cycles of war, chaos and suffering.

But now its leaders say that unless they get more help - international peacekeepers, weapons, training and money to pay their soldiers, among other things - this transitional government will fall, just like the 13 governments that came before it.

Fewer than a third of the promised African Union soldiers have shown up, the United Nations has shied away from sending peacekeepers any time soon, and even the Ethiopians are taking a back seat, often leaving the government's defense to teenage Somali troops with clackety guns who are clearly overwhelmed.

The Islamists have been gaining recruits, overrunning towns and getting increasingly bold. The new prime minister, credited as the government's best - and possibly last - hope, is reaching out to them, and some are receptive. But it is unclear whether he has the power within his own divided government to strike a peace deal before it is too late.

The looming failure is making many people here and abroad question the strategy of installing the transitional government by force. In December 2006, Ethiopians troops, aided by American intelligence, ousted the Islamist administration that briefly controlled Mogadishu, bringing the transitional government to the city for the first time.

The Bush administration said it was concerned about terrorists using Somalia as a sanctuary. The hunt for them has continued with a recent American cruise missile strike aimed at a terrorist suspect in southern Somalia, but it missed, wounded several civilians and promptly incited protests.

Many Somalis and diplomats from other Western countries also question the State Department's decision this month to label a Somali resistance group a terrorist organization, something that many fear will only bolster its profile among the increasingly disillusioned populace

"The Americans are really bungling this one," said a Western diplomat. "Their strategy is only radicalizing the population."

In recent weeks, the Islamists have routed warlords and militiamen who have been absorbed into the government forces and are undermining what little progress transitional leaders have made with their predatory tactics, like looting food. After 17 years of civil war, Somalia's violence seems to be driven not so much by clan hatred, ideology or religiosity but by something much simpler: survival.

"We haven't been paid in eight months," said a government soldier named Hassan. "We rob people so we can eat."

Nur Hassan Hussein, the prime minister, did not deny that government troops were robbing civilians. "This is the biggest problem we have," he said.

But, he said, he does not have the money to pay them. Each month, more than half of government's revenue, mostly from port taxes, disappears - stolen by "our people," the prime minister said.

That leaves Nur with about $18 million a year in government money to run a failed state of nine million of some of the world's neediest, most collectively traumatized people.

And "failed state" may be a generous term. In many ways, Somalia is not a state at all, but an ungoverned space between its neighbors and the sea. Sometimes it seems that if anything binds this country together, it is scar tissue.

Take Hassan Ali Elmi, who was blinded by a bullet in 1992 and has been living ever since in a cell-like room in the gutted former ministry of public works. His son tugs him into town to beg for the equivalent of a few pennies a day, which buy less and less. At night, he lies on a thin foam mattress and waits for the shooting to stop. It doesn't.

"All Somalia, all gun," he says.

His neighbors are recently displaced people living in cardboard huts that crumble in the rain. Aid groups say that more than half of Mogadishu's estimated one million people is on the run.

Many of the same elements that lined up in the early 1990s to create a famine are lining up again - war, drought, displacement, skyrocketing food prices and aid workers pulling out. The United Nations World Food Program said Thursday, in a warning titled "Somalia Sinking Deeper into Abyss of Suffering," that the country was the most dangerous in the world for aid workers.

Most Somalis do not argue with that. They say Mogadishu is more capriciously violent than it has ever been, with roadside bombs, militias shelling one another across neighborhoods, doctors getting shot in the head and 10-year-olds hurling grenades. Police officials say that many insurgents are actually hungry children paid a few dollars for their work.

In the shrinking zone that the government controls in southern Mogadishu, a couple of buildings have been splashed with a fresh coat of paint and new immigration forms at the airport ask travelers for their name, purpose of journey and caliber of weapon. Girls wearing bandanas dribble basketballs in a gym. Men sell fish by the seaside. A beat of life goes on. But north Mogadishu is another story.

"It's like 'Mad Max' out there," said Abdi Awaleh Jama, an ambassador at large, pointing from the presidential palace north toward the expanse of huts and ruins stretching into the distance.

In the rat-tat-tat of nightly machine gunfire, people are beginning to hear the government's death knell. Many residents have mixed feelings about this. They contend that the government has enabled warlords. They say, almost without exception, that things were better under the Islamists, but they fear what lies ahead.

Nur, a former Red Crescent official who became prime minister in November, is trying to peel away moderate Islamists from militant ones and get them to negotiate. He is making concessions to business leaders, who are widely suspected of financing the Islamists out of clan allegiances, and allowing them to form their own protection force. UN officials are trying to boost Nur's prospects by providing $14 million to pay key government salaries and fix up ministries.

"This is urgent," said William Paton, the acting UN coordinator for Somalia. "They are on thin ice."

Government officials say much of the resistance is simply spoilers who are deeply invested in the status quo of chaos, like gun runners, counterfeiters and importers of expired baby formula.

But some of the men believed to be the biggest spoilers are part of the government. To get clan support - and, just as crucially, more militiamen - transitional leaders have cut deals with warlords like Mohammed Dheere, now Mogadishu's mayor, and Abdi Qeybdid, now the police chief. These are the same men who were paid by the CIA in 2006 to fight the Islamists, a move that backfired because the population turned against them, mostly because of their legacy of terrorizing civilians.

Hassan, the government soldier, says he has been in one of these warlord militias since he was 8. He toted his first Kalashnikov at age 10. He cannot read or write. He has thin wrists, a delicate face, empty eyes and a wife and two children to feed, which is why he said he routinely sticks people up. "We are losing," he said.

He said many of his friends were defecting to the Islamists because that was the only way to survive.

The Islamists have briefly captured several towns in recent weeks, freeing prisoners, snatching weapons and then melting back into the bush. Gone are the beards and the checkered scarves they used to wear. Many, like a young man named Elmi, are clean-shaven and favor crisply pressed suits.

Elmi, who like Hassan said he could not reveal his last name, said business owners have sold gold, real estate and sheep to raise money for the Islamists. Elmi said he was part of the battle at the market on March 20 that began with the looting, and that the government lost three trucks, which was corroborated by government soldiers.

"We were there because we are everywhere," Elmi said.

Abdirizak, the government official, buried some of the victims of that battle, young government troops who were slipped into graves behind the presidential palace in the moonlight.

One soldier was named Abdi Rashid. He had been wounded in another firefight about a month ago, and according to Abdirizak, "he shouldn't even have been out there that day. It's just that we don't have enough guys."

Abdi Rashid was shot in the heart at the market as the Islamists surrounded government troops. His last words to his friends, who wanted to carry him to safety, were, "Get out of here, get out of here."

Abdirizak fell silent.

"I'm not sure how long I'll stay," he finally said. " I want to help. But I didn't come here to get killed."