Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-30-2003, 09:24 AM
|
#1
|
LQ Newbie
Registered: Sep 2003
Posts: 28
Rep:
|
how to stop a process?
how to i stop a program for a certain amount of time like in millisecs and micro secs? i try using sleep but it must be in integer.
|
|
|
11-30-2003, 09:32 AM
|
#2
|
Member
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555
Rep:
|
If you are talking C, try usleep(microseconds)
Regards
Martin
|
|
|
12-02-2003, 09:18 AM
|
#3
|
LQ Newbie
Registered: Sep 2003
Posts: 28
Original Poster
Rep:
|
i have done the usleep but then it dont really work well
how do i test whether the correct timing was done?
like supposed
sending A was to be done in 10us and B 500us and C 50us
how do i check whether the correct timing was done
|
|
|
12-02-2003, 10:54 AM
|
#4
|
Member
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555
Rep:
|
I have no idea. I don't think you can expect that kind of precision out of an operating system that doesn't claim to be an RTOS (Real Time OS).
Take a look at this little test:
Code:
#include <sys/timeb.h>
#include <unistd.h>
int main ()
{
struct timeb time1;
struct timeb time2;
unsigned int millisecdiff;
ftime(&time1);
/*sleep 1 millisecond*/
usleep(1000);
ftime(&time2);
millisecdiff= time2.millitm - time1.millitm + 1000 * (time2.time - time1.time);
printf ("Milliseconds elapsed: %d\n", millisecdiff);
}
You could expect that this should return 1 every time and possibly 2 a few times. It doesn't! It varies between 2 and as much as 14 on my machine.
May I ask what on earth you need that kind of precision for?
Regards
Martin
|
|
|
12-03-2003, 12:09 PM
|
#5
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
can u give a sleep(0.001) for a 1ms delay?
then theres always usleep(xxx).
|
|
|
12-04-2003, 12:01 PM
|
#6
|
LQ Newbie
Registered: Sep 2003
Posts: 28
Original Poster
Rep:
|
sleep in linux must be an integer
|
|
|
12-04-2003, 01:36 PM
|
#7
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
yes i know, but put sleep (0.001), and see - it WILL compile with no warnings even, and give (im assuming its a msec cos it is definitely less time than a sleep(1)) a delay of a msec.
(yes, im on linux too)
Last edited by h/w; 12-04-2003 at 01:38 PM.
|
|
|
12-04-2003, 03:10 PM
|
#8
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
The compiler doesn't give any error or even warnings when passing "0.01", not even with all warnings turned on (gcc -Wall -pedantic -ansi ...). But this is not only for sleep(). I've no idea why.
Probably sleep() has a minimal sleeping time, like nanosleep() has (10 or 1 ms depending on platform).
Anyways, it's not the right way to have a program sleep for a while. Use usleep(), or better yet, nanosleep() or select().
|
|
|
12-04-2003, 03:16 PM
|
#9
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
u think it could be to do with gcc3.x?
|
|
|
12-04-2003, 04:52 PM
|
#10
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
I just tried with gcc-2.95.
Same thing: no warnings.
|
|
|
12-04-2003, 07:10 PM
|
#11
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
this program:
Code:
#include <unistd.h>
int main(int argc, char **argv)
{
if(argc == 1) sleep(.1);
else if(argc == 2) sleep(1);
}
does this:
Code:
<:n00b@highjack3d:> ltrace ./a.out 1
__libc_start_main(0x08048328, 2, 0xbffffa94, 0x08048230, 0x08048388 <unfinished ...>
sleep(1) = 0
+++ exited (status 0) +++
<:n00b@highjack3d:> ltrace ./a.out
__libc_start_main(0x08048328, 1, 0xbffffa94, 0x08048230, 0x08048388 <unfinished ...>
sleep(0) = 0
+++ exited (status 0) +++
anything less than 0 is interpreted as 0.
|
|
|
12-04-2003, 07:33 PM
|
#12
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
ahh great. thanks man.
may i ask what u used to debug there?
Last edited by h/w; 12-04-2003 at 07:36 PM.
|
|
|
12-04-2003, 07:39 PM
|
#13
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
sorry for askin - didnt realize ltrace was a tool out there. was thinking it was something inside some debugger like gdb. 
|
|
|
12-05-2003, 10:58 AM
|
#14
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
yea ltrace is a great tool, as is 'strace' if you hadn't known of that already. 
|
|
|
12-05-2003, 11:02 AM
|
#15
|
Senior Member
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286
Rep:
|
yeah, strace and objdump, i have tried, and they produce similar looking outputs to what you posted. which is why i was wondering what ltrace was.

|
|
|
All times are GMT -5. The time now is 05:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|