Help using C

By lex37 ·
Using C in Linux, create a program the implements the parent/child and shared memory paradigms. Its suppose to establish a shared memory segment and write the string (null terminated) raaabcc into it. (the initial r means the remainder of the buffer is ready to be RLE compressed). A fork will then be called (pid = fork(); ). The child process will change the contents of the shared memory segment d3a1b2c and then exit. (the initial d means the compression of the buffer is done). The parent will wait for the child to complete and will then print the contents of the shared memory segment to stdout. You can use shmget, shmat, shmdt and schctl. The parent sets up the environment before the child starts and cleans it up after the child exits. The child should make these calls only if it must. Only if it cannot do its task without calling one or more of them.

I never used C before and not good at Linux. I started it off with:

#include sys/types.h>
#include sys/shm.h>
#include sys/stat.h>
#include stdio.h>
#include unistd.h>

int main()
pid_t pid;
int segment_id;= //shared memory ID
char* shared_memory;
// pointer to shared memory segment
// fork a child
pid = fork();

if (pid < 0) //error
fprintf(stderr, "fork failed");
else if (pid ==0) //child process
else //parent process, will wait for //child to complete
wait (NULL);
printf("child complete");

const int size = 4096; //size of shared

segment_id = shmget(IPC_PRIVATE,size, S_IRUSR | S_IWUSR);
// allocate a shared memory segment

shared_memory = (char *) shmat(segment_id, NULL, 0);
//attach the shared segment

sprintf(shared_memory, "raaabcc");
// writes to shared memory

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Share your knowledge

Related Discussions

Related Forums