lmenus
lmenus

Reputation: 613

How do operating systems know which process is calling a function?

Let's say we have two applications open, A and B. B uses the local environment's call to display a notification to the user, for example displayNotification("Hello."). However, the system displays in the title "Application B says" and then the desired text. How do operating systems know the name of the application that called the function? How do they handle this?

Another example is multitasking. Let's say both applications decide to start a timer with the same interval period. However, we would like to pause the interval of the application that is not in foreground to save the resources of the device and only resume it when the application is brought back to foreground. Again, how is this case handled?

I realise these questions might beg for a good read, so could anyone point me to a good book on this topic? Thank you.

Upvotes: 0

Views: 74

Answers (2)

user3344003
user3344003

Reputation: 21627

How do operating systems know the name of the application that called the function? How do they handle this?

This is technically not the function of the operating system (if we consider the operating system to be the kernel). It is the graphical user interface (X, UIS, Windoze, etc.) that knows the name of the application. Typically there is some initialization function of the GUI that gives the application name or similar.

Let's say both applications decide to start a timer with the same interval period. However, we would like to pause the interval of the application that is not in foreground to save the resources of the device and only resume it when the application is brought back to foreground. Again, how is this case handled?

Operating system timers work in the background. If you wanted to pause the timer for a background application you would do that programmatically. The application would need to have its own counter and you'd set a timer to go off ever second or so. When the timer goes off, you increment a counter and set a new timer. If the timer goes off when you are in the background, you don't set a new timer. When the application receives notification that it is in the foreground, it sets a new timer.

If you wanted to find more information, you'd need to look at the user interface documentation for whatever system you are interested in.

Upvotes: 0

Dave Ross
Dave Ross

Reputation: 3491

The operating system is in charge of everything. If an app is running in the foreground, it's because the OS told it to. So it knows what's going on. On Modern CPU hardware, the OS runs in a lower ring than other processes, so the CPU gives it the ability to interrupt other code and do other things to it.

Internally, the OS keeps track of multitasking with structures called Process Control Blocks. They track things like the process name, process ID, memory allocation, timers, and so on. PCB are like big pieces of scratch paper where the OS records everything it needs to know about a process.

Interval timers are usually managed by the operating system itself, so if the OS wants to pause a process's timers, it looks in its Process Control Block to see what timers it has allocated and pauses them itself.

If you want a book to learn more about operating systems, I recommend hunting down an older copy of Silberschatz, Gagne, and Galvin's Operating Systems Concepts ("The Dinosaur Book"). The new editions cover more complex modern operating systems and, since it's a textbook, cost a ton of money. Tannenbaum's Modern Operating Systems comes highly recommended as well.

Upvotes: 1

Related Questions