Created
December 26, 2010 17:54
-
-
Save alesya-h/755539 to your computer and use it in GitHub Desktop.
Лаба по Операционным сетям. Извини что с такой задержкой.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <dirent.h> | |
#include <semaphore.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
sem_t * out_sem; | |
struct maxfile{ | |
char * name; | |
size_t size; | |
}; | |
int getsize(char *name) | |
{ | |
struct stat buf; | |
stat(name,&buf); | |
return buf.st_size; | |
} | |
void processdir(char *name) | |
{ | |
pid_t pid; | |
DIR *dir; | |
struct dirent *cur_entry; | |
char *path; | |
char *filename; | |
int total; | |
pid = getpid(); | |
sem_wait(out_sem); | |
printf("started %d for %s\n",pid,name); | |
sem_post(out_sem); | |
total = 0; | |
dir=opendir(name); | |
chdir(name); | |
path = getwd(NULL); | |
while(cur_entry = readdir(dir)){ | |
total++; | |
sem_wait(out_sem); | |
printf("pid: %7d\t | %35s\t | %35s\t | %10d bytes | total: %d\n", | |
pid, path, cur_entry->d_name, getsize(cur_entry->d_name), total); | |
sem_post(out_sem); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
struct dirent * cur_entry; | |
struct maxfile max; | |
char *comp_name; // compound name | |
char *ent_name; // entry name | |
DIR *dir_desc; | |
DIR *dir; | |
int outfile; | |
long int sumsize; | |
int filenum; | |
int procnum; | |
int N; | |
pid_t p; | |
int index; | |
// check | |
if(argc != 3){ | |
printf("usage: ./max <dir> <outfile>"); | |
exit(1); | |
} | |
printf("dir: %s\nfile: %s\n",argv[1],argv[2]); | |
// initialization | |
out_sem = sem_open("max_sem", O_CREAT); | |
sem_post(out_sem); | |
procnum = 0; | |
sumsize = 0; | |
comp_name = (char*)calloc(1024,sizeof(char)); | |
strcpy(comp_name,argv[1]); | |
comp_name[strlen(comp_name)+1] = 0; | |
comp_name[strlen(comp_name)] = '/'; | |
ent_name = comp_name + strlen(comp_name); | |
dir = opendir(argv[1]); | |
outfile = open(argv[2],O_WRONLY); | |
if(!dir){ | |
printf("\"%s\" is not a directory\n",argv[1]); | |
exit(2); | |
} | |
if(!outfile){ | |
printf("Can't open file \"%s\" for writing. Exiting.\n",argv[1]); | |
exit(3); | |
} | |
chdir(argv[1]); | |
printf("N = "); | |
scanf("%d",&N); | |
N--; // 4 process maximum means 3 child + 1 parent. Now N will be max child. | |
// main program loop | |
while(cur_entry = readdir(dir)){ | |
if(strcmp(cur_entry->d_name,"..") && strcmp(cur_entry->d_name,".")){ | |
strcpy(ent_name,cur_entry->d_name); | |
if(dir_desc = opendir(comp_name)){ // this is a directory | |
sem_wait(out_sem); | |
printf("DIR: %s\n", comp_name); | |
sem_post(out_sem); | |
closedir(dir_desc); | |
if(procnum<N){ | |
procnum++; | |
}else{ | |
wait3(0,0,0); // wait any child process to exit | |
} | |
p = fork(); | |
if(!p){ | |
processdir(comp_name); | |
exit(0); | |
} | |
}else{ // this is a file | |
sem_wait(out_sem); | |
printf("FILE: %s\n", comp_name); | |
sem_post(out_sem); | |
filenum++; | |
sumsize += getsize(comp_name); | |
if(getsize(comp_name) > max.size){ | |
max.size = getsize(comp_name); | |
max.name = comp_name; | |
} | |
} | |
} | |
} | |
while(procnum--){ | |
wait3(0,0,0); | |
} | |
printf("Catalog: %s\nTotal files: %d\nSum file size: %ld bytes\nMax file name: %s", | |
argv[1],filenum,sumsize,max.name); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment