Using Semaphores

Create a directory named semaphores in your course repository.  In the semaphores directory write a program that does the following.

Create a program in a file named parent.c that creates a single semaphore and a 32-byte segment of shared memory.  The program spawns two child processes; one to run a program named reader and the other to run a program named writer.  The parent should pass both the semaphore id and shared memory id to both child programs. The program should be compiled in an executable named parent.

Create a program in a file named reader.c that gains access to both the semaphore and the shared memory.  The program should create an infinite loop and query the semaphore. When the value of the semaphore is 0, it should print to standard out the string held in the shared memory segment and then change the value of the semaphore to 1. The program should be compiled in an executable named reader.

Create a program in a file named writer.c that gains access to both the semaphore and the shared memory.  The program should create an infinite loop and query the semaphore.  When the value of the semaphore is 1, it should prompt the user to enter a string, read in the string and store it in the shared memory segment.  After writing the string to shared memory, the program should change the value of the semaphore to 0.The program should be compiled in an executable named writer.

Scheduling

A thread is the smallest sequence of instructions that can be managed independently by a scheduler.  Each process has at least one thread of execution, but may have more, each running concurrently.  A set of threads in a process share some of the resources of the process including the code and address space.  Next week we’ll look at the pthreads library on Linux in detail.  Today, however, we’ll investigate the various scheduling algorithms that are built into the Linux kernel and the various system calls that can be used to modify and query how a process is scheduled.

The Linux man pages are divided into 8 sections (https://www.kernel.org/doc/man-pages/).  During the first half of the semester we’ve been querying functions in sections 2 and 3.  Today, I’d like to draw your attention to a file on scheduling in section 7.  Please study the sched(7) man page and then answer the following questions.

1. What is a scheduler?
2. How does the scheduler make its decisions?
3. What are the normal scheduling policies?
4. What are the real time scheduling policies?
5. What must sched_priority be set to for the normal scheduling policies?
6. What values can sched_priority be set to for the real-time policies?
7. What methods can be used to get the min and max sched_priority values for a specific
scheduling policy?
8. In general, how does a scheduler choose which thread to run?
9. The scheduler is preemptive. What does that mean?
10. Which threads will a thread with SCHED_FIFO scheduling preempt?
11. When a thread with SCHED_FIFO scheduling becomes runnable, where is it inserted in
the queue of runnable threads.
12. What does sched_yeild() do when called in a thread scheduled as SCHED_FIFO?
13. How long does a thread with SCHED_FIFO scheduling run?
14. What is the difference between SCHED_FIFO and SCHED_RR?
15. How can you programmatically determine the time quantum for a thread scheduled as
SCHED_RR?
16. What is a sporadic task?
17. What does the kernel do to ensure deadline scheduling guarantees for threads
scheduled as SCHED_DEADLINE?
18. Which threads to SCHED_DEADLINE threads preempt?
19. Which scheduling algorithm does Linux use by default?
20. What must the static priority be set to for threads scheduled as SCHED_OTHER? 21. How is the dynamic priority of a thread determined for threads scheduled as
SCHED_OTHER?
22. How can the nice value of a thread be set programmatically?
23. What is the range of nice values?
24. For threads scheduled as SCHED_OTHER, how does the nice value affect the time slices
for the treads?
25. What is the difference between SCHED_BATCH and SCHED_OTHER?
26. Describe the SCHED_IDLE scheduling algorithm.
27. Name all of the scheduling algorithms provided by the Linux kernel.
28. How do you programmatically prevent child processes from inheriting its parent
process’ privileged scheduling policy?
29. If the reset-on-fork flag is set and the calling thread has a scheduling policy of
SCHED_FIFO or SCHED_RR, what will the policy be for a child process?
30. What version of the kernel is running on cs.bridgewater.edu?
31. What determines if a thread is privileged or not?
32. For the kernel running on cs.bridgewater.edu, what changes can an unprivileged thread
make to affect its scheduling?
33. What are the techniques used to prevent a runaway real-time or deadline process?