Operating system - Windows process management

1. Experimental topic

Windows process management

2. The purpose of the experiment

(1) Learn to use VC to write basic Win32 Consol Application (console application).

(2) Through the programming and debugging operations of creating a process, observing a running process, and terminating a process, further familiarize yourself with the process concept of the operating system and understand the "lifetime" of a Windows process.

(3) By reading and analyzing the experimental program, learn the basic programming methods of creating a process, observing a process, terminating a process, and synchronizing parent and child processes.

3. Experiment content (experimental principle/theoretical knowledge used, algorithm/program flow chart, steps and methods, key codes)

1-1 Write a basic Win32 Consol Application

The main purpose of this experiment is to be familiar with writing C/C++ console programs. For creating a C/C++ console program, I usually use two methods, and explain their advantages and disadvantages one by one.

The first way is to click File – New – File… on the menu bar, select C/C++ source in the pop-up window, and then select whether the code type is C or C++, and then set the location where the file is saved, and then a .c will be generated (.cpp) file, you can write c (c++) code in this file. After writing, click Build – Build and run on the menu bar or press F9 directly to view the output. This method creates a C/C++ console program. The advantage is that it is light. Only three files are generated at the end, the source code.c file, the .o file and the exe file. The disadvantage is that it does not support debugging. Suitable for lightweight development

The second method is to create a new project, click File – New – Project… on the menu bar, select Console Application in the pop-up interface, then select the code type and file storage directory, and then it will generate a folder for the project, and then there is a main.c (main.cpp) file, write code in this file. The core file in this project is .cbp, and many subordinate files will be generated. The advantage is that it is easy to debug and can be debugged step by step with Debug. The disadvantage is that there are many generated files, which is suitable for large-scale project development.

The course design of this operating system is to use the second method to generate C++ project writing.

1-2 Create process

1. Create a C++ project, copy the 1-2 code in the guide to the main.cpp file, and then compile and run

2. Modify the code according to the comments in the code to see what is different. There are two modifications

3. Analyze why such a result occurs by consulting relevant information

key code

    BOOL bCreateOK=::CreateProcess(

                       szFilename, // the name of the application that generated this EXE

                       szCmdLine, // flag to tell it to act like a subprocess

                       NULL, // default process security

                       NULL, // default thread safety

                       FALSE, // don't inherit the handle

                       CREATE_NEW_CONSOLE, // use new console

                       NULL, // new environment

                       NULL, // current directory

                       &si, // start information

                       &pi) ; // process information returned

1-3 Simple communication between parent and child processes and process termination

1. Create a C++ project, copy the 1-3 codes in the guide to the main.cpp file, and press F9 to view the running results

2. Modify the code according to the comments and view the results

3. Check the information and analyze the reasons

key code

// Determine whether its behavior is a parent process or a child process

    if (argc>1 && :: strcmp(argv[1], "child" ) == 0)

    {

        Child() ;

    }

    else

    {

        Parent() ;

    }

4. Experimental results and analysis

(1) Basic Win32 Console Application:

Expected result: Compilation succeeds and an executable is generated. After running the executable file, the running result is displayed in the command prompt window.

  Analysis: This experiment is mainly to verify the correctness of the compilation environment and ensure that the basic Win32 console application can be successfully compiled and executed.

 

(2) Create a process:

Expected result: Every time the program is run, it starts a clone process of itself, and displays each process's system process ID and position in the process list.

    Analysis: By creating a process, multiple instances of the same process can be generated. Each process will display its own system process ID and position in the process list. By modifying how nClone is defined and initialized, the number of processes spawned can be controlled.

 

In the first modification, nClone is defined as 0, and it is initialized to 0. This means that when the program starts running, the initial clone ID will be 0.

In the second modification, the initialization of nClone is modified to 0, but the defined position is different from the first modification.

When modifying the value of nClone, that is, after :: sscanf(argv[1] , "%d" , &nClone) ;, nClone=0; is reassigned. At this time, nClone is always 0, and the process continues to fork.

Starting from the main() function, first judge the value of argc (the initial value of argc is 1 by default). Because argc is not satisfied to be greater than 1, argv[1] cannot be assigned to nClone; then nClone < c_nCloneMax, then call StartClone (++nClone ) function to create a child process; after the child process is created, the value of argc becomes 2, and then assign the self-incremented nClone to argv[1], and then continue to execute the main() function until (nClone >c_nCloneMax), jump out and end Create a new process.

(3) Simple communication between parent and child processes and termination process:

   Expected result: The parent process creates the child process and passes the clone number to the child process via a command line argument. After receiving the clone number, the child process displays its own system process ID and clone number in the command prompt window. The parent and child processes are synchronized through a mutex (Mutex).

  Analysis: Starting from the main() function, first judge the value of argc (the initial value of argc is 1 by default), and decide whether to proceed with the parent process or the child process. Because argc is not satisfied to be greater than 1, the parent() function is called, and the parent() is executed. Call StartClone() during the function process; then assign argv[1] to child through sprintf(szCmdLine, ""%s"child", szFilename), and then call the child() function after the condition is met; since the mutex signal is set, then Only one process is allowed, so only when the parent process releases the mutex signal hMutexSuicide, the child process detects that the process ends

A rough description of the program execution process:

1. The program starts to run and enters the main() function.

2. Determine whether the current process is a parent process or a child process according to the command line parameters.

3. If it is the parent process, execute the Parent() function.

4. In the Parent() function, create a mutex object, and then call the StartClone() function to create a child process.

5. The parent process waits for the user's keyboard input.

6. When the user presses the keyboard, the parent process releases the ownership of the mutex and sends a signal to the child process.

7. The child process executes the Child() function.

8. In the Child() function, the child process opens the mutex object created by the parent process and waits for the signal of the mutex.

9. When the child process receives the signal of the mutex, it outputs the corresponding information, closes the handle of the mutex, and then ends the child process.

10. The parent process releases the handle to the mutex and ends the execution of the parent process.

5. Summary and experience

1. Processes are a fundamental concept in operating systems, and understanding process creation, execution, and management is important for learning operating systems and programming.

2. Windows provides a wealth of APIs and functions to manage processes, such as CreateProcess, GetModuleFileName and GetProcessId. Familiarity with the use of these functions facilitates the creation, control, and communication of processes.

3. Creating subprocesses is a common concurrent programming pattern, which can achieve parallel processing tasks by cloning the main process, and each process can be executed independently.

4. Processes can communicate through command line parameters, and the parent process can pass information to the child process, and observe and analyze the output results of the child process.

5. Mutex (Mutex) is a mechanism for inter-process synchronization, which can ensure sequential execution and mutual exclusive access to shared resources among multiple processes.

6. Debugging tools and trace statements are very helpful for understanding the execution process of the program and debugging errors, and can deepen the understanding of process management.

7. The code examples in the experiment are based on Windows system and Win32 API, but the concept and principle of process management are also common in other operating systems. By learning Windows process management, it can provide the basis for understanding and applying other operating systems.

Guess you like

Origin blog.csdn.net/CSH__/article/details/131382449