Documentation about a Code ( Threads)

I have a code but i dont understand it. Can someone explain me what happens in the code?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <math.h>

int total = 0;
int activ = 0;

struct thr_parameter {

int start;
int end;
int thr_num;
};

void *Sector(void *arg)
{
while (activ != 0) {
printf("-> %d\n", total);
usleep(500000);
}
}
void *Thread_main(void *arg)
{
int i, j, k;
int prim;
int num = 0;
struct thr_parameter *p = arg;

activ++;
printf("Worker %d: Ready for Service\n", p->thr_num);
printf("Worker %d: ", p->thr_num);
printf("Starting calculation from %d to %d\n", p->start, p->end);

for (i = p->start; i <= p->end; i++) {
if (i == 1)
continue;
else if (i == 2 || i == 3)
prim = 1;
else {
prim = 1;
k = sqrt(i);
for (j = 2; j <= k; j++) {
if (i % j == 0) {
prim = 0;
break;
} }
}
if (prim) {
total++;
num++;
}
}
printf("Worker %d: Found %d Primes\n", p->thr_num, num);
activ--;
}

int main(int argc, char *argv[])
{

int threads = 0;
int l = 0;
int i = 1;
int sektor1 = 1;
int sektor2 = 0;
int maximum;
int j;
double diff;

struct thr_parameter *t_param;
struct timespec start, finish;

pthread_t *t;
pthread_t sector;

clock_gettime(CLOCK_MONOTONIC, &start);

while ((j = getopt(argc, argv, "N:t:")) != -1) {
switch (j) {
case 'N':
maximum = atoi(optarg);
break;
case 't':
threads = atoi(optarg);
break;
default:
break;
}
}

if (maximum < 2 || threads < 1)
exit(EXIT_FAILURE);


l = maximum / threads;

if (maximum % threads)
l++;

t = malloc(threads * sizeof(*t));

for (i = 0; i < threads; i++) {
sektor2 = sektor1 + l - 1;
if (sektor2 > maximum)
sektor2 = maximum;

t_param = malloc(sizeof(*t_param));
t_param->thr_num = i;
t_param->start = sektor1;
t_param->end = sektor2;

if (pthread_create(&t[i], NULL, Thread_main, (void *)t_param)) {
perror("ERROR");
exit(EXIT_FAILURE);
}
sektor1 = sektor2 + 1;
}

if (pthread_create(&sector, NULL, Sector, NULL)) {
perror("ERROR");
exit(EXIT_FAILURE);
}

if (pthread_join(sector, NULL)) {
perror("ERROR");
exit(EXIT_FAILURE);
}

for (i = 0; i < threads; i++) {
if (pthread_join(t[i], NULL)) {
perror("ERROR");
exit(EXIT_FAILURE);
}
}

printf("Total Prime Count: %d\n", total);

clock_gettime(CLOCK_MONOTONIC, &finish);

diff = (finish.tv_sec - start.tv_sec);
diff += (finish.tv_nsec - start.tv_nsec)/1000000000.0;

printf("RunTime: %f\n", diff);

free(t_param);
free(t);

return 0;
}
First, code is much easier to read, when it is posted within code tags and is indented too. See http://www.cplusplus.com/articles/jEywvCM9/


Which features in this program you are not familiar with?
It appears to count prime numbers up to some limit called maximum. It uses multiple threads to do it. There are two command line arguments: -N lets you specify the maximum prime. -T lets you specify how many threads to use. Try running it with these command line options. It should help you figure out how it works:

-N 100000 -T 1
-N 100000 -T 2
-N 100000 -T 4
-N 500000 -T 4
Topic archived. No new replies allowed.