HUNTSVILLE, Texas (AP) _ More than 6,000 inmates in the nation's second-largest prison system were confined to cells today in an emergency lockdown prompted by escalating gang violence that left four inmates dead within 24 hours.

''We're at war,'' Prison Director O.L. McCotter said Monday. ''Gang activity is at an alarming level. Our major concern is that it appears to be open warfare.''

As of Monday, eight inmates had been killed in as many days, pushing the number of homicides in the Texas system this year to 26, one more than last year's record of 25, officials said.

''It's very tense,'' prison spokesman Phil Guthrie said Monday. ''Any time you have a potential gang war going on, nobody plays by normal rules. We're sitting on a time bomb.''

Texas Department of Correction officials ordered strict security measures over the Labor Day weekend because they feared a system-wide uprising, said Al Hughes, elected chairman of the prison board on Monday.

Officials learned of the alleged takeover plot from informants and by screening mail between inmates, Hughes said. The attempts never materialized.

Most of the recent violence stems from a war between two Hispanic gangs known as the Texas Syndicate and the Texas Mafia, officials said.

''Essentially what you have is two rival gangs struggling for supremacy and power over their peers,'' Guthrie said. ''We have information that the two gangs are putting on a big recruiting drive.''

The killings, he said, ''are part of a code to demonstrate loyalty to the gang. Part of the initiation rite is that you beat someone or stab someone.''

Prison officials estimate that about 750 of the system's 37,000 inmates are active members of the six to eight gangs known to be active behind the walls of the 37 units.

McCotter's lockdown order affected inmates in maximum, medium and close custody in 13 units statewide. ''I'll lock down every unit if I have to,'' he said.

Monday's slaying victim was Leonel Perez, 31, serving an eight-year sentence for burglary. He was stabbed 15 times, apparently with a 10-inch metal rod sharpened to a point that prison spokesman Charles Brown said was found near his body. A 31-year-old inmate is suspected of the attack, Brown said.

Three prisoners were stabbed to death Sunday during an exercise period at the Darrington Unit, south of Houston. Two inmates were in custody. An 8-inch- long flat piece of metal and a boning knife were found nearby, Brown said.

Officials declined to give the gang affiliations of victims and attackers.

Besides the lockdown, McCotter ordered known gang leaders segregated from the population, accelerated their reclassification so they could be moved to higher-security cells, obtained a court order to restrict inmate-to-inmate mail exchange and set up a task force so authorities could share information.

McCotter also said the Legislature would be asked to make the killing of an inmate punishable by death. ''We very badly need that capability,'' he said.

Despite the rash of gang violence, Guthrie said Monday that prison officials were encouraged that the stabbing rate of 36 per month a year ago was now down to 24. ''On balance, overall, violence is lower,'' he said.

In addition, recent shakedowns of cells turned up fewer weapons, he said. ''But they could be hiding them better.''