Anu
Anu

Reputation: 19

Sleep command usage in linux

As a part of my academic project I have to execute a C program.

I want to get the execution time of the program. For that I have to sleep all other processes in Linux for some seconds. Is there any method for doing that?

(I have tried using the time command in Linux but it is not working properly: it shows different execution time when I am executing the same program. So I am computing execution time by seeing the difference between start time and end time).

Upvotes: 1

Views: 489

Answers (6)

eyalm
eyalm

Reputation: 3366

Try executing your application with nice -n 20. It may help to make the other processes quieter.

nice man page

Upvotes: 0

Choon Keat
Choon Keat

Reputation: 24

There will always be a difference in execution time for things no matter how many processes you shut down, polling, IO, background daemons all affect execution priority.

The academic approach would be to run a sizeable sample and take statistics, you might also want to take a look at sar to log the background. To invalidate any readings you might take

Upvotes: 0

Use the time command. The sum user + sys will give you the time your programm used the CPU directly plus the time the system used the CPU on behalf of your program. I think it is what you want to know.

Upvotes: 0

alinsoar
alinsoar

Reputation: 15793

It is impossible to get the exact time of execution of a process into a system in which the scheduler commutes from 1 process to the other.

The Intel processors inserted a register that counts the number of clocks, but even so it is impossible to measure the time.

There is a book that you can find as PDF on google, "Computer Systems: A Programmer's Perspective" -- In this book an whole chapter is dedicated to time measurements.

Upvotes: 1

Andrew Aylett
Andrew Aylett

Reputation: 40710

The usual way to do this is to try to quiesce the machine as much as possible, then take several measurements and average them. It's advisable to discard the first reading, as that's likely to involve population of caches.

Upvotes: 1

jthill
jthill

Reputation: 60295

About the best way I can think of is to drop to single-user mode, which you get with

# init 1

on pretty much any distribution. This will also stop X, you'll be on a raw console. Handling interrupts from stray mouse movement is likely to be one of the reasons for whatever variability you're seeing, so that's a good thing.

When you want your full system back, init 3 is probably the one, that or init 5.

Upvotes: 1

Related Questions