473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

using threads to create 75000 files

Hi,

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
  1. use threads;
  2.  
  3. &prepare_multi_data(1000,"C:\\Auto CIFS\\","dump",2048);
  4.  
  5. sub prepare_multi_data {
  6.     my $number_of_files = shift;
  7.     my $working_folder = shift;
  8.     my $file_type = shift;
  9.     my $size = shift;
  10.  
  11.     my $ctr;
  12.  
  13.     if (!&create_multi_files($number_of_files,$working_folder,$file_type, $size)){
  14.         return 0;
  15.     }else {
  16.         for ($ctr=0;$ctr<$number_of_files;$ctr++) {
  17.             push(@files,"$file_name$ctr");
  18.         }
  19.         return \@files;
  20.     }
  21. }
  22.  
  23. sub create_multi_files {
  24.     my $number_of_files = shift;
  25.     my $working_folder = shift;
  26.     my $file_type = shift;
  27.     my $size = shift;
  28.  
  29.     $file_name = "File-".time;
  30.     my ($ctr, $pid, @cpids, $loop);
  31.     for ($ctr=0;$ctr<$number_of_files;$ctr++) {
  32.  
  33.             if (!defined($pid = fork())) {
  34.                 return 0;
  35.             }elsif ($pid == 0) {
  36.                 if (!&create_file($working_folder, $file_type, $size, "$file_name$ctr")) {
  37.                             kill 9,@cpids;
  38.                             return 0;
  39.                     }
  40.                     exit 0;
  41.             }else {
  42.                     $cpids[$ctr] = $pid;
  43.             }
  44.     }
  45.  
  46.     for ($loop=0;$loop<$number_of_files;$loop++) {
  47.             waitpid($cpids[$loop],0);
  48.     }
  49.     return 1;
  50. }
  51.  
  52. sub create_file {
  53.     my $working_folder = shift;
  54.     my $file_type = shift;
  55.     my $size = shift;
  56.     my $file_name = shift;
  57.  
  58.     if (open(FH, ">$working_folder/$file_type/$file_name")){
  59.             while ($size>=1048576) {
  60.                 print FH ("0" x 1048576);
  61.                 $size = $size - 1048576;
  62.             }
  63.             print FH "0" x $size;
  64.         return 1;
  65.     }
  66.     else{
  67.         return 0;
  68.     }
  69.     close(FH);
  70.  
  71. }
  72.  
Thanx in advance.
Jan 7 '08 #1
4 1676
eWish
971 Expert 512MB
Please explain what you mean by failing? Do you get any error messages?

--Kevin
Jan 7 '08 #2
numberwhun
3,509 Expert Mod 2GB
You haven't run out of disk space, have you? Also, do you have the "use strict" and "use warnings" pragmas in your code?

Regards,

Jeff
Jan 7 '08 #3
KevinADC
4,059 Expert 2GB
You need to do some error checking here:

if (open(FH, ">$working_folder/$file_type/$file_name")){

and see why the files are failing. There does appear to be an extra slash in the path statement, after $working_folder, because you already had the trailing slash when you defined the variable: C:\\Auto CIFS\\

Also in the "create_file" sub, you are not closing the filehandle properly. You have two return() functions then the close() function. The return() functions never allow the sub to get to the close() function, so move the close(FH) up to just after the file is finished printing. Try something like this for that sub:

Expand|Select|Wrap|Line Numbers
  1. sub create_file {
  2.     my $working_folder = shift;
  3.     my $file_type = shift;
  4.     my $size = shift;
  5.     my $file_name = shift;
  6.  
  7.     open(FH, ">$working_folder$file_type/$file_name") or die "$working_folder$file_type/$file_name: $!";
  8.    while ($size>=100) {
  9.         print FH ("0" x 100);
  10.         $size = $size - 100;
  11.     }
  12.     print FH "0" x $size;
  13.     close(FH);
  14.     return 1;
  15. #    }
  16. #    else{
  17. #        return 0;
  18. #    }
  19.  
  20.  
  21. }
and see if you get any errors associated with opening a new file. Scale down the tests from 75000 until you start getting errors. Start with 1000 files and work your way up. You may also want to sleep() before the script opens a new file because you are using "time" in the file names:

$file_name = "File-".time;

so try adding: sleep(1); so at least one second passes between new file creations.
Jan 7 '08 #4
Thank you , Thanks for the help .

Its working,
Jan 8 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sstark | last post by:
Hi, I have a web/cgi script that allows users to upload a file to the web server. I want to only allow files up to a certain size, which is stored in $imageFileMaxSize (typically 75K). That part...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
1
by: Mamatha | last post by:
Hi friends, I have an application using mutithreads.In that application one threads writes the data in to a file. Another thread reads the data from same file. So some errors while at the time...
5
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2...
6
by: Alexander Walker | last post by:
Hello Is it inefficient to create an application that has many threads that individually may do a small amount of work over a given period of time as opposed to an application that has a...
3
by: ano | last post by:
I create a method that copy files from one folder to another (see the ex. code). A new thread will create every time when user click the button. The button is enable only when the copy process was...
4
by: gnassar | last post by:
Hello, I've written to this group a couple times and would like to initially start by thanking those who reply to the posts. I seem to be having some issues that are out of my understanding at...
4
by: Marcus Alves Grando | last post by:
Hello list, I have a strange problem with os.walk and threads in python script. I have one script that create some threads and consume Queue. For every value in Queue this script run os.walk()...
3
by: Pinux | last post by:
Hi, I am writing a multi-threads encryption application. The idea of the code is to create a number of threads to encrypt files. I have a thread pool say the maximum threads is 10. If the number...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.