Putting other processes to sleep

This is a discussion on Putting other processes to sleep within the C Programming forums, part of the General Programming Boards category; Hi, does anyone know how to force a process thats not the "current process" to sleep. For example I am ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    30

    Putting other processes to sleep

    Hi,
    does anyone know how to force a process thats not the "current process" to sleep. For example I am trying to reduce the load on a server. I write a small program that forces some CPU intensive processes to go to sleep for a little while. Does anyone know how to put "other" processes to sleep?

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, if you could do it, that would be system dependent, and second, you can't quite do it that way.

    What you CAN do, in most systems, is to lower the priority of tasks/processes, assuming you have sufficient rights to do that. This will make other processes that have higher priority to run "first" if they are runnable.

    Most OS's actually do this all by themselves, as in a CPU intensive task will get a lowered priority, and a IO intensive task (technically, a task that falls asleep or becomes "non-runnable" with short amounts of CPU usage) gets a boost. On top of that, there's usually a "base priority", that tells the OS an overall priority for a task - this can be used to make sure that some CPU intensive important task still gets priority over some CPU intensive task that is less important.

    But as a rule, there's no interface to put tasks to sleep for a set amount of time - and it would be pretty meaningless to do so, if the system becomes idle because of it - that's a waste of time, if there are technically runnable processes in the system.

    It would be good if you explained a bit more about what you are ACTUALLY trying to achieve. What is the process you want to "put to sleep", why is "your task more important", and "why can't you just raise the priority of your task"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    30

    Response

    Its a Web server environment with many competing processes running.

    Nice just doesn't do anything. There are a number of non-critical processes that run at regular intervals, they tend to overload the server and I was hoping there was some way to take one of these demanding cpu processes off the RUN QUEUE and place it on the WAIT QUEUE for a certain amount of time. Or even better how can you reduce the processes CPU time slice? Basically what I want to do is control the INTENSITY at which processe are run through the CPU. Rather than have a process run at 100% in 5 minutes - I'd like to run the process at 10% for 50 minutes - so it has less immedaite impact on short term server loads.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "nice" should do that perfectly fine. The only problem might be if you haven't got enough memory, and the background processes get to run when other processes are needing more memory, and that causes thrashing.

    I have run heavy tasks in the background on my Linux-based machines, on all CPU's, and they were at "nice 19" - a compile or other CPU and IO intensive tasks would run VERY quickly on this system. No problem at all.

    You could of course, assuming you have source access, modify the tasks so that they call "usleep" every now and again, that would put them back on the wait queue until the sleep finishes - which means at least one time quanta for the other processes to run.

    There's however absolutely no point in running for 50 minutes at 10% - that's just a waste of time. If there is any work to be done, the CPU should be running at 100% - if there isn't, then that's another story.

    Why are you saying that "nice doesn't work"? What evidence do you have? How are you measuring this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by brett View Post
    Hi,
    does anyone know how to force a process thats not the "current process" to sleep. For example I am trying to reduce the load on a server. I write a small program that forces some CPU intensive processes to go to sleep for a little while. Does anyone know how to put "other" processes to sleep?

    Thanks in advance
    Yes absolutely. Why rely on kernel scheduling when you can do it manually?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    The bottom line is that if you don't have the source code for it then you can't do what you want.
    You can lower its priority and run higher priority tasks to try and cause near starvation for that process, but that's only going to increase total CPU usage during that time anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    I believe it's possible to open a process, enumerate its threads and put each to sleep? That would basically "pause" the program, but I wouldn't recommend it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    30

    Response

    Quote Originally Posted by matsp View Post
    "nice" should do that perfectly fine. The only problem might be if you haven't got enough memory, and the background processes get to run when other processes are needing more memory, and that causes thrashing.

    I have run heavy tasks in the background on my Linux-based machines, on all CPU's, and they were at "nice 19" - a compile or other CPU and IO intensive tasks would run VERY quickly on this system. No problem at all.

    You could of course, assuming you have source access, modify the tasks so that they call "usleep" every now and again, that would put them back on the wait queue until the sleep finishes - which means at least one time quanta for the other processes to run.

    There's however absolutely no point in running for 50 minutes at 10% - that's just a waste of time. If there is any work to be done, the CPU should be running at 100% - if there isn't, then that's another story.

    Why are you saying that "nice doesn't work"? What evidence do you have? How are you measuring this?

    --
    Mats

    I have tried using nice many times now and it is completely useless. The problem is when a lot of processes want to run at once I need a way to make the time critical ones run without any noticable slow down. Those processes that are not time critical can just idle along until the critical ones have finished - that's what I want. Why on earth the Linux kernal has nothing available to do this I don't understand. I could make everything Real Time priority but thats a big complex mess requiring advanced scheduling modifications. I simply want to make certain processes leave the CPU alone and stay idle for a while. I speak from 4 years experience adminstrating servers and seeing the loads spike and go all over the place. This is a great failing of the Linux Kernal - there's very little available to manipulate process CPU intensity.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Those processes that are not time critical can just idle along until the critical ones have finished - that's what I want.
    You could set a a mutex on an object and have the lesser priority threads wait on the mutex while the other important threads continue to execute. According to the operating system all of your threads are important and they each get a slice of time. Windows has a fairly decent priority system that allows you to bump up and bump down thread priority. I would be very surprised if Linux did not have something like that.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by brett View Post
    I have tried using nice many times now and it is completely useless. The problem is when a lot of processes want to run at once I need a way to make the time critical ones run without any noticable slow down. Those processes that are not time critical can just idle along until the critical ones have finished - that's what I want. Why on earth the Linux kernal has nothing available to do this I don't understand. I could make everything Real Time priority but thats a big complex mess requiring advanced scheduling modifications. I simply want to make certain processes leave the CPU alone and stay idle for a while. I speak from 4 years experience adminstrating servers and seeing the loads spike and go all over the place. This is a great failing of the Linux Kernal - there's very little available to manipulate process CPU intensity.
    If a process has a lock on some resource, and you put it to sleep, and one of your critical processes needs that resource, you've possibly deadlocked everything.

    If you want to write your own process scheduler, then go for it, but modern operating systems currently do not support detailed process scheduling. It's considered something that happens inside kernel-land and not something for a user to mess with.

    If a few periodic processes throw your web server program off balance, then one of three things is possibly the issue:

    • Your critical program sucks.
    • The non-critical program sucks.
    • Your hardware sucks.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    If you really want to stop another process, kill it with SIGSTOP. You can only do this if you have permission, i.e. you own the process, or you are root. To restart it, kill it again with SIGCONT.

    This is a bad idea though. Certain naively coded programs will misbehave when they receive signals (including SIGSTOP) which they were not expecting to receive.

    You really need to find a higher level way of controlling load on this system. Perhaps you just need another box.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Trying something either invasive or drastic wil likely only screw things up.
    Just upgrade your hardware.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > The problem is when a lot of processes want to run at once
    > I need a way to make the time critical ones run without any noticable slow down.
    If you have a high priority process waiting for file I/O, then it really doesn't matter what your priorities are, you're stuck. So rather than do nothing, the kernel will work it's way through the priority list (and last time run list) to work out what else can be done in the meantime. Yes, even really nice processes will get a look-in every so often.

    You're probably right, nice may have little effect on a bunch of processes which are I/O bound to begin with. If you have a process which uses 5 minutes (50 elapsed) of CPU time at 10%, you could run that process on a bare machine and get very nearly the same result if that process is involved in a lot of I/O.

    Whilst modern disk transfer rates are in the 100MB/s+ mark (about 10x slower than RAM), disk seek times are still measured in milliseconds (about 1000000x slower!). As soon as that happens, you wave "bye bye" to any sense of keeping the CPU at 100% on your process.

    For your key processes, I would suggest you use the various process monitoring tools (begin with 'ps') to find out exactly how much time is spent on useful work and waiting for I/O.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Linux Putting my children to sleep with futex!?
    By Abs in forum Linux Programming
    Replies: 18
    Last Post: 02-12-2009, 05:43 PM
  3. Sleep works with just one thread, but not 2
    By finkus in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2005, 08:17 PM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-27-2005, 11:59 PM
  5. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21