CS427-Shared-Memory

Requirement

Write a program whose main routine obtains one parameter n from the user, i.e. passed to your program when it was invoked from the shell. Your program shall then create a shared memory and a child process. The shared memory shall have a size of BUF_SZ*sizeof(long int), where BUF_SZ is defined as 6 using a macro, e.g. “#define BUF_SZ 6“.

The child process should obtain the value of n (you actually have multiple options for doing that) and create a sequence of length n, whose elements are of type long int, and implements the equation z = y! (i.e. the factorial) where y is the index of the element, for example, the fourth element (index 3) shall have a value of 3! = 3x2x1 = 6, whereas the first element (index 0) shall have a value of 0!=1 . If n=4, then the sequence shall be 1, 1, 2 and 6.

The child process shall create the elements, one at a time, and wait for a random interval of time (0 to 2.999 seconds) between generating elements of the sequence. As soon as an element is generated, the child places the element in the shared memory by organizing it as described below.

The parent process shall NOT wait for the child to exit but instead shall print an element as soon as it arrives into the shared buffer (again, in a manner similar to slides 25-28 of lecture 5)

Hint: Use fflush() to ensure printf’s are printed immediately into the screen.

  • Repeat this, except that now you do not create a shared memory, but rather use an ordinary pipe to pass the sequence instead.
  • Repeat this, except that now you do not create a shared memory or a child process, but use a child thread instead. By definition, The parent and child threads already share their memory spaces.