Project 1: System Calls and Context SwitchMeasurements In this project, you’ll measure the costs of

Project 1: System Calls and Context SwitchMeasurements In this project, you’ll measure the costs of a system call andthe cost of a context switch. o system_call.c – measures the time it takes to do a systemcall. o context_switch.c – measures the time it takes to do a contextswitch. • A makefile for easy compilation Hints: Measuring the cost of a system call is relatively easy. Forexample, you could repeatedly call a simple system call (e.g.,performing a 0-byte read), and time how long it takes; dividing thetime by the number of iterations gives you an estimate of the costof a system call. One thing you’ll have to take into account is the precision andaccuracy of your timer. A typical timer that you can use isgettimeofday(); read the man page for details.What you’ll see there is that gettimeofday()returns the time in microseconds since 1970; however, this does notmean that the timer is precise to the microsecond. Measureback-to-back calls to gettimeofday() to learnsomething about how precise the timer really is; this will tell youhow many iterations of your null system-call test you’ll have torun in order to get a good measurement result. If gettimeofday() is not precise enough foryou, you might look into using the rdtscinstruction available on x86 machines. Measuring the cost of a context switch is a little trickier. Thelmbench benchmark does so by running two processes on a single CPU,and setting up two UNIX pipes between them; a pipe is just one ofmany ways processes in a UNIX system can communicate with oneanother. The first process then issues a write to the first pipe,and waits for a read on the second; upon seeing the first processwaiting for something to read from the second pipe, the OS puts thefirst process in the blocked state, and switches to the otherprocess, which reads from the first pipe and then writes to thesecond. When the second process tries to read from the first pipeagain, it blocks, and thus the back-and-forth cycle ofcommunication continues. By measuring the cost of communicatinglike this repeatedly, lmbench can make a good estimate of the costof a context switch. You can try to recreate something similarhere, using pipes, or perhaps some other communication mechanismsuch as UNIX sockets. One difficulty in measuring context-switch cost arises insystems with more than one CPU. (Un?)Fortunately, the C4 labmachines are multi-processor: run lscpu andnproc on one of the machines to see this. What youneed to do on such a system is ensure that your context-switchingprocesses are located on the same processor. Fortunately, mostoperating systems have calls to bind a process to a particularprocessor; on Linux, for example, thesched_setaffinity() call is what you’re lookingfor. Check it out on C4 lab machines with:
man 2 sched_setaffinity
By ensuring both processes are on the same processor, you aremaking sure to measure the cost of the OS stopping one process andrestoring another on the same CPU. Attached