Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:56:08+00:00 2026-06-13T21:56:08+00:00

I’m trying to learn how to work with fork() to create new processes and

  • 0

I’m trying to learn how to work with fork() to create new processes and pipes to communicate with each process. Let’s say I have a list that contains 20 words, and I create 3 processes. Now, I need to distribute the words between the processes using pipes, and the each process will sort the list of words it receives. The way I want to achieve this is like this:

Word1 => Process1
Word2 => Process2
Word3 => Process3
Word4 => Process1
Word5 => Process2
Word6 => Process3
.
.
.

So each process will have a list of words to sort, and eventually I’ll use MergeSort to merge all the sorted lists returned by each process. I’m not sure how to use pipes to communicate with each process (e.g. feed each process with a word). Any help that would put me on the right track would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:56:10+00:00Added an answer on June 13, 2026 at 9:56 pm

    Try this code for size. It uses a fixed number of child processes, but you can change that number by adjusting the enum MAX_KIDS (it was mostly tested with that set at 3; I later changed it to 5 just to make sure).

    #include <signal.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    typedef struct Child
    {
        FILE *fp_to;
        FILE *fp_from;
        pid_t pid;
    } Child;
    
    enum { P_READ, P_WRITE };   /* Read, write descriptor of a pipe */
    enum { MAX_LINE = 4096 };
    
    static void be_childish(void);
    static void distribute(size_t nkids, Child *kids);
    static void err_exit(const char *fmt, ...);
    static void merge(size_t nkids, Child *kids);
    static void wait_for_kids(size_t nkids, Child *kids);
    
    static int make_kid(Child *kid)
    {
        int pipe1[2];   /* From parent to child */
        int pipe2[2];   /* From child to parent */
        if (pipe(pipe1) != 0)
            return -1;
        if (pipe(pipe2) != 0)
        {
            close(pipe1[P_READ]);
            close(pipe1[P_WRITE]);
            return -1;
        }
        if ((kid->pid = fork()) < 0)
        {
            close(pipe1[P_READ]);
            close(pipe1[P_WRITE]);
            close(pipe2[P_READ]);
            close(pipe2[P_WRITE]);
            return -1;
        }
        else if (kid->pid == 0)
        {
            dup2(pipe1[P_READ], STDIN_FILENO);
            dup2(pipe2[P_WRITE], STDOUT_FILENO);
            close(pipe1[P_READ]);
            close(pipe1[P_WRITE]);
            close(pipe2[P_READ]);
            close(pipe2[P_WRITE]);
            /* Reads standard input from parent; writes standard output to parent */
            be_childish();
            exit(0);
        }
        else
        {
            kid->fp_to   = fdopen(pipe1[P_WRITE], "w");
            kid->fp_from = fdopen(pipe2[P_READ], "r");
            close(pipe1[P_READ]);
            close(pipe2[P_WRITE]);
            return 0;
        }
    }
    
    int main(void)
    {
        enum { NUM_KIDS = 5 };
        Child kids[NUM_KIDS];
        struct sigaction act;
    
        sigfillset(&act.sa_mask);
        act.sa_flags   = 0;
        act.sa_handler = SIG_DFL;
        sigaction(SIGCHLD, &act, 0);
    
        for (int i = 0; i < NUM_KIDS; i++)
        {
            if (make_kid(&kids[i]) != 0)
                err_exit("Fault starting child %d\n", i);
        }
    
        distribute(NUM_KIDS, kids);
        merge(NUM_KIDS, kids);
    
        wait_for_kids(NUM_KIDS, kids);
        return(0);
    }
    
    static void err_exit(const char *fmt, ...)
    {
        va_list args;
        va_start(args, fmt);
        vfprintf(stderr, fmt, args);
        va_end(args);
        exit(1);
    }
    
    static int qs_compare(const void *v1, const void *v2)
    {
        const char *s1 = *(char **)v1;
        const char *s2 = *(char **)v2;
        return(strcmp(s1, s2));
    }
    
    static char *estrdup(const char *str)
    {
        size_t len = strlen(str) + 1;
        char *out = malloc(len);
        if (out == 0)
            err_exit("Out of memory!\n");
        memmove(out, str, len);
        return(out);
    }
    
    static void be_childish(void)
    {
        char **lines = 0;
        size_t num_lines = 0;
        size_t max_lines = 0;
        char input[MAX_LINE];
    
        while (fgets(input, sizeof(input), stdin) != 0)
        {
            if (num_lines >= max_lines)
            {
                size_t n = (2 * max_lines + 2);
                void *space = realloc(lines, n * sizeof(char *));
                if (space == 0)
                    err_exit("Out of memory!\n");
                lines = space;
                max_lines = n;
            }
            lines[num_lines++] = estrdup(input);
        }
    
        qsort(lines, num_lines, sizeof(char *), qs_compare);
    
        for (size_t i = 0; i < num_lines; i++)
        {
            if (fputs(lines[i], stdout) == EOF)
                err_exit("Short write to parent from %d\n", (int)getpid());
        }
    
        exit(0);
    }
    
    static void distribute(size_t nkids, Child *kids)
    {
        char   input[MAX_LINE];
        size_t n = 0;
    
        while (fgets(input, sizeof(input), stdin) != 0)
        {
            if (fputs(input, kids[n].fp_to) == EOF)
                err_exit("Short write to child %d\n", (int)kids[n].pid);
            if (++n >= nkids)
                n = 0;
        }
    
        /* Close pipes to children - let's them get on with sorting */
        for (size_t i = 0; i < nkids; i++)
        {
            fclose(kids[i].fp_to);
            kids[i].fp_to = 0;
        }
    }
    
    static void read_line(Child *kid, char *buffer, size_t maxlen, int *length)
    {
        if (fgets(buffer, maxlen, kid->fp_from) != 0)
        {
            *length = strlen(buffer);
            buffer[*length] = '\0';
        }
        else
        {
            buffer[0] = '\0';
            *length = -1;
            fclose(kid->fp_from);
            kid->fp_from = 0;
        }
    }
    
    static int not_all_done(size_t nkids, int *lengths)
    {
        for (size_t i = 0; i < nkids; i++)
        {
            if (lengths[i] > 0)
                return 1;
        }
        return 0;
    }
    
    static void min_line(size_t nkids, int *len, char **lines, size_t maxlen,
                         Child *kids, char *output)
    {
        size_t  min_kid = 0;
        char   *min_str = 0;
        for (size_t i = 0; i < nkids; i++)
        {
            if (len[i] <= 0)
                continue;
            if (min_str == 0 || strcmp(min_str, lines[i]) > 0)
            {
                min_str = lines[i];
                min_kid = i;
            }
        }
        strcpy(output, min_str);
        read_line(&kids[min_kid], lines[min_kid], maxlen, &len[min_kid]);
    }
    
    static void merge(size_t nkids, Child *kids)
    {
        char line_data[nkids][MAX_LINE];
        char *lines[nkids];
        int  len[nkids];
        char output[MAX_LINE];
    
        for (size_t i = 0; i < nkids; i++)
            lines[i] = line_data[i];
    
        /* Preload first line from each kid */
        for (size_t i = 0; i < nkids; i++)
            read_line(&kids[i], lines[i], MAX_LINE, &len[i]);
    
        while (not_all_done(nkids, len))
        {
            min_line(nkids, len, lines, MAX_LINE, kids, output);
            fputs(output, stdout);
        }
    }
    
    static void wait_for_kids(size_t nkids, Child *kids)
    {
        int pid;
        int status;
    
        while ((pid = waitpid(-1, &status, 0)) != -1)
        {
            for (size_t i = 0; i < nkids; i++)
            {
                if (pid == kids[i].pid)
                    kids[i].pid = -1;
            }
        }
    
        /* This check loop is not really necessary */
        for (size_t i = 0; i < nkids; i++)
        {
            if (kids[i].pid != -1)
                err_exit("Child %d died without being tracked\n", (int)kids[i].pid);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.