Lab Assignment--Semaphores Part 1


(Please do this lab in LINUX)

Objectives:

Part 1-- Working with Unions

  1. Create a union capable of holding:
  2. Declare a union variable
  3. Assign "hello" to the char array part of the union variable
  4. Print this char array (using the union) and use the sizeof function to see the size of the pointer to char array within the union
  5. Assign 5.557111111111111 to the double part of the union variable
  6. Print this double (using the union) and use the sizeof function to see the size of the double within the union
    Hint: you might want to use printf("%10.15f",arg.dbl) to see the full length
  7. Assign the pointer to an int array to a declared array of three ints with values 1937006915, 1668248096, and 8555
  8. Print the three values in the array and the size of the pointer to an array of ints
  9. Reprint the char array, double and int array to see what the values are now.
  10. Print the size of the union

Questions

  1. In the reprint of the char array, double, and int. What do you notice? Is the result the same on Hercules as in Linux in CL122? Explain what you see.
  2. What is the size of the union and why?

Part 2-- Creating and working with Semaphores

Share a semaphore between a child and parent. Note: this program is not yet implementing semaphores for restricting access on a resource.
  1. Create a set of four semaphores
  2. Use fork to create a parent and child
  3. In the child:
    1. use IPC_STAT to get the number of semaphores in the set
    2. use this number to cycle through all of the semaphores in the set
    3. Set the values individually using SETVAL
  4. In the parent:
    1. wait for the child to finish to guarantee that the semaphore has been set
    2. use GETALL to get all the values of the semaphores in the set
    3. remove the semaphore using IPC_RMID