How number of thread should be accepted via command line using pthread?

In C you handle command line arguments by having main take two arguments.

In C you handle command line arguments by having main take two arguments, int main(int argc, char** argv) in which argc is the number of command line arguments (including the program itself) and argv is a pointer to a memory location where argc-1 pointers to strings with the actual arguments are located. Example: int main(int argc, char** argv) { printf("The program was executed as %s. \n", argv0); printf("The arguments were:\n"); for (int I = 1; I The integer is given as a string, so we have to convert it using atoi: if (argc!

= 2) { printf("Need exactly one argument! \n"); return 1; } int num_threads = atoi(argv1); // Convert first argument to integer. If (num_threads \n"); return 2; } Now what you do is simply create an array of thread handles, pthread_t* threads = malloc(num_threads*sizeof(pthread_t)); and use it to store the thread handles as you start num_threads number of threads using pthread_create.

If you are not familiar with pthreads at all, I recommend this short tutorial.

Threads = malloc(num_threads*sizeof(pthread_t)); tr = pthread_create( &threads, NULL, NULL, NULL); passing argument 1 of ‘pthread_create’ from incompatible pointer type (warning it shows) – Tauquir Oct 5 '10 at 11:11 thanks, Yes I am not familiar with pthread. How should I do for sleep for random intervals Once awake, write its own name in the string – Tauquir Oct 5 '10 at 11:13 Regarding the code in your comment: &threads means the address of the pointer threads. You want &threadsi (the address of the i'th element of the array threads) when creating the i'th thread.

– gspr Oct 5 '10 at 12:03 I would post your question about sleeping and waking at random intervals as a separate stackoverflow question. – gspr Oct 5 '10 at 12:04.

If you were using a threading framework like OpenMP, then this is all handled automatically simply by setting the environment variable OMP_NUM_THREADS. But if you're implementing threads "manually", you'll need to do it the way most runtime configuration is done: either by parsing argv, or by setting an environment variable and using getenv().

Usually, you would just pass it like any other argument. I've used code similar to the following in projects before to specify fixed thread counts. This is fairly simple but suitable for situations where you don't need the full power of thread pooling (though you could just as easily set a minimum and maximum thread count the same way).

#include #define THRD_DFLT 5 #define THRD_MIN 2 #define THRD_MAX 20 static int numThreads = 0; int main (int argCount, char *argVal) { if (argCount > 1) numThreads = atoi (argVal1); if ((numThreads THRD_MAX)) { printf ("Number of threads outside range %d-%d, using %d\n", THRD_MIN, THRD_MAX, THRD_DFLT); numThreads = THRD_DFLT; }.

– Oli Charlesworth Oct 5 '10 at 9:59 Actually I normally use argCount and argVal since I prefer more readable names (the same reason I rarely use I as a counter in real code), but I was in a hurry :-) – paxdiablo Oct 5 '10 at 10:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions