Guest User

Untitled

a guest
Jul 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <semaphore.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8.  
  9. sem_t * out_sem;
  10.  
  11. struct maxfile{
  12. char * name;
  13. size_t size;
  14. };
  15.  
  16. int getsize(char *name)
  17. {
  18. struct stat buf;
  19. stat(name,&buf);
  20. return buf.st_size;
  21. }
  22.  
  23. void processdir(char *name)
  24. {
  25. pid_t pid;
  26. DIR *dir;
  27. struct dirent *cur_entry;
  28. char *path;
  29. char *filename;
  30. int total;
  31. pid = getpid();
  32. sem_wait(out_sem);
  33. printf("started %d for %s\n",pid,name);
  34. sem_post(out_sem);
  35. total = 0;
  36. dir=opendir(name);
  37. chdir(name);
  38. path = getwd(NULL);
  39. while(cur_entry = readdir(dir)){
  40. total++;
  41. sem_wait(out_sem);
  42. printf("pid: %7d\t | %35s\t | %35s\t | %10d bytes | total: %d\n",
  43. pid, path, cur_entry->d_name, getsize(cur_entry->d_name), total);
  44. sem_post(out_sem);
  45. }
  46. }
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50. struct dirent * cur_entry;
  51. struct maxfile max;
  52. char *comp_name; // compound name
  53. char *ent_name; // entry name
  54. DIR *dir_desc;
  55. DIR *dir;
  56. int outfile;
  57. long int sumsize;
  58. int filenum;
  59. int procnum;
  60. int N;
  61. pid_t p;
  62. int index;
  63.  
  64. // check
  65. if(argc != 3){
  66. printf("usage: ./max <dir> <outfile>");
  67. exit(1);
  68. }
  69. printf("dir: %s\nfile: %s\n",argv[1],argv[2]);
  70.  
  71. // initialization
  72.  
  73. out_sem = sem_open("max_sem", O_CREAT);
  74. sem_post(out_sem);
  75. procnum = 0;
  76. sumsize = 0;
  77. comp_name = (char*)calloc(1024,sizeof(char));
  78. strcpy(comp_name,argv[1]);
  79. comp_name[strlen(comp_name)+1] = 0;
  80. comp_name[strlen(comp_name)] = '/';
  81. ent_name = comp_name + strlen(comp_name);
  82. dir = opendir(argv[1]);
  83. outfile = open(argv[2],O_WRONLY);
  84. if(!dir){
  85. printf("\"%s\" is not a directory\n",argv[1]);
  86. exit(2);
  87. }
  88. if(!outfile){
  89. printf("Can't open file \"%s\" for writing. Exiting.\n",argv[1]);
  90. exit(3);
  91. }
  92. chdir(argv[1]);
  93. printf("N = ");
  94. scanf("%d",&N);
  95. N--; // 4 process maximum means 3 child + 1 parent. Now N will be max child.
  96.  
  97. // main program loop
  98. while(cur_entry = readdir(dir)){
  99. if(strcmp(cur_entry->d_name,"..") && strcmp(cur_entry->d_name,".")){
  100. strcpy(ent_name,cur_entry->d_name);
  101. if(dir_desc = opendir(comp_name)){ // this is a directory
  102. sem_wait(out_sem);
  103. printf("DIR: %s\n", comp_name);
  104. sem_post(out_sem);
  105. closedir(dir_desc);
  106. if(procnum<N){
  107. procnum++;
  108. }else{
  109. wait3(0,0,0); // wait any child process to exit
  110. }
  111. p = fork();
  112. if(!p){
  113. processdir(comp_name);
  114. exit(0);
  115. }
  116. }else{ // this is a file
  117. sem_wait(out_sem);
  118. printf("FILE: %s\n", comp_name);
  119. sem_post(out_sem);
  120. filenum++;
  121. sumsize += getsize(comp_name);
  122. if(getsize(comp_name) > max.size){
  123. max.size = getsize(comp_name);
  124. max.name = comp_name;
  125. }
  126. }
  127. }
  128. }
  129. while(procnum--){
  130. wait3(0,0,0);
  131. }
  132. printf("Catalog: %s\nTotal files: %d\nSum file size: %ld bytes\nMax file name: %s",
  133. argv[1],filenum,sumsize,max.name);
  134.  
  135. return 0;
  136. }
Add Comment
Please, Sign In to add comment