My requirement is to create 75000 files.
I have a perl script to create files , it is working fine for small number of files but the script is exiting while creating large number of files. For this I used threads. The following is the script I used. Please help me where the script is failing. I have been fighting with this since a week.
Expand|Select|Wrap|Line Numbers
- use threads;
- &prepare_multi_data(1000,"C:\\Auto CIFS\\","dump",2048);
- sub prepare_multi_data {
- my $number_of_files = shift;
- my $working_folder = shift;
- my $file_type = shift;
- my $size = shift;
- my $ctr;
- if (!&create_multi_files($number_of_files,$working_folder,$file_type, $size)){
- return 0;
- }else {
- for ($ctr=0;$ctr<$number_of_files;$ctr++) {
- push(@files,"$file_name$ctr");
- }
- return \@files;
- }
- }
- sub create_multi_files {
- my $number_of_files = shift;
- my $working_folder = shift;
- my $file_type = shift;
- my $size = shift;
- $file_name = "File-".time;
- my ($ctr, $pid, @cpids, $loop);
- for ($ctr=0;$ctr<$number_of_files;$ctr++) {
- if (!defined($pid = fork())) {
- return 0;
- }elsif ($pid == 0) {
- if (!&create_file($working_folder, $file_type, $size, "$file_name$ctr")) {
- kill 9,@cpids;
- return 0;
- }
- exit 0;
- }else {
- $cpids[$ctr] = $pid;
- }
- }
- for ($loop=0;$loop<$number_of_files;$loop++) {
- waitpid($cpids[$loop],0);
- }
- return 1;
- }
- sub create_file {
- my $working_folder = shift;
- my $file_type = shift;
- my $size = shift;
- my $file_name = shift;
- if (open(FH, ">$working_folder/$file_type/$file_name")){
- while ($size>=1048576) {
- print FH ("0" x 1048576);
- $size = $size - 1048576;
- }
- print FH "0" x $size;
- return 1;
- }
- else{
- return 0;
- }
- close(FH);
- }