#include<stdio.h>
#include "headfork.h"
#include "headshr.h"
#include "headsem.h"
main()
{
int shmid,shmid1,shmid2;
int *index,*ans,*lock1;
int id,n,npc,i,j=1;
ans=shared(sizeof(int)*2,&shmid);
lock1=shared(sizeof(int)*2,&shmid1);
index=shared(sizeof(int)*2,&shmid2);
lock_init(lock1);
printf("Enter ant No: ");
scanf("%d",&n);
printf("Enter the no.of proc: ");
scanf("%d",&npc);
*ans=1;
*index=1;
id=p_fork(npc);
for(i=1;i<=n;i++)
{
locksem(lock1);
j=*index;
*index=*index +1;
unlock(lock1);
if(j>n)
break;
*ans = *ans * j;
}
p_join(npc,id);
printf("The Factorial of %d is: %d",n,*ans);
}
OUTPUT
knoppix@ttyp0[pp]$ cc fact_ss.c
knoppix@ttyp0[pp]$ ./a.out
Enter ant No: 5
Enter the no.of proc: 3
The Factorial of 5 is: 120