Search code examples
clinuxprocesspipesignals

Linux C language pipes, forks and signals


I need help with a homework assignment, the thing is I have to create a simulation of a cans assembly line, by using processes, pipes, forks and signals, I can't use another IPC mechanism as mutex or semaphores.

I know how to fork a process and have a child, in this case, I need to create a can and then pass it through the several stations (child processes) one by one, the stations are like melting, painting, packaging, and like these.

Should I use a structure for the can? or the PID of a child process?

The thing is how can I send a can from station A to station B, then from B to C and that, using pipes, I know how to send a message using a pipe from parent to one child, but in this case I need to communicate all the childs (stations)

further, this has to be on a GUI using QT or GTK (I prefer GTK even I don't know it well seems more simple) and the program needs that if I pause the simulation it has to tell me how many cans were packaged, which station has cans on it, and generate a final report, by each 100 cans, they form a package of cans, I need to say how many packages were made.

I had read a lots of guides, saw some videos, try some examples, but I just can't get the idea of how to know in which child am I currently so I can know which station is and know what to do in that station, and I don't know how to pass the can through all the stations one by one.


Solution

  • The proposal from comments, in pseudocode (turning into C is homework, so I'm not doing that):

    make P-A pipe
    make A-B pipe
    make B-C pipe
    
    if fork()
        // process A
        while (read can from P-A pipe)
            do something to can
            write can to A-B pipe
    else if fork()
        // process B
        while (read can from A-B pipe)
            do something to can
            write can to B-C pipe
    else if fork()
        // process C
        while (read can from B-C pipe)
            do something to can
            write can to final output
    else
        // parent
        get can from input/file (or make a new can)
        write can to P-A pipe