Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

array search, multiple processes

Posted on 2011-03-22
5
Medium Priority
?
468 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 85

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 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 …
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

926 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