操作系统代写|Operating Systems 2o22F: Tutorial 6

这是一篇来自加拿大的关于处理一些程序相关实验的操作系统代写

 

A: Getting Started

1.Make sure you system has the a recent version of bcc-tools (https://github.com/iovisor/bcc/blob/master/INSTALL.md) installed.The class VM has this already installed; on other systems, you’ll have to install it yourself.
2.Download 3000pc.zip (https://homeostasis.scs.carleton.ca/~soma/os-2021f/code/3000pc.zip), unpack, and run make to compile 3000pc-fifo and 3000pc-rendezvous.
3. Note that these programs take 3 arguments each:
The number of events to process
The number of events to produce before the producer sleeps for 1 second The number of events to consume before the consumer sleeps for 1 second4.Run both programs with the same arguments of your choice. Do this a few times. Dothey behave the same way? Do you notice any differences?
5.Repeat the above experiment, this time running each program under strace (with the -fflag to trace children too). Do you notice any difference in the system calls each program makes?
6.In the next question, you will be playing around with trace from bcc-tools.As it makesuse of eBPF,a Linux kernel extension mechanism, it needs to be run with root privileges.Because the command isn’t in a standard directory in the class VMs, you’ll need to become root with sudo -i and then run trace (rather than sudo trace). We willexplore eBPF more in later tutorials.
7.Here are some example trace commands to play with. You can learn more about tracefrom its man page. Note these assume you unpacked the 3000pc.zip in the Documentsfolder.
Report on the string passed to pick_word ( on its return :

trace ‘r:/home/student/Documents/3000pc /3000pc-fifo:pick_word”%s””, arg1′
Print the buffer passed to the read function as a string (on read’ s return) for process 6312(running 3000pc-fifo,won’t work on 30o0pc-rendezvous) :
trace -p 6312 ‘r:pthread :read “%s”, arg2’
(Normally read and write are in the main C library and so would be denoted by r:c: read; however we are linkingagainst the pthread library as well and it replaces the read implementation.)
Print calls to sem_wait in process 6312 and the value of the semaphore at function entry and return.
trace -p 6312 ‘p:pthread: sem_wait(int wsem) “entry: %d”,*sem’’r:pthread:sem_wait(int *sem) “exit: %id”,*sem’
If you want to automatically trace the producer process,use
trace -p`pidof -s 3000pc-fifo` …
To trace the consumer,use
trace -p ‘pidof 3000pc-fifo | awk ‘iprint $2}’` …

trace allows you to do many things that you can do with gdb; however, it is designed to work onproduction applications and thus is very efficient and much less intrusive. In technical terms, gdb,strace, and ltrace use the ptrace system call, while trace uses eBPF.

B: Producer/Consumer with Pipes
1.Examine the source code of 3000pc-fifo. Explain the following:
a. What does the call to pipe(pipefd) on line 192 do? Hint: Look at the manpage forpipe(2)
b.How does the consumer receive words to consume?c. How does the producer send words to the consumer?
d. Why is the call to srandom(time(NULL)) on line 169 necessary?2.
3.Replace the call to srandom(time(NULL)) on line 169 with srandom(42). Whatdifferences do you notice in 3000pc-fifo’s behavior?
4. What descriptors are opened by 3000pc-fifo? What are they for and what files do thepoint to? How did you find this information? Hint: Try checking in /proc.
5.What do you think would happen if you had multiple producers and multipleconsumers in 3000pc-fifo? Would things break or work normally?
6.(Advanced) Implement multiple producers and consumers in 3000pc-fifo.Compareyour results with your answer to question 4.

C: Producer/Consumer with Shared Memory1.Examine the source code of 3000pc-rendezvous.Explain the following:

a. What are a few ways that 3000pc-rendezvous is different from 3000pc-fifo?b. What does the call to mmap on line 335 do? How did you figure this out?
c. How does the producer notify the consumer that the queue is no longer empty?d.How does the consumer notify the producer that the queue is no longer full?2.What arguments can you provide to make the producer wait for the consumer? Hint:Check the size of the queue.
3. What arguments can you provide to make the consumer wait for the producer?4.Another student tells you that the difference between processes and threads is thatprocesses never share memory, while threads do. s this statement correct or incorrect?How can the behavior of 3000pc-rendezvous help justify your answer?
5. Note that 3000pc-rendezvous uses /dev/urandom to pick its words instead of the clibrary’s random functions. How do these two methods differ? You may wish to look atman urandom(4).
6.Change the calls to pthread_mutexattr_setpshared and pthread_condattr_setpshared totake 0 instead of 1.How does the behavior or 3000pc-rendezvous change? Does
anything break? Demonstrate this using your answers to question 2 and question 3.7.3000pc-rendezvous can deadlock when run for long enough. 3000pc-rendezvous-timeout should not deadlock, but it may pause sometimes. What is the differencebetween these two programs? Why would this effect the tendency to deadlock?
8.Benchmark all three programs using the time command.For example, run 3000fifo asfollows:
time (./3000pc-fifo 100000 0 0 > fifo-output.log)Which one is fastest? Why?
9.(Advanced) lmplement multiple producers and consumers in 3000pc-rendezvous.Compare your results to what you did in 3000pc-fifo.