Saturday, 9 November 2019

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.

Saturday, 24 August 2019

Processes and Threads (Quiz)

Question 1
The list of processes waiting for a particular I/O device is called a(n) ____.
  1. standby queue
  2. device queue
  3. ready queue
  4. interrupt queue
Solution: device queue

Question 2
A _________________ saves the state of the currently running process and restores the state of the next process to run.
  1. save-and-restore
  2. state switch
  3. context switch
  4. process control block
Solution: context switch

Question 3
The ____ of a process contains temporary data such as function parameters, return addresses, and local variables. 
  1. text section
  2. data section
  3. program counter
  4. stack 
 Solution: stack

Question 4
A process that has terminated, but whose parent has not yet called wait(), is known as a ________ process. 
  1. zombie
  2. orphan
  3. terminated
  4. init 
 Solution: zombie

Question 5
The _______ process is assigned as the parent to orphan processes.
  1. zombie
  2. init
  3. main
  4. renderer 
 Solution: init

Question 6
Which of the following statements is true?
  1.  Shared memory is typically faster than message passing.
  2. Message passing is typically faster than shared memory.
  3. Message passing is most useful for exchanging large amounts of data.
  4. Shared memory is far more common in operating systems than message passing. 
Solution: Shared memory is typically faster than message passing.

Question 7
A process control block ____.
  1. includes information on the process's state
  2. stores the address of the next instruction to be processed by a different process
  3. determines which process is to be executed next
  4. is an example of a process queue 
 Solution: includes information on the process's state

 Question 8
All processes in UNIX first translate to a zombie process upon termination.
  1. True
  2. False 
 Solution: True

Question 9
Child processes inherit UNIX ordinary pipes from their parent process because:
  1.  The pipe is part of the code and children inherit code from their parents.
  2. A pipe is treated as a file descriptor and child processes inherit open file descriptors from their parents.
  3. The STARTUPINFO structure establishes this sharing.
  4. All IPC facilities are shared between the parent and child processes. 
Solution: A pipe is treated as a file descriptor and child processes inherit open file descriptors from their parents.

Question 10
The _____________ refers to the number of processes in memory.
  1. process count
  2. long-term scheduler
  3. degree of multiprogramming
  4. CPU scheduler
Solution: degree of multiprogramming

Question 11
_____ is not considered a challenge when designing applications for multicore systems.
  1. Deciding which activities can be run in parallel
  2. Ensuring there is a sufficient number of cores
  3. Determining if data can be separated so that it is accessed on separate cores
  4. Identifying data dependencies between tasks.

Solution: Ensuring there is a sufficient number of cores

Question 12
Which of the following would not be an acceptable signal handling scheme for a
multithreaded program?
  1. Deliver the signal to the thread to which the signal applies.
  2. Deliver the signal to every thread in the process.
  3. Deliver the signal to only certain threads in the process.
  4. Do not deliver the signal to any of the threads in the process.
Solution: Do not deliver the signal to any of the threads in the process.

Question 13
The ____ multithreading model multiplexes many user-level threads to a smaller or equal number of kernel threads.
  1. many-to-one model
  2. one-to-one model
  3. many-to-many model
  4. many-to-some model
Solution: many-to-many model

Question 14
A _____ uses an existing thread — rather than creating a new one — to complete a task.
  1. lightweight process
  2. thread pool
  3. scheduler activation
  4. asynchronous procedure call
Solution: thread pool

Question 15
_________ involves distributing tasks across multiple computing cores.
  1. Concurrency
  2. Task parallelism
  3. Data parallelism
  4. Parallelism
Solution: Task parallelism

Question 16
A ____ provides an API for creating and managing threads.
  1. set of system calls
  2. multicore system
  3. thread library
  4. multithreading model
Solution: thread library

Question 17
Thread-local storage is data that ____.
  1. is not associated with any process
  2. has been modified by the thread, but not yet updated to the parent process
  3. is generated by the thread independent of the thread's process
  4. is unique to each thread
Solution: is unique to each thread

Question 18
Signals can be emulated in windows through ____.
  1. asynchronous procedure calls
  2. local procedure calls
  3. remote procedure calls
  4. synchronous procedure calls 
 Solution: asynchronous procedure calls

Question 19
Pthreads refers to ____.
  1. the POSIX standard.
  2. an implementation for thread behavior. 
  3. a specification for thread behavior.
  4. an API for process creation and synchronization. 
 Solution: a specification for thread behavior.

Question 20
According to Amdahl's Law, what is the speedup gain for an application that is 60% parallel and we run it on a machine with 4 processing cores? 
  1. 1.82
  2. 0.7
  3. 0.55
  4. 1.43 
 Solution: 1.43 

Open Source Development Tools and Overview (Quiz)

Question 1
________ is the proper filename for the file that contains targets, dependencies and rules which are used to define how your source code should be built into a binary executable.
  1. compiler
  2. make
  3. gcc
  4. Makefile
Solution:  Makefile

Question 2
What editor are you required to use in order to write source code on Linux?
  1. nano
  2. vim
  3. Any editor you are comfortable with
  4. emacs
Solution:  Any editor you are comfortable with

Question 3
Which gcc compiler option should you use in order to include symbols for debugging?
  1. -Wextra
  2. -o
  3. -Wall
  4. -g  
Solution: -g

Question 4
Before committing changes, what git command is used in order to include updates made to files that already exist in a local git repository? 
  1. update
  2. pull
  3. add
  4. push  
Solution: add

Question 5
When working with a remote git repository, what command should be used in order to synchronize changes from the the remote repository into the local repository? 
  1. update
  2. add
  3. push
  4. pull  
Solution: pull

Question 6
Which is not another term for kernel mode?
  1. system mode
  2. operating mode
  3. privileged mode 
  4. supervisor mode 
Solution: operating mode

Question 7
A(n) ________ is the unit of work in a system
  1. timer
  2. process
  3. operating system
  4. mode bit  
Solution: process

Question 8
What statement concerning privileged instructions is considered false?
  1. They can be executed in user mode.
  2. They are used to manage interrupts.
  3. They can be executed in kernel mode.
  4. They may cause harm to the system. 
Solution: They can be executed in user mode.

Question 9
A(n) ____ is a custom build of the Linux operating system
  1. installation
  2. LiveCD
  3. distribution
  4. VMWare Player
 Solution: distribution

Question 10
The most common secondary storage device is ____.
  1. magnetic disk
  2. tape drives
  3. random access memory
  4. solid state disks
 Solution: magnetic disk

Question 11
The two separate modes of operating in a system are
  1. user mode and kernel mode
  2. supervisor mode and system mode
  3. physical mode and logical mode
  4. kernel mode and privileged mode  
Solution: user mode and kernel mode

Question 12
Embedded computers typically run ____ operating system
  1. a clustered
  2. a network
  3. the Windows XP
  4. a real-time
Solution: a real-time

Question 13
Which of the following operating systems is not open source?
  1. PCLinuxOS
  2. BSD UNIX
  3. Linux
  4. Windows 
Solution:  Windows

Question 14
Microkernels use _____ for communication.
  1. virtualization
  2. message passing
  3. shared memory
  4. system calls 
Solution: message passing

Question 15
If a program terminates abnormally, a dump of memory may be examined by a ____ to determine the cause of the problem. 
  1. module
  2. control card
  3. debugger
  4. shell 
 Solution: debugger

Question 16
_____ allow operating system services to be loaded dynamically.
  1. Virtual machines
  2. Modules
  3. File systems
  4. Graphical user interfaces
Solution: Modules

Question 17
_____ provide(s) an interface to the services provided by an operating system.
  1. System calls
  2. Communication
  3. Simulators
  4. Shared memory
Solution: System calls

Question 18
A microkernel is a kernel ____.
  1. that is compressed before loading in order to reduce its resident memory size
  2. that is compiled to produce the smallest size possible when stored to disk
  3. containing many components that are optimized to reduce resident memory size
  4. that is stripped of all nonessential components 
 Solution: that is stripped of all nonessential components

Question 19
A _____ is an example of a systems program.
  1.  text formatter
  2. database system
  3. command interpreter
  4. Web browser 
Solution: command interpreter

Question 20
A boot block ____.
  1. typically only knows the location and length of the rest of the bootstrap program
  2. is composed of multiple disk blocks
  3. typically is sophisticated enough to load the operating system and begin its execution
  4. is composed of multiple disk cylinders 
Solution:
typically only knows the location and length of the rest of the bootstrap program