10.Write a program to calculate factorial of a number using thread library

Write a program to calculate factorial of a number using thread library.

//cskecode
//cskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
int fact;
void *run(void *param);
int main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if(argc !=2)
{
printf("Error !");
return 1;
}
if(atoi(argv[1])<O)
{
printf("no. Should be +ive");
return 1;
}
pthread_attr_init(&attr);
pthread_create(&tid, &attr,run, argv[1]);
pthread_join(tid,NULL);
printf("fact= %d in", fact);
}
void *run(void *param)
{
int i, upper;
fact=1;
upperratoi(param);
if(upper>0)
for(i=1;i<=upper; ++i)
fact*=i;
pthread_exit(0);
}
//cskecode.blogspot.com

Output:


No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...