Parallel Processing Tutorial 06
Que 1) Write a program to display Factorial using
self scheduling.
//factorial of number using self scheduling
#include<stdio.h>
#include "pheader.h"
int main()
{
int
no,icount,i,id,nproc,parfact,shmidfact,shmidindex,shmidlock;
int *fact,*index,*lock;
fact=(int
*)sharei(sizeof(int),&shmidfact);
index=(int
*)sharei(sizeof(int),&shmidindex);
lock=(int
*)sharei(sizeof(int),&shmidlock);
*fact=1;
*index=2;
nproc=5;
printf("Enter Number to find Factorial :");
scanf("%d",&no);
spin_lock_init(lock);
id=process_fork(nproc);
parfact=1;
while(1)
{
spin_lock(lock);
//cricital region
i=*index;
*index=*index+1;
spin_unlock(lock);
if(i>no)
break;
parfact=parfact*i;
}
spin_lock(lock);
//cricital
region
*fact=*fact
* parfact;
spin_unlock(lock);
printf("\n Partial Factorial of Process:%d is
%d",id,parfact);
process_join(id,nproc);
printf("\n Final Factorial=%d\n",*fact);
cleanup_memory(&shmidindex);
cleanup_memory(&shmidfact);
cleanup_memory(&shmidlock);
return 0;
}