The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process find grep find mtime find null character in a unix file glance unix grep multiple lines grep or grep recursive gzip password hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script

View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 02-22-2005
Perderabo's Avatar
Perderabo Perderabo is online now
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,253
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
Reply With Quote