选择显示字体大小

利用api在windows下创建进程和线程


  前言:

  谈到在windows创建线程的例子,在网上的很多的参考都是基于mfc的。其实,就操作系统实验这个前提而言,大可不必去碰那个大型的mfc的框架。[1]中提到了在windows命令控制台下创建进程及线程的方法,本文对[1]中的方法加以了实现,并做些简单的进程及线程测试程序。

  1、实验准备:

  要实验的windows下的多线程实验,应做如下准备:

  a) 在新建中选”win32 console application”àan empty project

  b) 选”工程”à”设置”选项,在”设置”中选择“c/c++”标签,在”project option”中,将”mld”参数改成“mtd”(如图1)。


(图片较大,请拉动滚动条观看)
图1

  以上两步对实验成功至关重要,否则,即是代码无误,在连接时同样会出现问题。

  2、windows下进程的创建:

  在[1]中,windows的进程和线程模型被描述成”多进程,基于单进程的多线程”。

  在创建一个线程时,windows会做大量的工作---创建一个新的地址空间,为进程分配资源以及创建一个基线程

  createprocess函数的原型如下:

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
)

  虽然有很多参数,不过在现阶段的实验级别,大多数参数只要用默认值即可。

  下面要做的关于windows使用进程的实验,在linux系统下,可以使用类似:

  execve(char* cmdname , char* cmdargu)的语句从一个程序中去执行其它的程序。

  而如果在windows下,当使用createprocess去执行相应的功能时,只要去改变cmdline中的内容即可,其它的参数使用默认值,具体见代码1:

  代码1执行的功能是从命令行中启动这个名叫的launch的测试程序,在launch后面应加上保存有需要打开程序路径的文件名:

  如在命令行中键入:

>launch set.txt

  而set.txt中的内容为:

c:\\windows\\system32\\calc.exe
c:\\windows\\system32\\notepad.exe new.txt
c:\\windows\\system32\\charmap.exe

  路径的前半部分为”c:\\windows\\”,这当然要视你的windows系统的类型以及系统盘的存放位置而定。如果是nt或2000的机器,则应使用winnt.

/*测试程序1:

  示例如何使用进程的launch程序,通过在命令行中加载相应的命令文件,去按照命令文件中指定的程序路径打开相应的程序去执行*/

#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 &#37;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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons