You are on page 1of 2

Problem Set #3 Shared Memory and Semaphores

Moti Geva December 18, 2011

Problem #1 Parallel Sort

You are required to sort the content of a le in parallel. You will receive a le name (argv[1]) which you should read into a shared memory segment. The read content should be regarded as an integer array. Each item in the array can have a size of 1, 2, 4 or 8 bytes and can be either signed or unsigned. Size and sign are given in argv[3] and argv[4] respectively. All integers in the array are of the same size and sign. You should sort the integer array (in the shared memory) using the number of processes given in argv[2]. Finally, when the sorting has nished, the shared memory segment should be printed to stdout.

1.1

Input

1. argv[1] - le name. 2. argv[2] - number of parallel processes. 3. argv[3] - item size (can be 1, 2, 4, or 8). 4. argv[4] - signed or unsigned.

1.2

Output

The sorted array printed to stdout.

Notes
1. File size can be obtained using stat(), lseek() or by any other means. 2. Upon any system error you should use perror("< f ailed call >") followed by exit(-1).

Problem #2 Shared Memory FIFO

Write a queue (FIFO) of bytes (stream) using shared memory which has a predened amount memory. Each byte in the stream is read a pre-dened number of times by dierent readers. For example, the FIFO can be 1MB in size, and read by 6 dierent readers. Note that eciency is important, that is, you should try to limit the number of system calls and mutual exclusions. Your code should be in the form of a library, and expose the following functions: SYNOPSIS int alloc fifo(const int key, const int size, const int readers, const int writers); int write to fifo(const int fo id, const void *bu, const int len); int read from fifo(const int fo id, const int reader id, void *bu, const int maxsize); int free fifo(const int fifo id); DESCRIPTION alloc fifo allocates a new FIFO of size size bytes, to which the number of readers is readers, and number of writers is writers. The function returns the fo id of the allocated FIFO or -1 on failure. Note that the key should be used as the shared memorys key. write to fifo writes buer bu of length len to the FIFO identied by fo id. Return values and behavior is similar to pipe. read from fifo reads from the FIFO fo id to buer bu maximum of maxlen bytes. The reader id identies the reader, and should be 0 reader id < readers. Return values and behavior is similar to pipe. free fifo Releases the resources of the FIFO identied by fo id.

3.1

Notes

1. Reading and writing is on stream based, that is, if a writer has written two items of 100B each and a reader made two reads of 10B, then the rst read returns the rst 10B of the rst item and the second read returns the next 10B of the rst item. 2. You may assume the limit of readers and writers combined is 32 (see ipcs -l). 3. Eciency will be tested, however, rst make sure your code works and only then worry about eciency. You may want to optimize your code when having only a single writer (specically in terms of mutual exclusions). 4. In case of failure, use perror() with the failed call and return -1, i.e., dont exit and dont change errno.

Good luck!
2

You might also like