简体   繁体   中英

How can I prevent the printings of different child processes from overlapping

this is my 1st question here so be gentle :) ! My code basically creates 3 processes via forking:

READER - that's in charge of reading triangles from stdin and writing them to both pipes (explained in a moment).

PERI - in charge of reading triangles from the pipe, calculating their perimeters and printing them.

AREA - likewise, except for the area.

My main creates 2 pipes for both PERI and AREA to be able to communicate with READER. So in a nutshell - READER gets data and writes it to both pipes, while AREA and PERI read from respective pipes and do their jobs. In addition, after each process knows it's finished the job...it should print a message to stderr. My problem is that the messages overlap.

Here is the code for the AREA proc for example. The PERI is almost identical:

void startAreaProc(int* readerAreaPipe, int* readerPeriPipe)
{
    struct triangle currTriangle;
    int count = 0;
    int numTriangles = 0;

    close(readerAreaPipe[1]);   //AREA does not write to the pipe
    close(readerPeriPipe[0]);
    close(readerPeriPipe[1]);
    while(1)
    {
        count = read(readerAreaPipe[0], &currTriangle, sizeof(struct triangle));
        if(count == 0)
        {
            break;
        }

        numTriangles++;
        //sleep(1);
        calc_area(&currTriangle);
    }

    close(readerAreaPipe[0]);  //finished reading - cleanup
    //sleep(1);
    fprintf(stderr ,"AREA pid %d processed %d triangles.\n", getpid(), numTriangles);
}

The main problem is that the printings of AREA and PERI often overlap...for example this is a typical output for an input of 1 triangle: "AREA pid PERI pid 34603488 processed processed 11 triangles. triangles." instead of "AREA pid 3460 processed 1 triangles." followed by the other.

NOTE - i've noticed that the printings of the actual perimeters and areas for each triangle (not the msgs printed at the end of each process in the example) don't overlap, and those are printed with printf instead of fprintf (the end-process msgs need to go to stderr, that's why I used fprintf)..

help please???

NOTE - i've noticed that the printings of the actual perimeters and areas for each triangle (not the msgs printed at the end of each process in the example) don't overlap, and those are printed with printf instead of fprintf

The code snippet you posted here does not print the area of any triangles. I'm assuming that AREA prints into the pipe and then READER prints on the screen. If that's the case, then you should really not expect the ouput to be mangled, because the only program printing those messages to the screen (READER) is doing so sequentially.

It looks to me that PERI and AREA stop when READER closes its write-side of the pipes. The easiest solution in my mind would be for you to close PERI's read pipe first and wait for it to write a "good-bye" message on the other pipe before you close AREA's pipe.

NOTE - i've noticed that the printings of the actual perimeters and areas for each triangle (not the msgs printed at the end of each process in the example) don't overlap, and those are printed with printf instead of fprintf

If the line buffering behavior of printf (ie stdout ) satisfies your demands, you can achieve the same for fprintf(stderr, …) by writing this at the start of your program:

    setvbuf(stderr, NULL, _IOLBF, BUFSIZ);      // make stderr line buffered

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM