473,796 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using threads to create 75000 files

2 New Member
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 1700
eWish
971 Recognized Expert Contributor
Please explain what you mean by failing? Do you get any error messages?

--Kevin
Jan 7 '08 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
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 Recognized Expert Specialist
You need to do some error checking here:

if (open(FH, ">$working_fold er/$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_fil e" 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
npankajk
2 New Member
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
2666
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 works correctly, i.e. the script does not allow uploads larger than $imageFileMaxSize. The problem is, when a user attempts to upload a file larger than $imageFileMaxSize, the on_error routine is not called; instead the script reloads from its...
6
3206
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 list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
1
1371
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 of reading. Now i want to create a files and threads at runtime to avoid these errors. How can i create mutiple files and threads for reading data from that files at runtime.Is there any way?
5
2586
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 or more threads are created, the ImportOneFile method attempts to add a previously added file. If I allow 4 maximum threads and process 4 files, the last file is attempted 4 times and none of the other files are added to the destination. If I...
6
5401
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 smaller number of threads that do a larger amount of work over a given time period here is an oversimplified example application 1 has 4 threads
3
1533
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 finished( ThreadState = Stop ) My question is when ThreadState = Stop, the system will release the Memory or not? Is my code generated multiply threads?
4
1307
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 the moment and hopefully someone knowledgeable will be able to guide me the right way. I have created a program that utilizes three threads minimum. The initial gui thread, and two worker threads. I haven't set the worker threads as background...
4
3404
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() and printing root dir. But if i increase number of threads the result are inconsistent compared with one thread. For example, run this code plus sort with one thread and after run again with ten threads and see diff(1).
3
8062
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 of tasks (number of files waiting to be encrypted) is larger than the thread pool size, I first create 10 new threads to execute the encryption function. Then I wait for any of the threads to be signaled in the thread pool using WaitForMultipleObjects...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.