freeing problem with concurrent processes

This is a discussion on freeing problem with concurrent processes within the C Programming forums, part of the General Programming Boards category; the program partitions a file to several files then creates processes to sort them. Code: fpArray = malloc ( numOfProcesses ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    freeing problem with concurrent processes

    the program partitions a file to several files then creates processes to sort them.
    Code:
    fpArray = malloc ( numOfProcesses * sizeof( FILE* ) );
    processIDs = malloc ( numOfProcesses * sizeof (pid_t*));
    when i check it with valgrind, there is no problem with main process but for child processes it says

    32 bytes in 1 blocks are still reachable in loss record 1 of 2
    ==2338== at 0x1B901E28: malloc (vg_replace_malloc.c:131)
    ==2338== by 0x8048733: main (psort.c:50)
    ==2338==
    ==2338==
    ==2338== 32 bytes in 1 blocks are still reachable in loss record 2 of 2
    ==2338== at 0x1B901E28: malloc (vg_replace_malloc.c:131)
    ==2338== by 0x804871B: main (psort.c:49)

    for these lines. at the end of the main process i have below lines
    Code:
    free (fpArray);
    free (processIDs);
    how can i get rid of this error

    edit:

    i have the same problem for the same code with threads.
    Last edited by alavardi; 03-07-2005 at 11:54 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The error isn't in any of those lines. It's in you moving your pointer around some place and losing what you allocate.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    ok, here is whole code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    //#include <fcntl.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <errno.h>
    #include <math.h>
    
    #define LINE_LENGTH 1000
    
    typedef struct heap_node{
    
    	char* data;
    	struct BinaryNode* left;
    	struct BinaryNode* right;
    
    }heapNode;
    
    heapNode* root;
    
    void childProcess(void);
    
    int main (int argc, char* argv[]){
    
    	int numOfProcesses;
    	char* inputFileName;
    	char* outputFileName;
    	char filename[50];
    	char line[LINE_LENGTH];
    	int r; //M%N
    	int i; //for FOR loops
    	int j; //for FOR loops
    	FILE** fpArray;
    	FILE* inputFile;
    	int numOfLines = 0;
    	pid_t* processIDs;
    
    	if( argc < 6 || argc > 7){
    		printf("Wrong number of arguments\n");
    		printf("Usage: \n");
    		printf("psort -n <numOfProcesses> -i <inputfile> -o <outputfile> [-d]\n");
    		exit(1);
    	}
    	else {
    		numOfProcesses = atoi (argv[2]);
    		inputFileName = argv[4];
    		outputFileName = argv[6];
    		fpArray = malloc ( numOfProcesses * sizeof( FILE* ) );
    		processIDs = malloc ( numOfProcesses * sizeof (pid_t*));
    		inputFile = fopen(inputFileName, "r");
    
    		//count num of lines in the file
    		while( fgets(line, sizeof(line), inputFile) ){
    			 ++numOfLines;
    		}
    		//printf("File contains %d lines.\n", numOfLines);
    		rewind (inputFile);
    		for( i=0; i<numOfProcesses; ++i){
    			/* make a string that will be the filename */
    			sprintf(filename, "%d.%d.txt", (int) getpid (),(i+1));
    			//fpArray[i] = fopen(filename, "w");
    			fpArray[i] = fopen(filename, "w");
    			if (fpArray[i] == NULL)
    			{
    	  			perror ("main():");	 //print the reason why fopen failed
    	  			exit (1);
    			}
    
    		}
    		r = (numOfLines%numOfProcesses);
    
    		//partition the file
    		for (i=0; i<(numOfProcesses-1); ++i ){
    
    			//write M-r/N records to file
    			for (j=0; j<((numOfLines-r)/numOfProcesses); ++j){
    
    				fgets(line, sizeof(line), inputFile);
    				fprintf(fpArray[i], "%s", line);
    			}
    		}
    		//write (M-r/N)+r records to the last file
    		for (j=0; j<(((numOfLines-r)/numOfProcesses)+r); ++j){
    
    			fgets(line, sizeof(line), inputFile);
    			//fprintf(fpArray[numOfProcesses-1], "%s", line);
    			fprintf(fpArray[i], "%s", line);
    		}
    		for (i=0; i<(numOfProcesses); ++i ){
    			fclose(fpArray[i]);
    		}
    
    		fclose(inputFile);
    
    		//create processes
    		for (i=0; i<numOfProcesses; ++i){
    
    			processIDs[i] = fork();
    
    			if (processIDs[i] < 0)
    			{
          				perror ("main():");	 //if fork has failed,
    						   	//print the reason why it has failed
          				exit (1);		 //terminate
        			}
    
    			if (processIDs[i] == 0){
    				childProcess();
    			}
    
    			//else
    				//parentProcess(processID);
    
    		}//end of create processes
    
    		//printf("I am parent process and my pid: %d\n", (int) getpid() );
    
    		for (i=0; i<numOfProcesses; ++i){
    			waitpid(processIDs[i], NULL, 0);
    		}
    		/*for(i=0; i<numOfProcesses; ++i){
    			free (fpArray[i]);
    
    		}*/
    		free (fpArray);
    		free (processIDs);
    		exit(0); //the execution shouldn't reach here
    		//return 0;
    
    	}//end of else
    }//end of main
    
    void childProcess(void){
    	//printf("I am a child process and my pid: %d\n", (int) getpid() );
    	//free (fpArray);
    	//free (processIDs);
    	exit(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Stopping Processes Question
    By brett in forum Linux Programming
    Replies: 3
    Last Post: 06-24-2007, 10:15 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21