COMP9812 Operating Systems Assignment: Interprocess Communication
VerifiedAdded on  2023/05/31
|34
|3181
|51
Practical Assignment
AI Summary
This assignment solution explores inter-process communication (IPC) between a producer and consumer using shared memory in a Linux environment. The implementation covers creating and destroying shared memory segments, sharing single and multiple messages, introducing delays in message sending, implementing semaphores for synchronization, and creating a character-based circular buffer. The code examples in C language demonstrate key concepts of shared memory, mutual exclusion, and process synchronization. The report details the code implementation for each task, including the use of system calls like shmget, shmat, semget, and semop to manage shared memory and semaphores, offering a practical understanding of IPC mechanisms in operating systems. Desklib provides this document as a resource for students.

Operating System
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Table of Contents
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.................................................................................................................................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>
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>
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

#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);
#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);
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

s = shm;
s += 11;
*s = 0;
while(*shm != '*')
sleep (1);
return 0;
}
Output
s += 11;
*s = 0;
while(*shm != '*')
sleep (1);
return 0;
}
Output

⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

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;
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;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

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

}
Output
Output
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Task 3: Sharing a series of Messages between the Producer and Consumer
Here, the task is with respect to sharing a series of messages that are sent from the
producer to consumer (Singh., 2011).
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
Here, the task is with respect to sharing a series of messages that are sent from the
producer to consumer (Singh., 2011).
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

#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);
#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;
}
#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;
*shm = '*';
return 0;
}
#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;
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 34
Related Documents
Your All-in-One AI-Powered Toolkit for Academic Success.
 +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.




