Question
Project Description: Your job is to modify this Node.js program (rabbit.js) by adding another entry for 55 months population. When you run this project, you
Project Description:
Your job is to modify this Node.js program (rabbit.js) by adding another entry for 55 months population.
When you run this project, you should see something like this in the browser:
If you click any links at the bottom, you should get something like this:
In the meantime your Node.js server log should give you the number of milliseconds it takes to calculate this number. See screenshot below.
As you can see from the timers, it takes significantly longer to calculate for 44 months. To make the matter worse, when the Node.js is calculating for 44 months population, the whole application gets stuck. This shows that Node.js uses a single thread to process multiple requests, so if one request causes a lot of CPU consumption in Node.js server, other requests have to wait.
Now add another entry for 55 months population, then try to click on it, your Node.js may hang on you and you may never get a response back. This is thread starvation. You have to stop the Node.js server and restart to recover.
CODE
/** * app.js * unit 04 lab * This program is a server-side JavaScript program that * simulates rabbit reproduction using Fibonacci sequence. * This project is adapted from text book example Listing 2-24 starveit.js on page 29. */ var http = require('http'); // function to calculate rabbit reproduction using fibonacci sequence function fibonacci(n) { if (n return 1; else return fibonacci(n - 2) + fibonacci(n - 1); } var server = http.createServer(function (request, response) { if (request.method == 'GET' && request.url == '/') { response.write(''); response.write(''); response.write('Rabbit Population Forcast '); response.write(''); response.write('Rabbit Population Forecast
') response.write('') response.write('
- '); response.write('
- 1 month population '); response.write('
- 10 months population '); response.write('
- 30 months population '); response.write('
- 44 months population '); response.write('
Pairs of rabbits after 1 month is: ' + fibonacci(1) + '
'); console.timeEnd('1m_timer') response.write('Rabbit Population Forecast
') response.write('') response.write('- '); response.write('
- 1 month population '); response.write('
- 10 months population '); response.write('
- 30 months population '); response.write('
- 44 months population '); response.write('
Pairs of rabbits after 10 months is: ' + fibonacci(10) + '
'); console.timeEnd('10m_timer'); response.write('Rabbit Population Forecast
') response.write('') response.write('- '); response.write('
- 1 month population '); response.write('
- 10 months population '); response.write('
- 30 months population '); response.write('
- 44 months population '); response.write('
Pairs of rabbits after 30 months is: ' + fibonacci(30) + '
'); console.timeEnd('30m_timer'); response.write('Rabbit Population Forecast
') response.write('') response.write('- '); response.write('
- 1 month population '); response.write('
- 10 months population '); response.write('
- 30 months population '); response.write('
- 44 months population '); response.write('
Pairs of rabbits after 44 months is: ' + fibonacci(44) + '
'); console.timeEnd('44m_timer'); response.write('Rabbit Population Forecast
') response.write('') response.write('- '); response.write('
- 1 month population '); response.write('
- 10 months population '); response.write('
- 30 months population '); response.write('
- 44 months population '); response.write('
Error: invalid request!
'); response.write(''); response.write(''); response.end(); } }); server.listen(3333); console.log("Server is running at http://localhost:3333"); Person 1X Rabbit Population Forc xStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started