厦门申通快递价格,锵锵三人行 视频,灰烬出装
进程:是程序的执行过程,具有动态性,即运行的程序就叫进程,不运行就叫程序 ,每个进程包含一到多个线程。
线程:系统中的最小执行单元,同一进程中有多个线程,线程可以共享资源,一旦出现共享资源,必须注意线程安全!!
先阐述一下进程和线程的概念和区别,这是一个许多大学老师也讲不清楚的问题。
进程(process)是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位。程序只是一组指令的有序集合,它本身没有任何运行的含义,只是一个静态实体。而进程则不同,它是程序在某个数据集上的执行,是一个动态实体。它因创建而产生,因调度而运行,因等待资源或事件而被处于等待状态,因完成任务而被撤消,反映了一个程序在一定的数据集上运行的全部动态过程。
线程(thread)是进程的一个实体,是cpu调度和分派的基本单位。线程不能够独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
线程和进程的关系是:线程是属于进程的,线程运行在进程空间内,同一进程所产生的线程共享同一内存空间,当进程退出时该进程所产生的线程都会被强制退出 并清除。线程可与属于同一进程的其它线程共享进程所拥有的全部资源,但是其本身基本上不拥有系统资源,只拥有一点在运行中必不可少的信息(如程序计数器、 一组寄存器和栈)。
根据进程与线程的设置,操作系统大致分为如下类型:
(1)单进程、单线程,ms-dos大致是这种操作系统;
(2)多进程、单线程,多数unix(及类unix的linux)是这种操作系统;
(3)多进程、多线程,win32(windows nt/2000/xp等)、solaris 2.x和os/2都是这种操作系统;
(4)单进程、多线程,vxworks是这种操作系统。
在操作系统中引入线程带来的主要好处是:
(1)在进程内创建、终止线程比创建、终止进程要快;
(2)同一进程内的线程间切换比进程间的切换要快,尤其是用户级线程间的切换。
另外,线程的出现还因为以下几个原因:
(1)并发程序的并发执行,在多处理环境下更为有效。一个并发程序可以建立一个进程,而这个并发程序中的若干并发程序段就可以分别建立若干线程,使这些线程在不同的处理机上执行。
(2)每个进程具有独立的地址空间,而该进程内的所有线程共享该地址空间。这样可以解决父子进程模型中,子进程必须复制父进程地址空间的问题。
(3)线程对解决客户/服务器模型非常有效。
win32进程间通信的方式主要有:
(1)剪贴板(clip board);
(2)动态数据交换(dynamic data exchange);
(3)部件对象模型(component object model);
(4)文件映射(file mapping);
(5)邮件槽(mail slots);
(6)管道(pipes);
(7)win32套接字(socket);
(8)远程过程调用(remote procedure call);
(9)wm_copydata消息(wm_copydata message)。
2、获取进程信息
在win32中,可使用在psapi .dll中提供的process status helper函数帮助我们获取进程信息。
(1)enumprocesses()函数可以获取进程的id,其原型为:
bool enumprocesses(dword * lpidprocess, dword cb, dword*cbneeded); |
参数lpidprocess:一个足够大的dword类型的数组,用于存放进程的id值;
参数cb:存放进程id值的数组的最大长度,是一个dword类型的数据;
参数cbneeded:指向一个dword类型数据的指针,用于返回进程的数目;
函数返回值:如果调用成功,返回true,同时将所有进程的id值存放在lpidprocess参数所指向的数组中,进程个数存放在cbneeded参数所指向的变量中;如果调用失败,返回false。
(2)getmodulefilenameexa()函数可以实现通过进程句柄获取进程文件名,其原型为:
dword getmodulefilenameexa(handle hprocess, hmodule hmodule,lptstr lpstrfilename, dword nsize); |
参数hprocess:接受进程句柄的参数,是handle类型的变量;
参数hmodule:指针型参数,在本文的程序中取值为null;
参数lpstrfilename:lptstr(表示指向字符/字符串的指针)类型的指针,用于接受主调函数传递来的用于存放进程名的字符数组指针;
参数nsize:lpstrfilename所指数组的长度;
函数返回值:如果调用成功,返回一个大于0的dword类型的数据,同时将hprocess所对应的进程名存放在lpstrfilename参数所指向的数组中;加果调用失败,则返回0。
通过下列代码就可以遍历系统中的进程,获得进程列表:
//获取当前进程总数 enumprocesses(process_, sizeof(process_ids), &num_processes); //遍历进程 for (int i = 0; i < num_processes; i++) { //根据进程id获取句柄 process[i] = openprocess(process_query_information | process_vm_read, 0, process_ids[i]); //通过句柄获取进程文件名 if (getmodulefilenameexa(process[i], null, file_name, sizeof(filename))) cout << filename << endl; } |
dword waitforsingleobject( handle hhandle, // 等待对象的句柄 dword dwmilliseconds // 等待毫秒数,infinite表示无限等待 ); |
dword waitformultipleobjects(dword ncount,const handle* phandles,bool bwaitall,dword dwmilliseconds); |
bool closehandle(handle hobject); |
c运行时库 | 库文件 |
single thread(static link) | libc.lib |
debug single thread(static link) | libcd.lib |
multithread(static link) | libcmt.lib |
debug multithread(static link) | libcmtd.lib |
multithread(dynamic link) | msvert.lib |
debug multithread(dynamic link) | msvertd.lib |
error lnk2001: unresolved external symbol __endthreadex error lnk2001: unresolved external symbol __beginthreadex |
uint threadfunction(lpvoid pparam) { //线程处理代码 return0; } |
while(1) { waitforsingleobject(…,…);//或waitformultipleobjects(…) //do something } |
#include "windows.h" #include <process.h> class exampletask { public: void taskmain(lpvoid param); void starttask(); }; void exampletask::taskmain(lpvoid param) {} void exampletask::starttask() { _beginthread(taskmain,0,null); } int main(int argc, char* argv[]) { exampletask realtimetask; realtimetask.starttask(); return 0; } |
error c2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' none of the functions with this name in scope match the target type |
#include "windows.h" #include <process.h> class exampletask { public: void taskmain(lpvoid param); }; void exampletask::taskmain(lpvoid param) {} int main(int argc, char* argv[]) { exampletask realtimetask; _beginthread(exampletask::taskmain,0,null); return 0; } |
error c2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' none of the functions with this name in scope match the target type |
#include "windows.h" #include <process.h> class exampletask { public: void static taskmain(lpvoid param); void starttask(); }; void exampletask::taskmain(lpvoid param) {} void exampletask::starttask() { _beginthread(taskmain,0,null); } int main(int argc, char* argv[]) { exampletask realtimetask; realtimetask.starttask(); return 0; } 和 #include "windows.h" #include <process.h> class exampletask { public: void static taskmain(lpvoid param); }; void exampletask::taskmain(lpvoid param) {} int main(int argc, char* argv[]) { _beginthread(exampletask::taskmain,0,null); return 0; } |
#include "windows.h" #include <process.h> class exampletask { public: friend void taskmain(lpvoid param); void starttask(); }; void taskmain(lpvoid param) { exampletask * ptaskmain = (exampletask *) param; //通过ptaskmain指针引用 } void exampletask::starttask() { _beginthread(taskmain,0,this); } int main(int argc, char* argv[]) { exampletask realtimetask; realtimetask.starttask(); return 0; } |
handle createthread( lpsecurity_attributes lpthreadattributes,//pointer to a security_attributes structure size_t dwstacksize, //initial size of the stack, in bytes. lpthread_start_routine lpstartaddress, lpvoid lpparameter, //pointer to a variable to be passed to the thread dword dwcreationflags, //flags that control the creation of the thread lpdword lpthreadid //pointer to a variable that receives the thread identifier ); |
uintptr_t _beginthread( void( __cdecl *start_address )( void * ), //start address of routine that begins execution of new thread unsigned stack_size, //stack size for new thread or 0. void *arglist //argument list to be passed to new thread or null ); uintptr_t _beginthreadex( void *security,//pointer to a security_attributes structure unsigned stack_size, unsigned ( __stdcall *start_address )( void * ), void *arglist, unsigned initflag,//initial state of new thread (0 for running or create_suspended for suspended); unsigned *thrdaddr ); |
void exitthread(uint fuexitcode ); |
bool terminatethread(handle hthread,dword dwexitcode); |
dword resumethread(handle hthread); |
dword suspendthread(handle hthread); |
bool setthreadpriority(handle hthread, int npriority); |
void sleep(dword dwmilliseconds); |
int getthreadpriority (handle hthread); |
bool winapi getexitcodethread( handle hthread, lpdword lpexitcode ); |
bool winapi getthreadcontext( handle hthread, lpcontext lpcontext ); bool winapi setthreadcontext( handle hthread, const context *lpcontext ); |
#define win32_lean_and_mean #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <conio.h> dword winapi threadfunc(lpvoid); int main() { handle hthrd1; handle hthrd2; dword exitcode1 = 0; dword exitcode2 = 0; dword threadid; hthrd1 = createthread(null, 0, threadfunc, (lpvoid)1, 0, &threadid ); if (hthrd1) printf("thread 1 launched\n"); hthrd2 = createthread(null, 0, threadfunc, (lpvoid)2, 0, &threadid ); if (hthrd2) printf("thread 2 launched\n"); // keep waiting until both calls to getexitcodethread succeed and // neither of them returns still_active. for (;;) { printf("press any key to exit..\n"); getch(); getexitcodethread(hthrd1, &exitcode1); getexitcodethread(hthrd2, &exitcode2); if ( exitcode1 == still_active ) puts("thread 1 is still running!"); if ( exitcode2 == still_active ) puts("thread 2 is still running!"); if ( exitcode1 != still_active && exitcode2 != still_active ) break; } closehandle(hthrd1); closehandle(hthrd2); printf("thread 1 returned %d\n", exitcode1); printf("thread 2 returned %d\n", exitcode2); return exit_success; } /* * take the startup value, do some simple math on it, * and return the calculated value. */ dword winapi threadfunc(lpvoid n) { sleep((dword)n*1000*2); return (dword)n * 10; } |
#define win32_lean_and_mean #include <stdio.h> #include <stdlib.h> #include <windows.h> dword winapi threadfunc(lpvoid); int main() { handle hthrd; dword threadid; int i; for (i = 0; i < 5; i++) { hthrd = createthread(null, 0, threadfunc, (lpvoid)i, 0, &threadid); if (hthrd) { printf("thread launched %d\n", i); closehandle(hthrd); } } // wait for the threads to complete. sleep(2000); return exit_success; } dword winapi threadfunc(lpvoid n) { int i; for (i = 0; i < 10; i++) printf("%d%d%d%d%d%d%d%d\n", n, n, n, n, n, n, n, n); return 0; } |
#include <win32.h> #include <stdio.h> #include <process.h> unsigned counter; unsigned __stdcall secondthreadfunc(void *parguments) { printf("in second thread...\n"); while (counter < 1000000) counter++; _endthreadex(0); return 0; } int main() { handle hthread; unsigned threadid; printf("creating second thread...\n"); // create the second thread. hthread = (handle)_beginthreadex(null, 0, &secondthreadfunc, null, 0, &threadid); // wait until second thread terminates waitforsingleobject(hthread, infinite); printf("counter should be 1000000; it is-> %d\n", counter); // destroy the thread object. closehandle(hthread); } |
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论