SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
Operating System Labs: Shared Memory

Purpose

As seen in the lab on synchronization methods, shared blocks of memory are a common solution
when programing. A block of memory is 'shared' if it is mapped into the address space of two or
process. This is different than having threads started by a process and sharing certain variables.
Although care is required to synchronize access to this block of memory, this is a useful
technique for applications were it is not practical to move/copy large chunks of data around. In
some cases it is faster to just work on the same buffer. On the other hand, we can also use the
buffer itself to move chunks of data between processes in a producer/consumer relationship.

When a process stores information into a location in the shared memory, it can be read by any
other process that is using the same segment of shared memory. Within an individual computer,
uniprocessor or multiprocessor, shared memory is normally the fastest way for two process to
share information.

Objectives

       Get an introduction to programming with shared memory
       Use the shared memory to solve a typical IPC problem

Procedure

The shared memory API commonly used in contemporary versions of POSIX was introduced in
System V Unix. Even though the mechanism allows multiple processes to map a common
memory segment into their own address spaces – it is logically part of the memory manager – it
was designed and implemented in System V Unix as part of the IPC (inter-process
communication) mechanism.

The Shared Memory API

Shared memory can be used to allow any process to dynamically define a new block of new that
is independent of the address space created for the process before it began to execute. Every
Linux process is created with a large virtual address space, only a portion of which is used to
reference the compiled code, static data, stack, and heap. The remaining addresses in the virtual
address space are initially unused. A new block of shared memory, once defined, is mapped into
a block of the unused virtual addresses, after which the process can read and write the shared
memory as if it were ordinary memory. Of course, more than one process can map the shared
memory block into its own address space, so code segments that read or write shared memory are
normally considered to be critical sections (and should be protected by a synchronization
primitive such as a mutex, a semaphore or a monitor as we've seen in the bounded buffer lab).

The following four system calls define the kernel interface for shared memory support.

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>



Page 1 of 4
int shmget(key_t key, int size, int shmflg);
void *shmat(int shmid, const void *shmaddr, int shmflg);
void *shmdt(char *shaddr);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);

       shmget() creates the shared memory block.
       shmat() maps an existing shared memory block into a process's address space.
       shmdt() removes (unmaps) a shared memory block from the process's address space.
       shmctl() is a general-purpose function (in the style of ioctl()) used to perform all other
        commands to the shared memory block.

To create a new block of shared memory, the process must call shmget(). If shmget()
successfully creates a new block of memory, then it returns a shared memory identifier of type
int. The shared memory identifier is a handle into a kernel data structure.

The first arguments to shmget() is of type key_t. This may be either the key of an existing
memory block, 0, or IPC_PRIVATE. When it is IPC_PRIVATE, the shmget() call creates a new
block of shared memory. When it is 0, and the IPC_CREAT flag is set in the third argument,
shmflg, a new block of memory will be created. A process that wants to reference a shared
memory block that was created by another process can set the key argument to the key value of
an existing memory block. The shmflg also defines the access permissions for user, group, and
world accesses to the memory block as its lower 9 bits (by using the same bit pattern as for file
protection). The second argument specifies the size of the new block of memory. All memory
allocation operations are in terms of pages. That is, if a process requests 1 byte of memory, then
the memory manager allocates a full page (PAGE_SIZE = 4,096 bytes on i386 machines). Thus
the size of the new shared memory block will be the value of the size argument rounded up to the
next multiple of the page size. A size of 1 to 4,096 results in a 4K (one-page) block, 4,097 to
8,192 results in an 8K (two-page) block, and so on.

Often different processes want to attach to the same memory segment, which requires that both of
them use the same key. A useful function in this situation is:
key_t ftok(const char *pathname, int proj_id);
which converts a pathname (which must refer to an existing, accessible file) and a project ID (a
nonzero integer) to a key.

The void *shmat(int shmid, const void*shmaddr, int shmflg) system call maps the memory block
into the calling process's address space. The shmid argument is the result returned by the
shmget() call that created the block. The shmaddr pointer is the virtual address to which the first
location in the shared memory block should be mapped. The value of shmaddr should be aligned
with a page boundary. If the calling process does not wish to choose the address to which the
memory block will be mapped, it should pass a value of 0 for shmaddr and the operating system
will choose a suitable address at which to attach the memory segment. If shmaddr is specified,
and SHM_RND is asserted in shmflg, then the address will be rounded down to a multiple of the
SHMLBA constant. The shmflg is used in the same way as the corresponding flag in shmget(),
that is, to assert a number of different 1-bit flags to the shmat() system call. In addition to
asserting the SHM_RND flag, the calling process can assert SHM_RDONLY to attach the
memory block so that it can be only read, and not also written.

When a process finishes using a shared memory block, it calls void *shmdt(char *shaddr), where

Page 2 of 4
shaddr is the address used to attach the memory block. Note that the call to shmdt only detaches
the memory segment from the address space of the current process, it does not delete the shared
memory segment (even if the process detached was the last process using it). To delete the shared
memory segment shmctl() must be used.

The final shared memory call is int shmctl(int shmid, int cmd, struct shmid_ds *buf). This call
performs control operations on the shared memory block descriptor. The shmid argument
identifies the shared memory block, the cmd argument specifies the command to be applied and
the buf argument represent a pointer to a structure containing the different properties to be read
from or applied to the shared memory segment. Check the manual page of shmctl to see the
possible values of the cmd argument and the effect of each of them. If the cmd argument is
IPC_RMID, then the shared memory segment is removed (if it's not in use) or it is marked for
removal when the last process detaches from it.

Here is a simple example of creating a block of shared memory:

#include <sys/shm.h>

#define SHM_SIZE 2000
...
int shm_handle;
char* shm_ptr;

// create shared memory
shm_handle = shmget(IPC_PRIVATE,SHM_SIZE,IPC_CREAT | 0644);

// valid handle?
if(shm_handle == -1) {
        printf("shared memory creation failedn");
        exit(0);
}

// attach to shared block, see man pages for detail
shm_ptr = (char*)shmat(shm_handle,0,0);
if (shm_ptr == (char *) -1) {
      printf("Shared memory attach failed");
      exit(0);
}

// do work, share results through the shared memory segment
// have other processes start and also use the same shared memory
...

// detach from the shared memory segment
shmdt(shm_ptr);

// remove the shared memory segment
shmctl(shm_handle,IPC_RMID,0);




Page 3 of 4
Semaphores

Like in the case of any shared resource, the access to the shared memory must be synchronized. A
simple way of synchronizing the access to shared memory regions is to use semaphores. A
semaphore is a synchronization primitive represented by an integer value on which two
operations can be performed: signal and wait. These are both atomic operations,            signal
increments the value of the semaphore and wait decrements the value of the semaphore. If the
value of the semaphore is zero, the process that performs a wait operation on that semaphore will
block until another process performs a signal operation on the semaphore.

The POSIX standard specifies the following functions for working with semaphores:

sem_open() - initializes and opens a new semaphore
sem_t *sem_open(const char *name, int oflag,
                  mode_t mode, unsigned int value);

sem_wait() - decrements and locks the semaphore (the wait operation)
int sem_wait(sem_t *sem);

sem_post() - increments and unlocks the semaphore (the signal operation)
int sem_post(sem_t *sem);

Check the manual pages of this functions for additional information on how to use them.


Assignment

We have already done a lab about the producer-consumer problem. In this assignment you have
to solve this problem again, but this time using a shared memory region as the common buffer
shared between two distinct processes (last time we used two threads within the same process)
and semaphores for synchronizing the access to the memory region.

You will have to create two distinct programs, producer and consumer each of them taking a
single argument, the name of a file:
$ ./producer input_file
$ ./consumer output_file
The producer will read data from the input_file and store it into the shared buffer and the
consumer will read the data from the shared buffer and save it into the output_file. Both processes
repeat this until the entire content of the input file has been transferred to the output file, after
which they terminate. At the end, the output_file should be identical to the input file.
The input_file can be any type of file (image, audio or video file), so you shouldn't assume that
it's a text file.

Deliverables

Follow all given directions for submitting your code, makefile and “readme.txt”. Name your
source files producer.c (or producer.cc) and consumer.c (or consumer.cc).




Page 4 of 4

Contenu connexe

Tendances

CHAP7.DOC.doc
CHAP7.DOC.docCHAP7.DOC.doc
CHAP7.DOC.docbutest
 
Cassandra Compression and Performance Evaluation
Cassandra Compression and Performance EvaluationCassandra Compression and Performance Evaluation
Cassandra Compression and Performance EvaluationSchubert Zhang
 
CUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : NotesCUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : NotesSubhajit Sahu
 
Porting MPEG-2 files on CerberO, a framework for FPGA based MPSoc
Porting MPEG-2 files on CerberO, a framework for FPGA based MPSocPorting MPEG-2 files on CerberO, a framework for FPGA based MPSoc
Porting MPEG-2 files on CerberO, a framework for FPGA based MPSocadnanfaisal
 
unix interprocess communication
unix interprocess communicationunix interprocess communication
unix interprocess communicationguest4c9430
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxSoumen Santra
 
Python Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inPython Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inLearnbayin
 
java memory management & gc
java memory management & gcjava memory management & gc
java memory management & gcexsuns
 

Tendances (20)

CHAP7.DOC.doc
CHAP7.DOC.docCHAP7.DOC.doc
CHAP7.DOC.doc
 
Cassandra Compression and Performance Evaluation
Cassandra Compression and Performance EvaluationCassandra Compression and Performance Evaluation
Cassandra Compression and Performance Evaluation
 
Pthread
PthreadPthread
Pthread
 
Memory model
Memory modelMemory model
Memory model
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
CUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : NotesCUDA by Example : Advanced Atomics : Notes
CUDA by Example : Advanced Atomics : Notes
 
Computer Science Homework Help
Computer Science Homework HelpComputer Science Homework Help
Computer Science Homework Help
 
Porting MPEG-2 files on CerberO, a framework for FPGA based MPSoc
Porting MPEG-2 files on CerberO, a framework for FPGA based MPSocPorting MPEG-2 files on CerberO, a framework for FPGA based MPSoc
Porting MPEG-2 files on CerberO, a framework for FPGA based MPSoc
 
Operating System Assignment Help
Operating System Assignment HelpOperating System Assignment Help
Operating System Assignment Help
 
Cuda 2
Cuda 2Cuda 2
Cuda 2
 
unix interprocess communication
unix interprocess communicationunix interprocess communication
unix interprocess communication
 
Threads Advance in System Administration with Linux
Threads Advance in System Administration with LinuxThreads Advance in System Administration with Linux
Threads Advance in System Administration with Linux
 
Sysprog 14
Sysprog 14Sysprog 14
Sysprog 14
 
Python Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inPython Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.in
 
P-Threads
P-ThreadsP-Threads
P-Threads
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
System Programming - Threading
System Programming - ThreadingSystem Programming - Threading
System Programming - Threading
 
cpu-affinity
cpu-affinitycpu-affinity
cpu-affinity
 
java memory management & gc
java memory management & gcjava memory management & gc
java memory management & gc
 

En vedette

Adv420 final
Adv420 finalAdv420 final
Adv420 finalshipleyl
 
Magic submitter + addon 2012 برنامج
Magic submitter + addon 2012 برنامجMagic submitter + addon 2012 برنامج
Magic submitter + addon 2012 برنامجMarik Alaa-ddine
 
Toms Poetry Anthology
Toms Poetry AnthologyToms Poetry Anthology
Toms Poetry Anthologypun4000
 
A0 ad276c eacf-6f38-e32efa1adf1e36cc
A0 ad276c eacf-6f38-e32efa1adf1e36ccA0 ad276c eacf-6f38-e32efa1adf1e36cc
A0 ad276c eacf-6f38-e32efa1adf1e36ccPrabhu Prabhu
 
Chapter 2 Objectives of Performance appraisal
Chapter 2 Objectives of Performance appraisalChapter 2 Objectives of Performance appraisal
Chapter 2 Objectives of Performance appraisalmahmoodku
 
4 cas traumatismes oculars
4 cas traumatismes oculars4 cas traumatismes oculars
4 cas traumatismes ocularsformaciocamfic
 
大家一起學Python
大家一起學Python大家一起學Python
大家一起學PythonLun Dong
 
galeria de imagenes
galeria de imagenesgaleria de imagenes
galeria de imagenesyonaleth
 
Maila rosario colleg (technical writing)
Maila  rosario colleg (technical writing)Maila  rosario colleg (technical writing)
Maila rosario colleg (technical writing)Jane Tumanguil
 
Visual Resume
Visual ResumeVisual Resume
Visual ResumeMaxxClark
 
Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...
Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...
Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...Marie Caroline Estoque
 
системы счисления
системы счислениясистемы счисления
системы счисленияkozarezov94
 
Performance Appraisal defined and tools Chapter -1
Performance Appraisal defined and tools Chapter -1Performance Appraisal defined and tools Chapter -1
Performance Appraisal defined and tools Chapter -1mahmoodku
 
Performance Appraisal and its objectives by mahmood qasim
Performance  Appraisal and its objectives by mahmood qasimPerformance  Appraisal and its objectives by mahmood qasim
Performance Appraisal and its objectives by mahmood qasimmahmoodku
 
Nerissa Arendela Bocalan
Nerissa Arendela BocalanNerissa Arendela Bocalan
Nerissa Arendela BocalanErisse Vee
 

En vedette (20)

Adv420 final
Adv420 finalAdv420 final
Adv420 final
 
Slidecast
SlidecastSlidecast
Slidecast
 
Magic submitter + addon 2012 برنامج
Magic submitter + addon 2012 برنامجMagic submitter + addon 2012 برنامج
Magic submitter + addon 2012 برنامج
 
Galeria
GaleriaGaleria
Galeria
 
Slidecast final
Slidecast finalSlidecast final
Slidecast final
 
Toms Poetry Anthology
Toms Poetry AnthologyToms Poetry Anthology
Toms Poetry Anthology
 
Icd 10
Icd 10Icd 10
Icd 10
 
A0 ad276c eacf-6f38-e32efa1adf1e36cc
A0 ad276c eacf-6f38-e32efa1adf1e36ccA0 ad276c eacf-6f38-e32efa1adf1e36cc
A0 ad276c eacf-6f38-e32efa1adf1e36cc
 
Chapter 2 Objectives of Performance appraisal
Chapter 2 Objectives of Performance appraisalChapter 2 Objectives of Performance appraisal
Chapter 2 Objectives of Performance appraisal
 
4 cas traumatismes oculars
4 cas traumatismes oculars4 cas traumatismes oculars
4 cas traumatismes oculars
 
大家一起學Python
大家一起學Python大家一起學Python
大家一起學Python
 
galeria de imagenes
galeria de imagenesgaleria de imagenes
galeria de imagenes
 
Maila rosario colleg (technical writing)
Maila  rosario colleg (technical writing)Maila  rosario colleg (technical writing)
Maila rosario colleg (technical writing)
 
Visual Resume
Visual ResumeVisual Resume
Visual Resume
 
Ijcta2011020338
Ijcta2011020338Ijcta2011020338
Ijcta2011020338
 
Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...
Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...
Cdocumentsandsettingssompong y-hosal04nbmydocumentsprincipleofsuccess-1008080...
 
системы счисления
системы счислениясистемы счисления
системы счисления
 
Performance Appraisal defined and tools Chapter -1
Performance Appraisal defined and tools Chapter -1Performance Appraisal defined and tools Chapter -1
Performance Appraisal defined and tools Chapter -1
 
Performance Appraisal and its objectives by mahmood qasim
Performance  Appraisal and its objectives by mahmood qasimPerformance  Appraisal and its objectives by mahmood qasim
Performance Appraisal and its objectives by mahmood qasim
 
Nerissa Arendela Bocalan
Nerissa Arendela BocalanNerissa Arendela Bocalan
Nerissa Arendela Bocalan
 

Similaire à Lab8

Chapter 8 : Memory
Chapter 8 : MemoryChapter 8 : Memory
Chapter 8 : MemoryAmin Omi
 
ECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docxECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docxtidwellveronique
 
ECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docxECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docxtidwellveronique
 
ECECS 472572 Final Exam ProjectRemember to check the errata
ECECS 472572 Final Exam ProjectRemember to check the errata ECECS 472572 Final Exam ProjectRemember to check the errata
ECECS 472572 Final Exam ProjectRemember to check the errata EvonCanales257
 
Please do ECE572 requirementECECS 472572 Final Exam Project (W.docx
Please do ECE572 requirementECECS 472572 Final Exam Project (W.docxPlease do ECE572 requirementECECS 472572 Final Exam Project (W.docx
Please do ECE572 requirementECECS 472572 Final Exam Project (W.docxARIV4
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagementpradeepelinux
 
Chapter 8 memory-updated
Chapter 8 memory-updatedChapter 8 memory-updated
Chapter 8 memory-updatedDelowar hossain
 
Bab 4
Bab 4Bab 4
Bab 4n k
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationRJ Mehul Gadhiya
 
SO-Memoria.pdf
SO-Memoria.pdfSO-Memoria.pdf
SO-Memoria.pdfKadu37
 
Operating system Memory management
Operating system Memory management Operating system Memory management
Operating system Memory management Shashank Asthana
 
maXbox Starter 42 Multiprocessing Programming
maXbox Starter 42 Multiprocessing Programming maXbox Starter 42 Multiprocessing Programming
maXbox Starter 42 Multiprocessing Programming Max Kleiner
 
Operating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementOperating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementAjit Nayak
 

Similaire à Lab8 (20)

Chapter 8 : Memory
Chapter 8 : MemoryChapter 8 : Memory
Chapter 8 : Memory
 
ECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docxECECS 472572 Final Exam ProjectRemember to check the errat.docx
ECECS 472572 Final Exam ProjectRemember to check the errat.docx
 
ECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docxECECS 472572 Final Exam ProjectRemember to check the err.docx
ECECS 472572 Final Exam ProjectRemember to check the err.docx
 
ECECS 472572 Final Exam ProjectRemember to check the errata
ECECS 472572 Final Exam ProjectRemember to check the errata ECECS 472572 Final Exam ProjectRemember to check the errata
ECECS 472572 Final Exam ProjectRemember to check the errata
 
Please do ECE572 requirementECECS 472572 Final Exam Project (W.docx
Please do ECE572 requirementECECS 472572 Final Exam Project (W.docxPlease do ECE572 requirementECECS 472572 Final Exam Project (W.docx
Please do ECE572 requirementECECS 472572 Final Exam Project (W.docx
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagement
 
Chapter 8 memory-updated
Chapter 8 memory-updatedChapter 8 memory-updated
Chapter 8 memory-updated
 
Opetating System Memory management
Opetating System Memory managementOpetating System Memory management
Opetating System Memory management
 
Bab 4
Bab 4Bab 4
Bab 4
 
Memory management in linux
Memory management in linuxMemory management in linux
Memory management in linux
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
SO-Memoria.pdf
SO-Memoria.pdfSO-Memoria.pdf
SO-Memoria.pdf
 
SO-Memoria.pdf
SO-Memoria.pdfSO-Memoria.pdf
SO-Memoria.pdf
 
Operating system Memory management
Operating system Memory management Operating system Memory management
Operating system Memory management
 
Chap7
Chap7Chap7
Chap7
 
NUMA
NUMANUMA
NUMA
 
Linux memory
Linux memoryLinux memory
Linux memory
 
maXbox Starter 42 Multiprocessing Programming
maXbox Starter 42 Multiprocessing Programming maXbox Starter 42 Multiprocessing Programming
maXbox Starter 42 Multiprocessing Programming
 
Operating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementOperating Systems Part III-Memory Management
Operating Systems Part III-Memory Management
 
Nachos 2
Nachos 2Nachos 2
Nachos 2
 

Lab8

  • 1. Operating System Labs: Shared Memory Purpose As seen in the lab on synchronization methods, shared blocks of memory are a common solution when programing. A block of memory is 'shared' if it is mapped into the address space of two or process. This is different than having threads started by a process and sharing certain variables. Although care is required to synchronize access to this block of memory, this is a useful technique for applications were it is not practical to move/copy large chunks of data around. In some cases it is faster to just work on the same buffer. On the other hand, we can also use the buffer itself to move chunks of data between processes in a producer/consumer relationship. When a process stores information into a location in the shared memory, it can be read by any other process that is using the same segment of shared memory. Within an individual computer, uniprocessor or multiprocessor, shared memory is normally the fastest way for two process to share information. Objectives  Get an introduction to programming with shared memory  Use the shared memory to solve a typical IPC problem Procedure The shared memory API commonly used in contemporary versions of POSIX was introduced in System V Unix. Even though the mechanism allows multiple processes to map a common memory segment into their own address spaces – it is logically part of the memory manager – it was designed and implemented in System V Unix as part of the IPC (inter-process communication) mechanism. The Shared Memory API Shared memory can be used to allow any process to dynamically define a new block of new that is independent of the address space created for the process before it began to execute. Every Linux process is created with a large virtual address space, only a portion of which is used to reference the compiled code, static data, stack, and heap. The remaining addresses in the virtual address space are initially unused. A new block of shared memory, once defined, is mapped into a block of the unused virtual addresses, after which the process can read and write the shared memory as if it were ordinary memory. Of course, more than one process can map the shared memory block into its own address space, so code segments that read or write shared memory are normally considered to be critical sections (and should be protected by a synchronization primitive such as a mutex, a semaphore or a monitor as we've seen in the bounded buffer lab). The following four system calls define the kernel interface for shared memory support. #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> Page 1 of 4
  • 2. int shmget(key_t key, int size, int shmflg); void *shmat(int shmid, const void *shmaddr, int shmflg); void *shmdt(char *shaddr); int shmctl(int shmid, int cmd, struct shmid_ds *buf);  shmget() creates the shared memory block.  shmat() maps an existing shared memory block into a process's address space.  shmdt() removes (unmaps) a shared memory block from the process's address space.  shmctl() is a general-purpose function (in the style of ioctl()) used to perform all other commands to the shared memory block. To create a new block of shared memory, the process must call shmget(). If shmget() successfully creates a new block of memory, then it returns a shared memory identifier of type int. The shared memory identifier is a handle into a kernel data structure. The first arguments to shmget() is of type key_t. This may be either the key of an existing memory block, 0, or IPC_PRIVATE. When it is IPC_PRIVATE, the shmget() call creates a new block of shared memory. When it is 0, and the IPC_CREAT flag is set in the third argument, shmflg, a new block of memory will be created. A process that wants to reference a shared memory block that was created by another process can set the key argument to the key value of an existing memory block. The shmflg also defines the access permissions for user, group, and world accesses to the memory block as its lower 9 bits (by using the same bit pattern as for file protection). The second argument specifies the size of the new block of memory. All memory allocation operations are in terms of pages. That is, if a process requests 1 byte of memory, then the memory manager allocates a full page (PAGE_SIZE = 4,096 bytes on i386 machines). Thus the size of the new shared memory block will be the value of the size argument rounded up to the next multiple of the page size. A size of 1 to 4,096 results in a 4K (one-page) block, 4,097 to 8,192 results in an 8K (two-page) block, and so on. Often different processes want to attach to the same memory segment, which requires that both of them use the same key. A useful function in this situation is: key_t ftok(const char *pathname, int proj_id); which converts a pathname (which must refer to an existing, accessible file) and a project ID (a nonzero integer) to a key. The void *shmat(int shmid, const void*shmaddr, int shmflg) system call maps the memory block into the calling process's address space. The shmid argument is the result returned by the shmget() call that created the block. The shmaddr pointer is the virtual address to which the first location in the shared memory block should be mapped. The value of shmaddr should be aligned with a page boundary. If the calling process does not wish to choose the address to which the memory block will be mapped, it should pass a value of 0 for shmaddr and the operating system will choose a suitable address at which to attach the memory segment. If shmaddr is specified, and SHM_RND is asserted in shmflg, then the address will be rounded down to a multiple of the SHMLBA constant. The shmflg is used in the same way as the corresponding flag in shmget(), that is, to assert a number of different 1-bit flags to the shmat() system call. In addition to asserting the SHM_RND flag, the calling process can assert SHM_RDONLY to attach the memory block so that it can be only read, and not also written. When a process finishes using a shared memory block, it calls void *shmdt(char *shaddr), where Page 2 of 4
  • 3. shaddr is the address used to attach the memory block. Note that the call to shmdt only detaches the memory segment from the address space of the current process, it does not delete the shared memory segment (even if the process detached was the last process using it). To delete the shared memory segment shmctl() must be used. The final shared memory call is int shmctl(int shmid, int cmd, struct shmid_ds *buf). This call performs control operations on the shared memory block descriptor. The shmid argument identifies the shared memory block, the cmd argument specifies the command to be applied and the buf argument represent a pointer to a structure containing the different properties to be read from or applied to the shared memory segment. Check the manual page of shmctl to see the possible values of the cmd argument and the effect of each of them. If the cmd argument is IPC_RMID, then the shared memory segment is removed (if it's not in use) or it is marked for removal when the last process detaches from it. Here is a simple example of creating a block of shared memory: #include <sys/shm.h> #define SHM_SIZE 2000 ... int shm_handle; char* shm_ptr; // create shared memory shm_handle = shmget(IPC_PRIVATE,SHM_SIZE,IPC_CREAT | 0644); // valid handle? if(shm_handle == -1) { printf("shared memory creation failedn"); exit(0); } // attach to shared block, see man pages for detail shm_ptr = (char*)shmat(shm_handle,0,0); if (shm_ptr == (char *) -1) { printf("Shared memory attach failed"); exit(0); } // do work, share results through the shared memory segment // have other processes start and also use the same shared memory ... // detach from the shared memory segment shmdt(shm_ptr); // remove the shared memory segment shmctl(shm_handle,IPC_RMID,0); Page 3 of 4
  • 4. Semaphores Like in the case of any shared resource, the access to the shared memory must be synchronized. A simple way of synchronizing the access to shared memory regions is to use semaphores. A semaphore is a synchronization primitive represented by an integer value on which two operations can be performed: signal and wait. These are both atomic operations, signal increments the value of the semaphore and wait decrements the value of the semaphore. If the value of the semaphore is zero, the process that performs a wait operation on that semaphore will block until another process performs a signal operation on the semaphore. The POSIX standard specifies the following functions for working with semaphores: sem_open() - initializes and opens a new semaphore sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned int value); sem_wait() - decrements and locks the semaphore (the wait operation) int sem_wait(sem_t *sem); sem_post() - increments and unlocks the semaphore (the signal operation) int sem_post(sem_t *sem); Check the manual pages of this functions for additional information on how to use them. Assignment We have already done a lab about the producer-consumer problem. In this assignment you have to solve this problem again, but this time using a shared memory region as the common buffer shared between two distinct processes (last time we used two threads within the same process) and semaphores for synchronizing the access to the memory region. You will have to create two distinct programs, producer and consumer each of them taking a single argument, the name of a file: $ ./producer input_file $ ./consumer output_file The producer will read data from the input_file and store it into the shared buffer and the consumer will read the data from the shared buffer and save it into the output_file. Both processes repeat this until the entire content of the input file has been transferred to the output file, after which they terminate. At the end, the output_file should be identical to the input file. The input_file can be any type of file (image, audio or video file), so you shouldn't assume that it's a text file. Deliverables Follow all given directions for submitting your code, makefile and “readme.txt”. Name your source files producer.c (or producer.cc) and consumer.c (or consumer.cc). Page 4 of 4