School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,134 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,023 people online right now. Registration is fast and FREE... Join Now!




Parent/child shared memory

 

Parent/child shared memory, OS

infanity

29 Oct, 2009 - 01:41 AM
Post #1

New D.I.C Head
*

Joined: 27 Apr, 2009
Posts: 9

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;
}



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:47PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month