JPEG文件的读写(二)

36 篇文章 0 订阅
15 篇文章 0 订阅

昨天晚上我回去,实验一次在java中调用外部程序实验, 外部程序是这样写的:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int getIntNumber();
  4. int main(int argc, char *argv[])
  5. {
  6.     int number = getIntNumber();
  7.     printf("%d", number);
  8. }
  9. /*
  10.  * get the random int number.
  11.  */
  12. int getIntNumber()
  13. {
  14.      srand((int)time(0));
  15.      return rand();
  16. }

[lidh04@localhost~$] gcc -o number number.c
[lidh04@localhost~$] ls
number.c            number

调用number程序是用java写的,程序代码如下:

  1. public class TestProcess {
  2.     public static void main(String[] args) {
  3.         Process proc = null;
  4.         int round = 0;
  5.         while (true) {
  6.             proc = Runtime.getRuntime().exec(args);
  7.             // wait for subprocess
  8.             Thread.sleep(100);
  9.             DataInputStream in = new DataInputStream(proc.getInputStream());
  10.             int len = in.available();
  11.             byte[] datas = new byte[len];
  12.             while (true) {
  13.                 int readLen = in.read(datas);
  14.                 if (readLen == -1break;
  15.             }
  16.             System.out.print("round " + round + ": ");
  17.             System.out.println(new String(datas);
  18.             if (round++ == 1000break;
  19.         }
  20.     }
  21. }

[lidh04@localhost~$] javac TestProcess
[lidh04@localhost~$] java TestProcess ./number
round 0: 1023909900
round 1: 3343243422
round 2: 1223234323
round 3: 1232345677
round 4: 2134565777

程序就在这时就不动了,估计是死锁了, 我在java调用程序中添加了好几处Thread.sleep(100),但还是不管用.看了看j2sdk有关
Process文档, 有一段话:
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

感觉不应该在09行添加Thread.sleep(100), 因为文档说了,如果没有及时读取子进程输出流或写子进程的输入流会导致死锁.我删除了这条语句还是一样死锁. 总之,我还没有弄懂为什么会发生死锁.怎样避免死锁. 希望有高手能够指点.
为了解决调用外部程序死锁的问题,我用了Shell脚本:

  1. #!/bin/sh
  2. # combine java and c language programs.
  3. ############## functions ################
  4. auto_register()
  5. {
  6.     # download the vercode img.
  7.     echo "downloading vercode..."
  8.     java URLClient http://172.16.3.89/authImg
  9.     echo "done!"
  10.     # invoke the c program
  11.     vercode=`./UseFreeImageLib vercode.jpg`
  12.     # send post request
  13.     name=`echo $RANDOM | md5sum | head -c 12`
  14.     #passwd=`echo $RANDOM | md5sum | head -c 18`
  15.     passwd=$name
  16.     echo "register name: $name passwd: $passwd vercode: $vercode"
  17.     java URLClient $name $passwd $passwd lidh@ss.com $vercode
  18. }
  19. ###### main body ######
  20. i=0
  21. while [ 1 ]
  22. do
  23.     auto_register
  24.     ((i++))
  25.     if [ $i -eq 1000 ]
  26.     then
  27.         break
  28.     fi
  29. done

[lidh04@localhost~$]./combine_c_java.sh
downloading vercode...
done!
register name: 915e104ff6ea passwd: 915e104ff6ea vercode: 3545
code    200
downloading vercode...
done!
register name: 828007765429 passwd: 828007765429 vercode: 2357
code    200
downloading vercode...
done!
register name: 9ed66b143039 passwd: 9ed66b143039 vercode: 3173
code    200
downloading vercode...
done!
register name: 334e1d6c4f64 passwd: 334e1d6c4f64 vercode: 3353
code    200
downloading vercode...
done!
register name: c2b75da4c41a passwd: c2b75da4c41a vercode: 3511
code    200
.....

就可以了.看来程序之间的合作非Shell不行啊. 还是Shell来得好:)







<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值