I have a program which reads entries from a directory. These entries are stored in a vector called fdata.
Using vectors gives me an easy way to sort the entries.

The program copies files from one directory (the source directory) onto another directory (the destination directory). While the copying is performed the program will show a progress bar to indicate how long time the copy process will be using.

By using fork() I thought I could make a parent process performing the copying, and a child process which monitors the copying by reading the destination file size and using that data to display a progress bar.

But my problem is that when I run the code above I get the following error message when the first file copy operation is completed:
"terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc"

So I need a way to tell the child process that the copy operation is terminated. How can I achieve that ?

// == Definitions == //

struct paraminfo {
  std::string srcdir;
  std::string dstdir;
}

struct finfo {
    string filename;
    long fsize;
    unsigned char DirType;
    bool operator() (finfo i, finfo j)
        {
        if(i.fsize == j.fsize)
             return i.filename < j.filename;
         return (i.fsize > j.fsize);
        }
} fcont;


vector<finfo> fdata;

string srcfile, dstfile
FILE *iFile, *oFile;
pid_t pPID;
int index1;
int SLEEPTIME = 2;

// == Code snippet == //

  for (index1=0;index1<fdata.size();index1++){

        srcfile = paramflags->srcdir + "/" + fdata[index1].filename;
        dstfile = paramflags->dstdir + "/" + fdata[index1].filename;
        iFile = openfile(srcfile,"read");
        oFile = openfile(dstfile,"write");

        if (fdata[index1].fsize == 0){
            fclose(oFile);   
            fclose(iFile);   
        }

        else if (fdata[index1].fsize != 0){
         pPID = fork();

         if (pPID > 0){  
                 while (GetFileSize(dstfile) < fdata[index1].fsize)
                 {
 //                  >> Progress bar displays here <<
                     sleep(SLEEPTIME);
                 }
         }
         else if (pPID == 0){

                filecopy(iFile,oFile,buf);
                fclose(oFile);
                fclose(iFile);
         }

        }
    }

Recommended Answers

All 2 Replies

This is a situation where posix threads may be more appropriate than fork/exec processes.

Yes, I know.
Then I'll have to figure out how this can be done when I crosscompile the shit for use in OpenEmbedded Linux... :-\

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.