apologize me i've put the wrong code,
it should be,
main process,
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSZ 4
#define SHMSZ2 1
main()
{
int noofvals=0,i,num,shmid,*shm,*s,shmid2,j;
char *shm2,*s2;
printf("Enter the Number Values you enter\n");
scanf("%d",&noofvals);
int *arr=malloc(noofvals*sizeof(int));
for(i=0;i<noofvals;i++)
{
printf("Enter the Number %d\n",i+1);
scanf("%d",&num);
arr[i]=num;
}
printf("Numbers Read\n");
for(i=0;i<noofvals;i++)
{
printf("%d,",arr[i]);
}
printf("\n");
key_t key;
key=5678;
if((shmid=shmget(key, SHMSZ, IPC_CREAT | 0666))<0)
{
perror("shmget");
exit(0);
}
if ((shm = shmat(shmid, NULL, 0)) == (int *) -1)
{
perror("shmat");
exit(1);
}
key_t key2;
key2=5679;
if((shmid2=shmget(key2, SHMSZ2, IPC_CREAT | 0666))<0)
{
perror("shmget");
exit(0);
}
if((shm2=shmat(shmid2, NULL, 0))==(char *) -1)
{
perror("shmat");
exit(0);
}
// sending the Number of Values
printf("Sending the Number of Elements to Receive\n");
*shm2='w';
s=shm;
*s=noofvals;
while(*shm2!='r')
{
wait(1);//waiting for Clients Reply
}
printf("Element Count Sent\n");
*shm=NULL;
// sending the Elements
num=0;
*shm2='w';
while(num<noofvals)
{
for(i=0;i<4;i++)
{
printf("Sending %d\n",arr[num]);
*s=arr[num];
num++;
if(num>=noofvals)
{break;}
*s++;
}
printf("\n");
*shm2='w';
while(*shm2!='r')
{
wait(1);//waiting for Client's Reply
}
}
s=shm;
for(i=0;i<noofvals;i++)
{
printf("Sent val %d\n",*s);
*s++;
}
*shm2='w';
// Server Exiting
printf("Server Exiting\n");
}
client process,
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSZ 4
#define SHMSZ2 1
main()
{
int noofvals=0,i,num,shmid,*shm,*s,shmid2,j;
char *shm2,*s2;
key_t key;
key=5678;
key_t key2;
key2=5679;
if((shmid=shmget(key, SHMSZ, 0666))<0)
{
perror("shmget");
printf("error 1");
exit(0);
}
if((shmid2=shmget(key2, SHMSZ2, 0666))<0)
{
perror("shmget");
exit(0);
}
if ((shm = shmat(shmid, NULL, 0)) == (int *) -1)
{
perror("shmat");
exit(1);
}
if((shm2=shmat(shmid2, NULL, 0))==(char *) -1)
{
perror("shmat");
exit(0);
}
// wait till server Sends the Number of Elements
while(*shm2!='w')
{
wait(1);//waiting for Server to Send Data
}
noofvals=*shm;
*shm=NULL;
printf("Received the Number of Elements as %d\n",noofvals);
*shm2='r';
int arr[noofvals];
// Receive Elements
printf("Waiting\n");
while(*shm2!='w')
{
wait(1);//waiting for Server to Send Data
}
s=shm;
num=0;
while(num<noofvals)
{
for(i=0;i<4;i++)
{
printf("Received %d\n",*s);
arr[num]=*s;
num++;
if(num>=noofvals)
{break;}
s++;
}
printf("\n");
*shm2='r';
while(*shm2!='w')
{
wait(1);//waiting for Client's Reply
}
}
*shm2='r';
int div=(int) ((noofvals/2)+1);
printf("\n\ndiv main - %d, div main2 - %d \n\n",div,(noofvals-div));
int *ar1=malloc(div*sizeof(int));
int *ar2=malloc((noofvals-div)*sizeof(int));
for(i=0;i<div;i++)
{
ar1[i]=arr[i];
printf("div - %d - %d\n",i,ar1[i]);
}
printf("\n");
int lp=noofvals-div;
for(i=0;i<lp;i++)
{
ar2[i]=arr[div];
printf("div - %d - %d\n",div,ar2[i]);
div++;
}
for(i=0;i<div;i++)
{
printf("ptr arr %d\n",ar1[i]);
}
for(i=0;i<lp;i++)
{
printf("ptr arr2 %d\n",ar2[i]);
}
}