Question: Please explain what this C Program does #include #include #include int sum; void *runner( void *param); int main( int argc, char *argv[]) { pthread_t tid;

Please explain what this C Program does

#include

#include

#include

int sum;

void *runner(void *param);

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

{

pthread_t tid;

pthread_attr_t attr;

if (argc != 2)

{

fprintf(stderr,"usage: a.out ");

return -1;

}

if (atoi(argv[1]) < 0) {

fprintf(stderr,"%d must be >= 0 ",atoi(argv[1]));

return -1;

}

//default

pthread_attr_init(&attr);

//create

pthread_create(&tid,&attr,runner,argv[1]);

//join

pthread_join(tid,NULL);

//print

printf("sum = %d ",sum);

}

void *runner(void *param)

{

int i, upper = atoi(param);

sum = 0;

for (i = 1; i <= upper; i++)

sum += i;

pthread_exit(0);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!