Solved

array search, multiple processes

Posted on 2011-03-22
5
451 Views
Last Modified: 2012-05-11
I want to search for a key in an array, dividing it to k parts, and using k child processes.
But my code has problem...
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main ()
{
   int k=2;//number of childs
   int N=10;//array length
   int A[]={2,6,-8,9,10,1,6,3,7,4};
   int i;int key=1;//we search key in array A
   int position=-1;
   for (i=1;i<=k;i++){//create k childs
       int pid=fork();
       if (pid==0){//child
          int start=(N/k)*i;
          int end=start+(N/k);
          int j;
          printf("\nchild%d pid %d ppid %d\n",i,getpid(), getppid());
          for(j=start; j<end; j++){
                       if (A[j]==key) {position=j;printf("position=%d\n",position);break;}
          }
          exit(0);//child terminates
       }
      else if (pid>0){//parent         
          printf("\nparent pid %d \n",getpid());
          wait(0);
       }
   }
return 0;

}

Open in new window

0
Comment
Question by:dimi67
  • 4
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 35193609
what problem does it have?
did you want the children to run simultaneously?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35193753
For one thing you start at i=1
then start=(N/k)*i is never 0
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35193768
Also, when doing a fork (or any library call that can err), you should always test for an error.
Here is an example that illustrates how to check for errors:
    http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35193816
In each child, you should print out the start and end values to see another problem.

This was not a problem; but here is a nice thing to do so that you can initialize the array without having to modify the size.
   int A[]={2,6,-8,9,10,1,6,3,7,4};  
   int N=sizeof(A)/sizeof(A[0]);//array length

Open in new window


Also, remember that arrays run from 0 .. N-1 where N is the number of elements in the array.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35194224
Take a look at this link to see how to wait for multiple children:
     http://www.linuxforums.org/forum/programming-scripting/175801-waiting-multiple-childs-c-waitpid.html

To test for error (which I always recommend doing for any library call):
     if (!WIFEXITED(pidStatus) || WEXITSTATUS(pidStatus) != 0) ...
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux ssh 4 57
Acrinis True image 2 54
Codiing Non-Existent Links 4 64
I think my Ubuntu 12.10 box is hacked, but not sure... 13 49
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Fine Tune your automatic Updates for Ubuntu / Debian
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now