windows

在windows下的system函数中命令可以不区别大小写! 
功 能: 发出一个DOS命令

#include <stdlib.h>int system(char *command);

  执行成功返回0,执行不成功由于不同的操作返回的值不同,可以查手册看

#include<stdio.h>
#include<stdlib.h>
int main()
{printf("About to spawn and run a DOS command\n");system("dir");return 0;
}

自动关机代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main()
{char order[10];system("color 0C");//设置默认控制台前景个背景色system("date /T");//该函数可以返回当前系统日期system("TIME /T");//该函数可以返回当前系统时间flag:printf("输入\"我是猪\",否则电脑两分钟关机!!!\n");system("shutdown -s -t 120");scanf("%s",order);if(strcmp(order,"我是猪")==0){printf("恭喜你成功的定位自己的身份!!!关机动作取消\n");system("shutdown -a");system("pause");}elsegoto flag;return 0;
} 

定时关机:

#include<stdio.h>
#include<stdlib.h>    //可以输入system用以键入DOS管理窗口界面下的cmd中的命令
#include<string.h>
void print()
{printf("****************关机程序****************\n");printf("****1.实现在十分钟内的定时关闭计算机****\n");printf("****2.立即关闭计算机********************\n");printf("****3.注销计算机************************\n");printf("****4.取消自动关机**********************\n");printf("****5.退出系统**************************\n");
}int main()
{system("title C语言关机程序");//设置cmd窗口宽度system("color 2C");//设置默认控制台前景个背景色system("date /T");system("TIME /T");char cmd[20] = "shutdown -s -t ";char t[5];print();
flag:printf("请输入您的选择1-5:");int c;scanf("%d", &c);if(c>5||c==0){printf("您输入的不合法,请重新输入.\n");fflush(stdin);goto flag;}getchar();switch(c){case 1:printf("您想在多少秒后自动关闭计算机?(0~600)\n");scanf("%s", t);system(strcat(cmd, t));break;case 2:system("shutdown -p");break;case 3:system("shutdown -l");break;case 4:system("shutdown -a");case 5:return 0;default:printf("Error!\n");}system("pause");return 0;
}

删除文件:

#include<stdio.h>
#include<stdlib.h>
int main()
{system("del d:\123.txt");return 0;
}

Linux

system源码

#include  <sys/wait.h>
#include  <erron.h>
#include  <signal.h>
#include  <unistd.h>
int system(const char* cmdstring)
{ pid_t pid;int status;struct sigaction ignore,saveintr,savequit;sigset_t chldmask,savemask;if(cmdstring==NULL)return 1;ignore.sa_handler=SIG_IGN;if(sigaction(SIGINT,&ignore,&saveintr)<0)return -1;if(sigaction(SIGQUIT,&ignore,&savequit)<0)return -1;sigemptyset(&chldmask);sigaddset(&chldmask,SIGCHLD);if(sigpromask(SIG_BOLCK,&chllmask,&savemask)return -1;if((pid=fork())<0)status=-1;else if(pid==0){sigaction(SIGINT,&saveintr,NULL);sigaction(SIGQUIT,&savequit,NULL);sigpromask(SIG_SETMASK,&savemask,NULL);execl("/bin/sh","sh","-c",cmdstring,(char*)0);_exit(127);}else {while(waitpid(pid,&status,0)<0){if(errno!=EINTR){status=-1;break;}}if(sigaction(SIGINT,&saveintr,NULL)<0)return -1;if(sigaction(SIGQUIT,&savequit,NULL)<0)return -1;if(sigpromask(SIG_SETMASK,&savemask,NULL)<0)return -1;}return status;
}

  man:system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

  如果父进程正在捕捉SIGCHLD信号,那么正在执行system函数的时候,应当阻塞对父进程递送SIGCHLD信号.否则,当system创建的子进程结束的时候,system的调用者可能错误的认为,它自己的一个子进程结束了.于是,调用者将会调用一种wait函数以获得子进程的终止状态,这样就阻止了system函数获得子进程的终止状态,并将其作为返回值。

  system()函数调用/bin/sh来执行参数指定的命令,/bin/sh 一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令;

  1. 在该command执行期间,SIGCHLD是被阻塞的,收不到内核发送的SIGCHLD信号
  2. 在该command执行期间,SIGINT和SIGQUIT是被忽略的,意思是进程收到这两个信号后没有任何动作。

返回值

  The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).If the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.

  system执行流程

  1. fork一个子进程
  2. 在子进程中调用exec函数去执行command
  3. 在父进程中调用wait去等待子进程结束
  4. 对于fork失败,system()函数返回-1

注意:

  1. 如果exec执行成功,也即command顺利执行完毕,则返回command通过exit或return返回的值。(注意,command顺利执行不代表执行成功,比如command:"rm debuglog.txt",不管文件存不存在该command都顺利执行了)
  2. 如果exec执行失败,也即command没有顺利执行,比如被信号中断,或者command命令根本不存在,system()函数返回127.
  3. 如果command为NULL,则system()函数返回非0值,一般为1.
  4. command命令返回0时,system返回0
  5. 如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno来确认执行成功
  6. 在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。system函数已经被收录在标准c库中,可以直接调用
system("mkdir $HOME/.SmartPlatform/");
system("mkdir $HOME/.SmartPlatform/Files/");
system("cp mainnew.cpp $HOME/.SmartPlatform/Files/"); 

linux windows c system 函数简介相关推荐

  1. Linux下使用system()函数一定要谨慎

    转载自:http://my.oschina.net/renhc/blog/53580 linux尽量避免使用system. 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system( ...

  2. windows下system函数的使用

    system函数 是可以调用一些DOS命令,比如 system("cls");//清屏,等于在DOS上使用cls命令 写可执行文件路径,可以运行它···· 下面列出常用的DOS命令 ...

  3. Windows下system()函数详解

    Start~~ 在C++/C的Windows环境中,对于控制台窗口的处理,有一个函数必须知道: system(" "); 是的,没错.它可以调动Cmd控制台中的命令,来方便程序员的 ...

  4. Linux下使用system函数获取命令执行返回结果

    在Linux C语言中,需要获取设备挂载和空间容量信息,这时候最简单的方式就是使用命令工具进行查询,但是system函数调用之能返回进行执行的状态,不能返回执行的结果:所以这里自己实现system函数 ...

  5. 【C/C++】Linux下使用system()函数一定要谨慎

    曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值.它所执行命令的返回值以及命令执行失败原 ...

  6. linux下system函数头文件,Linux C:system函数与后台作业

    system函数的参数(字符串)是要执行的命令,这个命令可以使用 & 运行后台作业,也可以一次执行多个程序. 示例1: #include int main() { int status; if ...

  7. linux c 内存操作函数 简介

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  8. Linux下system () 函数详解简介

    (执行shell 命令) 相关函数 fork,execve,waitpid,popen 表头文件 #include<stdlib.h> 定义函数 int system(const char ...

  9. linux下system函数的深入理解

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

最新文章

  1. android studio 跳转后保留原页面数据_Intent详解以及Activity的跳转与数据传递
  2. oracle 两层table of,ORACLE中嵌套表的基本知识
  3. linux查看系统内存和cpu使用率,查看Linux系统内存、CPU、磁盘使用率
  4. Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?
  5. WinRAR 命令行简体中文说明
  6. 杭州市政府数据容灾集中备份业务整体外包(2009)项目招标公告
  7. 一顿操作猛如虎!云原生应用为何如此优秀?
  8. 一手云端,一手终端:比特大陆发布两款AI芯片,大步迈进AI领域
  9. Could not retrieve transaction read-only status from server
  10. MFC_选择目录对话框_选择文件对话框_指定目录遍历文件
  11. Web前端的状态管理
  12. submit和button的区别
  13. DevExpress Dashboard创建仪表盘参数
  14. 一个canvas的demo
  15. Oracle笔记整理
  16. C++ 句柄类的原理以及设计
  17. Android Studio最新稳定版下载 - 百度网盘(更新于2017年7月14日)
  18. wget:Unable to establish SSL connection错误
  19. 各种API以及事件初识——笔记
  20. 微软2008年7月「最有价值专家」(MVP)当选名单

热门文章

  1. 5GS 协议栈 — PFCP 协议 — BAR 缓存行为规则
  2. VMware 虚拟化编程(1) — VMDK/VDDK/VixDiskLib/VADP 概念简析
  3. 软件测试面试-如何测试一个杯子(转)
  4. 力扣(LeetCode)31
  5. Spring-boot国际化
  6. Redis分布式锁 Spring Schedule实现任务调度
  7. 180.4. WebSphere Commerce Engerprise 7.0 Feature Pack 2.iso
  8. Win2008R2配置WebDeploy发布网站
  9. c++11 字符串与int类型的转换
  10. Android 获取SDCard中某个目录下图片