logo

Inter-process Communication and Synchronisation

   

Added on  2023-06-03

11 Pages2268 Words343 Views
Inter-process Communication and Synchronisation
[Name]
Institution

Task 1: Create and destroy a shared memory segment
The producer was implemented by use of a few includes and functions. The main functions
used were
ftok; used to generate a unique key for the shared memory segment.
shmget : used to create the memory segment and obtain the shared memory ID
shmctl used in destroying the shared memory segment.
The producer was tested and confirmed to be running okay, and destroyed the shared memory
segment successfully after the 10 seconds sleep.
Task 2: Send a single message from producer to consumer
Stage 1: Modification of the Producer
The producer made in task 1 was modified to attach a the shared memory segment. This was
done by use of the shmat function as follows: char *sharedMemoryPTR = (char*)
shmat(shared_memory_id,(void*)0,0);
A for loop was then used to set a null on all the memory locations of the shared memory
segment. The MAX_SIZE constant has a value of 32, which is the maximum size of the
shared memory.
for (int c = 0; c <= MAX_SIZE; c++)
*s++ = '\0';
A message was then obtained from the user and set to the memory segment. To obtain the
message the code used was; fgets(message, sizeof message, stdin);
The message was written into the memory segment using the code;
strncpy(sharedMemoryPTR , message,MAX_SIZE );

To detach the shared memory segment, the function shmdt as follows;
shmdt(sharedMemoryPTR);
Finally,, the shared memory segment was destroyed using the function shmctl ;
shmctl(shared_memory_id, IPC_RMID, NULL);
Stage 2: consumer.c
A consumer was created ; to enable the consumer connect to the shared memory segment, a single key
was used for both the producer and the consumer. The key was therefore statically embedded in the
code with a definite value. The consumer connected to the shared memory, read the message in the
memory then detached from the segment. The screenshots below shows both the producer and
consumer running;

Task 3: Send a series of messages between producer and
consumer
Stage 1: Modification of the producer to handle a series of messages
The strategy employed in allowing the producer to handle a series of messages is the use of a
while loop, that runs infinitely until an EOF is encountered. For this case, the inputs are
entered by the user manually, so to send the EOF on Linux, one has to type Ctrl+D which
sends an EOF to the program. Since the program uses fgets to obtain the user inputs from
stdin, the EOF is interpreted as NULL. The code snippet for the loop is as shown below.
while (fgets(message, sizeof message, stdin) != NULL){
//Write the message to the memory segment
strncpy(sharedMemoryPTR , message,MAX_SIZE );
printf("Memory segment contains the message: \%s \n", sharedMemoryPTR);
printf("-Sleeping for 10 seconds!!...\n");
sleep(10);
printf( "Enter a message of up to 20 characters or Ctrl+D to exit:");
}
The screen shot below shows successful testing of the code; the program runs until Ctrl + D
is pressed which indicates EOF.

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Operating System
|30
|2055
|230

Shared Memory and Inter-Process Communication in Operating Systems
|34
|3181
|51

COMP9812 - Computer Networks And Operating Systems
|34
|3084
|208

Shared Memory Segment Creation and Destruction in C Programming
|29
|2258
|453