-2

In UIKit framework, there are methods associated with app’s life cycle events like applicationWillEnterForeground(_:) which are called at appropriate times.

How can I make a process receive such events from an operating system? If the operating system has to be programmed to be able to notify the processes of system events, how can it be done?

I have found that signals can be used but it is not usually used to transfer data. I read it here.

What I want to know is how a process can run certain block of code which may receive some data from the system when it enters background, enters full screen or the system has changed it’s theme such as a dark theme.

10
  • "How can I make a process receive such events from an operating system?" - by either utilizing the functionality of the programming language framework you are using, or by utilizing the specific callback functionality provided by the specific operating system's API you are using. See here for example. – Doc Brown Oct 1 '20 at 11:26
  • Thanks for the example. But I want the target process to run some code at an arbitrary time the OS decides. The operating system should be able to call or make the process to run an implemented function. I was thinking of sending a signal to the process which, when catches the signal, makes the main loop of the program to fetch some system state from system API and the main loop calls the appropriate function in the program. Does this approach practical? If not, please suggest me more. – Pineapple Seed Oct 1 '20 at 11:49
  • 1
    Running code at an arbitrary time the OS decides is what signals do, and they were so confusing and hard-to-use, that people now say you shouldn't use them! How it usually works is that the OS doesn't do that. Instead, the application calls the "wait for something to happen" OS function, which returns when something happens, and says what happened. – user253751 Oct 1 '20 at 12:17
  • Could you please show me some examples of such OS function in use? If possible, please include some open-source implementations. Thanks. – Pineapple Seed Oct 1 '20 at 12:21
  • 1
    @PineappleSeed: you are asking on the wrong site. Stackoverflow is where you can ask coding questions like this. But the question has to become more specific to succeed there. Tell them about your programming language environment, your operating system, and pick one example like how to implement a callback for "entering background" (whatever that means). Tell them also precisely which kind of data you expect to receive. And don't forget to delete this question here first, crossposts are not welcome by the community. – Doc Brown Oct 1 '20 at 12:26
1

The operating system should not just start running the code in the process. That's been tried before (UNIX signals) and it didn't work very well - it's very difficult to handle signals without bugs.

Generically: The operating system provides some kind of "wait for something to happen" syscall. The application then has a main loop that looks like:

while(true) {
    thing = waitForSomethingToHappen();
    handleThingThatHappened(thing);
}

On Linux, most I/O is based on file descriptors. You use poll (or one of the related functions) to ask the kernel to pause your process until to wait until certain file descriptors have data available to read - or, less commonly, until they have space in the buffer to write.

while(true) {
    struct pollfd fds[] = {
        {fd_1, POLLIN}, // want to know if there's data to read from fd_1
        {fd_2, POLLIN}, // want to know if there's data to read from fd_2
        {fd_3, POLLOUT}, // want to know if there's space to write to fd_3
    };
    poll(fds, 3); // error handling skipped for demonstration
    if(fds[1].revents & POLLIN) readData(fd_1);
    if(fds[2].revents & POLLIN) readData(fd_2);
    if(fds[3].revents & POLLIN) writeData(fd_3);
}

A process can have many file descriptors and it tells the OS which ones it wants to wait for. You have to explicitly connect to things. If you want to draw stuff on the screen, you have to connect an FD to the X server (which manages the screen) and when it has something to tell you, it sends you some data, which you read and process. When your window becomes the foreground, the X server will tell you and it's up to you to respond.

On Windows, there are several different mechanisms, but the most familiar is the Windows message queue. You use GetMessage to wait for a message:

struct MSG msg;
while(GetMessage(&msg, 0, NULL, NULL) > 0) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

Unlike Linux, you only have one message queue per thread (but not every thread has a message queue). If you want to wait for something that's not a message - like I/O - you typically have to start a new thread to do it.

When your window becomes the foreground, a message magically arrives in the queue of the thread which created the window. You still don't receive it until you call GetMessage.

3
  • I see. So the blocking system call has to be implemented in the OS or the display server. And the program can call it on its own thread. Am I right? – Pineapple Seed Oct 1 '20 at 14:07
  • @PineappleSeed All system calls are part of the kernel, including the ones that tell the kernel to make your program wait for a different program. – user253751 Oct 1 '20 at 14:23
  • I get it now. Thank you very much. – Pineapple Seed Oct 1 '20 at 14:30
0

First, the operating system has complete control of the computer. If it wants the app to do something it will do it.

The general mechanism specific to MacOS and iOS you are asking about is the Notification Center. Each application has an instance of the Notification Center. Your application registers code that it wants to be executed when some notification is received. The “springboard” process is part if the operating system, it both activates and deactivates processes and sends notifications to those processes as appropriate. It can do that because it is part of the OS and allowed to send notifications to other processes.

The OS will also call certain optional methods of the AppDelegate protocol if implemented.

2
  • So the ability to send notifications to other processes or apps is implemented in iOS. In UNIX operating systems, one process can send signals to other processes if allowed. What is the mechanism of sending notifications to processes in iOS called? I would like to know how does it work. Thanks. – Pineapple Seed Oct 4 '20 at 6:28
  • MacOS and iOS are Posix. You asked about a MacOS / iOS specific notification that the app will receive using a high level API. – gnasher729 Oct 4 '20 at 7:12

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.