Help needed on parallel processing using fork() in C

This is a discussion on Help needed on parallel processing using fork() in C within the C Programming forums, part of the General Programming Boards category; Hello, i need to use fork() for the below problem Problem statement is program would accept various 2d arrays as ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Unhappy Help needed on parallel processing using fork() in C

    Hello,

    i need to use fork() for the below problem

    Problem statement is program would accept various 2d arrays as inputs as long as the user wishes to enter.. Every individual sum of the array is calculated and sum of each array becomes the element of a resultant array and then the output would be the sum total of resultant array.My aim is to find and compare the computation speed by implementing the problem in two ways one is the normal program in C which would take care of the above and the other using fork() (I have implemented using the normal method whose code is pasted after the example. I need to know hw it can be implemented using fork() and how parallel processing of this problem can be done). Plz help!!!

    Example :
    number of matrices to be entered = 2
    dimension of 1st matrix = 2x2
    1st matrix : 1 2
    3 4
    dimension of 2nd matrix = 2x3
    2nd matrix 2 1 3
    1 3 5

    Resultant matrix : 10 (sum of elements of 1st matrix)
    15(sum of elements of 2nd matrix
    sum of resultant : 25

    Code :

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<alloc.h>
    #include <time.h>

    int main()
    {
    int num,p,q,i,j,k,result=0,array_result = 0;
    int arr[10];
    int **a;
    clrscr();
    printf("Enter the number of matrices : ");
    scanf("%d",&num);
    for(k=0;k<num;k++)
    {
    printf("Enter the dimension of %d matrix : ",k+1);
    scanf("%d%d : \n",&p,&q);
    a=(int **)malloc(p*sizeof(int *));

    for(i=0;i<p;i++)
    {
    a[i] = (int *)malloc(q*sizeof(int));}


    for(i=0;i<p;i++)
    {
    for(j=0;j<q;j++)
    {
    scanf("%d",&a[i][j]);
    }

    result = sum(a,p,q);
    system("PAUSE");
    arr[k] = result;

    }


    {
    array_result = array_result + arr[k];
    }
    } printf("Resultant Array : Sum total of each input array \n");
    for(k=0;k<num;k++)
    printf(" %d\n",arr[k]);
    printf("Sum Total of Resultant Result : %d\n", array_result);
    printf("Elapsed time: %u secs.\n", clock()/CLOCKS_PER_SEC);


    return 0;
    }

    int sum(int **arr,int p,int q)
    {
    int res = 0,i=0,j=0;
    for(i=0;i<p;i++)
    for(j=0;j<q;j++)
    {
    res = res + arr[i][j];
    }
    return res;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I believe it will be difficult to pass the result back to the original process when using fork(), since although both processes have the same original memory content, whenever one or the other process WRITES to it's memory, it will be separated so that they have their own copy of the memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    plz let me know if there is any alternatives thru which it can be acheived? I need to proove that the computation speed using parallel processing is lesser than that of the normal method..

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if it's just the speed you want to know, I suppose fork would work. But if you also want to actually collate the results, I would suggest that a multithreaded approach would be more appropriate.

    Of course, you could use pipes or files to store the result and read the results from the main process - but it seems a bit crude and not really suitable for this task.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    could i get some futher help on this as to hw to proceed??

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. But the idea here is that you do the work, we advice, not we do the work, you sit back and relax.

    Which solution do you want? Just calculate the values independently in separate processes and not get a combined result, or to use threads to combine the result?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    yes.. i agree on that ..
    I need to perform the task of finding the sum of elements of each input array parallely and then get the combined result of all this sums.. not independently ..

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So have a look at the threading interface for your OS. Since you have been talking of fork, I presume it's Linux or some other Unix variant - which means that pthreads is probably what you want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Piping/Redirection/Background processing help needed
    By cr80expert5 in forum C Programming
    Replies: 1
    Last Post: 04-27-2009, 11:25 PM
  2. Parallel processing
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 04-27-2009, 12:23 PM
  3. Parallel processing with fork()
    By Mastiff in forum C Programming
    Replies: 7
    Last Post: 08-27-2008, 08:42 AM
  4. Help needed: Output to Parallel port.
    By Ingsy in forum C Programming
    Replies: 4
    Last Post: 10-10-2001, 01:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21