Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab7.1
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <semaphore.h>
- #define MAXSTRLENGTH 100
- void getFilePath(char *dirPath, char *fileName, char **buf) {
- memset(*buf, 0, MAXSTRLENGTH * sizeof(char));
- strcat(*buf, dirPath);
- if ((*buf)[strlen(*buf) - 1] != '/') {
- (*buf)[strlen(*buf)] = '/';
- }
- strcat(*buf, fileName);
- }
- void printFileInfo(char *filePath, struct stat fileStat) {
- char fileMode[10] = "----------";
- if (fileStat.st_mode & S_IRUSR) fileMode[1] = 'r';
- if (fileStat.st_mode & S_IWUSR) fileMode[2] = 'w';
- if (fileStat.st_mode & S_IXUSR) fileMode[3] = 'x';
- if (fileStat.st_mode & S_IRGRP) fileMode[4] = 'r';
- if (fileStat.st_mode & S_IWGRP) fileMode[5] = 'w';
- if (fileStat.st_mode & S_IXGRP) fileMode[6] = 'x';
- if (fileStat.st_mode & S_IROTH) fileMode[7] = 'r';
- if (fileStat.st_mode & S_IWOTH) fileMode[8] = 'w';
- if (fileStat.st_mode & S_IXOTH) fileMode[9] = 'x';
- printf("FILE FOUND || File path: %s || Size: %d bytes || Creation date: %.24s || Index descriptor: %d || File permissions: %s\n\n",
- filePath, (int) fileStat.st_size, ctime(&fileStat.st_ctime), (int) fileStat.st_ino, fileMode);
- }
- void searchFile(char *fileName, char *dirPath, sem_t sem, int pipeFiles[2], int pipeDirs[2]) {
- sem_wait(&sem);
- struct dirent *file;
- struct stat fileStat;
- DIR *dir = opendir(dirPath);
- char *filePath = malloc(MAXSTRLENGTH * sizeof(char));
- int localFileCount = 0, localDirCount = 0;
- while ((file = readdir(dir)) != NULL) {
- getFilePath(dirPath, file->d_name, &filePath);
- stat(filePath, &fileStat);
- if (S_ISREG(fileStat.st_mode)) {
- localFileCount++;
- // printf("Pid: %d || File path: %s || Size: %d bytes\n\n", getpid(), filePath, (int) fileStat.st_size);
- if (strcmp(file->d_name, fileName) == 0) {
- printFileInfo(filePath, fileStat);
- }
- }
- else if (S_ISDIR(fileStat.st_mode) && strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
- localDirCount++;
- pid_t pid = fork();
- if (pid == 0) {
- searchFile(fileName, filePath, sem, pipeFiles, pipeDirs);
- exit(0);
- } else if (pid == -1) {
- printf("Error while creating new process.\n\n");
- }
- }
- }
- int status;
- while (wait(&status) > 0) ;
- printf("Pid: %d || Amount of files opened: %d\n\n", getpid(), localFileCount);
- write(pipeFiles[1], &localFileCount, sizeof(int));
- write(pipeDirs[1], &localDirCount, sizeof(int));
- free(filePath);
- closedir(dir);
- sem_post(&sem);
- }
- void showTotalCount(int pipeFiles[2], int pipeDirs[2]) {
- int fileCount = 0, dirCount = 1, i = 0;
- close(pipeFiles[1]); close(pipeDirs[1]);
- while (read(pipeFiles[0], &i, sizeof(int))) {
- fileCount += i;
- }
- while (read(pipeDirs[0], &i, sizeof(int))) {
- dirCount += i;
- }
- close(pipeFiles[0]); close(pipeDirs[0]);
- printf("TOTAL FILE COUNT: %d\n TOTAL DIR COUNT: %d\n", fileCount, dirCount);
- }
- int main(int argc, char *argv[]) {
- if (argc != 4) return -1;
- sem_t sem;
- sem_init(&sem, 1, atoi(argv[3]));
- int pipeFiles[2], pipeDirs[2];
- pipe(pipeFiles); pipe(pipeDirs);
- searchFile(argv[1], argv[2], sem, pipeFiles, pipeDirs);
- sem_destroy(&sem);
- showTotalCount(pipeFiles, pipeDirs);
- return 0;
- }
- //Lab7.2
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <pthread.h>
- #include <string.h>
- #include <time.h>
- #define MAXSTRLENGTH 100
- void getFilePath(char *dirPath, char *fileName, char **buf) {
- memset(*buf, 0, MAXSTRLENGTH * sizeof(char));
- strcat(*buf, dirPath);
- if ((*buf)[strlen(*buf) - 1] != '/') {
- (*buf)[strlen(*buf)] = '/';
- }
- strcat(*buf, fileName);
- }
- void printFileInfo(char *filePath, struct stat fileStat) {
- char fileMode[10] = "----------";
- if (fileStat.st_mode & S_IRUSR) fileMode[1] = 'r';
- if (fileStat.st_mode & S_IWUSR) fileMode[2] = 'w';
- if (fileStat.st_mode & S_IXUSR) fileMode[3] = 'x';
- if (fileStat.st_mode & S_IRGRP) fileMode[4] = 'r';
- if (fileStat.st_mode & S_IWGRP) fileMode[5] = 'w';
- if (fileStat.st_mode & S_IXGRP) fileMode[6] = 'x';
- if (fileStat.st_mode & S_IROTH) fileMode[7] = 'r';
- if (fileStat.st_mode & S_IWOTH) fileMode[8] = 'w';
- if (fileStat.st_mode & S_IXOTH) fileMode[9] = 'x';
- printf("FILE FOUND || File path: %s || Size: %d bytes || Creation date: %.24s || Index descriptor: %d || File permissions: %s\n\n",
- filePath, (int) fileStat.st_size, ctime(&fileStat.st_ctime), (int) fileStat.st_ino, fileMode);
- }
- int fileCount = 0, dirCount = 1, threadCount = 1;
- struct arg {
- char *fileName;
- char *dirPath;
- int maxProc
- };
- void *searchFile(struct arg *pargs);
- void *searchFile(struct arg *pargs) {
- struct arg args = *pargs;
- struct dirent *file;
- struct stat fileStat;
- DIR *dir = opendir(args.dirPath);
- char *filePath = malloc(MAXSTRLENGTH * sizeof(char));
- int localFileCount = 0;
- pthread_t thread;
- while ((file = readdir(dir)) != NULL) {
- getFilePath(args.dirPath, file->d_name, &filePath);
- stat(filePath, &fileStat);
- if (S_ISREG(fileStat.st_mode)) {
- localFileCount++;
- // printf("Thread: %d || File path: %s || Size: %d bytes\n\n", pthread_self, filePath, (int) fileStat.st_size);
- if (strcmp(file->d_name, args.fileName) == 0) {
- printFileInfo(filePath, fileStat);
- }
- }
- else if (S_ISDIR(fileStat.st_mode) && strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
- dirCount++;
- while (threadCount >= args.maxProc) {
- sleep(10);
- }
- struct arg newargs;
- newargs.dirPath = malloc(MAXSTRLENGTH * sizeof(char));
- strcpy(newargs.dirPath, filePath);
- newargs.fileName = args.fileName;
- newargs.maxProc = args.maxProc;
- pthread_create(&(thread), NULL, searchFile, &newargs);
- threadCount++;
- pthread_join(thread, NULL);
- threadCount--;
- free(newargs.dirPath);
- }
- }
- printf("Thread: %lu || Amount of files opened: %d\n\n", pthread_self(), localFileCount);
- fileCount += localFileCount;
- free(filePath);
- closedir(dir);
- }
- int main(int argc, char *argv[]) {
- if (argc != 4) return -1;
- struct arg args = {argv[1], argv[2], atoi(argv[3])};
- searchFile(&args);
- printf("TOTAL FILE COUNT: %d\n TOTAL DIR COUNT: %d\n", fileCount, dirCount);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement