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

  What difference between sequential access and direct access

What is the difference between sequential access and direct access? What do the seekg() and seekp() functions do? What do the read() and write() functions do?

  Design and implement a class called statistician in c++

Design and implement a class called statistician in C++. After a statistician is initialized, it can be given a sequence of double numbers. Each number in the sequence is given to the statistician by activating a member function called next_number..

  How many members does class bagtype have

CIS242 Assignment- How many members does class bagType have? How many private members does class bagType have? How many constructors does class bagType have? How many constant functions does class bagType have?

  Write program to declare the array of type float

Write down the c++ program which declares the array of 50 components of type float. Initialize array so that first 25 components are equal to square of the index variable.

  A password must have at least eight characters

A password must have at least eight characters. A password must consist of only letters and digits. A password should contain at least one uppercase letter. A password must contain at least two digits.

  Differences between if/else selection and switch selection

What do you need to analyze when directing the flow of information in each case?

  Write menu driven program that make coffee shop operational

Write a menu-driven program that will make the coffee shop operational and Buy coffee in any size and in any number of cups.

  Write the c++ programming language that has an sql query

This laboratory provides some experience working with the C++ programming language that has an SQL query and SQL update embedded in it.

  Write a loop that read positive integers from standard input

Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive dupli..

  Write a code that efficiently executes designated operations

Write assembler code that efficiently executes the designated operations that are described in the comments for each section.

  Write program that captures and stores data about automobile

Suppose you were required to write a C++ program that captures and stores data about automobiles that a small dealership in your city sells. Consider the attributes that describe an automobile.

  Write a program that accepts as input, without prompts

Write a program that accepts as input, without prompts, a series of short values, terminated by end of file. The program outputs one line:

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