Implementing and building the prodcons program

Assignment Help C/C++ Programming
Reference no: EM133092684

CSC 452 - Project 3: DIY Semaphores

Project Description

We've have been learning about synchronization and solving the producer/consumer problem using semaphores. In this project, we will again modify the Linux kernel to add our own implementations of down() and up() as system calls and then use them to solve the producer/consumer problem.


How It Will Work

There will be a userspace application called prodcons that will implement the producer/consumer problem using processes. We will be using the fork() system call to create additional processes, with the number of consumers and producers specified on the command line followed by the size of the buffer.

If we run the executable as: ./prodcons 2 2 1000, we would see something like this output:

Producer A Produced: 0 Producer B Produced: 1 Producer A Produced: 2 Producer B Produced: 3 Producer A Produced: 4 Consumer A Consumed: 0 Consumer A Consumed: 1 Consumer B Consumed: 2

Basically, we will be producing sequential integers and then consuming them by printing them out to the screen. The program should run as an infinite loop and never deadlock. All producers and consumers share the same buffer (i.e., there is only one buffer total).

 

Syscalls for Synchronization

We need to create a semaphore data type and the two operations we described in class, down() and up(). To encapsulate the semaphore, we'll make a simple struct that contains the integer value:

struct csc452_sem

{

int value;

//Some process queue of your devising

};

We will then make two new system calls that each have the following signatures: asmlinkage long sys_csc452_down(struct csc452_sem *sem) asmlinkage long sys_csc452_up(struct csc452_sem *sem) to operate on our semaphores.

Sleeping

Waking Up

Atomicity

Implementation

Shared Memory in prodcons

Try different buffer sizes to make sure your program doesn't deadlock

Your well-commented prodcons program's source

sys.c containing your implementation of the system calls

As part of your down() operation, there is a potential for the current process to sleep. In Linux, we can do that as part of a two-step process.

1)      Mark the task as not ready (but can be awoken by signals):

set_current_state(TASK_INTERRUPTIBLE);

2)      Invoke the scheduler to pick a ready task:

schedule();

As part of up(), you potentially need to wake up a sleeping process. You can do this via:

wake_up_process(sleeping_task);

Where sleeping_task is a struct task_struct that represents a process put to sleep in your down(). You can get the current process's task_struct by accessing the global pointer variable current. You may need to save these someplace.

We need to implement our semaphores as part of the kernel because we need to do our increment or decrement and the following check on it atomically. In class we said that we'd disable interrupts to achieve this. In Linux, this is no longer the preferred way of doing in kernel synchronization since we might be running on a multicore or multiprocessor machine. Instead, the kernel provides two synchronization primitives: spinlocks and mutexes. For implementing system calls, we should use the provided mutex type and operations

We can create a mutex with a provided macro:

DEFINE_MUTEX(sem_lock);

We can then surround our critical regions with the following: mutex_lock(&sem_lock); mutex_unlock(&sem_lock);

There are two halves of implementation, the syscalls themselves, and the prodcons program. For each, feel free to draw upon the text and slides for this course.

To make our buffer and our semaphores, what we need is for multiple processes to be able to share the same memory region. We can ask for N bytes of RAM from the OS directly by using mmap():

void *ptr = mmap(NULL, N, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, 0, 0);

The return value will be an address to the start of this region in RAM. We can then steal portions of that page to hold our variables. For example, if we wanted two integers to be stored in the region, we could do the following:

int *first; int *second; first = ptr;

second = first + 1;

*first = 0;

*second = 0;

to allocate them and initialize them.

At this point we have one process and some RAM that contains our variables. But we now need to share that to a second process. The good news is that a mmap'ed region (with the MAP_SHARED flag) remains accessible in the child process after a fork(). So all we need to do for this to work is to do the mmap() in main before fork() and then use the variables in the appropriate way afterwards.

Setting up, building, and installing the Kernel

Follow the exact same steps as in project 2. I'd suggest starting from the original kernel source (you can download/extract [if you kept the download] a new VM image if you want, or simply delete the old linux kernel folder and extract the source anew).

Note this means you'll have the same two hour-long builds as anytime we add system calls the entire kernel will be rebuilt. Make sure you start this setup early.

Implementing and Building the prodcons Program

As you implement your syscalls, you are also going to want to test them via your co-developed prodcons program. The first thing we need is a way to use our new syscalls. We do this by using the syscall() function. The syscall function takes as its first parameter the number that represents which system call we would like to make. The remainder of the parameters are passed as the parameters to our syscall function.

We can write wrapper functions or macros to make the syscalls appear more natural in a C program. For example, since we are on the 32-bit version of x86, you could write:

void down(csc452_sem *sem) { syscall(387, sem);

}

And something similar for up().

Running prodcons

Make sure you run prodcons under your modified kernel.

File Backups

I suggest making a directory on Lectura under your home directory that no one else can see. If you have not done so for the other projects, on Lectura, do:

mkdir private chmod 700 private

Backup all the files you change under VirtualBox to your ~/private/ directory frequently!

Loss of work not backed up is not grounds for an extension. YOU HAVE BEEN WARNED.

Copying Files In and Out of VirtualBox

Once again, you can use scp (secure copy) to transfer files in and out of our virtual machine. You can backup a file named sys.c to your private folder with:

scp sys.c [email protected]:private

Hints and Notes

Requirements and Submission

You need to submit:

We will use the turnin program on lectura to turn in your project. If your files are named sys.c and prodcons.c, execute the following command from lectura in the directory that contains them:

turnin csc452-summer21-p3 sys.c prodcons.c

Reference no: EM133092684

Questions Cloud

What is the business case for csr : How can the Corporate Social Responsibility principle be applied to practice and what is the business case for CSR? Provide an example of a CSR leader enterpris
Describe actions that can be taken to increase credibility : Trainers always strive to improve training programs to be more applicable and acceptable by organizations. Describe 5 actions that can be taken to increase cred
What was Sam sales consultant cost percentage : In January, the restaurant's total sales revenue was $84,000. What was Sam's sales consultant cost percentage (%) in the month of January
Emerging trends and issues in a global market : Research three emerging trends and/ or issues in a global market and discuss how those trends and/or issues affect and change consumer behavior in a global mar
Implementing and building the prodcons program : We need to create a semaphore data type and the two operations we described in class, down() and up(). To encapsulate the semaphore, we'll make a simple struct that contains the integer value: Implementing and Building the prodcons Program
Find and interpret the slope : Write the number of apples she can buy as a linear function of number of pears. Find and interpret the slope.
What is the depreciation expense : It is estimated to have a useful life of 8 years and a salvage value of $5,000. What is the depreciation expense for 2016 using the straight-line method
Examine how horror film can address : Identify the roles of "looking" in cinema and how it relates to gender - Examine how horror film can address the divide between humanness/monstrosity
What was the value of his sales last year : This year, a salesman sells a total of $60,000 worth of steak knives by going door-to-door. What was the value of his sales last year

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd