473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C Buffer Problem?

2
I'm having issues with a program I'm developing for a class. It is a Lucas Number calculation program that requires a pipe and fork to be used. The source code for the file can be found at <link removed>

Can anyone help me out? here's the output I'm getting:

Create the pipe

Enter a number: 10
Create the child process using fork
parent is running...
about to write to the pipe
after writing to the pipe

child is running...
about to read from the pipe
after reading from the pipe
the value of n is: 10
the value in the buffer is: Writing to

fib( 0) = 0
Calculating...elapsed time = 8
fib( 1) = 1072693248
Calculating...elapsed time = 9
fib( 2) = 1072693248
Calculating...elapsed time = 10
fib( 3) = 1073741824
Calculating...elapsed time = 11
fib( 4) = 1074266112
Calculating...elapsed time = 12
fib( 5) = 1075052544
Calculating...elapsed time = 13
fib( 6) = 1075838976
Calculating...elapsed time = 14
fib( 7) = 1076494336
Calculating...elapsed time = 15
fib( 8) = 1077215232
Calculating...elapsed time = 16
fib( 9) = 1078001664
Calculating...elapsed time = 17
{cs1:~/unix} fib(10) = 1078689792

I admit I'm a total noob to C so any help would be greatly appreciated. The program is compiled on a Sun Solaris server and is running there. Here's the definition for lucas numbers: http://en.wikipedia.org/wiki/Lucas_prime

Thank you!
Nov 15 '07 #1
1 2119
pakixd
2
Code:

Expand|Select|Wrap|Line Numbers
  1. // includes stdio and time libraries
  2.  
  3. float    fib(int);
  4. int    fork(void);
  5. void   sleep(unsigned);
  6.  
  7. int main(void)
  8. {
  9.    int n; // input variable
  10.    int i; // for the loop
  11.    int start = time(NULL); // get the time the process started
  12.         char buf[100]; /* store the characters read */
  13.  
  14.    // pipe needs an integer array of size 2
  15.         int fd[2];
  16.         printf("Create the pipe\n\n");
  17.         pipe(fd); /* fd[0] read; fd[1] write */
  18.  
  19.  
  20.     printf("Enter a number: ");
  21.     scanf("%d", &n)
  22.         printf("Create the child process using fork\n");
  23.    if (fork() == 0) {    // child process
  24.         close(fd[1]); /* close write end */
  25.         printf("child is running...\n");
  26.         printf("about to read from the pipe\n");
  27.         n = read( fd[0], buf, 100); /* child reads from pipe */
  28.         printf("after reading from the pipe\n");
  29.         printf("the value of n is: %d\n",n);
  30.         printf("the value in the buffer is: %s\n\n", buf);
  31.       for (i = 0; i <= n; ++i) {
  32.          printf("fib(%2d) = %d\n", i, fib(i));
  33.          sleep(1);
  34.         }
  35.     }
  36.    else    {            // parent process
  37.         close(fd[0]); /* close read end - not needed for this example */     
  38.         printf("parent is running...\n");
  39.         printf("about to write to the pipe\n");
  40.         write( fd[1], "Writing to pipe\n", n); /* parent writes to pipe */
  41.         printf("after writing to the pipe\n\n");
  42.      for (i = 0; i < n; ++i) {
  43.          sleep(1);
  44.          printf("Calculating...elapsed time = %d\n", time(NULL) - start);
  45.       }
  46.     }
  47.    return 0; // exits program
  48. }
  49.  
  50. float fib(int n) // fibonacci function, recursive
  51. {
  52.    if (n <= 1)
  53.       return n;
  54.    else
  55.       return (fib(n - 1) + fib(n - 2));
  56. //float fi = (1 + sqrt(5))/2;
  57.   //return (pow(fi,(float)n) - pow(-fi,-(float)n))/sqrt(5);
  58. }
Note currently im trying to debug this using the fibonacci recursive function but i need it to do lucas numbers, which is the commented code in the line at the end
Nov 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: gaool | last post by:
Hello, I wrote a python script (python 2.3.3) to send some commands to a programm and to show on the screen the responses of this programm. I use "Pipedream.py" module in my script to speak with...
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: Tim | last post by:
Hi, I'm experiencing some problem with the following code: st = File.Open(sFilename, FileMode.Open, FileAccess.ReadWrite) br = New BinaryReader(st) Do Until br.PeekChar = -1 Dim buffer()...
12
by: Jim Rodgers | last post by:
I have a big asp file that has an error under certain conditions -- totally repeatable. However, it only fails when I set response.buffer = True at the top. WHen I set it False in order to debug...
6
by: nickdu | last post by:
I usually try to stay away from _alloca(). However, I'm considering using it for a logging function. Our current logging function maintains its own buffer which it grows to fit the string being...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
4
by: aki | last post by:
Hi all, i am writing codes for implementing network protocols. i have written a method which is receiving a packet from network. i have assumed that the packet i will receive will be of type...
4
by: Aditya | last post by:
Hi I am using recv() from socket.h in one of my TCP-client projects. The problem is that the buffer variable in recv(socketDescriptor, buffer, flags) points to some stray location and when the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.