Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:25:52+00:00 2026-05-26T15:25:52+00:00

I am tryimg to write a C program that will search an array of

  • 0

I am tryimg to write a C program that will search an array of integers for another given integer. However, to speedup the search, the search is done in parallel by two child processes. The parent process reads in the number of integers, and then the integers in an array. It also reads in the integer to be searched. It then creates two child processes. The first child process searches the first half of the array, and the second child process searches the second half. If the integer is found, its index in the array is sent to the parent through a pipe. If it is not found, a -1 is sent to the parent through a pipe. The parent waits for both child processes to finish and then prints an appropriate message.

I have consulted some books and this is what i came up with. There is a small problem though…the two child processes are running one after the other rather than parallel. what changes should i make?

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include <string.h>
void main ()
{
int i,status,num1,num2,num3,num4, fd1[2], fd2[2];
int a[1000];
char b[5],c[5],d[5],e[5];
pid_t pid1,pid2;

printf("\n\n\nEnter how many numbers - ");
scanf("%d",&num1);
//printf("\n\nEnter the %d numbers below -\n",num1);
for (i=0;i<num1;i++)
{
    printf("%d : ",i);
    scanf("%d",&a[i]);
}
printf("\n\nEnter the number to search - ");
scanf("%d",&num2);
pipe(fd1);
pipe(fd2);
pid1=fork();
if (pid1==0)
{

    printf("this is the child 1\n");        
    for (i=0;i<(num1/2);i++)
    {
        if (a[i]==num2) 
        {
            printf("found by process 1\n");
            sprintf(b,"%d",i);
            sprintf(c,"%d",-1);
            write(fd1[1],&b,4); 
            write(fd2[1],&c,4);
            //kill(0,1);
            break;
        }
        printf("%d\n",a[i]);

    }
    _exit ( EXIT_FAILURE ) ;
}
else 
if (pid1>0)
{
    pid2=fork();
    if (pid2==0)
    {
        printf("this is the child 2\n");        
        for (i=(num1/2);i<num1;i++)
        {
            if (a[i]==num2) 
            {
                printf("found by process 2\n");
                sprintf(b,"%d",-1);
                sprintf(c,"%d",i);
                write(fd1[1],&b,4);
                write(fd2[1],&c,4);                             
                //kill(0,1);
                break;
            }

            printf("%d\n",a[i]);

        }
        _exit(EXIT_FAILURE);
    }

}

if (waitpid (pid1, &status, 0)>0 && waitpid (pid2, &status, 0)>0)
{   

    read(fd1[0],d,4);
    read(fd2[0],e,4);
    num3=atoi(d);       
    num4=atoi(e);
    if (num3>0) printf("value of i is %d\n",num3);  
    if (num4>0) printf("value of i is %d\n",num4);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:25:53+00:00Added an answer on May 26, 2026 at 3:25 pm

    Your code is fine, but remember that processor time is given out in rather large slices. That is, a short computation is often finished before the other process has even received a processor time slice. Insert a few calls to sleep into your loops, and you will see the concurrency.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a fragment program that will take a texture and
Hey there. I'm trying to write a small program that will read the four
I am trying to write a program that displays the integers between 1 and
i am trying to write a program that close explorer then runs another program.
I'm trying to write a simple program that will receive a string of max
I am trying to write a java program that will automatically download and name
I am trying to write a Prolog program that will print out the male
I'm trying to write a simple program that will compare the files in separate
I'm trying to write a program in Scala, that will accept SOAP-requests, get the
I am writing a C program that will read and write from/to a serial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.