Link to home
Start Free TrialLog in
Avatar of KrAzY
KrAzY

asked on

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

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
Avatar of ComTech
ComTech

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
ASKER CERTIFIED SOLUTION
Avatar of BlackDiamond
BlackDiamond

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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: https://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.