读windows internal(四)
Services
几乎所有的操作系统都提供一种在系统启动时提供系统服务并且和交互用户无关的机制,在windows中这样的进程叫做windows服务,因为这些windows服务只和windows系统API进行交互;windows服务类似于UNIX中c/s结构的应用程序的服务器端的后台进程,An example of a Windows service might be a Web server because it must be running regardless of whether anyone is logged on to the computer and it must start running when the system starts so that an administrator doesn't have to remember, or even be present, to start it.
Windows 服务有三个部分组成: a service application, a service control program (SCP), and the service control manager (SCM). .
Service Applications
Service applications, such as Web servers, consist of at least one executable that runs as a Windows service. A user wanting to start, stop, or configure a service uses an SCP. 尽管windows提供有内建的SCPs来管理服务程序的start,stop,pause,and continue 等功能;但是一些服务程序还是包含有自己的SCP功管理员进行特殊的配置和管理服务。.
Service applications 其实就是一个简单的windows 可执行程序附加上可以和SCM交互的功能, Because most services don't have a user interface, they are built as console programs.
When you install an application that includes a service, the application's setup program must register the service with the system. To register the service, the setup program calls the Windows CreateService function, a services-related function implemented in Advapi32.dll (\Windows\System32\Advapi32.dll). Advapi32, the "Advanced API" DLL, implements all the client-side SCM APIs.
When a setup program registers a service by calling CreateService, a message is sent to the SCM on the machine where the service will reside. The SCM then creates a registry key for the service under HKLM\SYSTEM\CurrentControlSet\Services. The Services key is the nonvolatile representation of the SCM's database. The individual keys for each service define the path of the executable image that contains the service as well as parameters and configuration options.
After creating a service, an installation or management application can start the service via the StartService function. Because some service-based applications also must initialize during the boot process to function, it's not unusual for a setup program to register a service as an autostart service, ask the user to reboot the system to complete an installation, and let the SCM start the service as the system boots.
When a program calls CreateService, it must specify a number of parameters describing the service's characteristics. The characteristics include the service's type (whether it's a service that runs in its own process rather than a service that shares a process with other services), the location of the service's executable image file, an optional display name, an optional account name and password used to start the service in a particular account's security context, a start type that indicates whether the service starts automatically when the system boots or manually under the direction of an SCP, an error code that indicates how the system should react if the service detects an error when starting, and, if the service starts automatically, optional information that specifies when the service starts relative to other services.