MOOC《Linux操作系统编程》学习笔记-实验四
Posted 努力把握好每一天,只愿成为更好的自己
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MOOC《Linux操作系统编程》学习笔记-实验四相关的知识,希望对你有一定的参考价值。
实验四 进程控制实验
https://www.icourse163.org/learn/UESTC-1003040002?tid=1455108444#/learn/content?type=detail&id=1228729537&sm=1
程序流程图:
试验代码:
1 #include "stdio.h" 2 #include "stdint.h" 3 #include "sys/types.h" 4 #include "dirent.h" 5 #include "unistd.h" 6 #include "string.h" 7 #include "errno.h" 8 #include "fcntl.h" 9 #include <stdlib.h> 10 #include "sys/wait.h" 11 #include "sys/stat.h" 12 13 int main(int argc, char* argv[]) 14 { 15 char buff[256] = {0}; 16 17 if(1 == argc) 18 { 19 /* 获取当前工作目录 */ 20 if(NULL == getcwd(buff,sizeof(buff))) 21 { 22 perror("get work path error"); 23 } 24 printf("currWorkPath = %s \\n",buff); 25 } 26 else if(2 == argc) 27 { 28 /* 通过参数获取需要遍历的目录 */ 29 memcpy(buff,argv[1],sizeof(buff)); 30 printf("buff = %s \\n",buff); 31 } 32 else 33 { 34 printf("cmd para is error \\n"); 35 return 0; 36 } 37 38 /* 打开目录 */ 39 DIR *currentdir; 40 if((currentdir = opendir(buff)) == NULL) 41 { 42 printf("open directory fail \\n"); 43 return 0; 44 } 45 else 46 { 47 struct dirent * currentdp; 48 while((currentdp = readdir(currentdir))!= NULL) 49 { 50 if(currentdp->d_name[0] != \'.\') 51 { 52 struct stat currentStat; 53 uint8_t tempVar[256] = {0}; 54 strcat(tempVar,buff); 55 strcat(tempVar,"/"); 56 if(lstat(strcat(tempVar,currentdp->d_name),¤tStat) < 0) 57 { 58 perror("lstat error"); 59 printf("error path is %s \\n",currentdp->d_name); 60 continue; 61 } 62 pid_t pid_child,pid_return; 63 if(S_ISDIR(currentStat.st_mode)) 64 { 65 pid_child = fork(); 66 if(pid_child < 0) printf("Error occured on forking.\\n"); 67 else if(pid_child == 0) 68 { 69 char buffer[256] = {0}; 70 strcat(buffer,"./"); 71 execl("./progressTest","progressTest",strcat(buffer,currentdp->d_name),NULL); 72 exit(0); 73 } 74 } 75 else 76 { 77 pid_child = fork(); 78 if(pid_child < 0) printf("Error occured on forking.\\n"); 79 else if(pid_child == 0) 80 { 81 char buffer[256] = {0},buffer2[256] = {0}; 82 strcat(buffer,"/home/test/Desktop/test/"); 83 //strcat(buffer,&(buff[2])); 84 //strcat(buffer,"/"); 85 strcat(buffer2,buff); 86 strcat(buffer2,"/"); 87 execl("./test2_1/mycp","mycp",strcat(buffer2,currentdp->d_name),strcat(buffer,currentdp->d_name),NULL); 88 exit(0); 89 } 90 } 91 92 do{ 93 pid_return = waitpid(pid_child,NULL,WNOHANG); 94 }while(pid_return == 0); 95 if(pid_return == pid_child) 96 printf("successfullt get child %d\\n",pid_return); 97 else 98 printf("some error occured\\n"); 99 } 100 } 101 } 102 103 if(-1 == closedir(currentdir)) 104 { 105 printf("close directory fail \\n"); 106 } 107 return 0; 108 }
执行结果:
./progressTest successfullt get child 5436 successfullt get child 5438 successfullt get child 5439 successfullt get child 5440 successfullt get child 5441 successfullt get child 5443 successfullt get child 5435 successfullt get child 5445 successfullt get child 5446 successfullt get child 5448 successfullt get child 5449 successfullt get child 5450 successfullt get child 5451 successfullt get child 5447 successfullt get child 5453 successfullt get child 5454 successfullt get child 5455 successfullt get child 5456 successfullt get child 5457 successfullt get child 5458 successfullt get child 5459 successfullt get child 5460 successfullt get child 5461 successfullt get child 5471 successfullt get child 5452
以上是关于MOOC《Linux操作系统编程》学习笔记-实验四的主要内容,如果未能解决你的问题,请参考以下文章