Quote:
Originally Posted by stevefox
Thanks everyone,
When I run the script on one terminal and monitor the process by running the ps command repeatedly on a different terminal I can see that the process constantly changes from "R" mode to "S" mode and vice versa until the program ends. I don't know why it changes to "S" mode when my script does not have any "wait" command. So is there a way to always keep a process in "R" mode until the process ends?
Processes do not go to sleep because they feel like taking a nap. A sleeping process is waiting for an event to occur. You don't want your process to go to sleep? Make sure it never wants events to occur. Do not attempt any I/O. Do not reference any memory that is not locked into core. Do not communicate with, create, signal, or wait for any other processes. Only a very few system calls such as getpid() will never wait for an event.
Consider the write system call such as: write(fd, buff, length). If fd is writing to disk, Unix guarantees that the write is atomic. Once your process starts a write(), it will not die until the write() completes. If this write is to a file system file and the data block happen to be in core, that's fine, we can move your data into the buffer. But if it's not in core, we must read it in. During that read your process will go to sleep. That bothers you? What should the process do instead? Stay in a CPU and run a busy loop? Anyway to read the data in, we need a free buffer. You probably don't want to know what happens if a free buffer wasn't just laying around.
And to be complete... some OS's have intensified forms of sleep. Like:
wait -- sleeping process that has been swapped out
idle -- sleeping process that has been sleeping for several seconds or more