0

I have this assignment where I have to create a program on C/C++ for linux, where it sends a string char by char from one process to another, and when the second process receives the characters it uppercase them.

This is the code I wrote, if someone can tell me if the sharing process is done right or I'm missing something.

#include <iostream.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int childpid;
    char *string = "Hello";
    char *letter;

    childpid = fork();

    if(childpid == 0) {
        for (int i = 0; i<string.length; i++) {
            letter = string[i];
            printf(letter);
        }
    } else {
        read(letter);
        printf(letter.toupper());
    }

    return 0;
}

Code update:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    pid_t pid;
    int mypipe[2];
    int ret;
    char *string = "Hello";
    char *buf[20];
    int bytesread;

    ret = pipe(mypipe);

    if(ret == -1) {
        perro("Pipe failed");
        exit(1);
    }

    pid = fork();

    if(pid == 0) {
        for(var i = 0; i<string.length; i++) {
            write(mypipe[1],string[i],1);
        }
    } else {
        while( (bytesread = read(mypipe[0],buf,15)) > 0) 
        {
            buf[bytesread] = '\0';
            printf("buf: %s\n", buf.toupper()); 
        }
    }

    return 0;
}
8
  • 3
    You might want to read about pipe and dup2. Commented Mar 29, 2014 at 14:10
  • You may want to look at unix pipes. Type man 2 pipe in a terminal. Commented Mar 29, 2014 at 14:11
  • Your update looks basically ok. (1) read in loop. You'll probably be ok in this toy example but you are not guaranteed to get your entire read request in one call. (2) close your pipe ends. It's not just good hygiene, it's how EOF is sent to the other side. Commented Mar 29, 2014 at 14:59
  • so i need to run the same loop in the parent process as I did in the child, and inside it read is that what you're saying ? Commented Mar 29, 2014 at 15:01
  • along the lines of "while ((bytesread = read(...)) > 0)" Commented Mar 29, 2014 at 15:07

1 Answer 1

2

When you fork() the string variable is present in parent as well as child process. You are simply making toupper() in one of the process. What you are missing is that there is no communication between parent and child. Parent is suppose to pass the char to the child and then the child should make it toupper(). As suggested one way to achieve this communication is through pipe.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you check my update and tell me if I did it correct.
It looks good. Your read needs to be in loop. Refer sample. Note how hard coding of third parameter in read and write is avoided. Also I guess you are writing C program (and not a C++ program). So remove iostream inclusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.