Hey guys. I am suppose to write a program that implements the parent/child and shared memory paradigms. Its done on Linux, BSD or Mac OS X system using C, cannot be written on Microsoft Windows.
The program is to establish a shared memory segment and write the string "raabcc" into it. The intial r means the remainder of the buffer is ready to be RLE compressed. A fork will then be called. The child then process and changes the contents of the shared memory segments to d3a1b2c and then exits. The d means the compressions of the buffer is done.
The parent will wait for the child to be completed and then prints the contents of the shared memory segment to stdout.
This is what i have so far... Can someone help me please.. I have sleepless nights for past week and its due in some few hours.
CODE
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
/* Identifier for the shared memory segment */
int segment_id;=
/* Pointer to the shared memory segment */
char*shared_memory;
/* Size (in bytes) of the shared memory segment */
const int size = 4096;
/* Writes the string to the shared memory segment */
sprintf(shared_memory, "raaabcc");
{
pid_t pid;
/*child process */
pid = fork();
if (pid < 0) {/* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) {/* child process */
/* Child detaches shared memory segment */
shmdt(shared_memory);
*/ Writes new string to shared memory */
sprintf(shared_memory, "d3a1b2c");
execlp("/bin/ls","ls",NULL);
}
else {/* parent process */
/* Parent waits for child to finish */
wait(NULL)
/* Prints out the string from shared memory */
printf("*%s\n", shared_memory);
/* Detaches the shared memory segment */
shmdt(shared_memory);
/* Removes the shared memory segment */
shmctl(segment_id, IPC_RMID, NULL);
/* Ends Program */
return 0;
}