i have an assigment to fork the main program to 4 sons
each will sort an array in a different way

for (sonID=0;sonID<4;sonID++)
	{	
		status=fork();
		if (status<0)
			error("Can't Fork!");
		if (status==0)
		{
			checkFiles(argc,argv,&in,&out,sonID);// opening the files for use
			readFromFile(&nameList,&sizeOfList,&numOfStrings,in);
			sortArray(numOfStrings,&nameList,sonID);
			outputToFile(nameList,numOfStrings,out); //output the array to out file
			freeArray(sizeOfList,nameList); //freeing the whole array cells
			closeFiles(&in,&out); // closing the files		
		}
	}
	for (sonID=0;sonID<4;sonID++)
	{
		wait(&status);
	}

now if i call freeArray i get

*** glibc detected *** double free or corruption (fasttop): 0x09fb94e8 ***

and first file has the array 2nd file has 2xarray 3rd has 4xarrays etc...
and if i dont call it the files dont have anything in them except the first one
any ideas?

freeArray is simple

void freeArray(const int sizeOfList,char **nameList)
{
	int index;
	for (index=0;index<sizeOfList;index++)
		free (nameList[index]);
	free(nameList);
}

and here is the opening file function (dont think the problam is there but who knows )

char *fileName;
	switch(sonID){
		case(SONONE):fileName="id_up.out";
			break;
		case(SONTWO):fileName="name_down.out";
			break;
		case(SONTHREE):fileName="name_up.out";
			break;
		case(SONFOUR):fileName="id_down.out";
			break;
		default:break;
	}
	if (argc!=CORRECT_PARAMETERS) error("Bad Number of parameters");
		
	if (argc>FIRST_INPUT)
	{	
		*in = fopen(argv[FIRST_INPUT], "r");
		
		if (*in==NULL)
			error("Can't open input file");
	}
	else
			error("No 1st Input. go to README for insturctions.");

	*out = fopen(fileName, "w");

	if (*out==NULL) error("Can't open output file");

cheers thanks!

i moved everything from the loop into a function and now it works
without errors .... any idea why?
thought i now encounter another problem after the output to files
this is the main

for (sonID=0;sonID<4;sonID++)
	{	
		status=fork();
		if (status<0)
			error("Can't Fork!");
		if (status==0)
		{
			doSon(argc,argv,sonID);
		}
	}
	for (sonID=0;sonID<4;sonID++)
	{
		wait(&status);
	}
	
	for (sonID=0;sonID<4;sonID++)
	{	
		status=fork();
		if (status<0)
			error("Can't Fork!");
		if (status==0)
		{
			doSonPrint(sonID);
		}
	}
	for (sonID=0;sonID<4;sonID++)
	{
		wait(&status);
	}

and dosonprint is

void doSonPrint(sonID)
{
	printf("son number %d\n",sonID);
	if (sonID==0)
		if((execlp("cat","cat","id_up.out",NULL)!=0))
								perror("execlp error");
}

now this should be called only once but i get the file posted in my stdout
like 15 times.
thanks for trying ... :(

the whole problem... is because i forgot one thing
found out just now .... exit(1) at end of each child
thanks and sorry for being dumb wasting too many hours
and your time reading

This question has already been answered. Start a new discussion instead.