LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-14-2004, 05:57 PM   #1
beginner_84
LQ Newbie
 
Registered: Aug 2004
Posts: 11

Rep: Reputation: 0
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
 
Old 08-14-2004, 06:39 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
USE CODE TAGS TO POST CODE.
 
Old 08-14-2004, 07:11 PM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
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.
 
Old 08-14-2004, 07:56 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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);
}
 
Old 08-14-2004, 09:00 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 08-15-2004, 03:32 AM   #6
beginner_84
LQ Newbie
 
Registered: Aug 2004
Posts: 11

Original Poster
Rep: Reputation: 0
thx a lot kev ur advice did help and so too osvaldomarquesi didnt knew passing integers required & in write
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Infinite Loop ewt3y Programming 3 08-16-2005 10:48 AM
Linux Infinite Reboot Loop SNunweiler Linux - Software 2 05-10-2005 01:31 PM
Java Stopping an Infinite loop oulevon Programming 3 10-18-2004 11:11 PM
fetchmail stuck in infinite loop Prommy Linux - Software 0 02-17-2004 09:15 AM
Sendmail build - infinite loop?? kstarks Linux - Software 0 10-02-2003 08:13 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration