Advertisement
nblknn

OS 7

Dec 17th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.86 KB | None | 0 0
  1. //Lab7.1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include <semaphore.h>
  12.  
  13. #define MAXSTRLENGTH 100
  14.  
  15. void getFilePath(char *dirPath, char *fileName, char **buf) {
  16.     memset(*buf, 0, MAXSTRLENGTH * sizeof(char));
  17.     strcat(*buf, dirPath);
  18.     if ((*buf)[strlen(*buf) - 1] != '/') {
  19.         (*buf)[strlen(*buf)] = '/';
  20.     }
  21.     strcat(*buf, fileName);
  22. }
  23.  
  24. void printFileInfo(char *filePath, struct stat fileStat) {
  25.     char fileMode[10] = "----------";
  26.     if (fileStat.st_mode & S_IRUSR) fileMode[1] = 'r';
  27.     if (fileStat.st_mode & S_IWUSR) fileMode[2] = 'w';
  28.     if (fileStat.st_mode & S_IXUSR) fileMode[3] = 'x';
  29.     if (fileStat.st_mode & S_IRGRP) fileMode[4] = 'r';
  30.     if (fileStat.st_mode & S_IWGRP) fileMode[5] = 'w';
  31.     if (fileStat.st_mode & S_IXGRP) fileMode[6] = 'x';
  32.     if (fileStat.st_mode & S_IROTH) fileMode[7] = 'r';
  33.     if (fileStat.st_mode & S_IWOTH) fileMode[8] = 'w';
  34.     if (fileStat.st_mode & S_IXOTH) fileMode[9] = 'x';
  35.     printf("FILE FOUND || File path: %s || Size: %d bytes || Creation date: %.24s || Index descriptor: %d || File permissions: %s\n\n",
  36.     filePath, (int) fileStat.st_size, ctime(&fileStat.st_ctime), (int) fileStat.st_ino, fileMode);
  37. }
  38.  
  39. void searchFile(char *fileName, char *dirPath, sem_t sem, int pipeFiles[2], int pipeDirs[2]) {
  40.     sem_wait(&sem);
  41.     struct dirent *file;
  42.     struct stat fileStat;
  43.     DIR *dir = opendir(dirPath);
  44.     char *filePath = malloc(MAXSTRLENGTH * sizeof(char));
  45.     int localFileCount = 0, localDirCount = 0;
  46.  
  47.     while ((file = readdir(dir)) != NULL) {
  48.         getFilePath(dirPath, file->d_name, &filePath);
  49.         stat(filePath, &fileStat);
  50.         if (S_ISREG(fileStat.st_mode)) {  
  51.             localFileCount++;    
  52.          //   printf("Pid: %d || File path: %s || Size: %d bytes\n\n", getpid(), filePath, (int) fileStat.st_size);
  53.             if (strcmp(file->d_name, fileName) == 0) {
  54.                 printFileInfo(filePath, fileStat);
  55.             }
  56.         }
  57.         else if (S_ISDIR(fileStat.st_mode) && strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  58.             localDirCount++;
  59.             pid_t pid = fork();
  60.             if (pid == 0) {
  61.                 searchFile(fileName, filePath, sem, pipeFiles, pipeDirs);
  62.                 exit(0);
  63.             } else if (pid == -1) {
  64.                 printf("Error while creating new process.\n\n");
  65.             }
  66.         }
  67.     }
  68.  
  69.     int status;
  70.     while (wait(&status) > 0) ;
  71.     printf("Pid: %d || Amount of files opened: %d\n\n", getpid(), localFileCount);
  72.     write(pipeFiles[1], &localFileCount, sizeof(int));
  73.     write(pipeDirs[1], &localDirCount, sizeof(int));
  74.     free(filePath);
  75.     closedir(dir);
  76.     sem_post(&sem);
  77. }
  78.  
  79. void showTotalCount(int pipeFiles[2], int pipeDirs[2]) {
  80.     int fileCount = 0, dirCount = 1, i = 0;
  81.     close(pipeFiles[1]); close(pipeDirs[1]);
  82.     while (read(pipeFiles[0], &i, sizeof(int))) {
  83.         fileCount += i;
  84.     }
  85.     while (read(pipeDirs[0], &i, sizeof(int))) {
  86.         dirCount += i;
  87.     }
  88.     close(pipeFiles[0]); close(pipeDirs[0]);
  89.     printf("TOTAL FILE COUNT: %d\n TOTAL DIR COUNT: %d\n", fileCount, dirCount);
  90. }
  91.  
  92. int main(int argc, char *argv[]) {
  93.     if (argc != 4) return -1;
  94.     sem_t sem;
  95.     sem_init(&sem, 1, atoi(argv[3]));
  96.     int pipeFiles[2], pipeDirs[2];
  97.     pipe(pipeFiles); pipe(pipeDirs);
  98.     searchFile(argv[1], argv[2], sem, pipeFiles, pipeDirs);
  99.     sem_destroy(&sem);
  100.     showTotalCount(pipeFiles, pipeDirs);
  101.     return 0;
  102. }
  103.  
  104.  
  105. //Lab7.2
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. #include <dirent.h>
  109. #include <sys/stat.h>
  110. #include <pthread.h>
  111. #include <string.h>
  112. #include <time.h>
  113.  
  114. #define MAXSTRLENGTH 100
  115.  
  116. void getFilePath(char *dirPath, char *fileName, char **buf) {
  117.     memset(*buf, 0, MAXSTRLENGTH * sizeof(char));
  118.     strcat(*buf, dirPath);
  119.     if ((*buf)[strlen(*buf) - 1] != '/') {
  120.         (*buf)[strlen(*buf)] = '/';
  121.     }
  122.     strcat(*buf, fileName);
  123. }
  124.  
  125. void printFileInfo(char *filePath, struct stat fileStat) {
  126.     char fileMode[10] = "----------";
  127.     if (fileStat.st_mode & S_IRUSR) fileMode[1] = 'r';
  128.     if (fileStat.st_mode & S_IWUSR) fileMode[2] = 'w';
  129.     if (fileStat.st_mode & S_IXUSR) fileMode[3] = 'x';
  130.     if (fileStat.st_mode & S_IRGRP) fileMode[4] = 'r';
  131.     if (fileStat.st_mode & S_IWGRP) fileMode[5] = 'w';
  132.     if (fileStat.st_mode & S_IXGRP) fileMode[6] = 'x';
  133.     if (fileStat.st_mode & S_IROTH) fileMode[7] = 'r';
  134.     if (fileStat.st_mode & S_IWOTH) fileMode[8] = 'w';
  135.     if (fileStat.st_mode & S_IXOTH) fileMode[9] = 'x';
  136.     printf("FILE FOUND || File path: %s || Size: %d bytes || Creation date: %.24s || Index descriptor: %d || File permissions: %s\n\n",
  137.     filePath, (int) fileStat.st_size, ctime(&fileStat.st_ctime), (int) fileStat.st_ino, fileMode);
  138. }
  139.  
  140. int fileCount = 0, dirCount = 1, threadCount = 1;
  141.  
  142. struct arg {
  143.     char *fileName;
  144.     char *dirPath;
  145.     int maxProc
  146. };
  147.  
  148. void *searchFile(struct arg *pargs);
  149.  
  150. void *searchFile(struct arg *pargs) {
  151.     struct arg args = *pargs;
  152.     struct dirent *file;
  153.     struct stat fileStat;
  154.     DIR *dir = opendir(args.dirPath);
  155.     char *filePath = malloc(MAXSTRLENGTH * sizeof(char));
  156.     int localFileCount = 0;
  157.     pthread_t thread;
  158.  
  159.     while ((file = readdir(dir)) != NULL) {
  160.         getFilePath(args.dirPath, file->d_name, &filePath);
  161.         stat(filePath, &fileStat);
  162.         if (S_ISREG(fileStat.st_mode)) {  
  163.             localFileCount++;
  164.          //   printf("Thread: %d || File path: %s || Size: %d bytes\n\n", pthread_self, filePath, (int) fileStat.st_size);  
  165.             if (strcmp(file->d_name, args.fileName) == 0) {
  166.                 printFileInfo(filePath, fileStat);
  167.             }
  168.         }
  169.         else if (S_ISDIR(fileStat.st_mode) && strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  170.             dirCount++;
  171.             while (threadCount >= args.maxProc) {
  172.                 sleep(10);
  173.             }
  174.             struct arg newargs;
  175.             newargs.dirPath = malloc(MAXSTRLENGTH * sizeof(char));
  176.             strcpy(newargs.dirPath, filePath);
  177.             newargs.fileName = args.fileName;
  178.             newargs.maxProc = args.maxProc;
  179.             pthread_create(&(thread), NULL, searchFile, &newargs);
  180.             threadCount++;
  181.             pthread_join(thread, NULL);
  182.             threadCount--;
  183.             free(newargs.dirPath);
  184.         }
  185.     }
  186.     printf("Thread: %lu || Amount of files opened: %d\n\n", pthread_self(), localFileCount);
  187.     fileCount += localFileCount;
  188.     free(filePath);
  189.     closedir(dir);
  190. }
  191.  
  192. int main(int argc, char *argv[]) {
  193.     if (argc != 4) return -1;
  194.     struct arg args = {argv[1], argv[2], atoi(argv[3])};
  195.     searchFile(&args);
  196.     printf("TOTAL FILE COUNT: %d\n TOTAL DIR COUNT: %d\n", fileCount, dirCount);
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement