How can I run Perl system commands in the background

news/2024/5/18 21:35:46 标签: system, perl, signal, command, 语言, function

       这有一篇关于使用system函数在后台运行一个程序的文章,http://stackoverflow.com/questions/2711520/how-can-i-run-perl-system-commands-in-the-background

      文中有如下几点需要注意:

      1.Perl's system function has two modes:

         (1)taking a single string and passing it to the command shell to allow special charactors to be processed.

         (2)taking a list of strings,execing the first and passing the remaining strings as arguments

      2.You can try using fork,.Both the main process and the background one(the 'child' process) share the same STDIN,STDOUT,and STDERR filehandles .If both try to access them at once,strange things can happen.You may want to close or reopen these for the child.You can get around this with opening a pipe (see open in perlfunc) but on some system that the child process cannot outlive the parent.

      3.Signals. You'll have to catch the SIGCHLD signal,and possibly SIGPIPE too.SIGCHLD is sent when the backgrounded process finishs.SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die).This is not an issue with system("cmd"). 

      4.Zombies .You have to be prepared to "reap" the child process when it finishes.

         $SIG{CHLD}=sub{wait}; or $SIG{CHLD}='IGNORE';

You can also use a double fork.You immdiately wait() for your first child,and the init daemon will wait() for your grandchild once it exits

         unless($pid=fork){

                 unless(fork){

                         exec "what you really wanna do";

                         die "exec failed!";

                  }

                  exit(0);

          }

          waitpid($pid,0);

 

以上虽然是针对perl语言的,但这样情况同样也适应于C语言


http://www.niftyadmin.cn/n/923443.html

相关文章

linux系统解释器

原文:http://www.yuanma.org/data/2007/0821/article_2828.htm 引言 使用Shell进行工作的人们对 Unix/Linux下的Shell编程都很熟悉,在所有的Shell编程的书中都会提到#!/bin/bash,而这里到底包含了些什么?对操作系统而 言,这一行…

常见链表题目

原文:http://blog.csdn.net/hairetz/archive/2010/08/25/5836986.aspx 一些常见的单链表题目,总结思路和实现代码。 1.单链表的反序 2.给单链表建环 3.检测单链表是否有环 4.给单链表解环 5.检测两条链表是否相交 6.不输入头节点&am…

计算机专业面试笔试问题之大数据量,海量数据 处理方法总结

大数据量的问题是很多面试笔试中经常出现的问题,比如baidu google 腾讯 这样的一些涉及到海量数据的公司经常会问到。 下面的方法是我对海量数据的处理方法进行了一个一般性的总结,当然这些方法可能并不能完全覆盖所有的问题,但是这样的一些方…

Linux下如何解压bz2文件

tar.bz2 解压命令 bzip2 -d gcc-4.1.0.tar.bz2---上面解压完之后执行下面的命令。 tar -xvf gcc-4.1.0.tar 或 tar -xvf *.tar解完之后会出现多一个文件夹 gcc-4.1.0

linux进程间通信之信号量

信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制。相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时,进程也可以修改该标志。除了用于访问控制外,还可用于进程同步。…

Linux文件特殊权限 SUID/SGID/Sticky Bit

转自:http://os.51cto.com/art/200801/64465.htm 前面一直提到文件的重要权限,就是rwx这3个读、写、执行的权限。但是,怎么 /tmp权限有些奇怪?还有, /usr/bin/passwd也有些奇怪,怎么回事呢? […

设置文件创建方式屏蔽码(umask命令)

命令的格式为: umask [3位八进制数] umask命令把用户创建文件方式的屏蔽码置为参数规定的模式。3位八进制数分别表示文件主,同组用户和其他用户的读/写/执行权限。因此执行了 umask命令后,新建文件的实际权限为…

Linux mount中的几个基本命令

转自:http://os.51cto.com/art/201002/182606.htm Linux mount命令系统的特性,既可作为高校计算机专业Linux mount命令学习,也可以作为Linux mount命令系统开源爱好者、Linux mount命令系统用户的学习。 一、最基本的几个Linux mount命令&…