Lazarus - Chapter 10

download Lazarus - Chapter 10

of 2

Transcript of Lazarus - Chapter 10

  • 7/28/2019 Lazarus - Chapter 10

    1/2

    Chapter 10

    By Felipe Monteiro de Carvalho

    Processes and threads

    In all modern operating systems it is possible to run multiple programs at thesame time. On multiprocessor systems and multicore-PCs programs can actuallyrun concurrently. On single processor systems they only appear to do so, bymeans of the scheduler modern operating systems incorporate to handle thedivision 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 asingle process is 4 Gbytes. This limitation is imposed by the 32-bit address bus, whichcan 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 availableto the process. This address space has to store not only the program itself, but also itsdata, its resources and any loaded libraries.

    The process itself does not know about a forced context change. Each process thinks itis 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 processsinformation 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 bedoing some other needed task such as saving data in the background. For this reason aprocess is often split into small independent parts, called . Each process has at leastone thread. All the threads of one process share a common address space (in contrast tothe 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 carefulwhen we access this common memory, because synchronization problems may arise. It isassumed that threads have a cooperative character and can access their common memorywithout restrictions.

    threads

    Each process can tell the operating system how it should respond to errors. Possibleerrors are critical errors, protected memory access violations, and failure to open arequested file.One process is not allowed to access the address space of another process. The operatingsystem monitors this. But sometimes processes need to exchange data. How then canthey do that? For this purpose the operating system offers so-called .

    The kernel is the lowest level of the operating system, responsible for task-schedulingand other system operations. Because the operating system has control over all processes,it is in a position to organise synchronization and communication between processes.

    kernel objects

    Lazarus - the complete guide 595

  • 7/28/2019 Lazarus - Chapter 10

    2/2

    10 Chapter Processes and threads

    Each process or thread is represented by a kernel object. Because each process has atleast one thread, communication always takes place between the threads of the processesinvolved. 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

    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 callanother program to create a new process instance. To do this, the operating systeminitializes 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 . Its encapsulation inPascal looks like this:

    10.1 Processes

    CreateProcess API

    function (

    : ;

    : ;

    ,

    : ;

    : ;

    : ;

    : ;

    : ;

    const : ;

    var : ) : ;

    StdCall;

    CreateProcess

    lpApplicationName PChar

    lpCommandLine PChar

    lpProcessAttributes

    lpThreadAttributes PSecurityAttributes

    bInheritHandles Bool

    dwCreationFlags DWord

    lpEnvironment Pointer

    lpCurrentDirectory PChar

    lpStartupInfo TStartupInfo

    lpProcessInformation TProcessInformation Bool

    Lazarus offers a wrapper called for this Win32 API function. This directlycontrols 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 LCLwrapping, most of that need not concern us.The component has many properties, most of which correspond to thearguments of the Windows API function.

    A short description of these properties follows:

    If this boolean property is , the program will be executed with thespecified options. This corresponds to calling the method.

    If the property is a running application is ended.

    TProcess

    TProcess

    CreateProcess

    Active: True

    Execute

    False,

    Lazarus - the complete guide596