Solved

array search, multiple processes

Posted on 2011-03-22
5
448 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

706 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

18 Experts available now in Live!

Get 1:1 Help Now