![]() |
|
|||||||
| Home | Forums | Register | Rules & FAQ | Donate | Members List | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, and other high level languages here. |
![]() |
|
|
Submit Tools | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Pipelining Processes
I am doing a program that will calculate the 199th fibonacci number using pipelines and multiple processes for each calculation. I have figured everything out except why none of the processes before the last one never execute the findfib() function;
Our teacher gave us the pipeline code to redirect stdin and stdout, but even when i execute the example file he gave us with it. It still is just running the findfib function at the last process only, I would go ask him about it but today he doesn't have any office hours. All the right headers are there. I have written the code for calculate the numbers using strings and whatnot but this is the only thing that is holding me back from completing. It seems as though the child never equals the parent which makes it not go into the if parent parent. Code:
int
main(int argc, char *argv[])
{
int i,nproc;
if(chkargs(argc,argv) == 0) exit(1);
nproc = strtol(argv[1],0,0);
mkpipeln(&i,nproc);
findfib(i,nproc);
exit(0);
}
void
mkpipeln(int *id, int np)
{ int fd[2]; pid_t child;
for(*id = 1; *id < np; (*id)++) {
if(pipe(fd) < 0) die("pipe-1 failed");
if((child = fork()) < 0) die("fork failed");
if(child > 0){
if(dup2(fd[1],1) < 0) die("dup2-1 failed");
if((close(fd[0]) < 0) || (close(fd[1]) < 0)) die("close-1 failed");
break;
}
else{
if(dup2(fd[0],0) < 0) die("dup2-2 failed");
if((close(fd[0]) < 0) || (close(fd[1]) < 0)) die("close-2 failed");
}
}
printf("id: %d\n", *id);
}
void
findfib(int id, int np)
{ printf("id: %d\n", id);
}
-tn |
| Forum Sponsor |
|
|
|
|||
|
Well I did a little more testing on it and the problem seems to be that the parent process is not breaking out of the for loop like it is suppose to and not executing findfib() until the last process.
Code:
void doall(int id, int np)
{
printf("ID: %d\n",id);
}
void mkpipeln(int *id, int np)
{ int fd[2]; pid_t child;
for(*id = 1; *id < np; (*id)++){
pipe(fd);
child = fork();
if(child > 0){
dup2(fd[1],1);
if((close(fd[0])<0)||(close(fd[1])<0)) die("close-1 failed");
break;}
else{
printf("Parent: %d\n",(int)getppid());
printf("Child: %d\n",(int)getpid());
dup2(fd[0],0);
if((close(fd[0])<0)||(close(fd[1])<0 )) die("close-1 failed");
}
}
}
Quote:
|
| Forum Sponsor |
|
|
|
||||
|
We have some rules here, and one of them is:
(6) Do not post classroom or homework problems. But this code looks like a mess. I'll bend the rules a tad and make a few remarks. They all apply to your first post. I'm ignoring your second post which seems to be a fragment that doesn't fit it the the first post. By default fd 1 is standard out and you can do a printf and see the result. Screw up fd 1, and now you have no more printf output. That is what you're doing here. I haven't run your code, but I've stared at it for awhile. I think the children are calling printf but with a trashed fd 1. This is one of several reasons why you should use stderr rather than stdout to debug stuff. So switch to fprintf(stderr,....) and stuff will be more clear. Think about the first child process... it does: dup2(fd[1],1); if((close(fd[0])<0)||(close(fd[1])<0)) die("close-1 failed"); break; so its fd 1 is no longer the terminal, it's the pipe. When the child printf's the data will be available to the parent on fd 0. Then child breaks out and dutifully calls printf, returns, and calls findfib. But it's all for naught since the parent never reads this data on fd 0. Worse than that, the parent loops and spawns another child. Each child in turn is connected briefly to the parent's fd 0. The last child stays connected, but the parent still never reads any data from the pipe. This program is not doing anything to find Fibonacci numbers. When I taught C, I had my students find a Fibonacci number via recursion which makes a bit more sense than this. And I would settle for the 20th Fibonacci number. F(199)=173402521172797813159685037284371942044301 and even if you use 64 bit unsigned integers, the largest integer available to you is 18446744073709551616. Special techniques are required to deal with very large integers and they are beyond what a beginning C programmer can handle. I take a dim view of your instructor's assignment, his code, and his policy of not being available to you for questions. This is why I bent the rules a bit here. Don't expect regular help with homework. Reference: The first 300 Fibonacci numbers, completely factorised |
|
|||
|
I know it is not right to post homework, but I wasn't asking for anyone to do the work, i was looking for something to point out something that I have been overlooking for the last 10 hours. Your right this program does not calculate the fibonacci numbers but it was a test program for the function that the teacher had given us in class to create the pipelines but when i ran the code i got the problem that i had posted, and was looking for a answer to the one problem I was having. Also his office hours were canceled today, he is usually around but today he wasn't. The only thing i was asking was how come the processes weren't executing properly with the code that the teacher had given us. I was just wondering if it was correct or if it was wrong because the Parent process never breaks out of the loop.
Like i had said I finished all the rest of the program, I am using c-strings (char *a, and char *b) to store the numbers in, to add them I have 3 variables, numa, numb, and carry. I reverse the 2 c-strings containing the numbers i want to add and reverse them, take the first numbers, add them, if there is a carry then it will be added to the next 2 numbers in the c-string. Then once that is done the c-string c contains the result and is sent with c-string b to the next process to calculate the next number in the sequence. Thank you for your help -tn Last edited by Trivialnight : 02-22-2005 at 10:13 PM. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Identifying and grouping OS processes and APP processes | wilsonee | SUN Solaris | 0 | 09-17-2007 07:18 PM |
| Pipelining | bakunin | Shell Programming and Scripting | 6 | 07-24-2006 09:45 AM |
| Run away processes | lowtaiwah | UNIX for Advanced & Expert Users | 2 | 02-16-2006 12:45 AM |
| I need some example of Co-Processes | javalee | Shell Programming and Scripting | 3 | 04-03-2004 09:12 AM |
| processes | mma_buc_98 | UNIX for Dummies Questions & Answers | 1 | 08-13-2002 04:25 PM |