For my OS lab we have linux at college and the question is to find the factors of a number and pass it use pipe function from child to main process. I googled up the syntax of pipe and I find it difficult to understand the idea. You need a file pointer and all to pass an array back using pipe? Isn't the mechanism as simple as passing arguments to functions or returning values from a function? Please help

Recommended Answers

All 2 Replies

well by fork() you'll create a child process.
the pipe works like this: on one end you write something and on the other end you read.
the pipe is done like this, I'll write it in pseudocode:

    int a2b
    pipe(a2b)
    if fork()==0 then
        write(a2b[1], information, size)
        exit(0)
    endif
    wait(0)
    read(a2b[0], where, size)

so, when you're in the child process you'll solve the required problem, and write the answer to the pipe, then exit the child. Then, after the parent waits for the child to exit, he reads from the pipe the content.
Here, this is a great tutorial page about pipes, and UNIX processes: Read Me.

Here, to get you started. This computs the factors of a number:

#include <stdio.h>

int main(){
    int a, i, factors[200], elems=0;
    printf("Insert number: ");
    scanf("%d", &a);
    for (i=2;i<a/2+1;i++){
        if (a%i==0){
            factors[elems]=i;
            elems++;
        }
    }
    for (i=0;i<elems;i++){
        printf("Factors: %d\n", factors[i]);
    }
    return (0);
}
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.