日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

system函数-linux

發布時間:2025/3/15 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 system函数-linux 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

函數介紹:

system - execute a shell commandSYNOPSIS#include <stdlib.h>int system(const char *command);DESCRIPTIONThe system() library function uses fork(2) to create a child processthat executes the shell command specified in command using execl(3) asfollows:execl("/bin/sh", "sh", "-c", command, (char *) 0);system() returns after the command has been completed.

bash? -c? cal

bash -c binfile

andrew@andrew-Thurley:~$ bash -c cal八月 2018 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

?

測試函數:

#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/wait.h>char *cmd1 = "/bin/date";int main(void) {system("clear");system(cmd1);return 0; }

測試結果:

2018年 08月 28日 星期二 23:47:07 CST andrew@andrew-Thurley:~/work/apue.2e$

編寫自己的mysystem函數: bash -c binfilename

#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/wait.h>char *cmd1 = "date > s1.txt"; char *cmd2 = "date > s2.txt";void mysystem(char *cmd);int main(void) {system("clear");system(cmd1);mysystem(cmd2);return 0; }void mysystem(char *cmd) {pid_t pid;if(pid = fork() < 0){perror("fork error");exit(1);}else if(pid == 0){if(execlp("/bin/bash", "bin/bash", "-c", cmd, NULL) < 0){perror("execlp error");exit(1);}}wait(0);}

?

總結

以上是生活随笔為你收集整理的system函数-linux的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。