Blocked suspended to Ready suspended - operating-system

Let's say a process is waiting for I/O in the Blocked/suspend state and then the event occurs. Why that process goes to Ready/suspend state and not directly to Ready state?
It could go directly to Ready state and save some effort of the mid-term scheduler.

Ready/suspend generally mean that the process is in some sort of secondary or temporary memory, and can't be "ready" until it is brought back into main memory.

Related

Non-Preemptive multitasking Scheduling Algorithm

What causes transition of process from one state to another state if non-preemptive multitasking scheme is being used to manage multiple tasks in the system?
Suppose a process is in running state. Now it moves to waiting state for I/O. In that case, another ready process will be context switched and will be executed.

Why is an OS with only a ready and running state inefficient?

I'm studying for an operating systems exam and I cant think of a sufficient reason as to why the system would need more than two states. Could someone explain the reason further to me?
This is the question as its asked:
You would also want a 'waiting' state. When a process is waiting for some I/O input or input from another process, it would move to the waiting state and another process could run while it is waiting.
This might help you some: http://people.csail.mit.edu/rinard/osnotes/h6.html
What if a process wants or needs to sleep/delay/wait for some time/event?
Waiting for I/O to finish is a common example, which can be as simple as waiting for the next key press by the user or waiting for some client to connect over the network.

How can one thread determine if a different thread has crashed?

I have a background thread that is doing a bunch of work - loading the application. The main thread is displaying progress on a UIProgressView.
The background thread is being spawned with performSelectorInBackground (though, I'm not wed to this method if a different approach makes this problem easier to solve)
[self performSelectorInBackground:#selector(loadAppInBackground) withObject:self];
On a couple occasions a bug has caused the background thread to crash (different bugs as the app evolves) which results in the progress bar stopping, but the user getting no clear indication that anything is wrong.
I'd like to detect this situation and fail more gracefully than simply hanging until the user gives up on waiting.
Because the duration of the load process can vary greatly, simply timing out isn't an ideal option.
What's the best way for the foreground thread to detect that the background thread has failed? Since the foreground thread is busy dealing with the UI, would it require a second background thread to monitor the first? That seems ugly.
Is there some thread-to-thread communication mechanism that could be used to "ping" the background process? Better yet, a low level system mechanism of checking the status of other threads?
The debugger knows about all the threads that are running... and seems to know their status. I'm wondering if there's a call available to my app to do the same.
If the background task runs in some sort of regular cycle (eg, there's a big loop where much of the work gets done), it can set a flag every so often to indicate that it's still alive.
One way to do this is to have background thread store [NSDate timeIntervalSinceReferenceDate] somewhere, and, in your main thread, occasionally (perhaps on a timer) compare that to the current time. If the difference is greater than some reasonable limit you can guess that the background thread has died.
Another way is to have the background thread simply set a Boolean, and have the main thread interrogate that and clear it on some regular basis. If the Boolean doesn't get set again between main thread interrogations you can infer that it has died.
The first technique has the advantage that you can "tune" the "reasonable limit" to tolerate code (in either thread) that is somewhat irregular in it's timing. The second approach generally requires timings that are more predictable.
Of course with either approach you want to somehow avoid "blowing the whistle" if the background thread has just finished up and you simply haven't recognized that yet.
A common technique is to have an extra thread to check for life signs of the thread in question - a so called heartbeat thread. The heartbeat thread polls the thread by checking if it responds in a timely manner, if not, deems the thread dead and terminates it.
A simple heartbeat thread implementation would be to check a counter that is incremented regularly by the other thread, if the counter is not incremented within a certain time it is regarded as dead and then an appropriate action could be taken like restarting thread or killing app. Another more common way is if the hb thread sends messages to the thread and checks for a response with a timeout.
It seems like there is no mechanism in objective c to check the status of a background thread directly. Any of the answers provided are decent options... either timing out, or having the thread create some sort of evidence of its continued existence.
I was hoping for something a little more simple, reliable, and real-time.
I'm going to experiment with catching an exception in the thread and perhaps producing a notification like "BackgroundThreadException" that the foreground thread could listen for and react to.

Eclipse job blocks access to workspace

I have a long running operation that is run as a Job.
Problem is, as long as the job runs at the background , I'm denied of access to my workspace. (a GMF diagram, if relevant). ( Under the progress view, I can see a pending Job saying "waiting user operation")
Is there any flag or priority that needs to be changed in order to make the job a non-blocking one?
Thanks!
It is the developer that decides whether the job is blocking or not. Usually based on whether the internal structures are in a sane state during the execution of the job, etc.
There are no way the stop a job in the general case. Even if you can cancel the job, it is still the developer of the job that decides if and when to check for the cancel state...

How to properly shut down/restart NSThread after applicationDidEnterBackground/applicationWillEnterForeground

For performance reasons, I instantiate a dedicated NSThread to process incoming messages that are streamed from a network server. I use an NSOperation for the purpose of instantiating the connection and receiving incoming data through the NSURLConnection delegates, but as soon as new data comes in and gets parsed, I offload the processing of the message to the dedicated NSThread. The idea is to let one thread focus on receiving incoming messages and let the other thread just do the processing.
What's the proper way to shut down the NSThread when the applicationDidEnterBackground comes in?
Also, how should I restart the NSThread when applicationWillEnterForeground comes in?
Other than the main thread, it seems the state of other background threads is not maintained between going to sleep and restarting.
By the way, I'm all for using NSOperations for most tasks that have a measurable amount of work -- ie, accessing a resource over the network, performing a calculation, etc. However, in this case, I need to process messages on the fly on a long-living dedicated thread that is always there by calling performSelector:onThread:withObject:waitUntilDone: and passing it the target thread. It seems NSOperation isn't a good fit for this.
I would appreciate your input.
"For performance reasons"?
If processing doesn't take much time, run everything (including NSURLConnection) on the main thread. Concurrency bugs are a major pain.
If you want things to run serially, you can emulate a "single thread" with an NSOperationQueue with maxConcurrentOperations = 1. I'm pretty sure that NSOperationQueue uses thread pools (and on 4.0, GCD, which probably uses thread pools), which means you don't need to keep a thread running all the time.
Apart from that, your process is automatically suspended and resumed by the system, so you don't need to kill off threads.
I'm not sure what you mean by "the state of other background threads".