MOOC《Linux操作系统编程》学习笔记-实验五

2021/6/8 7:24:49

本文主要是介绍MOOC《Linux操作系统编程》学习笔记-实验五,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实验五 线程控制实验

https://www.icourse163.org/learn/UESTC-1003040002?tid=1455108444#/learn/content?type=detail&id=1228729538&sm=1

实验目的

 

 程序流程图:

 

实验代码:

  1 #include "stdio.h"
  2 #include "stdint.h"
  3 #include "stdlib.h"
  4 #include "pthread.h"
  5 #include "sys/types.h"
  6 #include "dirent.h"
  7 #include "unistd.h"
  8 #include "string.h"
  9 #include "fcntl.h"
 10 #include "sys/stat.h"
 11 #include "errno.h"
 12 
 13 void *thread_mycp(void *arg);
 14 void *thread_myls(void *arg);
 15 
 16 int main(int argc ,char ** argv)
 17 {
 18     char buff[256] = {0};
 19 
 20     if(1 == argc)
 21     {
 22         /* 获取当前工作目录 */
 23         if(NULL == getcwd(buff,sizeof(buff)))
 24         {
 25             perror("get work path error");
 26         }
 27         //printf("currWorkPath = %s \n",buff);
 28     }
 29     else if(2 == argc)
 30     {
 31         /* 通过参数获取需要遍历的目录 */
 32         memcpy(buff,argv[1],sizeof(buff));
 33         //printf("buff = %s \n",buff);
 34     }
 35     else
 36     {
 37         printf("cmd para is error \n");
 38         return 0;
 39     }
 40 
 41 
 42     pthread_t tid;
 43     char str[100];
 44     printf("create mainThread\n");
 45     //sprintf(str,"str from parent");
 46 
 47     pthread_create(&tid,NULL,(void *)thread_myls,buff);
 48     pthread_join(tid,NULL);
 49     printf("mainThread exit \n");
 50 
 51     return 0;
 52 }
 53 
 54 void *thread_myls(void *arg)
 55 {
 56     /* 打开目录 */
 57     DIR *currentdir;
 58     if((currentdir = opendir((char *)arg)) == NULL)
 59     {
 60         printf("open directory fail \n");
 61         return 0;
 62     }
 63     else
 64     {
 65         struct dirent * currentdp;
 66         while((currentdp = readdir(currentdir))!= NULL)
 67         {
 68             if(currentdp->d_name[0] != '.')
 69             {
 70                 struct stat currentStat;
 71                 uint8_t tempVar[256] = {0};
 72                 strcat(tempVar,(char *)arg);
 73                 strcat(tempVar,"/");
 74                 strcat(tempVar,currentdp->d_name);
 75                 //snprintf(tempVar,sizeof(tempVar),"%s/%s",(char *)arg,currentdp->d_name);
 76                 if(lstat(tempVar,&currentStat) < 0)
 77                 {
 78                     perror("lstat error");
 79                     //printf("error path is %s \n",currentdp->d_name);
 80                     continue;
 81                 }
 82                 pid_t pid_child,pid_return;
 83                 if(S_ISDIR(currentStat.st_mode)) 
 84                 {
 85                     pthread_t tid;
 86 
 87                     printf("create thread_myls\n");
 88                     if(0 == pthread_create(&tid,NULL,(void *)thread_myls,tempVar))
 89                         printf("create thread_myls\n");
 90                     else 
 91                         printf("create thread_myls error\n");
 92 
 93                     pthread_join(tid,NULL);
 94                     printf("thread_myls exit \n");
 95                 }
 96                 else
 97                 {
 98                     pthread_t tid;
 99                     char tempVar1[256] = {0};
100                     char *para_mycp[2] = {tempVar,tempVar1};
101                     //snprintf(tempVar1,sizeof(tempVar1),"/home/test/Desktop/test/%s",currentdp->d_name);
102                     strcat(tempVar1,"/home/test/Desktop/test1/");
103                     strcat(tempVar1,currentdp->d_name);
104                     printf("create thread_mycp\n");
105                     if(0 == pthread_create(&tid,NULL,(void *)thread_mycp,(void *)para_mycp))
106                         printf("create thread_mycp\n");
107                     else 
108                         printf("create thread_mycp error\n");
109 
110                     pthread_join(tid,NULL);
111                     printf("thread_mycp exit \n");
112                 }
113             }
114         }
115 
116         if(-1 == closedir(currentdir))
117         {
118             printf("close directory fail \n");
119         }
120     }
121 
122     return 0;
123 }
124 
125 void *thread_mycp(void *arg)
126 {
127     char **argv = arg;
128     int srcFile = open(argv[0],O_RDONLY);
129     if(-1 == srcFile)
130         perror(argv[0]);
131     {
132         int desFile = open(argv[1],O_CREAT|O_WRONLY|O_EXCL,0777);
133         if(-1 == desFile)
134         {
135             //printErr(argv[2]);
136             char s[100] = {0};
137             printf("the file %s already exists, if need merge please enter YES, otherwise please enter NO to overwrite \n",argv[2]);
138             scanf("%s",&s[0]);
139 
140             if(0 == strncasecmp(s,"YES",3))
141             {
142                 desFile = open(argv[1],O_WRONLY|O_APPEND);
143             }
144             else if(0 == strncasecmp(s,"NO",2))
145             {
146                 desFile = open(argv[1],O_WRONLY|O_TRUNC);
147             }else 
148             {
149                 printf("enter is Error \n");
150             } 
151         }
152        
153         if(-1 != desFile){
154             char ReadBuff[4096] = {0};
155             char i = 1;
156             do
157             {
158                 ssize_t ReadLen = read(srcFile,ReadBuff,sizeof(ReadBuff));
159                 if(-1 == ReadLen)
160                 {
161                     perror(argv[0]);
162                     i = 0;
163                 }
164                 else if(ReadLen > 0)
165                 {
166                     ssize_t WriteLen = write(desFile,ReadBuff,ReadLen);
167                     if(-1 == WriteLen)
168                     {
169                         perror(argv[1]);
170                         i = 0;
171                     }else i = 1;
172                 }else i = 0;
173             }while(i);
174 
175             int closeFile = close(desFile);
176             if(-1 == closeFile)
177                 perror(argv[1]);
178         } 
179 
180         int closeFile = close(srcFile);
181         if(-1 == closeFile)
182             perror(argv[0]); 
183     }
184     return 0;
185 }

执行结果:

 ./threadTest ../
create mainThread
create thread_myls
create thread_myls
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
thread_myls exit 
create thread_myls
create thread_myls
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
thread_myls exit 
create thread_myls
create thread_myls
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
thread_myls exit 
create thread_myls
create thread_myls
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
create thread_mycp
create thread_mycp
thread_mycp exit 
thread_myls exit 
mainThread exit

 



这篇关于MOOC《Linux操作系统编程》学习笔记-实验五的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程