1847334 Members
2293 Online
110264 Solutions
New Discussion

Test user process

 
SOLVED
Go to solution
peterchu
Super Advisor

Test user process

sorry to ask a script again,

I am not too understand shell script writing , could someone can help me ,

I want to have a shell script for testing , the process will run repeatly (looping), I want to test the CPU time , I have the below script to test :

while x=0
do
cp /tmp/abc.txt /tmp/def.txt
done

the process is non-stop running , but it is not fit my requirement , because the above script will generate a new process after the old process was completed , what I want is a SAME process is running non-stop (same PID ) , could someone post a script for me ? thx in advance.


could someone can post the script ? thx in advance.
5 REPLIES 5
Bill Hassell
Honored Contributor
Solution

Re: Test user process

I'm not sure what you are testing. The cp command will run slowly the first time through and then extremely fast for all other loops. If the file /tmp/abc.txt is small (just a few thousand lines), the disk will never be accessed after the first time through the loop. That's because the HP-UX buffer cache will hold the entire file and the cp task will be essentially memory-to-memory with occasional syncs run by the kernel.

Now it's important to understand the of shell scripts. When you run a script, your current shell will start a subshell to interpret the commands in the script. Your current shell will be idle, waiting for the subshell to finish. If you want your current shell to execute all the commands in the script, use the . (dot) command. What this does is to tell your current shell to interpret the commands, not start a subshell. Here is how it is done:

. /path_to_script

Rather than type the name, you put a dot-space in front of the script name. That prevents the forking of a subshell to run your script. The terminology in Unix is called sourcing.


Bill Hassell, sysadmin
Hein van den Heuvel
Honored Contributor

Re: Test user process


>> I want to have a shell script for testing, process will run repeatly (looping), I want to test the CPU time , I

As Bill already suggests, testing a user process, often to mimick a real process, is non trivial. It seems easy, but it really is not due to lack of randomness / caching / concurrency / think times and so on.

Perhaps you can articulate more clearly what you are trying to measure / mimick?

For you immediate question... any shell script will always use a fork to do a job, creating a (short lived) new process.
If you want to copy a file over and over (allthough that may seem silly to us) then you'll need a program of some sort to do so.
For example the simple perl script below:

hth,
Hein.

$in_file = shift @ARGV || die "Need input file as first argument";
$ou_file = shift @ARGV || die "Need output file as second argument";
while (1) {
open (IN,"<$in_file") || die "failed to open file $in_file for read";
open (OU,">$ou_file") || die "failed to open file $ou_file for write";
print OU $_ while ();
print "$i copies\n" unless ++$i%1000;
}

peterchu
Super Advisor

Re: Test user process

thx reply , I think I am not clearly state my requirement , my requirement is much simplier than what you expect .

I just want to have a script to run a process ( may be a looping ) , this process need to use the same PID . In my previous script , after the cp process finished , it will generate another process ( so the PID changed ) so not fit my requirement.
thx
john korterman
Honored Contributor

Re: Test user process

Hi,
do not try this unless you are sure that interrupt (normally ctrl C) works:

#!/usr/bin/sh

while true
do
a=a
done

as the above makes no I/O it will use all your cpu - but is that what you want?

regards,
John K.
it would be nice if you always got a second chance
Peter Godron
Honored Contributor

Re: Test user process

Hi,
You can not have two processes running at the same time witht he same process id.
When one job completes a new process id is allocated to the next job.
Even a loop generating child processes will do this.

Regards