<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3268" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT size=2>Hi , guys:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>I worte a c driver for parsing xml pieces. The driver
works , but when the c driver invoked , the erlang emulator will be halted until
the execution of c driver completed. during the
execution of the c driver code, other process of erlang get no chance to be
execute , evenif the usetage of cpu is around 50% (My cpu is duo core 2 ).
</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Does anybody have any idea about this issues?</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Any infomation about how to write a async driver is
appreciate.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>List below is pieces of my erl code and c dirver
code:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>The execution of ?INFO_MSG in test_async_normal will
start until test_async complete£¬ although the execution time
of test_async is more than 2 seconds.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>erlang:</FONT></DIV>
<DIV><FONT size=2>==========================================</FONT></DIV><FONT
size=2>
<DIV><BR>test_async_newpid() -><BR> <BR>
<BR> PID = spawn(?MODULE,
test_async_normal,[""]),<BR> <BR> PID2 =
spawn(?MODULE,
test_async,[""]),<BR> <BR> ?INFO_MSG("New
Pid for port communication:",[PID]),<BR>
<BR> <BR> ok.</DIV>
<DIV> </DIV>
<DIV><BR>test_async_normal(Data) -><BR>
<BR> <BR> timer:sleep(2*
1000),<BR> ?INFO_MSG("Hi , I am in new
Pid:",[erlang:self()]),<BR>
<BR> ok.</DIV>
<DIV> </DIV>
<DIV>test_async (Data) -><BR> <BR> Port =
open_port({spawn, vbp_xmlhandler},
[binary]),<BR> <BR> Bin =
erlang:list_to_binary("hello , i come from erlang: "),
%load_xml_bin(),<BR> <BR> <BR>
Port ! {self(), {command,
Bin}},<BR> <BR>
Ret_str = return_port_data(Port),<BR> <BR>
<BR> ?INFO_MSG("Return from port :
~p~n",[Ret_str]),<BR> <BR> %
?INFO_MSG("Return from port : ~s",[Res]),<BR>
<BR> port_close(Port),</DIV>
<DIV> </DIV>
<DIV> <BR>
<BR>ok.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>return_port_data(Port) -><BR>
receive<BR> {Port, {data, Data}} -><BR>
Data<BR> end. </FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT
size=2>====================================================</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>C driver code :</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT
size=2>-----------------------------------------------------</FONT></DIV>
<DIV><FONT size=2>static ErlDrvEntry vbp_xmlhandler_entry =
{<BR> NULL, /* init */<BR>
vbp_xmlhandler_start, <BR> vbp_xmlhandler_stop,
<BR> vbp_xmlhandler_async, /* output
*/<BR> NULL, /* ready_input
*/<BR> NULL, /* ready_output */
<BR>
"vbp_xmlhandler",
/* the name of the driver */<BR> NULL, /*
finish */<BR> NULL, /* handle
*/<BR> NULL, <BR> NULL, /*
timeout */<BR> NULL, /* outputv
*/<BR> NULL, /* ready_async
*/<BR> NULL, /* flush
*/<BR> NULL, /* call */<BR>
NULL /* event */<BR>};<BR></FONT></DIV>
<DIV><FONT size=2>DRIVER_INIT(vbp_xmlhandler) /* must match name in driver_entry
*/<BR>{<BR> return
&vbp_xmlhandler_entry;<BR>}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2><BR>/*<BR>// async interface for handle
command.<BR>//<BR>*/<BR>static void vbp_xmlhandler_async(ErlDrvData handle, char
*buff, int bufflen){<BR> iostream* stream;<BR> char*
test_data;<BR> char *in_data;<BR> int len;<BR> int i
;<BR> int loop_count;<BR> expat_data* d =
(expat_data*)handle;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=2> len = bufflen;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=2><BR> stream = create_stream();</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> <FONT color=#ff0000>// force a lot of cpu time to be
consumed in current invocation.<BR></FONT> loop_count = 1000000 *
80; <BR> for(i = 0;i <loop_count ;i++ ){<BR> in_data =
malloc(20);</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> free(in_data);<BR> }<BR></FONT><FONT
size=2></FONT></DIV>
<DIV><FONT size=2> in_data = malloc(len + 1);<BR> memset(in_data,0,len
+1);<BR> memcpy(in_data,buff,len);<BR> write_str_to_stream(stream,in_data);<BR> free(in_data);</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> write_str_to_stream(stream," [append by c
driver]");</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2> driver_output(d ->port, stream->buff,
stream->index); <BR> <BR> free_stream(stream);</FONT></DIV>
<DIV><FONT size=2>}</DIV></FONT>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT> </DIV></BODY></HTML>