Thread: Reading a large file using thread

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    8

    Reading a large file using thread

    Hello Guys,

    I need do read a text file from memory card using threading.

    I am able to do it with out using thread but because of some limitations, i have to do it with threading only.

    Here is the code to read file from memory card with out thread:
    Code:
    ##########################################
    #include <stdio.h>
    #include <stdlib.h>
    #include <XMC4500.h>        /* SFR declarations of the selected device */
    #include <DAVE3.h>     /* Declarations from DAVE3 Code 
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
    /* helper macro */
    #define CHECK_STATUS(stat, err)\
        if (stat != DAVEApp_SUCCESS)\
            return err
    
    
    /* globals here be tracked by debugger */
    
    volatile _Bool Test_Successfull=false;
    
    FATFS myfsObject;
    
    /* read from a file */
    int file_read(const char *fname)
    {
        FILE *ptr;
        status_t status;
        uint32_t block_size, result;
        uint32_t num_blocks;
        char temp_buffer[1024]; 
    
        ptr = fopen(fname, "r+");
        fseek(ptr, 0, SEEK_SET);
    
        if (ptr == NULL)
            return -1 ;
        block_size = 1024;
       num_blocks = sizeof(char);
    
       result = fread(&temp_buffer, num_blocks, block_size, ptr);
    
    
    
       if (result == 0)
    
                      return -1;
    
    }
    
    int main (void)
    {
    
      do {
          status_t Status ;
    
      struct stat fstat = {0};
    
              /* mount fs */
            Status =  mount(0, &myfsObject);
            if (Status != DAVEApp_SUCCESS)
                break ;
    
     /* read file and verify contents */
            if (file_read("FILE1.TXT") < 0)
                break ;
    
    /* Read file status information */
            Status = stat("FILE1.txt", &fstat);
            if (Status != 0)
                break ;
    
    
      }while (0);
    
            while (1);
    }
    
    
    #####################################
    it's been around a week, i have searching for some stuff to create threading in this program but may be i am too dumb or threading is very tough

    Any help would be highly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Status = mount(0, &myfsObject);
    Why are you trying to do this every time around the loop?

    > if (file_read("FILE1.TXT") < 0)
    > Status = stat("FILE1.txt", &fstat);
    Is your file system case sensitive?

    From the look of things, you're programming some target device, so you should at least post some kind of attempt at threads, as well as citing any reference material you have on your particular thread library.

    Maybe your thread library is POSIX compliant, but it's not a given.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    8

    tried version with thread

    Hello

    i have tried the following code but when the debugger reaches at ptr = fopen (fname, "r");

    it goes to run time error.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <XMC4500.h>        /* SFR declarations of the selected device */
    #include <DAVE3.h>            /* Declarations from DAVE3 Code Generation */
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    #define block_size   512
    
    /* helper macro */
    #define CHECK_STATUS(stat, err)\
        if (stat != DAVEApp_SUCCESS)\
            return err
    
    /* globals here be tracked by debugger */
    
    volatile _Bool Test_Successfull=false;
    
    /** File system object structure */
    
    FATFS myfsObject;
    char Buffer0[];
    
    /* Function to demonstrate the basic operations on a physical drive by 4 tasks */
    void SLTHA002_BasicDemo(void const *Argument);
    
    /* Task function */
    void SDMMC_lTaskFunction(void const *Arg);
    
    /* read from a file */
    int file_read(const char *fname);
    
    int SDMMC_StatusMultApp[5] = {DAVEApp_SUCCESS, SLTHA002_ERROR};
    
    volatile uint32_t ThreadCount ;
    
    /** Stores the parameters associated with each task */
    typedef struct TaskParamsType
    {
      uint8_t ThreadNumber;
      //char *Buffer;
    } TaskParamsType;
    
    int main(void)
    {
      /* DAVE Initialization */
      DAVE_Init();
      osThreadId ThreadId;
    
      /* Thread definition */
      osThreadDef(SLTHA002_BasicDemo, osPriorityBelowNormal, 1, 0);
      /* Thread creation */
      ThreadId = osThreadCreate(osThread(SLTHA002_BasicDemo),NULL);
      if (ThreadId != NULL )
      {
        /* Start the OS kernel explicitly.*/
        osKernelStart();
      }
      return 0;
    }
    
    /* read from a file */
    int file_read(const char *fname)
    {
        FILE *ptr;
        status_t status;
        uint32_t result;
        uint32_t num_blocks;
        char temp_buffer[1024];
    
      do {
          ptr = fopen(fname, "r");
    
          fseek(ptr, 0, SEEK_SET);
    
        if (ptr == NULL)
            break ;
       // block_size = sizeof(temp_buffer);
        num_blocks = sizeof(char);
    
       result = fread(&temp_buffer, num_blocks, block_size, ptr);
       status = fclose(ptr);
    
       if (result == 0)
    
           break;
    
      } while (0);
    
       return 1;
    
    }
    
    /**
      * Task Function used by 4 threads to perform basic operations on the card. */
    void SDMMC_lTaskFunction(void const *Arg)
    {
      char Buffer[10];
    
      FILE *ptr;
      int BytesWritten = 0;
      int BytesRead = 0;
    #if defined  (__GNUC__) || defined (  __TASKING__)
      struct stat FileStat = {0};
     #endif
      /* Buffer to store the data read from the card. */
      char ReadBuffer[30];
      /* Stores the renamed directory string */
      char RenameDirName[6];
      char temp_buffer[512];
      int result;
      status_t status;
    
    
      TaskParamsType *TaskParamPtr = (TaskParamsType *)Arg;
    
      if (TaskParamPtr->ThreadNumber == 1)
        {
            file_read("file1.TXT");
    
              /* read file and verify contents */
                  //  if (file_read("file1.TXT") < 0)
                   //     return -1;
    
        }
    
    
    
      /* Increment the thread count */
      ThreadCount++;
      /* Write the Test Status of the tasks into array */
      //SDMMC_StatusMultApp[TaskParamPtr->ThreadNumber] = Result ;
      /* Terminate the thread. */
      osThreadTerminate(osThreadGetId());
    }
    
    /**
     * Main thread Task function
     */
    void SLTHA002_BasicDemo(void const *Argument)
    {
      /* Thread ID'S of Task */
      osThreadId ThreadId[1];
      uint32_t Count =0;
      TaskParamsType TaskParams[1];
      /* Thread function Definition */
      osThreadDef(SDMMC_lTaskFunction, osPriorityNormal, 1, 0);
      int Result ;
      do
      {
    
      /* Mount the drive */
      Result =  mount(0, &myfsObject);
      if(Result != DAVEApp_SUCCESS)
      {
          break;
      }
    
      /* Thread  numbered*/
      for (Count=0; Count<1; Count++)
      {
        TaskParams[Count].ThreadNumber = Count + 1;
      }
    
        /* Creation of  task. */
        for (Count=0; Count<1; Count++)
        {
          ThreadId[Count] =  osThreadCreate(osThread(SDMMC_lTaskFunction),\
                                            (void *)&(TaskParams[Count]));
          if (ThreadId[Count] == NULL)
          {
            break;
          }
        }/* End of " if ( Status1[i]!= DAVEApp_SUCCESS)" */
        /* Main thread  will wait till all the thread completes their operation */
        while(1)
        {
          if (ThreadCount == 1)
          {
            break;
          }
        }
      } while(0);
      while(1);
    }


    Quote Originally Posted by Salem View Post
    > Status = mount(0, &myfsObject);
    Why are you trying to do this every time around the loop?

    > if (file_read("FILE1.TXT") < 0)
    > Status = stat("FILE1.txt", &fstat);
    Is your file system case sensitive?

    From the look of things, you're programming some target device, so you should at least post some kind of attempt at threads, as well as citing any reference material you have on your particular thread library.

    Maybe your thread library is POSIX compliant, but it's not a given.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since you state the problem occurs when you try to open a file I suggest you try a simpler program that just tries to open a file for writing, check to insure the file opened correctly, write something to the file, and then close the file. Be sure to check the return value of all functions. If this simple program fails then perhaps your embedded system doesn't contain a file system or there is some other problem with the file system.

    What compiler are you using?

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    8
    Hi

    I have made some changes to the task of threAD AND NOW THE PROGRAM IS WORKING.

    THANKS ALOT.

    I AM USING DAVE COMPILER

    Quote Originally Posted by jimblumberg View Post
    Since you state the problem occurs when you try to open a file I suggest you try a simpler program that just tries to open a file for writing, check to insure the file opened correctly, write something to the file, and then close the file. Be sure to check the return value of all functions. If this simple program fails then perhaps your embedded system doesn't contain a file system or there is some other problem with the file system.

    What compiler are you using?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading large data sets from a file
    By DMaz in forum C Programming
    Replies: 7
    Last Post: 01-18-2011, 07:05 PM
  2. [Large file][Value too large for defined data type]
    By salsan in forum Linux Programming
    Replies: 11
    Last Post: 02-05-2008, 04:18 AM
  3. Reading large number from file
    By waxydock in forum C Programming
    Replies: 38
    Last Post: 04-14-2007, 05:12 AM
  4. Reading large number from file
    By waxydock in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2007, 05:08 AM
  5. Optimizing large file reading
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2005, 03:51 AM

Tags for this Thread