Solved

array search, multiple processes

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

816 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

11 Experts available now in Live!

Get 1:1 Help Now