Logo 
Search:

C Programming Articles

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

Program to add 4 integer values using 2 processes

Posted By: Alfonsa Miller     Category: C Programming     Views: 3126

Write a C program to add 4 integer values using 2 processes.

Code for Program to add 4 integer values using 2 processes in C Programming

int pfork(int);
void process_join(int,int);
 
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
 
int main()
{
 
        int sum=0,sum0=0;
        int *ptr,shmid,id;
 
 
       shmid=shmget(IPC_PRIVATE,4,0666|IPC_CREAT);
 
        ptr=(int *)shmat(shmid,0,0);
 
        if(shmid < 0)
        {
                printf("Error\n");
        }
 
        id=pfork(2);
 
        if(id==0)
        {
                sum0=30+45;
                printf("parent performing addition\n");
                printf("the sum partial sum by parent is :%d\n",sum0);
                printf("\n");
 
 
        }
  else
        {
 
                if(id==1)
                {
                        *ptr=50+40;
 
                        printf("addition by child\n");
                        printf("partial sum by child is :%d\n",*ptr);
                        printf("\n");
                }
        }
 
        //printf("Calling process join for id %d\n",id);
         
        process_join(2,id);
 
        sum=sum0+*ptr;
 
        printf("The sum is : %d",sum);
 
        printf("\n");
        printf("End Parent\n");
        return 1;
}
 
int pfork(int x)
{
        int j;
        for(j=1;j<=(x-1);j++)
        {
                if((fork())==0)
                        return j;
        }
 
        return 0;
}
 
void process_join(int x,int id)
{
        int j,val;
        if(id==0)
        {
                for(j=1;j<=(x-1);j++)
                {
                        printf("parent process id %d\n\n",getpid());
                        printf("Parent calling wait:blocked state\n");
 
                        val=wait(0);
                        printf("The child deleted is : %d\n",val);
                }
        }
        else
        {
                printf("child pid %d\n",getpid());
                exit(0);
        }
        printf("after exit %d\n",getpid());
 
 
}




Output:

[04mca8@LINTEL 04mca8]$ cc add4int.c
[04mca8@LINTEL 04mca8]$ ./a.out
addition by child
partial sum by child is :90
 
child pid 3842
parent performing addition
the sum partial sum by parent is :75
 
parent process id 3841
 
Parent calling wait:blocked state
The child deleted is : 3842
after exit 3841
The sum is : 165
End Parent


  
Share: 


Didn't find what you were looking for? Find more on Program to add 4 integer values using 2 processes Or get search suggestion and latest updates.

Alfonsa Miller
Alfonsa Miller author of Program to add 4 integer values using 2 processes 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!