LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 11-30-2003, 09:24 AM   #1
frostmagic
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Rep: Reputation: 15
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.
 
Old 11-30-2003, 09:32 AM   #2
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
If you are talking C, try usleep(microseconds)

Regards
Martin
 
Old 12-02-2003, 09:18 AM   #3
frostmagic
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
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
 
Old 12-02-2003, 10:54 AM   #4
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
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
 
Old 12-03-2003, 12:09 PM   #5
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
can u give a sleep(0.001) for a 1ms delay?

then theres always usleep(xxx).
 
Old 12-04-2003, 12:01 PM   #6
frostmagic
LQ Newbie
 
Registered: Sep 2003
Posts: 28

Original Poster
Rep: Reputation: 15
sleep in linux must be an integer
 
Old 12-04-2003, 01:36 PM   #7
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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.
 
Old 12-04-2003, 03:10 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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().
 
Old 12-04-2003, 03:16 PM   #9
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
u think it could be to do with gcc3.x?
 
Old 12-04-2003, 04:52 PM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I just tried with gcc-2.95.
Same thing: no warnings.
 
Old 12-04-2003, 07:10 PM   #11
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 12-04-2003, 07:33 PM   #12
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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.
 
Old 12-04-2003, 07:39 PM   #13
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
sorry for askin - didnt realize ltrace was a tool out there. was thinking it was something inside some debugger like gdb.
 
Old 12-05-2003, 10:58 AM   #14
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
yea ltrace is a great tool, as is 'strace' if you hadn't known of that already.
 
Old 12-05-2003, 11:02 AM   #15
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop Startup Process easyE Linux - General 2 05-03-2005 05:37 PM
which process are safe to stop dkc_ace Linux - General 9 02-18-2004 03:09 PM
How to stop LP process at bootup? britishnemesis Linux - Software 1 10-27-2003 01:04 AM
rc.d with K to stop the process... psyklops Linux - General 1 10-18-2003 01:14 PM
How to stop SSHD process???? adamrau Linux - Security 2 12-07-2001 03:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration