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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-14-2004, 05:57 PM
|
#1
|
LQ Newbie
Registered: Aug 2004
Posts: 11
Rep:
|
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
|
|
|
08-14-2004, 06:39 PM
|
#2
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
USE CODE TAGS TO POST CODE.
|
|
|
08-14-2004, 07:11 PM
|
#3
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
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.
|
|
|
08-14-2004, 07:56 PM
|
#4
|
Member
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519
Rep:
|
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);
}
|
|
|
08-14-2004, 09:00 PM
|
#5
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
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.
|
|
|
08-15-2004, 03:32 AM
|
#6
|
LQ Newbie
Registered: Aug 2004
Posts: 11
Original Poster
Rep:
|
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 02:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|