Saturday, 23 November 2019

A bit string, 0111101111101111110, needs to be transmitted

Q: A bit string, 0111101111101111110, needs to be transmitted at the data link layer.
What is the string actually transmitted after bit stuffing?
 
Solution:
 
 The output is 011110111110011111010.

The following data fragment occurs in the middle of a data

Q: The following data fragment occurs in the middle of a data stream for which the byte-stuffing algorithm described in the text is used: A B ESC C ESC FLAG FLAG D.
What is the output after stuffing?
 
Solution:
 
 After stuffing we get: A B ESC ESC C ESC ESC ESC FLAG ESC FLAG D.

The following character encoding is used in a data link protocol

Q: The following character encoding is used in a data link protocol:
A:01000111; B:11100011; FLAG:01111110; ESC: 11100000
Show the bit sequence transmitted (in binary) for the four-character frame: A B ESC
FLAG when each of the following framing methods are used:
(a) Character count.
(b) Flag bytes with byte stuffing.
(c) Starting and ending flag bytes, with bit stuffing.
 
Solution:
 

a. 00000100 0100011111100011 11100000 01111110
b. 01111110 0100011111100011 11100000 11100000 11100000 01111110
01111110
c. 01111110 01000111110100011 111000000 011111010 01111110

An upper-layer packet is split into 10 frames, each of which has an 80 percent chance of arriving undamaged.

Q: An upper-layer packet is split into 10 frames, each of which has an 80 percent chance of arriving undamaged. If no error control is done by the data link protocol, how many times must the message be sent on average to get the entire thing through?

Solution:

First notice that no error checking is done by the data link protocol. What this means is that if an error occurs in a frame this information will not be known until all 10 frames have been transmitted and reassembled(the transport layer is doing some error control presumably, and will detect if there was an error in 1 or more frames and if so, request re-transmission). So in the case of this problem, we can't just repeatedly send the first frame until it gets through without error, then do the same for the second,etc. Instead, we must send all 10 frames. If they all get through without error we are done. Otherwise, we must send all 10again.
Because each frame has a probability of 0.8 of getting through correctly and errors can be considered independent events, the probability of the whole message getting through correctly on any given attempt is which is about 0.107 (or 10.7%). Call this value p.
Now notice that the probability of the message requiring i transmissions is .The reason is that if it takes exactly i transmissions, the first i-1 attempts must have failed(this happens with probability since the probability of a failed transmission is (1- probability of successful transmission) = (1-p). After these failed attempts,the ith attempt must succeed. This happens with probability p.
To compute the expected number of transmissions required(i.e.the average transmissions required) we weight all the possible transmissions required with their respective probabilities and then sum over all these possibilities. So the expected number of transmissions, E, is:

Notice that the last sum starts at i=0. This is fine since at i=0 =0, so we are just adding 0, something that doesn't change the value of the expression.
To evaluate this sum, we again use the trick of differentiating a series for which we already know the sum to get a form that is useful in the problem at hand.
It was proven in class that
Taking the derivative of both sides shows that .
Now, we just set r=1-p to get .Notice that we must have r<1 for the above equations to hold. But since r=1-p and ,r < 1 as needed.
So the answer is 1/0.107 or about 9.3 transmissions.
Now, we do the same problem, but take the probability of error-free transmission for a frame to be 0.99. In this case which is about 0.904 (quite a bit higher than the first part). The work we did in the first part still holds, so al lwe need to do is calculate which in this case is about 1.1.

Date Rate of noiseless 4-kHz

Q: Compare the maximum data rate of a noiseless 4-kHz channel using
(a) Analog encoding (e.g.,QPSK)with 2 bits per sample.
(b) The T1 PCM system.
 
Solution:

a) Maximum sampling rate = 2 x 30 = 60 Kbps.
4 level PCM will need 2 bits/sample.
Maximum datarate = 60 x 2 =120 Kbps
 
b)Maximum sampling rate = 2 x 30 = 60 Kbps.
6 level PCM will need 3 bits/sample.
Maximum datarate = 60 x 3 =180 Kbps

Saturday, 9 November 2019

Setting up a database and have auto-commit turned off

Q: You are working on setting up a database and have auto-commit turned off so you can easily undo mistakes. Unfortunately, your session crashes – the database itself is fine but your remote connection failed due to network troubles. You are disconnected long enough for your mysql session to terminate.
Once you manage to reconnect and start a new session, what happens to the changes that you made since your last manual commit (or entry of a statement equivalent to a commit) and why? Select the best answer.
You may want to read up the details.


1.
Because auto-commit is turned off, you have lost all of your work since you last manually committed your work. Any changes made since then have been rolled back.
2.
You will not have lost any work, because MySQL sessions automatically continue from where they left off last time. Any uncommitted work is still not committed.
3.
Whether or not you lost work depends on the type of commands in the current transaction. Actions less likely to interfere with another transaction are more likely to have been accepted.
4.
You will not have lost any work, because the end of a session acts as a commit.


Solution:
Option 4
Because a commit or rollback command will ends the current session and starts a new one
 

Best practice for securing user accounts for a MySQL

Q:
Which of the following are considered best practice for securing user accounts for a MySQL installation? Select all that apply.
Hint: This question goes somewhat further than what was covered in the lecture so you will probably need to do some reading to correctly answer this question.
1.
When connecting to a database, never send the password on the command line or code it in plain text into the front end. You can use a properties file, and can hide it by moving it elsewhere and/or changing its name. Better yet, use the mysql_config_editor to store authentication details in an encrypted file.
2.
Any code used to set up users and/or update passwords from the front end needs to ensure that communications with the database are encrypted so that passwords are not sent across the network in plain text.
3.
A MySQL account should only have the privileges that it absolutely needs; different user profiles generally warrant the creation of different accounts. Only administrative accounts should have access to things like triggers and procedures, and they should be restricted to logging in from specific IP addresses (or localhost) where possible.
4.
When giving edit privileges to a user, it is best to have them log in through a front end that does initial checks for attacks such as SQL injection. This front end should communicate via the database through specific procedure calls to insert or update data, to limit the ways in which data is changed. Such procedures should also perform data integrity checks before allow data to be changed.
5.
None of the above.
 
Solution:
1>
When connecting to a database, never send the password on the command line or
code it in plain text into the front end. You can use a properties file,
and can hide it by moving it elsewhere and/or changing its name.
Better yet, use the mysql_config_editor to store authentication details in an encrypted file

I feel the proper method to get passwords for MYSQL servers is to , to have a sign on prompt and get client to enter
the passwords. or have a secured , protected file to save the password.
so coding password in a plain text is highly unadvisable or getting it from command line.
and saving it in a config file using mysql_config editor is advisable.
so option A in your question is one among the best options to secure accounts.

2>Any code used to set up users and/or update passwords from the front end needs to ensure that communications with the database are encrypted
so that passwords are not sent across the network in plain text.
   Communications with database should always be encrypted because, any insert or update statements
might log user passwords as it is. Here privileges comes into picture, only user with super privileges should be allowed to update passwords.
so when password is entered from front end , it should be shown as ******.
also hashing the password , before it travels from user end to database also using secure networks such as https instead of http is a plus.
storing passwords in encrypted way , there is also unique salts for each encrypted passwords, ensure double security.
so option B in your question is one among the best options to secure accounts.
3>yes this is right, we cannot give all privileges to all users in system.
we should have administrative accounts/admins at different levels.
so option C is also one among the best options to secure accounts.
4>so when we are granting edit privileges to user, initially we would have to login as super user or root user.
I feel when entered as super user, we are compromising a lot on security.
its not just the procedures to perform integrity checks, privileges at table level, columns level, proxy privil, along with procedure privileges should be taken care of.
so option 4 is all good, but not the best among 4 described here.
 

Performance of the database

Q: A shop stores information on Customers(cid, name, address, last_visit), Inventory(iid, name, cost, price, stock) and Purchases( cid, iid, when, price, quantity) FK cid REF Customers(cid) ON DELETE RESTRICT ON UPDATE CASCADE, FK iid REF Inventory(iid) ON DELETE RESTRICT ON UPDATE CASCADE.
Which of the following actions are more likely than not to be true regarding the performance of the database? Select all that apply.

 
1. Using the CREATE INDEX statement to create an index to cid in Customers would make no difference at all.
2. Creating indexes on Purchases may be harmful if we are expecting very frequent INSERT operations.
3. Attribute iid of Inventory having an index is likely to assist with an INSERT operation on Customers.
4. Creating an index on the stock attribute of Inventory would be useful if changes are often due to customers purchasing items (the integer stored in stock decreases by the quantity of every purchase for the product with the iid given in the Purchases tuple), but there are few INSERT operations on the table.


Solution:
NOTE : The INSERT and UPDATE statements take more time on tables having indexes, whereas the SELECT statements become fast on those tables. The reason is that while doing insert or update, a database needs to insert or update the index values as well.
1. False
Explanation : Indexing in Customers is likely to improve its performance because it is less likely to have new customers very often so INSERT (or UPDATE) operations will be less on Customers. On the other hand, INSERT operations on Purchases is more likely and that would require lookup of cid in Customers table due to the presence of foreign key. So, indexing will decrease the lookup time in Customers table and hence, will increase the performance.
2. True
Explanation : As INSERT operations are expected to be more frequent, the indexing may slow down the operations and hence, may degrade the performance.
3. False
Explanation : INSERT operation on Customers has nothing to do with attribute iid of Inventory.
4. False
Explanation : Changes in stock attribute of Inventory due to customers purchasing items would be UPDATE operations. And UPDATE operations also take more time on tables having indexes and hence, may degrade the performance.
 

Triggers in MySQL

Q: Which of the following is true of triggers in MySQL? Select all that apply.
Note: The way triggers works has changed slightly in recent versions. This question refers to the most recent version (after 6 or later).

Solution:


1.
A trigger can call a stored procedure.
2.
A trigger attached to table A that performs an operation on a table B, can cause the activation (triggering) of one or more triggers that are attached to table B.
3.
Specifying a trigger as FOR EACH ROW means the trigger is activated once for each row that is inserted, updated, or deleted.
4.
It is not possible to have multiple triggers with the same trigger event and action time (before/after).
5.
A trigger that attempts to alter the table that it is attached to will result in an error.


Solution:
Solution for 1: False, you can only execute stored procedures and Triggers in a stored procedure cannot be called directly.
Solution for 2: False, it does not create activation of several triggers at table B.
Solution for 3: True, trigger is activated when DML operations are triggered once for each row that is updated
Solution for 4: False, you can only create one trigger for an event per table as well as trigger event and action time
Solution for 5: False, unless the trigger and table posses the same name
 

C-programming problem

Let R be a sequence. The inverse sequence of R is the sequence by reading the symbols in R backwards. For example, the inverse of 0113 is 3110. If the inverse of R is identical to R, then R is called a palindrome sequence. For example, R[9] = 011303110 is a palindrome sequence. Given a sequence S, if R is a subsequence of S and R is palindrome, then R is called a palindrome subsequence of S. One can then similarly define what a longest palindrome subsequence (LPS for short) of S is.
Problem:
Write a program that reads in a sequence of max length = 10,000. Then compute the LPS of a sequence input by the user. Output will print the LPS and its length.
Sample run:
Enter a sequence: 13112301728031631251841324
# an LPS (length = 13) for the second sequence is:
3112136312113

Solution:

/* C program to find the the longest palindromic substring */

#include <stdio.h> //for standard I/O
#include <stdbool.h> //for boolean variable
#include <string.h> //for string functions strlen
// function to find the longest palindromic substring and
//print the substring & its length
int longestPalinStr(char str[])
{
// find the length of the input string
int n = strlen(str);
//array for memoization
bool array[n][n];

//declare array as false(0) initially
memset(array, 0, sizeof(array));
//length of 1 character substring
int maxLen = 1;
//for length 1 substring set true(1)
for (int k = 0; k < n; k++)
array[k][k] = true;
//checking for substring of length 2.
int start = 0;
for (int i = 0; i < n-1; i++)
{
if (str[i] == str[i+1])
{
array[i][i+1] = true;
start = i;
maxLen = 2;
}
}
//loop for substrings of lengths > 2
for (int k = 3; k <= n; k++)
{
for (int i = 0; i < n-k+1 ; i++)
{
//for last index
int j = i + k - 1;
//check for the contained substring to be palindrome
//using array[i+1][j-1] and boundary characters using str[i] and str[j]
if (array[i+1][j-1] && str[i] == str[j])
{
//if above 'if' is true then set position of array[i][j] to be true
array[i][j] = true;
//to set position of start
//and update maxLen
if (k > maxLen)
{
maxLen = k;
start = i;
}
}
}
}
printf(" The longest palindromic substring is: ");
int last = start + maxLen - 1;
//print the substring from start index to last index
for( int i = start; i <= last; i++ )
printf("%c",str[i]);

printf("\nLength is: %d" , maxLen);
}
//main function
int main()
{
//decalre a character array of size 10000
char ch[10000];
//read the string from console
scanf("%s",&ch);
//call thelongestPalinStr() to find the longest palindromic substring of ch[]
longestPalinStr(ch);
return 0;
}


Scheduling, Synchronization and Deadlock (Quiz)

Question 1
Which of the following scheduling algorithms must be nonpreemptive?
  1. Shortest Job First
  2. Round Robin
  3. First Come, First Served
  4. Priority Algorithms
Solution: First Come, First Served

Question 2
____ scheduling is approximated by predicting the next CPU burst with an exponential average of the measured lengths of previous CPU bursts. 
  1. Shortest Job First
  2. Round Robin
  3. Multilevel queue
  4. First Come, First Served 
 Solution: Shortest Job First

Question 3
With the following process list, what is the Average Turnaround Time for the Round Robin scheduling algorithm with a time quantum of 5ms? 

Submission Time Process Run Time
0ms 10ms
5ms 20ms
15ms 10ms
30ms 20ms
40ms 10ms
60ms 10ms
  1.  20
  2. 23
  3. 25
  4. 27
Solution: 27

Question 4
____ is the number of processes that are completed per time unit.
  1. Response time
  2. Turnaround time
  3. Throughput
  4. CPU utilization 
 Solution: Throughput

Question 5
Which of the following is true of cooperative scheduling?
  1.  A process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state.
  2. A process switches from the running state to the ready state when an interrupt occurs.
  3. It requires a timer.
  4. It incurs a cost associated with access to shared data. 
Solution: A process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state.

Question 6
A significant problem with priority scheduling algorithms is _____.
  1. determining the length of the time quantum
  2. complexity
  3. determining the length of the next CPU burst
  4. starvation
Solution: starvation

Question 7
______ allows a thread to run on only one processor.
  1. Load balancing
  2. Processor affinity
  3. Processor set
  4. NUMA
Solution: Processor affinity

Question 8
With the following process list, what is the Average Response Time for the First Come, First Served scheduling algorithm? 
 

Submission Time Process Run Time
0ms 10ms
5ms 20ms
15ms 10ms
30ms 20ms
40ms 10ms
60ms 10ms
  1. 10ms
  2. 7.5ms
  3. 12.5ms
  4. 5ms
Solution: 10ms

Question 9
The ______ occurs in First Come, First Served scheduling when a process with a long CPU burst occupies the CPU. 
  1. waiting time
  2. convoy effect
  3. systemcontention scope
  4. dispatch latency 
 Solution: convoy effect

 

Stored procedures and functions are available in the current database

Q: Which of the following commands is the best way to generate an overview of what stored procedures and functions are available in the current database and what each does? Assume that the developer(s) of the database have followed the good coding guidelines laid out in this unit.


1.
SHOW PROCEDURES;
2.
SHOW PROCEDURE STATUS WHERE db = DATABASE();
3.
SELECT * FROM INFORMATION_SCHEMA.ROUTINES;
4.
SELECT name, comment, type FROM mysql.proc WHERE db = DATABASE();

Solution:
 
2.SHOW PROCEDURE STATUS WHERE db = DATABASE();
This is the ideal statement to list the procedures stored in the database mentioned in the search condition.
Only SHOW PROCEDURES is not a valid statement as it also needs STATUS
SELECT * FROM INFORMATION_SCHEMA.ROUTINES; this statement selects all the rows from the table ROUTINES and displays the result.
SELECT name, comment, type FROM mysql.proc WHERE db = DATABASE(); This is the selective display of certain columns from the table mysql.proc from the database db.