LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   infinite loop (https://www.linuxquestions.org/questions/programming-9/infinite-loop-217631/)

beginner_84 08-14-2004 04:57 PM

infinite loop
 
int main()
{
int i1,i2;
int i,j,k,count1,count2;
int a[100],count,find,to_child1[2],to_child2[2];
scanf("%d",&count);
for(i=0;i<count;++i)
scanf("%d",&a[i]);
scanf("%d",&find);
pipe(to_child1);
pipe(to_child2);
if(fork()==0)
{
j=dup(to_child1[1]);
for(i=0;i<=count/2;++i)
{
printf("%d\n",i);
if(a[i]==find)
{
write(j,i,sizeof(i));
exit(0);
}
}
i=-1;
write(j,i,sizeof(i));
exit(0);
}
if(fork()==0)
{
j=dup(to_child2[1]);
for(i=count-count/2;i<count;++i)
{
printf("%d\n",i);
if(a[i]==find)
{
write(j,i,sizeof(i));
exit(0);
}
}
i=-1;
write(j,i,sizeof(i));
exit(0);
}
j=dup(to_child1[0]);
k=dup(to_child2[0]);
i=-1;
count1=read(j,i1,sizeof(i1));
count2=read(k,i2,sizeof(i2));
printf("%d %d",count1,count2);
if(i1==-1)
{
if(i2==-1)
printf("-1");
else printf("%d",i2);
}
else printf("%d",i1);
}



can ne1 plz say why is this getting into infinite loop its supposed to take n integers and search for a number in 2 child process if found the child returns index else they return -1

infamous41md 08-14-2004 05:39 PM

USE CODE TAGS TO POST CODE.

kev82 08-14-2004 06:11 PM

as infamous41md points out above(quite harshly) your code would be easier to read if it were formatted in some way but as this is your first post i think we can be quite forgiving.

if you dont know what code tags are check here

anyway back to your question, im pretty sure that pipes block any read/write attempts unless there is exactly one read descriptor and one write descriptor open, hence after the fork() you have to close the appropriate descriptor before you can transfer data through the pipe. check out my links(in my profile page) for an ebook called Advanced Linux Programming, it has a chapter on pipes you can read to see if your doing stuff right.

osvaldomarques 08-14-2004 06:56 PM

Hi beginner_84,
Your code is a little ugly so I needed to do some changes to understand what you were trying to do. After understand it, I found you are improperly using an integer variable as the buffer pointer to your reads and writes. This code is still not behaving properly as its exit code is always different from zero but the child processes are working now.
Code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
  int i1
    ,i2;
  int i
    ,j
    ,k
    ,count1
    ,count2;
  int a[100]
    ,count
    ,find
    ,to_child1[2]
    ,to_child2[2]
    ;
  printf("Enter count: ");
  scanf("%d",&count);
  for(i=0;i<count;++i)
  {
    printf("Enter element # %d: ", i + 1);
    scanf("%d",&a[i]);
  }
  printf("Enter find element: ");
  scanf("%d",&find);
  pipe(to_child1);
  pipe(to_child2);
  if(fork()==0)
  {
    j=dup(to_child1[1]);
    for(i=0;i<=count/2;++i)
    {
      printf("line %d: %d\n", __LINE__, i);
      if(a[i]==find)
      {
        write(j,&i,sizeof(i));
        exit(0);
      }
    }
    i=-1;
    write(j,&i,sizeof(i));
    exit(0);
  }
  if(fork()==0)
  {
    j=dup(to_child2[1]);
    for(i=count-count/2;i<count;++i)
    {
      printf("line %d: %d\n", __LINE__, i);
      if(a[i]==find)
      {
        write(j,&i,sizeof(i));
        exit(0);
      }
    }
    i=-1;
    write(j,&i,sizeof(i));
    exit(0);
  }
  j=dup(to_child1[0]);
  k=dup(to_child2[0]);
  i=-1;
  count1=read(j,&i1,sizeof(i1));
  count2=read(k,&i2,sizeof(i2));
  printf("line %d: %d %d\n", __LINE__, count1,count2);
  if(i1==-1)
  {
    if(i2==-1)
      printf("-1");
    else
      printf("line %d: %d\n", __LINE__, i2);
  }
  else
    printf("line %d: %d\n", __LINE__, i1);
}


infamous41md 08-14-2004 08:00 PM

kev << LOL if you think that was harsh u realllly don't know me :]

OP <<
1) you have a buffer overflow. you can't ask the user to enter an index into an array without checking that value against the bounds of the array.
2) you dont check ANY functions for error returns. it's impossible to debug a program without error checking.
3) why are you dup'ing the pipes? there's no need for that, just read or write to them as necessary.

beginner_84 08-15-2004 02:32 AM

thx a lot kev ur advice did help and so too osvaldomarquesi didnt knew passing integers required & in write


All times are GMT -5. The time now is 01:10 AM.