linux c编程中调用 shell 命令
2015-07-07 14:51
791 查看
system 函数,会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。
/******************************************************* 本程序作为一个父进程,父进程调用子进程,然后监视子进程 的允许情况。 可以将本进程作为守护进程(具体百度) 可以把本程序添加到 /etc/rc.local 中,实现开机自启动(具体百度) ********************************************************/ #include <fcntl.h> #include <stdio.h> #include <string.h> #include <linux/stat.h> #include <sys/wait.h> #include <errno.h> #include <stdlib.h> #include <stddef.h> #include <nvram.h> #include <unistd.h> int main(void) { int ret; while(1){ printf("开始启动子进程\r\n"); /* 调用 system 函数。该函数会去调用子进程, 假设子进程为 /home 目录下的 client 程序 使用 system 当调用子进程后,父进程将等待 子进程退出 */ ret = system("/home/client"); /* 程序能运行到这里,说明子进程已经退出了 ret 为返回值,返回值为 子进程 main 函数中的 return 值。但实际并不相等,具体百度 */ printf("子进程退出,返回值 %d\r\n", ret); /* 对返回值做个判断,是否是正常退出,也就是 return 0 */ if((-1 == ret) || (WEXITSTATUS(ret) != 0xa)){ //错误 printf("非正常退出子进程\r\n"); } } }
相关文章推荐
- Linux远程管理器xshell和xftp使用教程
- shell基础学习-难点重点学习
- ShellExecute使用详解
- WinCE自启动Shell的问题
- FTP自动化上传的Shell脚本
- cygwin&nbsp;:bash:&nbsp;$&#039;…
- 35、Linux中shell数据重定向(输入重定向与输出重定向)
- 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较
- shell相关指令介绍$*和$#以及$?和if [[ ! -z $1 ]]
- Linux常用命令(vim+bash+hadoop)
- 提高 Vim 和 Shell 效率的 9 个建议
- linux shell 不同进制数据转换(二进制,八进制,十六进制,base64) (转)
- Shell之date用法
- 几个有用的Shell,解析json,解析URL
- ubuntu shell脚本开机自动运行的方法
- shell排序
- Linux学习笔记 (八)Shell概述
- SSH Secure Shell显示服务器Tomcat后台内容
- 一个Hashell模块
- android ADB 基本shell命令