Shared Memory and Inter-Process Communication in Operating Systems

Verified

Added on  2023/05/31

|34
|3181
|51
AI Summary
This report sheds light on shared memory between the producer and the consumer in operating systems. It covers six tasks such as creating and destroying the segment of shared memory, sharing one message, sharing a series of messages, delaying the message sent by the producer, implementing semaphores, and character-based circular buffer.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Operating System

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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
Document Page
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>
Document Page
#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);

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
s = shm;
s += 11;
*s = 0;
while(*shm != '*')
sleep (1);
return 0;
}
Output
Document Page
Document Page
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;

Paraphrase This Document

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

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
#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);
Document Page
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;
Document Page
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

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Output
Document Page
Task 4: Delaying the Message Sent by the Producer
Here, the message sent by the producer to the consumer, is delayed with the use of
Nano sleep (Kothari et al., 2016).
Program
#include <stdio.h>
#include <time.h>
void delay(int number_of_seconds)
{
int milli_seconds = 1000000 * number_of_seconds;
clock_t start_time = clock();
while (clock() < start_time + milli_seconds)
;
}
int main()
{
Document Page
int i;
for (i = 0; i < 10; i++) {
delay(1);
printf("%d producer\n", i + 1);
}
return 0;
}
Output

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
Document Page
Task 5: Implementing Semaphores
Here, the task of resolving the problem of semaphore is completed, by implementing
the semaphore for the producer and consumer (Leva et al., 2013).
Program
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <pthread.h>
#include <stdlib.h>
#define BUFSIZE 10
Document Page
#define MUTEX 0
#define FULL 1
#define EMPTY 2
int semid;
union semun {
int val; /* Value for SETVAL */
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET
*/
unsigned short *array;/* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-
specific) */
};
int sem_create(int nsems) {
int id;
key_t key = 1234;
int semflg = IPC_CREAT | 0666;
id = semget(key, nsems, semflg);
if(id < 0)
{
perror("semget:");
exit (1);

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
return id;
}
void sem_initialise(int semno, int val) {
union semun un;
un.val = val;
if(semctl(semid, semno, SETVAL, un) < 0)
{
// printf("%d\n", semno);
perror("semctl:");
exit(2);
}
}
void *producer(void *id);
void *consumer(void *id);
void wait(int semno);
void signal(int semno);
int buffer[BUFSIZE], data;
int in = 0;
int out = 0;
Document Page
int i = 10000;
int j = 10000;
int main (int argc, char *argv[]) {
semid = sem_create(3);
sem_initialise(MUTEX, 1);
sem_initialise(FULL, 0);
sem_initialise(EMPTY, 10);
pthread_t prod, cons;
pthread_create(&prod, NULL, producer, (void *)semid);
pthread_create(&cons, NULL, consumer, (void *)semid);
pthread_exit(NULL);
return 0;
}
void *producer(void *id) {
int semid = (int) id;
data = 0;
while(i--) {
wait(EMPTY);
Document Page
wait(MUTEX);
/** Critical Section **/
buffer[in] = data;
in = (in + 1) % BUFSIZE;
data = (data + 1) % BUFSIZE;
printf("Parent process:%d\n", data);
//printf("P\n");
signal(MUTEX);
signal(FULL);
}
pthread_exit(NULL);
}
void *consumer(void *id) {
int semid = (int) id;
while(j--)
{
wait(FULL);
wait(MUTEX);
/** Critical Section **/
data = buffer[out];
out = (out + 1) % BUFSIZE;

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
printf("Child process:%d\n", data);
/** **/
signal(MUTEX);
signal(EMPTY);
}
pthread_exit(NULL);
}
void wait(int semno) {
struct sembuf buf;
buf.sem_num = semno;
buf.sem_op = -1;
buf.sem_flg = 0;
if(semop(semid, &buf, 1) < 0) {
perror("semop:");
exit(2);
}
}
void signal(int semno) {
struct sembuf buf;
buf.sem_num = semno;
Document Page
buf.sem_op = 1;
buf.sem_flg = 0;
if(semop(semid, &buf, 1) < 0)
{
perror("semop:");
exit(2);
}
}
Output
Document Page

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Task 6: Character-based circular buffer
Here, the task of creating a circular buffer depending on the character, for the
producer and consumer is done (Rule, 2011).
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
typedef struct
{
char name[50];
char surname[50];
Document Page
char id[50];
float salary;
}EMPLOYEE;
typedef struct
{
int front,rear;
EMPLOYEE arr[MAX];
}C_BUFFER;
int isFull(C_BUFFER *buffer)
{
return ((buffer->rear+1) % MAX == buffer->front);
}
int isEmpty(C_BUFFER *buffer)
{
return (buffer->front == buffer->rear);
}
int insertToBuffer(C_BUFFER *buffer,EMPLOYEE *emp)
{
if(isFull(buffer))
return 0;
Document Page
buffer->front=(buffer->front+1) % MAX;
buffer->rear=(buffer->rear+1) % MAX;
buffer->arr[buffer->rear]=*emp;
return 1;
}
int deleteFromBuffer(C_BUFFER *buffer,EMPLOYEE *emp)
{
if(isEmpty(buffer))
return 0;
buffer->front=(buffer->front+1) % MAX;
*emp=buffer->arr[buffer->front];
return 1;
}
void insertToFile()
{
FILE *f;
C_BUFFER *buffer;
if(isEmpty(buffer))
return;
while(isFull(buffer))
fwrite(buffer->arr,sizeof(EMPLOYEE),buffer->rear,f);

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
fclose(f);
}
int printBuffer(C_BUFFER *buffer)
{
if(isEmpty(buffer))
return 0;
int i=buffer->front;
if(buffer->front <= buffer->rear)
{
while(i <= buffer->rear)
printf("%s %s %s %f",
buffer->arr[i++].name,buffer->arr[i++].surname,buffer->arr[i+
+].id,buffer->arr[i++].salary);
}
else
{
while(i <= MAX-1)
printf("%s %s %s %f",
buffer->arr[i++].name,buffer->arr[i++].surname,buffer->arr[i+
+].id,buffer->arr[i++].salary);
i=0;
while(i <= buffer->rear)
printf("%s %s %s %f",
Document Page
buffer->arr[i++].name,buffer->arr[i++].surname,buffer->arr[i+
+].id,buffer->arr[i++].salary);
}
return 1;
}
void readFromFile()
{
FILE *f;
C_BUFFER buffer;
EMPLOYEE emp;
while(fread(&emp,sizeof(EMPLOYEE),1,f))
printBuffer(&buffer);
fclose(f);
}
void readEmployee(EMPLOYEE *emp)
{
printf("Name: ");
scanf("%s",emp->name);
printf("Surname: ");
scanf("%s",emp->surname);
printf("ID: ");
scanf("%s",emp->id);
Document Page
}
int main(int argc,char **argv)
{
C_BUFFER buffer;
EMPLOYEE emp;
buffer.front=buffer.rear=-1;
char option;
printf("Option 'I': insert element to buffer.\n");
printf("Option 'D': delete element from buffer.\n");
printf("Option 'P': print buffer.\n");
printf("Option '0': exit.\n");
while(1)
{
fflush(stdin);
printf("\nEnter option: ");
scanf("%c",&option);
if(option == 'I')
{
if(insertToBuffer(&buffer,&emp) == 0)

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
printf("Buffer is full.");
else if(insertToBuffer(&buffer,&emp))
{
printf("Enter data: \n");
readEmployee(&emp);
}
}
else if(option == 'D')
{
if(deleteFromBuffer(&buffer,&emp) == 0)
printf("Buffer is empty.");
else if(deleteFromBuffer(&buffer,&emp))
printf("Data about employee %s %s are deleted.",emp.name,emp.surname);
}
else if(option == 'P')
{
if(printBuffer(&buffer) == 0)
printf("Buffer is empty.");
else printBuffer(&buffer);
}
else if(option == '0')
{
Document Page
printf("End.");
break;
}
else printf("Unknown option");
}
return 0;
}
Output
Document Page
2. Conclusion
In this report, all the tasks are accomplished. The producer and consumer are able to
share memory, then send and receive the messages. For the producer’s message delay key is
also created with the help of Nano sleep. Further, for the producer and consumer the
semaphore is implemented. At last, the task of creating a circular buffer depending on the
character is completed.
References
Ahmad, A. (2010). Operating system. New Delhi: Knowledge Book Distributors.
Hogan, C. and Epping, D. (2014). Essential Virtual SAN. Upper Saddle River, N.J., VM
Ware Press.
Kothari, D., Vasudevan Shriram, K., R M D, S. and V, S. (2016). Linux. London: New
Academic Science.
Leva, A., Maggio, M., Papadopoulos, A. and Terraneo, F. (2013). Control-Based
Operating System Design. Stevenage: IET.
Rule, D. (2011). How to Cheat at Configuring VmWare ESX Server. Burlington: Elsevier
Science.
Singh. (2011). Operating system. [Place of publication not identified]: Global Vision
Publishing.
1 out of 34
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]