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 ;
}

调用color函数可以改变控制台的前景色和背景,具体参数在下面说明。

用 system(“color 0A”); 其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:

0=黑色 1=蓝色 2=绿色 3=湖蓝色 4=红色 5=紫色 6=黄色 7=白色 8=灰色 9=淡蓝色 A=淡绿色 B=淡浅绿色 C=淡红色 D=淡紫色 E=淡黄色 F=亮白色 

自动关机代码:

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

定时关机:

#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[] = "shutdown -s -t ";
char t[]; print();
flag:
printf("请输入您的选择1-5:"); int c;
scanf("%d", &c);
if(c>||c==)
{
printf("您输入的不合法,请重新输入.\n");
fflush(stdin);
goto flag;
}
getchar(); switch(c)
{
case :
printf("您想在多少秒后自动关闭计算机?(0~600)\n");
scanf("%s", t);
system(strcat(cmd, t));
break;
case :
system("shutdown -p");
break;
case :
system("shutdown -l");
break;
case :
system("shutdown -a");
case :
return ;
default:
printf("Error!\n");
}
system("pause");
return ;
}

删除文件:

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

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 ;
ignore.sa_handler=SIG_IGN;
if(sigaction(SIGINT,&ignore,&saveintr)<)
return -;
if(sigaction(SIGQUIT,&ignore,&savequit)<)
return -;
sigemptyset(&chldmask);
sigaddset(&chldmask,SIGCHLD);
if(sigpromask(SIG_BOLCK,&chllmask,&savemask)
return -; if((pid=fork())<)
status=-;
else if(pid==)
{
sigaction(SIGINT,&saveintr,NULL);
sigaction(SIGQUIT,&savequit,NULL);
sigpromask(SIG_SETMASK,&savemask,NULL);
execl("/bin/sh","sh","-c",cmdstring,(char*));
_exit();
}
else
{
while(waitpid(pid,&status,)<)
{
if(errno!=EINTR)
{
status=-;
break;
}
}
if(sigaction(SIGINT,&saveintr,NULL)<)
return -;
if(sigaction(SIGQUIT,&savequit,NULL)<)
return -;
if(sigpromask(SIG_SETMASK,&savemask,NULL)<)
return -;
}
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/");

c/c++中system函数在Linux和windows下区别的更多相关文章

  1. [笔记]linux下和windows下的 创建线程函数

    linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...

  2. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  3. linux和windows下TIME_WAIT过多的解决办法

    http://www.51testing.com/html/48/202848-249774.html linux和windows下TIME_WAIT过多的解决办法 http://m.sohu.com ...

  4. Tomcat日志文件的输出在Linux和Windows下的差异

    前言 最近老大发现Tomcat的日志文件catalina.out里存在着大量的和公司项目相关的log信息,因为一般都是会使用日志框架并另外将log信息输出到另外的文件里的,catalina.out文件 ...

  5. 从Docker在Linux和Windows下的区别简单理解Docker的层次结构

    上篇文章我们成功在Windows下安装了Docker,输出了一个简单的Hello World程序.本文中我们将利用Docker已有的云端镜像training/webapp来发布一个简单Python的W ...

  6. 【深度学习】在linux和windows下anaconda+pycharm+tensorflow+cuda的配置

    在linux和windows下anaconda+pycharm+tensorflow+cuda的配置 在linux和windows下anaconda+pycharm+tensorflow+cuda的配 ...

  7. protobuff 配合 libevent 在Linux 和windows 下的使用

    protobuff 配合 libevent 在Linux 和windows 下的使用待补全. libprotobuf.lib libproto-lite.lib libprotoc.lib

  8. tar.xz如何解压:linux和windows下tar.xz解压命令介绍

    在linux下怎么解压和压缩tar.xz文件? (本文由www.169it.com搜集整理) 在linux下解压tar.xz文件步骤 1 2 # xz -d ***.tar.xz  //先解压xz # ...

  9. Linux和Windows下查看环境变量方法对比

    摘自:Linux和Windows下查看环境变量方法对比 一.查看所有环境变量的名称和值 Linux下:export Windows下:set 二.根据名称查该环境变量的值 Linux下:echo $环 ...

随机推荐

  1. Android Fragment解析(上)

    今天被人问到了什么是Fragment,真是一头雾水,虽然以前也用到过,但不知道它是叫这个名字,狂补一下. 以下内容来自互联网,原文链接:http://blog.csdn.net/lmj62356579 ...

  2. java MongoDB查询(二)复杂查询

    前言 在上篇<java MongoDB查询(一)简单查询>中我们简单了解了下查询,但是仅仅有那些查询是不够用的,还需要复杂的查询,这篇就这点进行叙述. 1.数据结构 集合:firstCol ...

  3. Metasploit 使用简介

    Metasploit Framework 是非常优秀的开源渗透测试框架,像我这样的菜鸟刚刚听说,于是花时间好好研究了一下,整理了一下学习笔记,贴出来和大家一起交流.第一次写文章又不足的地方大家多多指点 ...

  4. nyoj——弃九法

    A*B Problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 设计一个程序求出A*B,然后将其结果每一位相加得到C,如果C的位数大于等于2,继续将C的各位数 ...

  5. iRSF快速简单易用的实现列表、排序、过滤功能

    IRSF 是由javascript编写,iRSF快速简单易用的实现列表.排序.过滤功能(该三种操作以下简称为 RSF ). iRSF由三个类组成. iRSFSource 数据源 iRSFFilter ...

  6. VS2015常用快捷键总结(转)

    生成解决方案 F6,生成项目Shift+F6 调试执行F5,终止调试执行Shift+F5 执行调试Ctrl+F5 查找下一个F3,查找上一个Shift+F3 附加到进程Ctrl+Alt+P,逐过程F1 ...

  7. Django WSGI,MVC,MTV,中间件部分,Form初识

    一.什么是WSGI? WEB框架的本质是一个socket服务端接收用户请求,加工数据返回给客户端(Django),但是Django没有自带socket需要使用 别人的 socket配合Django才能 ...

  8. Unix系统中system函数的返回值

    网上关于system函数的返回值说明很多很详细但却不直观,这里搬出apue 3rd Editon中实现system函数的代码来说明其返回值. #include <sys/wait.h> # ...

  9. Python3的第一个程序(三)

    现在,了解了如何启动和退出Python的交互式环境,我们就可以正式开始编写Python代码了. 在写代码之前,请千万不要用“复制”-“粘贴”把代码从页面粘贴到你自己的电脑上.写程序也讲究一个感觉,你需 ...

  10. Elasticsearch 在分布式系统中深度分页问题

    理解为什么深度分页是有问题的,我们可以假设在一个有 5 个主分片的索引中搜索. 当我们请求结果的第一页(结果从 1 到 10 ),每一个分片产生前 10 的结果,并且返回给 协调节点 ,协调节点对 5 ...