Hi guys so i'm writing a shared memory program and currently, I can create the shared memory, but I want the parent to write to the shared memory, and then I want the child to read from that shared memory, then the parent write to the shared memory and the child again read from it.
The shared memory is only going to be 32 bytes big, and i'm gareteened the fiels i'm reading in wont' be bigger than 32 bytes.
Currently my program is able to write 1 line of the file to shared memory then the other process is able to read that line, and write it to a file.
But the issue comes is now I want to read a whole file, sending 1 line of the file at a time.
Is there a way for me to tell the reading process to wait for the other process to write its line to the shared memory, and then tell the writing process, that yes the child has read that line of shared memory? So the writing process knows when to write the next line to shared memory?
Here's what my code looks like right now:
Code:
/**********************************************************************
Function : pipe_read
Description : read message from pipe into buffer of max bytes
Inputs : index to mypipe
buf to write characters from pipe
max is number of bytes maximum
Outputs : >0 if successful (number of bytes read), <0 otherwise
***********************************************************************/
int pipe_read( int index, char *buf, int max )
{
int read, write;
int bytes;
/* need to store vars -- in case of concurrent update */
read = only_pipe->read;
write = only_pipe->write;
printf("inside mypipe_read()\n");
printf("reading this from only_pipe->shm: %s",only_pipe->shm);
//copying from shared memory to buf so we can write buf to file.
strcpy(buf,only_pipe->shm);
return bytes;
}
/**********************************************************************
Function :pipe_write
Description : write message from buffer into shm of len bytes
Inputs : index to mypipe
buf to read characters for pipe
len is number of bytes to read
Outputs : >0 if successful (number of bytes read), <0 otherwise
***********************************************************************/
int pipe_write( int index, char *buf, int len )
{
int read, write;
printf("made it to mypipe_write()\n");
/* need to store vars -- in case of concurrent update */
read = only_pipe->read;
write = only_pipe->write;
printf("only_pipe->read: %d\n",read);
printf("only_pipe->write: %d\n",write);
printf("only_pipe->shm: %s",only_pipe->shm);
printf("only_pipe->size: %d\n",only_pipe->size);
printf("only_pipe->shmid: %d\n",only_pipe->shmid);
printf("buf passed in: %s",buf);
//copy buff into shared memory buff
printf("Copying passed in buffer to shared memory buffer...\n");
int i;
for(i = 0; i < len; i++)
{
only_pipe->shm[i] = buf[i];
}
only_pipe->shm[len] = '\0';
printf("only_pipe->shm: %s",only_pipe->shm);
return len;
}
I call these functions in other functions that actually read/write a file.
Code:
/**********************************************************************
Function : send_file
Description : Send file 'name' data over IPC channel 'index'
Inputs : name -- file path
index -- IPC descriptor
Outputs : 0 if successful, -1 otherwise
***********************************************************************/
int send_file( char *name, int index )
{
//holds the number of characters you read in a line
int readChars;
FILE *fp;
//open file for reading
fp=fopen(name, "r");
//EOF_FLAG will be 1 when EOF is found
int EOF_FLAG = 0;
//this will hold 1 line of the file at a time.
char text[MSG_SIZE];
printf("made it here!\n");
//while(EOF_FLAG != 1)
//{
readChars = readline(fp,text,MSG_SIZE,&EOF_FLAG);
//need to null terminate the array so you won't have garbadge at the end.
text[readChars] = '\0';
int length = strlen(text);
printf("length is size: %d\n",length);
printf("This is the text array I'm passing to mypipe_write(index,text,length): %s", text);
printf("wrote-> %d\n", mypipe_write(index,text,length));
// printf("read in: %s",text);
//}
close(fp);
return 0;
}
/**********************************************************************
Function : rcv_file
Description : Receive file 'name' data over IPC channel 'index'
Inputs : name -- file path
index -- IPC descriptor
Outputs : 0 if successful, -1 otherwise
***********************************************************************/
int rcv_file( char *name, int index )
{
FILE *fp;
fp = fopen(name,"w");
if(fp == NULL)
{
printf("Error creating the file\n");
return EXIT_FAILURE;
}
char readBuff[MSG_SIZE];
int readBytes = mypipe_read(index,readBuff,MSG_SIZE);
printf("Read %d bytes from the shared memory.\n",readBytes);
printf("Wrote to file: %s", readBuff);
fprintf(fp,"%s",readBuff);
close(fp);
return 0;
}
Any help would be great!
I could use sch_yeild() but the problem is, I want them to take turns, not 1 process to completely write the file, and when its done writing the file, tell the other process to start reading, because if thats the case the read process will only see the last line of the file because the writing process is only sending 1 line at a time to the shared memory as there is only 32 bytes of storage .