Aim: Program to demonstrate the shared memory model of programming in parallel processing
Source Code:
Output:
Comment bellow for your Query and Feedback
Source Code:
#include <stdio.h>
#include "shmlib.h"
#include <stdlib.h>
int main()
{
int id, id1, a,b,c,d, *sum0, *sum1;
sum0=(int *) shared(sizeof(int), &id1);
sum1=(int *) shared(sizeof(int), &id1);
*sum0= *sum1= 0;
printf("\n \n this program performs addition of four numbers entered by the user two child processes are created and each of them adds 2 numbers. at the end the child processes are killed and the parent processes adds the answer returned by both the child processes and display the final output \n");
printf("***********************");
printf("\n Enter any four number:");
scanf("%d%d%d%d", &a, &b, &c, &d);
id=create_process(3);
if(id==1)
*sum0=a+b;
else
*sum1=c+d;
join_process(3,id);
printf("\n All the child processes are killed");
printf("\n \n Sum of four numbers %d \n \n ",(*sum0 + *sum1));
free_shm(id1);
return 0;
}
#include "shmlib.h"
#include <stdlib.h>
int main()
{
int id, id1, a,b,c,d, *sum0, *sum1;
sum0=(int *) shared(sizeof(int), &id1);
sum1=(int *) shared(sizeof(int), &id1);
*sum0= *sum1= 0;
printf("\n \n this program performs addition of four numbers entered by the user two child processes are created and each of them adds 2 numbers. at the end the child processes are killed and the parent processes adds the answer returned by both the child processes and display the final output \n");
printf("***********************");
printf("\n Enter any four number:");
scanf("%d%d%d%d", &a, &b, &c, &d);
id=create_process(3);
if(id==1)
*sum0=a+b;
else
*sum1=c+d;
join_process(3,id);
printf("\n All the child processes are killed");
printf("\n \n Sum of four numbers %d \n \n ",(*sum0 + *sum1));
free_shm(id1);
return 0;
}
Output:
Comment bellow for your Query and Feedback
0 comments:
Confused? Feel free to ask
Post a Comment