/* Program to understand working of pthread library.
*/
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
int sum;
void *runner(void *param);
int main(int argc,char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if(argc!=2)
{
fprintf(stderr,"%d must be>=0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("Sum=%d\n",sum);
return 0;
}
void *runner(void *param)
{
int i,upper=atoi(param);
sum=0;
for(i=1;i<=upper;i++)
sum+=i;
pthread_exit(0);
}
*/
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
int sum;
void *runner(void *param);
int main(int argc,char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if(argc!=2)
{
fprintf(stderr,"%d must be>=0\n",atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("Sum=%d\n",sum);
return 0;
}
void *runner(void *param)
{
int i,upper=atoi(param);
sum=0;
for(i=1;i<=upper;i++)
sum+=i;
pthread_exit(0);
}
OUTPUT SCREEN:
No comments:
Post a Comment