473,626 Members | 3,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sleeping or waiting

Is there any platform independent way to make a C program wait for a
certain amount of time before performing a task? I don't mean:

while(difftime( clock1, clock()) < MAGICNUMBER);

Rather, I want something that cuts down on a program's use of
processor time when it is executing, like Java's Thread.sleep(in t
milliseconds) method. Currently, whenever I write programs, they take
up 50% of my processor exactly (since I have a hyperthreading
processor, the maximum any one thread can use is fifty percent, or so
I've been lead to believe). I would like to make it so that the
program takes a step every, say, 1/60th of a second, for a physics
simulation, but I would like any spare processor time to revert to the
system idle thread.
Nov 14 '05 #1
15 1974
Unix has
#include <unixstd.h>
unsigned int sleep(int seconds);

Windows has

#include <windows.h>
void Sleep(unsigned long miliseconds);

and most OSes provide the same functionality.

Look at the documentation of your system.
Nov 14 '05 #2

"jacob navia" <ja***@jacob.re mcomp.fr> a écrit dans le message de
news:cc******** **@news-reader4.wanadoo .fr...
Unix has
#include <unixstd.h>
unsigned int sleep(int seconds);


Sorry, the "seconds" argument should be unsigned int.
Nov 14 '05 #3
Blue Ocean wrote:
Is there any platform independent way to make a C program wait for a
certain amount of time before performing a task?


No. Such things are inherently OS-dependent.
Nov 14 '05 #4
bl*********@hot mail.com (Blue Ocean) writes:
Is there any platform independent way to make a C program wait for a
certain amount of time before performing a task? I don't mean:

while(difftime( clock1, clock()) < MAGICNUMBER);

Rather, I want something that cuts down on a program's use of
processor time when it is executing

[...]

No, there's no platform independent way of doing this. There should
be platform dependent ways of doing it on almost all platforms; for
something like this, #ifdef is your friend.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
jacob navia wrote:
"jacob navia" <ja***@jacob.re mcomp.fr> a écrit dans le message de
news:cc******** **@news-reader4.wanadoo .fr...
Unix has
#include <unixstd.h>
unsigned int sleep(int seconds);


Sorry, the "seconds" argument should be unsigned int.


And the header should be #include <unistd.h>

--
Arto Huusko
Nov 14 '05 #6
Arto Huusko wrote:
jacob navia wrote:
"jacob navia" <ja***@jacob.re mcomp.fr> a écrit dans le message de
news:cc******** **@news-reader4.wanadoo .fr...
Unix has
#include <unixstd.h>
unsigned int sleep(int seconds);


Sorry, the "seconds" argument should be unsigned int.


And the header should be #include <unistd.h>


Sorry for the OT, but since someone mentioned it..
Is "unistd" some sort of abbreviation for "unix standard" ?
I've always been curious about that header filename, and the
above seems to make sense, especially after discovering (and
subsequently reading) The Open Group's manpage for it.

If so, would anyone by chance know why the 'x' was omitted?
--
Eric Enright /"\
ericAtiptsoftDc om \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Nov 14 '05 #7
In <sl************ *****@yamano.ti ptsoft.com> er**@tiptsoft.c om (Eric Enright) writes:
Sorry for the OT, but since someone mentioned it..
Is "unistd" some sort of abbreviation for "unix standard" ?
Sort of.
I've always been curious about that header filename, and the
above seems to make sense, especially after discovering (and
subsequently reading) The Open Group's manpage for it.

If so, would anyone by chance know why the 'x' was omitted?


Have a close look at all the C89 standard header names. The same
reason applied, apparently, in the case of <unistd.h>, too.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
Dan Pop wrote:
In <sl************ *****@yamano.ti ptsoft.com> er**@tiptsoft.c om (Eric Enright) writes:
Sorry for the OT, but since someone mentioned it..
Is "unistd" some sort of abbreviation for "unix standard" ?


Sort of.
I've always been curious about that header filename, and the
above seems to make sense, especially after discovering (and
subsequentl y reading) The Open Group's manpage for it.

If so, would anyone by chance know why the 'x' was omitted?


Have a close look at all the C89 standard header names. The same
reason applied, apparently, in the case of <unistd.h>, too.


Indeed, I see the similar naming convention.
Thanks for the pointer.

--
Eric Enright /"\
ericAtiptsoftDc om \ / ASCII Ribbon Campaign
X Against HTML E-Mail
Public Key: 0xBEDF636F / \
Nov 14 '05 #9
In <sl************ *****@yamano.ti ptsoft.com> er**@tiptsoft.c om (Eric Enright) writes:
Dan Pop wrote:
In <sl************ *****@yamano.ti ptsoft.com> er**@tiptsoft.c om (Eric Enright) writes:
Sorry for the OT, but since someone mentioned it..
Is "unistd" some sort of abbreviation for "unix standard" ?


Sort of.
I've always been curious about that header filename, and the
above seems to make sense, especially after discovering (and
subsequent ly reading) The Open Group's manpage for it.

If so, would anyone by chance know why the 'x' was omitted?


Have a close look at all the C89 standard header names. The same
reason applied, apparently, in the case of <unistd.h>, too.


Indeed, I see the similar naming convention.


It's not the similar naming convention, it's the fact that no standard
header name exceeds 8 characters (the .h suffix included). unixstd.h
would take 9 characters. The care for not exceeding the 8 character limit
is obvious, as stddef.h and stdarg.h would have looked better with an s
at the end (before the suffix).

This implicit 8 character limit is gone in C99, where one can find
something as wide as inttypes.h.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2367
by: Shenron | last post by:
Hello, I've seen lots of queries which are sleeping with SHOW PROCESSLIST. Why are they sleeping? How can I prevent this sleeping queries? Because I can't running lots threads at the same time therefore I would prefer have none queries like that. Best Regards,
6
10569
by: ujjc001 | last post by:
Hello all. I have many connections that are sleeping. I can right click, kill, up to 2 at a time. It takes about 20 seconds to kill it. Is that normal? I tried to make a stored procedure to kill them but it ate all the power of the server, (all users yelled at me at the same time :o) ) Is this length of time normal for killing these processes? Thanks Jeff
4
6578
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times. There is no consistency when it fails. Could anyone here shed some light on how to debug/resolve the issue. I guess IBM looked at the issue and were not able to pinpoint where the issue is. When the program hangs and when force the DB2...
28
2678
by: Yannick Loth | last post by:
Hello I'm writing a program which needs to pause exactly some microseconds then go ahead. I'v tried the nanosleep() function but this is not much precise. I also tried it with raising the priority of my application, but it still isn't precise enough. What maximum precision can I hope to obtain on a linux station? Would someone have a suggestion on the implementation? Thanks Yannick
4
21114
by: Muscha | last post by:
Hello, I have a thread that in the middle of the execution I did Thread.Sleep(). How do I tell this thread to abort it sleep and continues? Is there a way? thanks, /m
0
1377
by: Ole Hanson | last post by:
Hi What's the best way of activating a sleeping process on my local machine? I have an application (.exe) minimized to a notifyicon in the tasktray. Now - if the user clicks the exe-file again in the filesystem, this will launch another instance of my exe. This is not desired - I want to activate/open the already running application "behind" the notifyicon. What options do I have for activating this sleeping exe? Note: I do NOT want...
1
29508
by: Chad | last post by:
I am using SQL Server 2000. In Enterprise Mgr, under Management/Current Activity, I see a list SPIDs. I have a multi-tiered .NET web app that uses ADO.NET to connect to the database. Each time a Stored Proc is called, a new connection is opened, the data retreieved, and the connection closed and set to Nothing (in VB.NET). Yet, I see several (15 or so) SPIDs for the user ID that the web app uses to connect to the database.
17
5657
by: Benny Raymond | last post by:
I have a thread that sleeps for 5 minutes once it's finished running a method and then it repeats itself if it's supposed to (bool = true). Prior to 2.0 I was able to resume the thread after marking the bool to false which would cause the thread to finish what it was doing and complete, or to wake up for it's sleep state and not loop back into itself anymore. How do we tell a thread to stop sleeping now that Thread.Resume is obsolete?
0
1982
by: vamsikrishnaatluri | last post by:
in the old sleeping barber problem ther is a small modification wherein there is a special customer who need not wait in the waiting hall but enters the saloon directly.. and if if no chair is empty he goes in and out of the saloon every 60 secs.. repeatedly till he gets his turn.. can anyone gimme a multithreaded program to solve this
5
6448
by: many_years_after | last post by:
Hi, pythoners: There is a problem I couldn't dispose. I start a thread in the my program. The thread will do something before executing time.sleep(). When the user give a signal to the main thread (such as click the 'end' button or close the window), the thread should end it's running. But how to end the threading when it's sleeping? I set an flag to the thread, but it doesn't work. I also thought to put 'time.sleep()' to the main...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.