Вы находитесь на странице: 1из 2

Chapter 10 Processes and threads

By Felipe Monteiro de Carvalho In all modern operating systems it is possible to run multiple programs at the same time. On multiprocessor systems and multicore-PCs programs can actually run concurrently. On single processor systems they only appear to do so, by means of the scheduler modern operating systems incorporate to handle the division of CPU time between running processes. Windows, MacOS X and the modern Unix derivatives all allow pre-emptive multitasking. In a 32-bit operating system the maximum amount of memory that can be assigned to a single process is 4 Gbytes. This limitation is imposed by the 32-bit address bus, which can address exactly 232 bytes = 4,294,967,296 bytes = 4,194,304 kbytes = 4,096 Mbytes = 4 Gbytes. This memory has to be requested as needed it is not immediately available to the process. This address space has to store not only the program itself, but also its data, its resources and any loaded libraries. The process itself does not know about a forced context change. Each process thinks it is the only one running in the CPU. This means that each context change is quite costly since all the current process information has to be saved, and all the other processs information has to be restored, before that process can continue to run. It seems like a good idea to create more than one process for a complex program, because while a process is waiting for user response another process can meanwhile be doing some other needed task such as saving data in the background. For this reason a process is often split into small independent parts, called threads. Each process has at least one thread. All the threads of one process share a common address space (in contrast to the different processes running in the operating system in different address spaces). All threads together can use this common storage area. Of course we have to be very careful when we access this common memory, because synchronization problems may arise. It is assumed that threads have a cooperative character and can access their common memory without restrictions. Each process can tell the operating system how it should respond to errors. Possible errors are critical errors, protected memory access violations, and failure to open a requested file. One process is not allowed to access the address space of another process. The operating system monitors this. But sometimes processes need to exchange data. How then can they do that? For this purpose the operating system offers so-called kernel objects. The kernel is the lowest level of the operating system, responsible for task-scheduling and other system operations. Because the operating system has control over all processes, it is in a position to organise synchronization and communication between processes.
Lazarus - the complete guide

595

10 Chapter Processes and threads

Each process or thread is represented by a kernel object. Because each process has at least one thread, communication always takes place between the threads of the processes involved. To make communication possible, the kernel makes use of special objects, the so-called synchronization objects. The main multitasking objects are: Process objects Thread objects Mutex objects Semaphore objects Event objects

10.1 Processes

A process can create other processes. Because a process is considered an independent program under Windows, MacOS X and Unix/Linux, this means a program has to call another program to create a new process instance. To do this, the operating system initializes the necessary kernel objects and makes available an individual address space. Particularly under Windows there are several convenient methods to start new processes. The most complex one of these is offered by the CreateProcess API. Its encapsulation in Pascal looks like this:
function CreateProcess( : PChar; lpApplicationName : PChar; lpCommandLine lpProcessAttributes, lpThreadAttributes : PSecurityAttributes; : Bool; bInheritHandles : DWord; dwCreationFlags : Pointer; lpEnvironment lpCurrentDirectory : PChar; const lpStartupInfo : TStartupInfo; var lpProcessInformation : TProcessInformation) : Bool; StdCall;

Lazarus offers a wrapper called TProcess for this Win32 API function. This directly controls calling new programs. As can be seen from the definition with its many options, we can influence the start of a new program in many ways, but thanks to the LCL wrapping, most of that need not concern us. The TProcess component has many properties, most of which correspond to the arguments of the CreateProcess Windows API function. A short description of these properties follows: Active: If this boolean property is True, the program will be executed with the specified options. This corresponds to calling the Execute method. If the property is False, a running application is ended.

596

Lazarus - the complete guide

Вам также может понравиться