Limited-time discount for students! | Solutions starting at $6 each

COMP9812 - Computer Networks And Operating Systems

Added on - 07 Oct 2021

  • COMP9812

    Course

  • 34

    Pages

  • 3084

    Words

  • 20

    Views

  • 0

    Downloads

Trusted by +2 million users,
1000+ happy students everyday
Showing pages 1 to 8 of 34 pages
Operating System
Table ofContents
1.Introduction.................................................................................................................................2
Task 1: Creating and Destroying the Segment of Shared Memory.................................................2
Task 2: Sharing only One Message between the Producer and Consumer.....................................4
Task 3: Sharing a series of Messages between the Producer and Consumer..................................6
Task 4: Delaying the Message Sent by the Producer........................................................................8
Task 5: Implementing Semaphores..................................................................................................10
Task 6: Character-based circular buffer.........................................................................................13
2.Conclusion..................................................................................................................................14
References..........................................................................................................................................15
1.Introduction
In today’s modern applications, inter-process communication is widely spread. This
report sheds light on shared memory between the producer and the consumer. It is difficult to
use shared memory transportation, but this method provides more control and speed.
However, there will be behaviour difference in different operating systems, which can be
dealt easily. Here, the system refers to message queue, which contains two-way
communication channels for the producer and the consumer to exchange messages. The
shared memory will be developed here with the help of C language, in the Linux operating
system.
The objective of this report is to accomplish six tasks such as, creating and destroying
the segment of shared memory, sending one message from the producer to the consumer,
sending series of messages from the producer to the consumer, delaying the message sent by
the producer, implementing semaphores and Character-based circular buffer. All these
objectives will be met in the following sections of the report.
Task 1: Creating and Destroying the Segment of Shared Memory
Here, the task refers to developing a segment of shared memory for the producer and
consumer. We create producer for sending the message and sharing the memory. There is
memory space for the producer so that it is possible for the producer to share the memory
with the consumer. This segment contains, owner name, bytes, shared memory and status.
The program is used to check the remaining memory space of the producer. The producer can
easily send the message to the consumer. Once the memory of the producer is checked, the
memory will be decreased and it will be shared with the consumer (Ahmad, 2010).
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

#define SHSIZE 100
int main(int argc,char*argv[])
{
int shmid;
key_t key;
char * shm;
char*s;
key=9876;
shmid=shmget(key,SHSIZE,IPC_CREAT | 0666);
if(shmid<0)
{
perror("shmget");
exit(1);
}
shm=shmat(shmid,NULL,0);
if(shm==(char*)-1)
{
perror("shmat");
exit(1);
}
memcpy(shm,"hello world",11);
s = shm;
s += 11;
*s = 0;
while(*shm != '*')
sleep (1);
return 0;
}

Output
Task 2: Sharing only One Message between the Producer and Consumer
Here, the task refers to sharing only one message that is sent from the producer to the
consumer, where the producer is referred as the sender and the receiver as the consumer.
After the message is received, it is displayed on the page of the consumer (Hogan and
Epping, 2014).
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHSIZE 100
int main(int argc,char*argv[])
{
int shmid;
key_t key;
char * shm;
char*s;
key=9876;
shmid=shmget(key,SHSIZE,IPC_CREAT | 0666);
if(shmid<0)
{
perror("shmget");
exit(1);
}
shm=shmat(shmid,NULL,0);
if(shm==(char*)-1)
{
perror("shmat");
exit(1);
}
for(s =shm; *s !=0; s++)
printf("%c", *s);
printf("\n");
*shm = '*';

return 0;
desklib-logo
You’re reading a preview
Preview Documents

To View Complete Document

Click the button to download
Subscribe to our plans

Download This Document