TUTORIAL : 5 Parallel Processing



1. WHAT IS CONTENTION? DEMONSTRATE CONCEPT OF CONTENTION BY WRITING C PROGRAM


#include<stdio.h>

#include"pheader.h"

int main()
{
        int shmidi,id,*i;

        i=sharei(4,&shmidi);

        *i=5;

        id=process_fork(2);

        *i=*i+1;

        printf("%d = %d \t i = %d \n",id,*i);

        process_join(id,2);
        cleanup_memory(&shmidi);
}

OUTPUT:
1 = 6        I = 1234514217
0 = 7        I = 1234514217
  



2. WRITE PROGRAM TO ADD 10 ELEMENT OF ARRAY USING SEMAPHORE.

Header.h

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
void spin_lock_init(int *lok)
{
        int init_sem_value=1;
        *lok=semget(IPC_PRIVATE,1,(0600|IPC_CREAT));
        if(*lok==-1)
        {
                printf("Error in semget...");
                exit(1);
        }
        if(semctl(*lok,0,SETVAL,init_sem_value)<0)
        {
                printf("Error in semctl...");
                exit(1);
        }
}
void spin_lock(int *lok)
{
        struct sembuf sembuffer, *sops;
                sops=&sembuffer;
                sops->sem_num=0;
                sops->sem_op=-1;
                sops->sem_flg=0;
        if(semop(*lok,sops,1)<0)
        {
                printf("semop lock error...");
                exit(1);
        }
}
void spin_unlock(int *lok)
{
        struct sembuf sembuffer, *sops;
                sops=&sembuffer;
                sops->sem_num=0;
                sops->sem_op=1;
                sops->sem_flg=0;
        if(semop(*lok,sops,1)<0)
        {
                printf("semop lock error...");
                exit(1);
        }
}


Sema_add.c

#include<stdio.h>
#include"head.h"
#include”pheader.h”

int main()
{
        int shmidlw1,shmidlw2,shidlw2,shmidi,shmidfsum,a[10],sum=0,*final_sum,id,*i,j,sep_i,*lw1,*lw2;
        for(j=0;j<10;j++)
        {
                printf("Enter The Value Of Element[%d] = ",j+1);
                scanf("%d",&a[j]);
        }
        i=sharei(4,&shmidi);
        lw1=sharei(4,&shmidlw1);
        lw2=sharei(4,&shmidlw2);
        final_sum=sharei(4,&shmidfsum);
        *i=-1;
        *final_sum=0;

        spin_lock_init(lw1);
        spin_lock_init(lw2);
        id=process_fork(2);

        up:
                spin_lock(lw1);
                *i=*i+1;
                sep_i=*i;
                spin_unlock(lw1);
        if(sep_i>9)
        {
                goto down;
        }
        else
        {
                sum+=a[sep_i];
                goto up;
        }




        down:
                spin_lock(lw2);
                *final_sum+=sum;
                spin_unlock(lw2);
                process_join(id,2);
                printf("Result = %d \n",*final_sum);

        cleanup_memory(&shmidi);
        cleanup_memory(&shmidlw1);
        cleanup_memory(&shmidlw2);
        cleanup_memory(&shmidfsum);

}

OUTPUT:

Enter The Value of Element[1] = 1
Enter The Value of Element[2] = 2
Enter The Value of Element[3] = 3
Enter The Value of Element[4] = 4
Enter The Value of Element[5] = 5
Enter The Value of Element[6] = 6
Enter The Value of Element[7] = 7
Enter The Value of Element[8] = 8
Enter The Value of Element[9] = 9
Enter The Value of Element[10] = 10

Result = 55



Related Posts Plugin for WordPress, Blogger...

Engineering material

GTU IDP/ UDP PROJECT

GTU IDP/ UDP PROJECT

Patel free software download

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP