进程管理 os编程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了进程管理 os编程相关的知识,希望对你有一定的参考价值。
参考技术A 分类: 电脑/网络 >> 程序设计 >> 其他编程语言问题描述:
实验一 进程管理
一、实验目的:
(1)通过本次实验,进一步理解进程PCB的概念,以及PCB如何实现,复习数据结构中如何对链的操作,包括建链、删除、插入结点等。
(2)通过实验使学生进一步了解进程、进程状态、进程控制块等基本概念。
二、实验要求:
(1)复习进程管理部分内容。
(2)运行示例程序,进一步明确实验要求。
(3)上机编程,调试程序。
三、实验内容:
(1)建立一个结点,即PCB块包括用户标识域、状态域(执行、等待、就绪)、link域。
(2)建立三个队列(执行队列、就绪队列、等待队列)。
(3)根据进程状态转换实现对三个队列的具体操作。
(4)运行结果。
解析:
创建进程的api函数
BOOL CreateProcess(
LPCWSTR pszImageName,
LPCWSTR pszCmdLine,
LPSECURITY_ATTRIBUTES psaProcess,
LPSECURITY_ATTRIBUTES psaThread,
BOOL fInheritHandles,
DWORD fdwCreate,
LPVOID pvEnvironment,
LPWSTR pszCurDir,
LPSTARTUPINFOW psiStartInfo,
LPPROCESS_INFORMATION pProcInfo
);
Parameters
pszImageName
[in] Pointer to a null-terminated string that specifies the module to execute.
The string can specify the full path and filename of the module to execute or it can specify a partial path and filename.
The lpszImageName parameter must be non-NULL and must include the module name.
pszCmdLine
[in, out] Pointer to a null-terminated string that specifies the mand line to execute.
The system adds a null character to the mand line, trimming the string if necessary, to indicate which file was used.
The lpszCmdLine parameter can be NULL. In that case, the function uses the string pointed to by lpszImageName as the mand line.
If lpszImageName and lpszCmdLine are non-NULL, * lpszImageName specifies the module to execute, and * lpszCmdLine specifies the mand line.
C runtime processes can use the argc and argv arguments.
If the filename does not contain an extension, .EXE is assumed.
If the filename ends in a period (.) with no extension, or if the filename contains a path, .EXE is not appended.
psaProcess
[in] Not supported; set to NULL.
psaThread
[in] Not supported; set to NULL.
fInheritHandles
[in] Not supported; set to FALSE.
fdwCreate
[in] Specifies additional flags that control the priority and the creation of the process.
The following creation flags can be specified in any bination, except as noted.
Value Description
CREATE_DEFAULT_ERROR_MODE Not supported.
CREATE_NEW_CONSOLE The new process has a new console, instead of inheriting the parent's console.
CREATE_NEW_PROCESS_GROUP Not supported.
CREATE_SEPARATE_WOW_VDM Not supported.
CREATE_SHARED_WOW_VDM Not supported.
CREATE_SUSPENDED The primary thread of the new process is created in a suspended state, and does not run until the ResumeThread function is called.
CREATE_UNICODE_ENVIRONMENT Not supported.
DEBUG_PROCESS If this flag is set, the calling process is treated as a debugger, and the new process is a process being debugged. Child processes of the new process are also debugged.
The system notifies the debugger of all debug events that occur in the process being debugged.
If you create a process with this flag set, only the calling thread (the thread that called CreateProcess) can call the WaitForDebugEvent function.
DEBUG_ONLY_THIS_PROCESS If this flag is set, the calling process is treated as a debugger, and the new process is a process being debugged. No child processes of the new process are debugged.
The system notifies the debugger of all debug events that occur in the process being debugged.
DETACHED_PROCESS Not supported.
INHERIT_CALLER_PRIORITY If this flag is set, the new process inherits the priority of the creator process.
Windows CE does not support the concept of a priority class. The priority of a thread is the only parameter that determines a thread's sche *** ng priority.
pvEnvironment
[in] Not supported; set to NULL.
pszCurDir
[in] Not supported; set to NULL.
psiStartInfo
[in] Not supported; set to NULL.
pProcInfo
[out] Pointer to a PROCESS_INFORMATION structure that receives identification information about the new process.
Return Values
Nonzero indicates success.
Zero indicates failure.
To get extended error information, call GetLastError.
Remarks
In addition to creating a process, it also creates a thread object.
The size of the initial stack for the thread is described in the image header of the specified program's executable file.
The thread begins execution at the image's entry point.
The new process and the new thread handles are created with full access rights. For either handle, the handle can be used in any function that requires an object handle to that type.
The process is assigned a 32-bit process identifier. The identifier is valid until the process terminates. It can be used to identify the process, or specified in the OpenProcess function to open a handle to the process.
The initial thread in the process is also assigned a 32-bit thread identifier. The identifier is valid until the thread terminates and can be used to uniquely identify the thread within the system. These identifiers are returned in the PROCESS_INFORMATION structure.
The following list shows the directories indicated by the pszImageName parameter in the order that Windows CE searches them:
The windows (\windows) directory
The root (\) directory of the device
An OEM-dependent directory, which include the Windows CE Debug shell directory for Platform Builder users only
The following list shows the directories indicated by the pszImageName parameter in the order that Windows CE 2.10 and later search them:
The windows (\windows) directory
The root (\) directory of the device
An OEM-dependent directory
The OEM-defined shell (\ceshell) directory (Platform Builder users only)
The following list shows the directories indicated by the pszImageName parameter in the order Windows CE 1.0 through 2.01 search them:
The root of the PC Card, if it exists
The windows (\windows) directory
The root (\ ) directory of the device
When specifying an application name in the pszImageName string, it does not matter whether the application name includes the filename extension.
Do not call CreateProcess from a DllMain function. This causes the application to stop responding.
The following registry subkey specifies a search path to use with the LoadLibrary function and CreateProcess:
HKEY_LOCAL_MACHINE\Loader
"SystemPath" = multi_sz:"\\path1\\"
"\\path2\\"
For efficiency, the path is searched before CESH but after the ROM and built in file systems. The path is only searched if the path of the file being looked for is not explicitly specified.
If the length of the SystemPath value exceeds 260 characters, the path is ignored. A change to the SystemPath key does not take effect until a Windows CE-based device is reset.
ExitThread, CreateThread, and a process that is starting (as the result of a call by CreateProcess) are serialized beeen each other within a process. Only one of these events can happen in an address space at a time. The following list shows the restrictions during the process:
During process startup and DLL initialization routines, new threads can be created but they do not begin execution until DLL initialization is pleted.
In a process, only one thread at a time can be in a DLL initialization or detach routine.
The created process remains in the system until all threads within the process are terminated and all handles to the process and its threads are closed through calls to CloseHandle.
The handles for the process and the main thread must be closed through calls to CloseHandle. If these handles are not needed, close them immediately after the process is created.
The following events occur when the last thread in a process terminates:
All objects opened by the process are implicitly closed.
The process's termination status, which is returned by GetExitCodeProcess, changes from its initial value of STILL_ACTIVE to the termination status of the last thread to terminate.
The thread object of the main thread is set to the signaled state, satisfying threads that were waiting on the object.
The process object is set to the signaled state, satisfying threads that were waiting on the object.
The handle returned by the CreateProcess function has PROCESS_ALL_ACCESS access to the process object.
Requirements
OS Versions: Windows CE 1.0 and later.
Header: Winbase.h.
Link Library: Coredll.lib.
创建线程的api函数
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpsa,
DWORD cbStack,
LPTHREAD_START_ROUTINE lpStartAddr,
LPVOID lpvThreadParam,
DWORD fdwCreate,
LPDWORD lpIDThread
);
Parameters
lpsa
[in] Ignored. Must be NULL.
cbStack
[in] Ignored unless the STACK_SIZE_PARAM_IS_A_RESERVATION flag is used; in that case the cbStack parameter specifies the virtual memory reserved for the new thread.
When ignored, the default stack size for a thread is determined by the linker setting /STACK.
lpStartAddr
[in] Long pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread; represents the starting address of the thread. For more information on the thread function, see ThreadProc.
Note It is invalid to set lpStartAddr to NULL. If this value is passed in the parameter, the function will return ERROR_INVALID_PARAMETER.
lpvThreadParam
[in] Long pointer to a single 32-bit parameter value passed to the thread.
fdwCreate
[in] Specifies flags that control the creation of the thread.
The following table shows the values for this parameter.
Value Description
CREATE_SUSPENDED The thread is created in a suspended state and does not run until the ResumeThread function is called.
The thread can be run immediately after creation if the flag is not specified.
STACK_SIZE_PARAM_IS_A_RESERVATION The cbStack parameter specified the maximum stack size instead of being ignored.
This is a Windows CE only flag for the CreateThread function and is used to reserve the stack size for the thread created.
By default, a thread is created with 64 KB stack reserved. You can use this flag to increase the stack size for the new thread that was created.
lpIDThread
[out] Long pointer to a 32-bit variable that receives the thread identifier.
If this parameter is NULL, the thread identifier is not returned.
Return Values
A handle to the new thread indicates success.
NULL indicates failure.
To get extended error information, call GetLastError.
Remarks
The number of threads a process can create is limited by the available virtual memory and depends on the default stack size.
If every thread has 1 MB of stack space, you can create a maximum of 32 threads.
If you reduce the default stack size, you can create more threads.
Your application will perform better if you create one thread per processor and build queues of requests for which the application maintains the context information. That is because a thread processes all requests in a queue before processing requests in the next queue.
The new thread handle is created with THREAD_ALL_ACCESS to the new thread. The handle can be used in any function that requires a thread object handle.
Thread execution begins at the function specified by the lpStartAddr parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.
If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing .dll files.
The thread is created with a thread priority of THREAD_PRIORITY_NORMAL.
Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.
When a thread terminates, the thread object attains a signaled state, satisfying threads that were waiting on the object.
The thread object remains in the system until the thread has terminated and all handles to it are closed through a call to CloseHandle.
The ExitThread function, CreateThread function, and a process that is starting (as the result of a call by CreateProcess) are serialized beeen each other within a process. Only one of these events can happen in an address space at a time.
The following list shows the restrictions during the process:
During process startup and DLL initialization routines, threads can be created, but they do not begin execution until DLL initialization is done for the process.
In a process, only one thread at a time can be in a DLL initialization or detach routine.
Requirements
OS Versions: Windows CE 1.01 and later.
Header: Winbase.h.
Link Library: Coredll.lib.
python多进程编程
""""
import os
import time
#fork只用于linux中
pid = os.fork()
print("hello")
if pid == 0:
print("子进程{},父进程是{}".format(os.getpid(),os.getppid()))
else:
print("父进程是{}".format(os.getppid()))
#加这个的目的是当父进程关闭的时候关闭子进程
time.sleep(2)
"""
from concurrent.futures import ProcessPoolExecutor
import multiprocessing
#多进程编程
import time
def get_html(n):
time.sleep(n)
print("sub_process")
return n
if __name__ == "__main__":
#process = multiprocessing.Process(target=get_html,args=(2,))
#process.start()
#process.join()
#print("main process")
#进程池
pool = multiprocessing.Pool(multiprocessing.cpu_count())
#result = pool.apply_async(get_html,args=(3,))
#等待所有任务完成加入
#pool.close()
#pool.join()
#print(result.get())
#imap
#for result in pool.imap(get_html,[1,5,3]):
# print("{} sleep success".format(result))
for result in pool.imap_unordered(get_html,[1,5,3]):
print("{} sleep success".format(result))
以上是关于进程管理 os编程的主要内容,如果未能解决你的问题,请参考以下文章