Linux中的system函数的实现和解释
2013-04-18 22:10
239 查看
#include <sys/wait.h> #include <errno.h> #include <signal.h> #include <unistd.h> int system(const char *cmdstring) /* with appropriate signal handling */ { pid_t pid; int status; struct sigaction ignore, saveintr, savequit; sigset_t chldmask, savemask; if (cmdstring == NULL) return(1); /* always a command processor with UNIX */ ignore.sa_handler = SIG_IGN; /* ignore SIGINT and SIGQUIT */ sigemptyset(&ignore.sa_mask); ignore.sa_flags = 0; if (sigaction(SIGINT, &ignore, &saveintr) < 0) return(-1); if (sigaction(SIGQUIT, &ignore, &savequit) < 0) return(-1); sigemptyset(&chldmask); /* now block SIGCHLD */ sigaddset(&chldmask, SIGCHLD); if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0) return(-1); if ((pid = fork()) < 0) { status = -1; /* probably out of processes */ } else if (pid == 0) { /* child */ /* restore previous signal actions & reset signal mask */ sigaction(SIGINT, &saveintr, NULL); sigaction(SIGQUIT, &savequit, NULL); sigprocmask(SIG_SETMASK, &savemask, NULL); execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); _exit(127); /* exec error */ } else { /* parent */ while (waitpid(pid, &status, 0) < 0) if (errno != EINTR) { status = -1; /* error other than EINTR from waitpid() */ break; } } /* restore previous signal actions & reset signal mask */ if (sigaction(SIGINT, &saveintr, NULL) < 0) return(-1); if (sigaction(SIGQUIT, &savequit, NULL) < 0) return(-1); if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0) return(-1); return(status); }
相关文章推荐
- Linux中的system函数的实现和解释
- Linux中的system函数的实现和解释
- Linux中的system函数的实现和解释
- linux网络编程之System V 消息队列:消息队列实现回射客户/服务器和 msgsnd、msgrcv 函数
- 被调用的linux系统函数system的是如何实现的!
- linux 系统信号忽略和system函数返回值问题(system的实现)
- Linux下c++调用自己编写的matlab函数:通过shell script system command实现
- LINUX中编写函数实现system功能
- LINUX中system()函数详解
- Unix/Linux fork() 函数一次调用2次返回实现原理(个人理解仅供参考)
- Linux下用 select 函数实现定时器
- linux c++ 绑定函数得简单实现
- 【C/C++】Linux下使用system()函数一定要谨慎
- Linux下c++调用自己编写的matlab函数:通过mcc动态链接库.so实现
- linux 下getch()函数实现
- 对于linux下system()函数的深度理解(整理)
- 抱歉,没分了!!!请教linux下,用C/C++编程,如何知道我要实现某一功能需要用到什么函数
- 【对转载进行了部分解释】CRoutingZone的实现机制 以及 process()中一些函数的意义
- 【C/C++】Linux下system()函数引发的错误
- Linux下system()函数返错,errno是ECHILD