0
$\begingroup$

In which of the following cases a process executing in user model is required to enter into the OS mode?

(a) Decreasing the value of unsigned integer value in a register to less than 0

(b) Accessing general purpose register

(c) Executing printf()

(d) Adding values in two registers using ADD

In my opinion, both (c) and (b) should use OS mode. But my solution manual says only (c) uses OS mode.

I believe that (b) requires the use of OS mode as well, because the processor may have more than one process executing at the same time. As both of them have access to the general purpose register, it is quite possible that one process may overwrite the contents of another process if switching takes place.

The only way it can be avoided is setting up of a semaphore. At the end of the day, it boils down to a critical section problem. So, for accessing and setting up a semaphore, OS mode is required.

So, in my opinion, (b) should be true as well.

Am I correct or where is my reasoning wrong regarding the problem?

Thank you!

$\endgroup$
  • 1
    $\begingroup$ Uh, (a) and (d) are also "accessing general purpose registers", so why wouldn't you also include them if you were going to include (b)? $\endgroup$ – Derek Elkins Jan 6 at 21:34
  • $\begingroup$ Hi @DerekElkins, that’s a pretty good point you pointed out... but it only makes me even more confused. Then all options should have an OS mode access. I guess the question boils down to the intrinsic details of whether multi programming is supported. What do you think about it? $\endgroup$ – Abhilash Mishra Jan 7 at 1:22
1
$\begingroup$

Computations on general-purpose registers ((a), (b), (d)) doesn't require any privileges.

Input/output requires privileges, to access a peripheral or to communicate with the part of the system that manages files. So (c) does generally require going to kernel mode. It's possible for some printf calls to remain entirely within the calling process, for example if output is buffered and the output of this call is going entirely inside the buffer. But in general a printf call does need to do actual I/O and thus does need a transition to kernel mode.

A processor does not really “have more than one process executing at the same time”. The processor alternates between executing different processes, but at a given point in time, it's only executing one process (or it's in kernel mode). On a multiprocessor machine, each processor¹ executes one process at a given time, but the different processors can be executing different processes. While a process is executing, it can access the general-purpose registers directly. It doesn't need the kernel for that. (And how would it ask the kernel, anyway? A process communicates with the kernel by placing the parameters of a system call in general-purpose registers and then invoking the system call instruction.)

Processes execute code as if the other processes didn't exist. When the kernel decides to switch to another process, it suspends the running process and unsuspends the process that it wants to run next. Part of this suspension mechanism is to save the values of the general-purpose registers into a dedicated memory area that belongs to the process that is being suspended. There is one such register store for each process². Part of the unsuspension mechanism is to restore the registers of the process that is being unsuspended. When a process is unsuspended, it keeps running where it left off, with the same values in registers as when it was suspended. This suspension/unsuspension mechanism is called a context switch.

A process never overwrites the registers used by another process because only the currently running process's register values are in the processor registers. The other processes' register values are in their register store. There has to be a context switch to change which process's register values are in the processor registers.

Switching between processes does not involve semaphores. It's the other way round: in order to use semaphores to synchronize between different processes, the processes need to call the kernel.

¹ Each core, each thread — whatever you call the single-threaded processing unit.
² More generally, for each kernel thread.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.