c - Discover number of CPU cores using threads -


I have an assignment in which I have to write a program on Linux in C (I use Sentos), which is the thread / Processes to determine the number of cores from the CPU First of all I tried to print the current time in Microsoft / Microsounds, because I know that 1 thread / core (or 2 with HT) can run. But more than 10 threads from the millisecond printed at the same time and none of the microseconds was equal. Second, I tried to measure the execution time of the thread with the clock, I have given 4 cores, at the same time the execution time of 4 thread should be taken as the execution of approximately 1. But any of my programs bring me close to the number of CPUs, can you help me with some suggestions?

Programming current time:

  pthread_t and [n]; Zero * APIS () {// Now time; // time (& amp; now); // printf ("% s", ctime (and now)); Structure Timel Start, End; Long meats, seconds, usercandes; Gettimeofday (and start, tap); // blue (2000); Gettimeofday (and end, tap); Seconds = end.TV_SEC - start.tv_sec; Useconds = end.tv_usec - start.tv_usec; Meat = second + usekkand; Printf ("Thread with TID:% d time has passed:% ld microsons \ n", (unsigned integer) pthread_self (), mtai); } Int main () {int i; {Pthread_create (& th [i], NULL, afis, NULL) for (i = 0; i & lt; N; i ++); } (I = 0; i   

Measuring the programming process:

  pthread_t and [n]; Zero * Funk (zero * arg) {int x; Int k; Int n = (int) arg; For (k = 0; k & lt; 10000000; k + = n) {x = 0; }} Int main () {int i, j; For (i = 0; i    

Pre class = "lang-none prettyprint-override"> Get real time (start time) Start each busy 40 threads for one second; Wait for them to get real time (time off)

If you analyze the time taken to perform 40 threads, you can see the number of cores , Or at least you can make an impression:

  If the time was around 40: if you have a core then time around 20 Was: You have two more on   

You are definitely the number of threads you started Just like you can customize, as well as allow them to sleep, but I think if you sleep for milliseconds only representative due to get time reference switches and background tasks.


Do something like this instead of sleeping in a thread:

  excessive qualityctas () {// calculation of some time Which is X (best will be above 1 second)}   

If you execute a thread once without starting, then what time will x . The reference time will be at that time.

If by executing the same task, by adding more and more pthread s ( y ), you can use the x Use more time, so you know that you have at least y core at some point, with the z thread, time is approximately 2x , at that point you will know that you have the z-1 core.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -