脚本和程序

一、通过脚本的方式实现

在Linux下面的简单脚本的写法(扩展名为 .sh)
先创建vim test.sh

 

 在其中写入你要的内容这里的意思是重复执行某一个文件三次

1 退出保存后
2 chmod  +x  test.sh
3 这里其实就是给test.sh权限 X表示可执行权限

然后

./test.sh

 

二、程序来控制

创建文件   test.c

 

 然后编译该文件,执行即可

 

 

 

三、今天学到的

 

就拿下面这个代码来吧解释吧,下面的代码是一个多线程的,假如我们要记录一个程序执行结果的过程

并且执行次数比较多,我们就可以通过程序控制程序来记录运行结果,下面是以这个代码的结果来记录

 

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 #include <stdlib.h>
 4 int g_data = 0;
 5 
 6 pthread_mutex_t mutex;
 7 pthread_cond_t cond;
 8 
 9 void *fun(void *arg)
10 {
11     printf("t1: %ld thread is creat\n",(unsigned long)pthread_self());
12     printf("t1:param is %d\n",*((int *)arg));
13     static int cnt;
14     while(1)
15     {
16         pthread_cond_wait(&cond,&mutex);
17         
18         printf("t1 run================================\n");
19         printf("t1: %d \n",g_data);
20         g_data = 0;
21         sleep(1);
22         if(cnt ++ ==10)
23         {
24             exit(1);
25         }
26         //    pthread_exit(NULL);
27         
28     }
29 }
30 
31 
32 void *fun1(void *arg)
33 {
34     printf("t2: %ld thread is creat\n",(unsigned long)pthread_self());
35     printf("t2:param is %d\n",*((int *)arg));
36     while(1)
37     {
38         printf("t2 : %d \n",g_data);
39         pthread_mutex_lock(&mutex);
40         g_data++;
41         pthread_mutex_unlock(&mutex);
42         if(g_data == 3)
43         {
44             pthread_cond_signal(&cond);
45         }
46         sleep(1);
47     }
48 }
49 int main()
50 {
51     int ret;
52     int ret1;
53     int param = 100;
54 
55     pthread_t t1;
56     
57     pthread_t t2;
58     ret = pthread_create(&t1,NULL,fun,(void *) &param);
59     pthread_mutex_init(&mutex,NULL);
60     pthread_cond_init(&cond,NULL);
61     if(ret == 0)
62     {
63 //        printf("main: creat success\n");
64     }
65     ret = pthread_create(&t2,NULL,fun1,(void *) &param);
66     if(ret1 == 0)
67     {
68 //        printf("main: creat success\n");
69     }
70     
71 //    printf("main: %ld\n",(unsigned long)pthread_self());
72 
73 
74     //while(1);
75     pthread_join(t1,NULL);
76     pthread_join(t2,NULL);
77     pthread_mutex_destroy(&mutex);
78     pthread_cond_destroy(&cond);
79     return 0;
80 }

先编译这段代码

1 gcc demo8.c  -pthread  -o demo8

生成的 demo8 的可执行文件,然后创建一个用来控制的程序

1 int main(int argc ,char **argv)
2 {
3     int time  = atoi(argv[1]);
4     int i;
5     for(i=0;i<time;i++)
6     {
7         system("./demo8");
8     }
9 }

照常编译gcc test1.c

 

./a.out 10 >> test.ret.tex &

 

这里的10 表示执行次数   >>表示将结果放到test.ret.tex中    &表示在后台执行

然后等运行结束,运行结果就在test.ret.tex中了

 

posted @ 2020-08-19 21:40  辉白  阅读(23)  评论(0)    收藏  举报