We help IT Professionals succeed at work.

Using Fork() and Pipes()? Read message in w/ Pipe()? Write message to screen w/Pipe()?

KrAzY
KrAzY asked
on
I Currently have an assignment that invloves reading in a string, encrypting it, and then outputing the encrypted string.

The encryption part I have written and confident it will work.  I am now working on the Forks and Pipes and I am having a lot of trouble.  I need to create two Forks (A client, and a server).  The Client Fork reads in the string to a Pipe. Then the Server Fork reads the string from the Pipe and encrypts it.  After it is encrypted it sends the string back to the Client Fork with a new Pipe(). Then the Client Fork reads the string from the Pipe() and outputs it to the screen.

I think I have created the Forks properly:
    for (i=1; i<=2; i++) {
        if ((pid = fork()) == -1) {
            /* Fork failed! */
            perror("fork");
            exit(1);
        }

My problems revolve around the pipes.


 for (i=1; i<=2; i++) {
     if ((pid = fork()) == -1) {
        /* Fork failed! */
        perror("fork");
        exit(1);
     }
        if (pid == CHILD) {
            switch(i) {
            case 1: /* Client */
              write(p[1], buf, strlen(newbuf) + 1);
              //READS FROM NEW PIPE THE ENCRYPTED STRING
              break;

              case 2: /* Server */
              read(p[0], p[1], strlen(buf) + 1);
              //ENCRYPT
              //WRITE TO NEW PIPE, CLIENT WILL READ IT
              break;
              }
        exit(0);
       }
}

Any  help would be appreciated, I am being kind of vague... just because Im not sure what exactly to ask


Thanks
Comment
Watch Question

Commented:
Hi KrAzY, you had accidentally duplicated your question.  I refunded your 200 point in the other and deleted it.  Thanks to Expert Axter who alerted us.

ComTech
Community Support Moderator
Krazy,
Your problem is that the forked processes are not running in the same memory space.  Fork make a copy of the code pages for each forked process, so after the fork, if you modify something in one process, it will NOT be modified in the other.  To access the arrays you want, you would have to use a semaphore, or some form of interprocess communication to tell the other processes what memory has changed.

A better solution would be to look at using the pthread library.  This will cause the same effect as forking a process, however you will still be utilizing the same memory "space" if you will.  That will allow you to access the array in both threads the way you have attempted to above.

Look at the pthread_create, pthread_join, and pthread_detach functions.  You should be able to just "man" for them if you're on a UNIX/Linux platform.

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "BlackDiamond" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, force-accepted by
Netminder
CS Moderator

Experts: please check for "Points For" questions -- there are a bunch in this TA.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.