Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

PROGRAM TO COMPUTE n! USING SELF-SCHEDULING

Posted By: Adele Miller     Category: C Programming     Views: 2181

PROGRAM TO COMPUTE n! USING SELF-SCHEDULING.

Code for PROGRAM TO COMPUTE n! USING SELF-SCHEDULING in C Programming

#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
 
  
Share: 


Didn't find what you were looking for? Find more on PROGRAM TO COMPUTE n! USING SELF-SCHEDULING Or get search suggestion and latest updates.

Adele Miller
Adele Miller author of PROGRAM TO COMPUTE n! USING SELF-SCHEDULING is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!