PLEASE HELP with execl() and fork() linux functions in a c++ program

must create a pipe to pass data between children

Page 1 of 1

1 Replies - 3058 Views - Last Post: 21 September 2010 - 10:47 PM Rate Topic: -----

#1 c++guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 09

PLEASE HELP with execl() and fork() linux functions in a c++ program

Posted 21 September 2010 - 10:13 PM

my main program must accept the names of input and output text files as command line arguments. it should create a pipe and then create 2 other children processes. the pipe is for communication between the children. after the children are created the parent must wait for children to complete before exiting with wait() function.

the first child process should replace its process image (with execl() function) with a read program that will open the specified input file, read it using the read() function, and write the contents to a pipe.

the second child process should replace its image with a write program that will read the data from pipe and then write to specified output file.

the read program should read in 512 characters at a time from the file. the read program should terminate after processing the file. the write program should exit once all of the data is written to the output file.

I am unsure where the second fork() function should go for one thing. when i run my program my output file is overwritten with just blank space which leads me to believe my write buffer is blank, so it is never getting the data from the pipe. also, how does the execl() function work exactly? in my messing around it seems like nothing executes after the execl() function call in a given if block.

//parent

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
        int rc, rc2, ptest;
        int aipipe[2];
        cout << "before child" << endl;
        ptest = pipe(aipipe);
        rc = fork();
        cout << rc << endl;
        cout << ptest << endl;

        if (rc == 0)
        {

                char readbuffer[512];
                sprintf(readbuffer, "%d", aipipe[1]);
                cout << "in first child" << endl;
                execl("./reader", "reader", argv[1], readbuffer, NULL);

        }

        else if (rc == -1)
        {
                cout << "fork call error" << endl;

        }



        cout << "hey" << endl;
        rc2 = fork();
        char writebuffer[512];

        if (rc2 == -1)
        {
                cout << "fork call error" << endl;
        }
        else if (rc2 == 0)
        {
                cout << "in second child" << endl;
                execl("./writer", "writer", argv[2], writebuffer, NULL);
        }


        close(aipipe[0]);
        close(aipipe[1]);
        wait();
        wait();

        return 0;
}











//reader

include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
        cout << "in reader" << endl;
        char readbuffer[512];
        int ipipefd = atoi(argv[2]);
        ifstream ifs(argv[1]);
        ifs.read(readbuffer, sizeof(readbuffer));
        write(ipipefd, readbuffer, ifs.gcount());

        return 0;
}












//writer

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
        cout << "in writer" << endl;
        char writebuffer[512];
        int rc;
        int ipipefd = atoi(argv[2]);
        ofstream ofs(argv[1]);
        rc = read(ipipefd, writebuffer, sizeof(writebuffer));
        ofs.write(writebuffer, rc);



        return 0;
}






Is This A Good Question/Topic? 0
  • +

Replies To: PLEASE HELP with execl() and fork() linux functions in a c++ program

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: PLEASE HELP with execl() and fork() linux functions in a c++ program

Posted 21 September 2010 - 10:47 PM

So many questions all in one lump.
Going at them one at a time might be a more successful way to get from where you are to where you want to be.

First thing.
Here's what execl() does:
http://linux.about.c...cmdl3_execl.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1