简体   繁体   中英

parent-child process communication through pipes

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);
}

}

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.

Are we sure your system has more than one core? If it has only one processor, the processes will run one after the other anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM