Unix-plug-monitor-child-progress

I have an application in which a parallel processing bit will be useful. For discussion purposes, suppose that there is a directory with 10 text files in it, and I want to run a program that deploys 10 processes, each of which takes one of the files and the top size of the file contents. I acknowledge that a parent program can wait for children to finish using one of the wait functions or using the select function .

I would like the parent process to track the progress of each forked process and display something like a progress bar when processes start.

My question is.

What would be reasonable alternatives for branching processes to pass this information to parents? What IPC methods are reasonable to use?

+3
source share
6 answers

In such a situation, when you want to track progress, the easiest alternative is to use shared memory. Each process updates its current value (for example, an integer) in a shared memory block, and the main process regularly reads the block. Basically, you do not need locking in this scheme. In addition, this application is of the type “poll”, because the wizard can read information whenever it wants, so you do not need event processing to process progress data.

+2

, " ?",

while (jobs_running) {
    pid = wait(&status);
    for (i = 0; i < num_jobs; i++)
        if (pid == jobs[i]) {
            jobs_running--;
            break;
        }
    printf("%i/%i\n", num_jobs - jobs_running, num_jobs);
}

. , , .

:

#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int child(int fd) {
    int i;
    struct timespec ts;
    for (i = 0; i < 100; i++) {
        write(fd, &i, sizeof(i));
        ts.tv_sec = 0;
        ts.tv_nsec = rand() % 512 * 1000000;
        nanosleep(&ts, NULL);
    }
    write(fd, &i, sizeof(i));
    exit(0);
}

int main() {
    int fds[10][2];
    int i, j, total, status[10] = {0};
    for (i = 0; i < 10; i++) {
        pipe(fds[i]);
        if (!fork())
            child(fds[i][1]);
    }
    for (total = 0; total < 1000; sleep(1)) {
        for (i = 0; i < 10; i++) {
            struct pollfd pfds = {fds[i][0], POLLIN};
            for (poll(&pfds, 1, 0); pfds.revents & POLLIN; poll(&pfds, 1, 0)) {
                read(fds[i][0], &status[i], sizeof(status[i]));
                for (total = j = 0; j < 10; j++)
                    total += status[j];
            }
        }
        printf("%i/1000\n", total);
    }
    return 0;
}

:

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>

int child(int *o, sem_t *sem) {
    int i;
    struct timespec ts;
    for (i = 0; i < 100; i++) {
        sem_wait(sem);
        *o = i;
        sem_post(sem);
        ts.tv_sec = 0;
        ts.tv_nsec = rand() % 512 * 1000000;
        nanosleep(&ts, NULL);
    }
    sem_wait(sem);
    *o = i;
    sem_post(sem);
    exit(0);
}

int main() {
    int i, j, size, total;
    void *page;
    int *status;
    sem_t *sems;
    size = sysconf(_SC_PAGESIZE);
    size = (10 * sizeof(*status) + 10 * sizeof(*sems) + size - 1) & size;
    page = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
    status = page;
    sems = (void *)&status[10];
    for (i = 0; i < 10; i++) {
        status[i] = 0;
        sem_init(&sems[i], 1, 1);
        if (!fork())
            child(&status[i], &sems[i]);
    }
    for (total = 0; total < 1000; sleep(1)) {
        for (total = i = 0; i < 10; i++) {
            sem_wait(&sems[i]);
            total += status[i];
            sem_post(&sems[i]);
        }
        printf("%i/1000\n", total);
    }
    return 0;
}

.. .

+2

( , - , , " " ):

  • fifos/named pipes
  • STDOUT
  • ( )
+1

, , , , , . pipe (2) , . , fd, - . ( , , , , , , --, , .)

+1

, - , , , . , , stdout/stderr ..

0

Boost.MPI . , :
www.boost.org/doc/html/mpi.html

0

Source: https://habr.com/ru/post/1704867/


All Articles