Pipelining Processes


 
Thread Tools Search this Thread
Top Forums Programming Pipelining Processes
# 1  
Old 02-22-2005
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);
}

Now shouldn't it print out each ID in each process?

-tn
# 2  
Old 02-22-2005
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");
        }
   }
}

Output for 7 processes if it helps at all:
Quote:
Parent: 6282
Child: 6283
Parent: 6283
Child: 6284
Parent: 6284
Child: 6285
Parent: 6285
Child: 6286
Parent: 6286
Child: 6287
Parent: 6287
Child: 6288
ID: 7
-tn
# 3  
Old 02-22-2005
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
# 4  
Old 02-23-2005
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-23-2005 at 12:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Pipelining with tar

Hello, I am currently interning at a place and my job is to essentially learn UNIX. My supervisor gives me problems here and there to help guide me with my learning but for the most part I'm doing this all by self-teaching myself. Needless to say I have run into a few obstacles...for... (12 Replies)
Discussion started by: huntreilly25
12 Replies

2. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

3. UNIX for Dummies Questions & Answers

Processes

Hi all, I needed a little help. It's OS thing. Suppose I have 2 machine connect over a network, I will call my machines as M1and M2. If I copy a files from M1 to M2. What tasks are these two machine performing for copy to work. My assumption is that M1 is performing read action and... (1 Reply)
Discussion started by: vishwesh
1 Replies

4. Shell Programming and Scripting

Bash pipelining

Hello, There is a symbolic link in a folder. I would like to read destination of this link and get base name of pointed file. Let's say, there is symlink : symlink -> ../file.txt. I can do that what I want by this script: readlink symlink | while read var do echo `basename $var` done but... (1 Reply)
Discussion started by: scdmb
1 Replies

5. Solaris

Identifying and grouping OS processes and APP processes

Hi Is there an easy way to identify and group currently running processes into OS processes and APP processes. Not all applications are installed as packages. Any free tools or scripts to do this? Many thanks. (2 Replies)
Discussion started by: wilsonee
2 Replies

6. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

7. Shell Programming and Scripting

Pipelining

My problem is more a question of how to do it more elegantly than how to do it at all. The problem: I have a pipeline which has to write to the screen AND to a logfile: proc1 | tee -a <logfile> What makes things difficult is i also need the return code of proc1. But proc1 | tee -a... (6 Replies)
Discussion started by: bakunin
6 Replies

8. Shell Programming and Scripting

I need some example of Co-Processes

I want to know how to work the Co-Processes in kornshell scripts. So, I very need some script about Co-Processes! thanks ...:) (3 Replies)
Discussion started by: javalee
3 Replies

9. UNIX for Dummies Questions & Answers

processes

What command string will locate ONLY the PID of a process and ouput only the number of PID of the process? (1 Reply)
Discussion started by: mma_buc_98
1 Replies

10. UNIX for Dummies Questions & Answers

co-processes

Is it possible to have a main script (i will call it main.ksh) that executes say, 4 other scripts (sub_prog_1.ksh, sub_prog_2.ksh etc..) from within this main.ksh (simultaneously/in parallel), have them run in the background and communicate back to main.ksh when complete? My guess is to use... (1 Reply)
Discussion started by: google
1 Replies
Login or Register to Ask a Question