Solved

array search, multiple processes

Posted on 2011-03-22
5
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Fine Tune your automatic Updates for Ubuntu / Debian
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

696 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