COMP9812 Operating Systems: Interprocess Communication & Solution

Verified

Added on  2023/06/03

|30
|2055
|230
Practical Assignment
AI Summary
This assignment solution focuses on interprocess communication (IPC) and synchronisation within an operating system context. It tackles the producer-consumer problem through several tasks. These tasks include creating and destroying shared memory segments, sending single and multiple messages between producer and consumer processes, implementing variable producer delays, utilizing semaphores for synchronisation, and constructing a character-based circular buffer. The code examples are provided in C programming language and are designed for a Unix/Linux environment, demonstrating practical applications of IPC concepts like shared memory and mutual exclusion. This solution would be helpful for students studying operating systems concepts and seeking practical implementation examples.
Document Page
Operating System
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Table of Contents
Task 1: Create and destroy a shared memory segment........................................................1
Task 2: Send a single message from producer to consumer.................................................1
Task 3: Send a series of messages between producer and consumer..................................1
Task 4: Variable producer delay............................................................................................1
Task 5: Implement semaphores..............................................................................................1
Task 6: Character-based circular buffer...............................................................................1
1
Document Page
Task 1: Create and destroy a shared memory segment
Producer.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<time.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;
2
Document Page
*s = 0;
{
int nanosleep(const struct timespec *req,struct timespec *rem);
{
while(*shm != '*')
nanosleep (1);
return 0;
}
}
}
Output
3
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4
Document Page
Task 2: Send a single message from producer to consumer
Consumer.c
#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);
5
Document Page
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;
}
6
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Output
7
Document Page
Task 3: Send a series of messages between producer and consumer
Producer.c (send message)
Producer.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<time.h>
#define SHSIZE 100
int main(int argc,char*argv[])
{
int shmid;
key_t key;
char * shm;
8
Document Page
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;
{
int nanosleep(const struct timespec *req,struct timespec *rem);
{
while(*shm != '*')
nanosleep (1);
return 0;
}
}
}
9
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Output
Consumer.c (receive message)
Consumer.c
#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[])
10
Document Page
{
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;
}
11
chevron_up_icon
1 out of 30
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]