Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

    To write a C program that will add elements of array using 3 child processes and a parent

    C
    c programming fork linux system calls unix
    2
    2
    13
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • darkpandawarrior
      darkpandawarrior last edited by

      To write a C program to take input of 30 integers in an array, create three new processes,
      first new process will sum the first 10 elements of array, second new process will sum next 10
      elements and third will sum last 10 elements of array. Parent process will do the sum of
      individual sum obtained by each child process.

      Reply Quote 0
        1 Reply Last reply

      • avan
        avan last edited by avan

        Hi @darkpandawarrior

        I have never done real programming in C but let's see if I can point you in the right direction. You are probably going to end up using global static variables

        static int *sum_10;
        

        So declare those outside of the main method.
        Then you will probably end up using mmap so something like

        sum_10 = mmap(NULL, sizeof *sum_10, PROT_READ | PROT_WRITE,
                           MAP_SHARED | MAP_ANONYMOUS, -1, 0);
        

        Then initialize each global variable somewhere in main

        *sum_10 = 0;
        

        Now you you have a set of global variables that can be shared with all processes which is the real challenge in this assignment.

        Hope you figure it out. Let me know if you need more help.

        Reply Quote 0
          1 Reply Last reply

        • First post
          Last post