i have to write programm, which can be started with options for a Lecture, but it seems that the option '-r' doesn't work.
I can compile it without any errors or waings but if I'm going to start it, it tells me that '-r' is a Non-Option which will be caused fromthe for-loop after the switch-case block.
#include <pthread.h> /* used for threading */
#include <stdio.h>
#include <unistd.h>
#include <ctype.h> /* used for isprint() function */
#include <stdlib.h> /* used for lrand48, labs, strtol etc. */
#include <time.h> /* used for clock and etc */
#include <assert.h> /* used for assert function */
int rflag = 0; /* Flag for min size (rand) */
int Rflag = 0; /* Flag for max size (rand) */
int rvalue = 0; /* Defualt min size */
int Rvalue = 100; /* Default max size */
int vflag = 0; /* Flag for verbose output */
exte const char *gitversion;
void *PrintHello(void *threadarg) {
/******************************************************
* - print in one line the Thread Number given in threadarg
* and "Hello World"
* - sleep a random time (see line below!)
******************************************************/
pthread_t tid = pthread_self(); /* The Thread Id of the current thread */
int num = *((int*) threadarg); /* Number of the Thread */
long sleeping_time = (lrand48() % (Rvalue - rvalue + 1) + rvalue);
if (sleeping_time < 0) {
sleeping_time = labs(sleeping_time);
}
printf("Hello everybody, I'm thread number %dn", num);
if (vflag == 1) {
printf("The Thread ID of Thread number %d is = %lu.n", num, tid);
}
sleep(sleeping_time);
/* say goodbye */
printf("%d: Thread is done after sleeping %ld[s]n", num, sleeping_time);
retu(NULL); /* Dummy to make -Wall happy ... */
}
/*
* Prints a little help to stdout
*/
void printHelp() {
printf("------------------------------HELP--------------------------------n");
printf("Usage: [Option] [Argument(If required!)]");
printf("Option '-h' - Prints this Help and the current GIT Revisio");
printf("Option '-v' - Prints more Informations corresponds to 'verbose'n");
printf("Option '-t' - Requiers a number as Argument which represents then");
printf(" Number of Thread which will be createdn");
printf("Option '-r' - Minimum Randomsizen");
printf("Option '-R' - Maximum Randomsizen");
printf("------------------------------------------------------------------n");
printf("Git Revision #: %sn", gitversion);
printf("------------------------------------------------------------------n");
}
int main(int argc, char *argv[]) {
void *status;
long t;
int hflag = 0;
int tflag = 0;
long NumberOfThreads = 4;
int c;
int i;
while((c = getopt(argc, argv, "ht:vr:R:")) != -1) {
switch(c) {
case 'h':
hflag = 1;
break;
case 't':
tflag = 1;
NumberOfThreads = strtol(optarg, NULL, 10);
/*assert(NumberOfThreads != 0);*/
break;
case 'v':
vflag = 1;
break;
case 'r':
rflag = 1;
rvalue = strtol(optarg, NULL, 10);
if (rvalue < 0) {
fprintf(stderr, "The number has to be greater than zero!n");
exit(-1);
}
break;
case 'R':
Rflag = 1;
Rvalue = strtol(optarg, NULL, 10);
if (Rvalue < 0) {
fprintf(stderr, "The number has to be greater than zero!n");
exit(-1);
} else if (Rvalue < rvalue) {
fprintf(stderr, "The max number for rand has to be greater than the min number!n");
exit(-1);
}
break;
case '?':
if (optopt == 't') {
fprintf(stderr, "Option -%c requires an argument!n", optopt);
} else if (optopt == 'r') {
fprintf(stderr, "Option -%c requires an argument!n", optopt);
} else if (optopt == 'R') {
fprintf(stderr, "Option -%c requires an argument!n", optopt);
} else if (isprint(optopt)) {
fprintf(stderr, "Unknow Option -%c!n", optopt);
} else {
fprintf(stderr, "Unknown Option character '\x%x'!n", optopt);
}
retu -1;
default:
fprintf(stderr, "Something did go wrong. Please use '-h' for help!n");
}
for (i = optind; i < argc; i++) {
fprintf(stderr, "Non-option argument %s!n", argv[i]);
exit(-1);
}
}
if (hflag == 1 && tflag == 0) {
printHelp();
exit(0);
} else if (hflag == 1) {
printHelp();
}
/*******************************************
* Initialise thread attributes here *
*******************************************/
pthread_t threads[NumberOfThreads];
long thread_args[NumberOfThreads];
int check_code;
/*
* Create Threads
*/
for (t = 0; t < NumberOfThreads; t++) {
thread_args[t] = t;
printf("Creating thread %ldn", t);
check_code = pthread_create(&threads[t], NULL, PrintHello, (void*) &thread_args[t]);
/* Check if everything gone right */
assert(check_code == 0);
/* verbose output */
if (vflag == 1) {
printf("Thread %ld is creted and will do his job now!n", t);
}
}
for (t = 0; t < NumberOfThreads; t++) {
check_code = pthread_join(threads[t], &status);
assert(check_code == 0);
printf("Main joining with thread %ldn", t);
if (vflag == 1) {
printf("Completed joining with thread %ld status = %dn", t, (int) status);
}
}
retu (0);
}
