Should I use sleep() function in C for long durations? [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a simple C program that will be running on my raspberry Pi. I am planning on taking data from sensors with an interval of 10-15 mins. Should I sleep()
the C program for this period in a loop then have it take the readings so on. Or should I not have a loop at all and have a command in cron tab to run the C program after every 15 mins or so. What are the advantages/disadvantages of sleep() in this case or is there a better approach this ?

Is data available in same machine where C program is running?
If not same, better to
1) have a small C collect data from sensor
2) have a cron task that runs every 15 min, and that invokes your C program
3) This way if network connection breaks between your C program and sensor where data available will not be a problem.
Also this approach helps you if any memory leak is there, also that would not be a problem.

Related

Does an interrupt handler have a timeout? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Testing a code that uses interrupts, I forced with a while(1); to try to remain in the interrupt handler, but I saw that the interrupt handler leaves and returns to main, so I assume it has some kind of timeout. If so, is it particular to the ISR, or it is a standard feature of interrupts?
It is not a standard feature of interrupts. You don't say what platform you are using, but in general, interrupt handlers should do only what is necessary, and return. The less time spent in the handler, the better.
It is possible your program has a watchdog timer somewhere, and when starved for processing time (because the ISR hung), the timer fires and is designed to exit your program.

Forcing a sensor to sleep on RaspberryPi [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to microprocessor programing and currently have a RGB sensor which reads an RGB value and increments a variable by an arbitrary number. I want the sensor to turn off for 0.3 seconds when I reach a certain value. Is there a way to do this or will I have to figure out a different way to throw out all the values the RGB sensor receives during that 0.3 second time span? I am writing in C.
Note: The sensor I am currently using is a TCS230.
According to the datasheet pin #3 is Output Enable ('OE, active low). So if you drive this pin high it should cut off the chip's output.
Or more to your question, it looks like if you drive pins S0 and S1 both low, it will place the chip in a "Power Down" state.
Whichever option you choose depends on what's more important. Do you want quickest reaction time, or do you want to conserve power? If you want the quickest reaction time, use 'OE. There is a typical 100ns delay between asserting this signal and the chip responding. The downside is the chip is still running during this whole time. If you choose the Power Down state, then you will save energy vs the Output Enable option, but the photodiodes have a typical 100 microsecond "recovery from power down" delay. Obviously that's a factor of 1000, and if you're doing time-critical work, probably not the best option.
Keep in mind, I have never used this chip in my life, just basing my answer a quick read of the datasheet.

bit banging i2c trouble shooting [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am trying to implement bit-banging i2c to communicate between an atmega128A's GPIO and an SHT21 (I2C bus was used for some other devices). The first task is to send a write sequence to the SHT21. Using an oscilloscope, I can see that the sequence sending out from the atmega has the correct start signal, correct order of bits, correct stop signal, signal levels looks correct. The serial analyzer from the scope reads out correct I2C message: S80W~A . Yet there is not any ACK response from the SHT21. The SDA and SCL are both pulled up to 3.3V via 6.7K resistors.
I really need help finding out what is wrong
Thank you so much.

C program that waits for user input for a specific number of seconds [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can you create C program that waits for user input for a specific number of seconds? After the time limit, the program closes, with or without input. Sample code please, using fork() and sleep(). Sorry I'm new at this.
Whoa. Sorry guys. This is not my post. Seems like someone used my account. And I can't delete it.
If you just want to sit the program down and wait... Make a loop checking for the input, use save the start time of the clock in a variable. Update the end time. Check in the loop if the time (here 5 seconds) has expired.
begin_t = clock();
// do-loop
/* read user input*/
end_t = clock();
// while( end_t - begin_t < 5 * CLOCKS_PER_SEC )
In my opinion usng fork() and sleep() is not the best way to achieve such a result. It's much better to use select() call which allows to wait for data with a timeout.
See the unix manual page on select() for some exemplary code.
The correct way to do this is to select STDIN for reading and set the timeout for however long you want. Select will either return STDIN as available for reading or return nothing, which indicates a timeout.
http://linux.die.net/man/2/select

Qmail - Change maximum retry time [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Hello I am running Plesk 10.4.4 with Qmail and noticed that one of my customers was spammed with an invalid address to which qmail kept trying to reply to. This unfortunately caused a major pile up (over 100 emails) in the system with some retries being held up for over 7 days and any new emails trying to go out would take up to 2 hours even if the addresses are correct.
Is there any way to tell qmail to not keep retrying and delete anything from queue that is over 2 hours old?
echo "7200" > /var/qmail/control/queuelifetime
/etc/init.d/qmail restart
7200 - it's 2 hours * 60 minutes * 60 seconds
But I think that 2 hours may be not enough, 1 or 2 days is OK for me.

Resources