前言:
谈到在windows创建线程的例子,在网上的很多的参考都是基于mfc的。其实,就操作系统实验这个前提而言,大可不必去碰那个大型的mfc的框架。[1]中提到了在windows命令控制台下创建进程及线程的方法,本文对[1]中的方法加以了实现,并做些简单的进程及线程的测试程序。
1、实验准备:
要实验的windows下的多线程实验,应做如下准备:
a) 在新建中选”win32 console application”àan empty project
b) 选”工程”à”设置”选项,在”设置”中选择“c/c++”标签,在”project option”中,将”mld”参数改成“mtd”(如图1)。
(图片较大,请拉动滚动条观看)
图1
| createprocess( lpctstr lpapplicationname, //pointer to name of executable moudle lptstr cmdline, //pointer to command line string lpsecurity_attributes lpprocessattributes, //pointer to process security attributes lpsecurity_attributes lpthreadattributes, //pointer to theread security attributes bool binherithandle , //handle inheritance flag dword dwcreationflag,//various creation flags lpvoid lpenviroment,//enviroment variable lpctstr lpcurrentdirectory, //child's current directory lpstartupinfo lpstartupinfo, //pointer to startupinfo lpprocess_information lpprocessinformation //pointer to process_information ) |
| #include <windows.h> #include <stdio.h> #include <string.h> #define max_line_len 80 int main(int argc,char* argv[]) { //local variables file* fid; char cmdline[max_line_len]; //createprocess parameters lpsecurity_attributes processa=null;//default lpsecurity_attributes threada=null;//default bool sharerights=true;//default dword creationmask=create_new_console;//window per process. lpvoid enviroment=null;//default lpstr curdir=null;//default startupinfo startinfo;//result process_information procinfo;//result //1.read the command line parameters if(argc!=2) { fprintf(stderr,"usage:lanch<launch_set_filename>\n"); exit(0); } //2.open a file that coutain a set of commands fid=fopen(argv[1],"r"); //3.for every command in the launch file while(fgets(cmdline,max_line_len,fid)!=null) { //read a command from the file if(cmdline[strlen(cmdline)-1]=='\n') cmdline[strlen(cmdline)-1]='\0';//remove newline //create a new process to execute the command zeromemory(&startinfo,sizeof(startinfo)); startinfo.cb=sizeof(startinfo); if(!createprocess( null,//file name of executable cmdline,//command line processa,//process inherited security threada, //thread inherited security sharerights,//rights propagation creationmask,//various creation flags enviroment,//enviroment variable curdir, //child's current directory &startinfo, &procinfo ) ) { fprintf(stderr,"creatprocess failed on error %d\n",getlasterror()); exitprocess(0); } } //terminate after all commands have finished. return 0; } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器