Readers-Writers problem:
- The readers-writers interactions would happen frequently in real systems.
- It has some variations; each is centered on priority of writers and readers.
- The details for “first readers-writers problem” is displayed below:
- This problem favors readers.
- It needs that no reader must be kept waiting lest a writer has already been granted approval to use object.
- There should be no reader waiting due to waiting of writer.
- The details for “second readers-writers problem” is displayed below:
- This problem favors writers.
- It requires that after a writer is set to write, it performs write as fast as possible.
- A reader arriving after writer should wait, even if writer is also waiting.
- The “w” semaphore controls access to critical sections that access shared object.
- The “mutex” semaphore would protect admittance to shared variable “readcnt”.
- It counts number of readers currently in critical section.
- A writer locks “w” mutex each time it would enter critical section and unlocks it each time it leaves.
- This guarantees that there exists at most one writer in critical section at any time point.
- The first reader who enter critical section locks “w” and last reader to leave critical section unlocks it.
- The “w” mutex is ignored by readers who enter and leave while other readers are present.
- A correct solution to either of readers-writers problem could result in starvation.
- A thread is been blocked indefinitely and is failed from making progress.
Explanation of Solution
C code for readers-writers problem:
//Include libraries
#include <stdio.h>
#include "csapp.h"
//Define constants
#define WrteLmt 100000
#define Pple 20
#define N 5
//Declare variable
static int readtms;
//Declare variable
static int writetms;
//Declare semaphore variable
sem_t mtx;
//Declare semaphore variable
sem_t rdrcnt;
//Declare reader method
void *reader(void *vargp)
{
//Loop
while (1)
{
//P operation
P(&rdrcnt);
//P operation
P(&mtx);
//Increment variable
readtms++;
//V operation
V(&mtx);
//V operation
V(&rdrcnt);
}
}
//Declare writer method
void *writer(void *vargp)
{
//Loop
while (1)
{
//P operation
P(&mtx);
//Increment value
writetms++;
//If condition satisfies
if (writetms == WrteLmt)
{
//Display
printf("read/write: %d/%d\n", readtms, writetms);
//Exit
exit(0);
}
//V operation
V(&mtx);
}
}
//Declare init method
void init(void)
{
//Declare variables
readtms = 0;
//Declare variables
writetms = 0;
//Call method
Sem_init(&mtx, 0, 1);
//Call method
Sem_init(&rdrcnt, 0, N);
}
//Define main
int main(int argc, char* argv[])
{
//Declare variable
int li;
//Declare thread variable
pthread_t lTd;
//Call method
init();
//Loop
for (li = 0; li < Pple; li++)
{
//If condition satisfies
if (li%2 == 0)
//Call method
Pthread_create(&lTd, NULL, reader, NULL);
//If condition does not satisfies
else
//Call method
Pthread_create(&lTd, NULL, writer, NULL);
}
//Call method
Pthread_exit(NULL);
//Exit
exit(0);
}
Explanation:
- The reader method decrements the reader count and semaphore initially.
- The reading operation is performed after that.
- The reader count and semaphore values are incremented after the operation.
- The writer method decrements the semaphore variable initially.
- The write operation is then performed.
- If count reaches limit of writers, then display count.
- The semaphore values are incremented after the operation.
read/write: 142746/100000
Want to see more full solutions like this?
Chapter 12 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Correct answer will be upvoted else downvoted. Computer science. positive integer is the gcd of that integer with its amount of digits. Officially, gcdSum(x)=gcd(x, amount of digits of x) for a positive integer x. gcd(a,b) means the best normal divisor of an and b — the biggest integer d to such an extent that the two integers an and b are detachable by d. For instance: gcdSum(762)=gcd(762,7+6+2)=gcd(762,15)=3. Given an integer n, track down the littlest integer x≥n to such an extent that gcdSum(x)>1. Input The primary line of input contains one integer t (1≤t≤104) — the number of experiments. Then, at that point, t lines follow, each containing a solitary integer n (1≤n≤1018). All experiments in a single test are unique. Output Output t lines, where the I-th line is a solitary integer containing the response to the I-th experiment.arrow_forwardCorrect answer will be upvoted else downvoted. Computer science. You are given two integers n and k. You ought to make a variety of n positive integers a1,a2,… ,a to such an extent that the total (a1+a2+⋯+an) is distinguishable by k and greatest component in an is least conceivable. What is the base conceivable most extreme component in a? Input The primary line contains a solitary integer t (1≤t≤1000) — the number of experiments. The solitary line of each experiment contains two integers n and k (1≤n≤109; 1≤k≤109). Output For each experiment, print one integer — the base conceivable most extreme component in cluster a to such an extent that the aggregate (a1+⋯+an) is distinct by k.arrow_forwardC-F languages (or see the following figure). In particular..it points out that the language {am,a"b" | m,n = N} is a deterministic C-F language, but not LL(k) for any k Palindromes over {a, b} Non-deterministic C-F {am,a"b" | m.ne N}; Deterministic C-F -LL(K) {ab❘n EN}- Regular To show the language is not LL(k) for any k, note that a grammar for this language is SAB A→ B -> 10 or S-A|B A→ B (you only need to answer one case here, either one). The language contains A as an element. Now consider the case k = 1 and consider the input string ab. When the first symbol is scanned, we get an 'a'. This information alone is not enough for us to make a proper choice. So we don't even know what to do with the first step in | the parsing process. For k = 2, if we consider the input string aabb, we face the same problem. For any k > 2, the input string ab would cause exactly the same problem. So this grammar is not LL(k) for any k. On the other hand, by putting proper instructions into the blanks…arrow_forward
- Write the algorithm for the problem that works in constant space and time complexity. Input: N OUTPUT: 1 if Tom wins, 0 if Jerry wins.arrow_forward4.Answer the question below .arrow_forwardCorrect answer will be upvoted else downvoted. Computer science. How about we characterize an augmentation activity between a string an and a positive integer x: a⋅x is the string that is an aftereffect of composing x duplicates of a consistently. For instance, "abc" ⋅ 2 = "abcabc", "a" ⋅ 5 = "aaaaa". A string an is separable by another string b if there exists an integer x with the end goal that b⋅x=a. For instance, "abababab" is separable by "stomach muscle", however isn't detachable by "ababab" or "aa". LCM of two strings s and t (characterized as LCM(s,t)) is the briefest non-void string that is separable by both s and t. You are given two strings s and t. Find LCM(s,t) or report that it doesn't exist. It very well may be shown that if LCM(s,t) exists, it is interesting. Input The main line contains one integer q (1≤q≤2000) — the number of experiments. Each experiment comprises of two lines, containing strings s and t (1≤|s|,|t|≤20). Each character in every one…arrow_forward
- Computer science. Correct answer will be upvoted else downvoted. Think about a n by n chessboard. Its columns are numbered from 1 to n from the top to the base. Its sections are numbered from 1 to n from the passed on to one side. A cell on a convergence of x-th line and y-th section is indicated (x,y). The fundamental corner to corner of the chessboard is cells (x,x) for all 1≤x≤n. A stage of {1,2,3,… ,n} is composed on the fundamental slanting of the chessboard. There is actually one number composed on every one of the cells. The issue is to segment the cells under and on the principle askew (there are by and large 1+2+… +n such cells) into n associated areas fulfilling the accompanying imperatives: Each district ought to be associated. That implies that we can move from any cell of a locale to some other cell of a similar area visiting just cells of a similar district and moving from a cell to a neighboring cell. The x-th area ought to contain cell on the fundamental…arrow_forwardIn practical life, the employees get salaries and pay taxes honestly. Sometimes, the process of drawing salariesand payment of taxes may lead to some interesting situation. Suppose, a person draws salary of Rs. 10,000 permonth. A certain percentage of tax is charged on that amount, which is deducted every month. But if the salaryof the person is more than Rs. 10,000 per month, then the tax rate is different. Similarly if a person is getting Rs.20,000 per month, he/she would be charged more under a different tax rate slab. The interesting situationdevelops if there is an anomaly in the tax rates i.e. a person who is getting higher salary takes home lesser moneyas compared to the other person with less gross salary.To further elaborate it, we suppose that there is company 'C' where 100 or less than 100persons are employed. The salaries of the employees and their tax rates are known to us.We are required to list those unlucky persons, who are getting lesser take-home salary(net salary)…arrow_forwardOverflows You have learned the concept of overflow (with say n! that overflows at 13! for integer) Now consider Fibonacci numbers this way: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, etc. A Fibonacci sequence f(n) is of the form: f(1) = f(2) = 1, and f(n+2) = f(n) + f(n+1) for n >= 1. 6. Write a Java computer program that prints out Fibonacci numbers in a certain data type from f(1) until f(n) with n an integer you input from the keyboard. 7arrow_forward
- Dingyu is playing a game defined on an n X n board. Each cell (i, j) of the board (1 2, he may only go to (2, n).) The reward he earns for a move from cell C to cell D is |value of cell C – value of cell D|. The game ends when he reaches (n, n). The total reward - is the sum of the rewards for each move he makes. For example, if n = 1 2 and A = 3 the answer is 4 since he can visit (1, 1) → (1, 2) → (2, 2), and no other solution will get a higher reward. A. Write a recurrence relation to express the maximum possible reward Dingyu can achieve in traveling from cell (1, 1) to cell (n, n). Be sure to include any necessary base cases. B. State the asymptotic (big-O) running time, as a function of n, of a bottom-up dynamic programming algorithm based on your answer from the previous part. Briefly justify your answer. (You do not need to write down the algorithm itself.)arrow_forwardCorrect answer will be upvoted else Multiple Downvoted. Computer science. inquiry is depicted by a couple of integers li, ri (1≤li<ri≤n). For each question, he needs to decide if there exists a decent aftereffect in s that is equivalent to the substring s[li… ri]. A substring s[i… j] of a string s is the string shaped by characters sisi+1… sj. String an is supposed to be an aftereffect of string b if a can be gotten from b by erasing a few characters without changing the request for the excess characters. An aftereffect is supposed to be acceptable in case it isn't coterminous and has length ≥2. For instance, in the event that s is "1100110", the aftereffects s1s2s4 ("1100110") and s1s5s7 ("1100110") are acceptable, while s1s2s3 ("1100110") isn't acceptable. Would you be able to help Hr0d1y answer each question? Input The primary line of the input contains a solitary integer t (1≤t≤100) — the number of experiments. The portrayal of each experiment is as per the…arrow_forwardgxgfarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education