c_header: system()(linux-stdlib.h)
一、源码
1 [root@rocky c]# cat systemcall.c
2 /*
3 *
4 *
5 * file_name = systemcall.c
6 *
7 *
8 *
9 */
10
11
12 #include<stdio.h>
13 #include<stdlib.h>
14 #include<string.h>
15
16
17 #include<unistd.h> // sleep()
18 #include<time.h> // time_t, time()
19
20
21 // <stdlib.h>: system()
22 void
23 msg1()
24 {
25 int size = 5;
26 char *cmd[] = {"pwd", "ls", "date", "uname -a", "cd ~; ls -l"};
27
28 for(int i=0; i<size; i++)
29 {
30 printf("\n");
31 printf("\n\t== run_cmd_start: [ %s ]\t==\n", cmd[i]);
32 system(cmd[i]);
33 printf("\t-- run_cmd_end: [ %s ]\t--\n", cmd[i]);
34 printf("\n");
35 }
36 }
37
38
39 // <stdlib.h>: system()
40 void
41 msg2(int size, char **cmd)
42 {
43
44 printf("\n");
45 printf("\n");
46 for(int i=1; i<size; i++)
47 {
48 printf("[%s]\n", cmd[i]);
49 system(cmd[i]);
50 printf("\n");
51 printf("\n");
52 }
53 printf("\n");
54 }
55
56
57 // <stdlib.h>: srand(), rand()
58 void
59 msg3()
60 {
61 const int SIZE = 10;
62 time_t t;
63 int rand_dig = 0;
64 printf("\n\trand_max = %d\n\n", RAND_MAX);
65 for(int i=1; i<= SIZE; i++)
66 {
67 printf("\twait[ 2 ] seconds...\n");
68 sleep(2);
69 srand( (unsigned)time(&t) );
70 rand_dig = ( rand()%100 );
71 printf("\t\trand_number<rang:0~100>[time_%d] = %d\n", i, rand_dig );
72 }
73 printf("\n\n");
74 }
75
76
77 // <stdlib.h>: abort(void); atexit(func); exit(int);
78 void
79 callee_by_atexit()
80 {
81 printf("\n\tOS: CALLEE_BY_ATEXIT()\n\n");
82 }
83
84
85 void
86 msg4()
87 {
88 // getting random number
89 time_t t;
90 int rand_range = 0;
91 srand(( unsigned)time(&t) );
92 // rand_range: 0 ~ 100
93 rand_range = rand()%100 ;
94
95 if(rand_range > 90)
96 {
97 printf("\n\t [rand_range = %d](range_range > 90); run_function[ abort() ]\n\n", rand_range);
98 abort();
99 }else if( rand_range < 40 )
100 {
101 printf("\n\t [rand_range = %d](range_range < 40); run_function[ exit(80) ]\n\n", rand_range);
102 exit(80);
103 }else
104 {
105 printf("\n\t [rand_range = %d]( 40 <= rand_range <= 90); run_function[ atexit(callee_by_atexit()) ]\n\n", rand_range);
106 atexit(callee_by_atexit);
107 }
108 }
109
110 // -- sperate line --
111
112
113
114
115 int
116 main(int argc, char *argv[], char *envp[])
117 {
118
119 // msg1();
120 msg2(argc, argv);
121 // msg3();
122 // msg4();
123
124 return 0;
125
126 }
127 [root@rocky c]#
128 [root@rocky c]#
二、运行结果:
1 [root@rocky c]# uname -a
2 Linux rocky 5.14.0-162.6.1.el9_1.0.1.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Nov 28 18:44:09 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
3 [root@rocky c]#
4 [root@rocky c]#
5 [root@rocky c]# gcc --version
6 gcc (GCC) 11.3.1 20220421 (Red Hat 11.3.1-2)
7 Copyright (C) 2021 Free Software Foundation, Inc.
8 This is free software; see the source for copying conditions. There is NO
9 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
11 [root@rocky c]#
12 [root@rocky c]#
13 [root@rocky c]# gcc -g -Wall -o systemcall systemcall.c && ./systemcall "ls -l" "pwd" "echo hello, world!"
14
15
16 [ls -l]
17 total 80
18 -rw-r--r-- 1 root root 625 Dec 28 03:11 arg_test.c
19 -rw-r--r-- 1 root root 900 Dec 31 01:04 asm_test.c
20 -rw-r--r-- 1 root root 539 Dec 29 01:25 char_test.c
21 -rw-r--r-- 1 root root 41 Jan 20 00:50 me.txt
22 -rw-r--r-- 1 root root 2352 Dec 28 23:56 pointer_array.c
23 -rw-r--r-- 1 root root 490 Dec 29 17:09 preprocessor.c
24 -rw-r--r-- 1 root root 2027 Jan 20 00:51 stdio_header.c
25 -rw-r--r-- 1 root root 1967 Jan 14 19:02 stdlib_header.c
26 -rwxr-xr-x 1 root root 32320 Jan 20 03:12 systemcall
27 -rw-r--r-- 1 root root 2035 Jan 20 03:08 systemcall.c
28 -rw-r--r-- 1 root root 154 Jan 4 21:08 template.c
29 -rw-r--r-- 1 root root 536 Dec 29 18:34 thread_test.c
30 -rw-r--r-- 1 root root 1166 Jan 4 23:38 time_header.c
31
32
33 [pwd]
34 /root/user/lidawei/tmp/c
35
36
37 [echo hello, world!]
38 hello, world!
39
40
41
42 [root@rocky c]#
43 [root@rocky c]#
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17026432.html