The OS keeps track of the Primary Memory; as in it keeps track of the memory which is in use and by whom, and the memory which isn't. Thus, it allocates the unused memory as and when a process or program requests it.
The Operating System allocates the Processor to a program and deallocates it when it is no longer required, ensuring that each program and application receives enough of the processor's time to function properly.
The I/O Controller of the OS keeps a track of all your devices and decides which process gets the device, when, and for how much time. It uses another API to request data from the device driver.