Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2. Give two examples of real-life (i.e., non-computer related) scenarios that exemplify:
a) Abstraction
b) Virtualization
Solution:
Abstraction:
Abstraction is a process of hiding the low level implementation details from the user, only the
functionality is exposed at high level.
Real World Example 1: Email is good example of abstraction. When we log into our account
we compose an email and send it to the recipient by clicking send button. What really happens in
the background is hidden from the user. There is a whole lot of background process involved
here, such as verifying recipient, sending request to email server, sending our email through
transportation protocols.
Real World Example 2: Mobile phones are another good examples of Abstraction. We use
mobile phones to make calls, send text messages etc. The background process of mobile phones
is hidden from us and only functionality is exposed to us.
Virtualization:
Real World Example 1: Flight Simulator used to train pilots is a good example of virtualization.
Flight simulators have software that gives you all of the information that a real airplane would
give you, your aircrafts systems info (fuel, electrical, propulsion) and your navigation info
(current location, wind speed, aircraft speed, and altitude), but you dont have an actual plane
that will crash and go boom if you do something wrong.
Real World Example 2: Another example of virtualization is online courses. Though the
Instructor is not present live, he/she can virtually explain the concepts and many people can
access the courses without actually have to move anywhere from home.
3. Consider the evolution of computers in general and the increase in processor speed in
particular. Suppose we were to design a system with a processor that is clocked at
10GHz. Further let us suppose that signals in our system can propagate at 80% of the
speed of light. Determine the maximum distance between the CPU and Cache memory
if we want to access cache data within one processor cycle.
Solution:
Given Clock rate, R= 10GHz,
9
Clock cycle= 1/R => 1/1 0 10
10
10
= 1 10
sec. => 1 10
sec
8
3
=> 24 10
meters
4. Write a simple sequence-number system through which two processes, P1 and P2, can
each obtain 30 unique integers, such that one receives all the odd and the other all the
even numbers. Use the fork() call to create P1 and P2. Given a file, F, containing a
single number, each process must perform the following steps:
a) Open F.
b) Read the sequence number N from the file.
c) Close F.
d) Output N and the process' PID (either on screen or test file).
e) Increment N by 1.
f) Open F.
g) Write N to F.
h) Flush F.
i) Close F.
What are the problems that you observe? Describe the behavior of your program and
provide evidence for your conclusion in form of test-output.
Solution:
In this program we create two processes: Child and Parent. Child process reads even numbers
and parent process reads odd numbers. Child is created using fork process. As we know that once
fork is used both child and Parent execute in parallel. So there exists a conflict in sharing of
resources from both process. To avoid this conflict we make one process to sleep while other
execute.
The execution happens in following way.
1. The file is read by both parent and child at same time and number is fetched.
2. If the number is even the parent process is made to wait or sleep for certain time till child
process increments the number and write back into the file.
3. Once the number turn to odd now parent and child access file. As the number is odd,
child process goes to sleep state and parent process executes the process by incrementing
the number and writes back to the file.
C Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int readnumber();
void writenumber();
int main()
{
pid_t pid;
/* fork a child process */
pid =fork();
int num;
if (pid < 0) { /* error occurred */
printf("Fork Failed to create process");
return 1;
}
else if(pid == 0){
/* child process */
int k;
for(k=0;k<=60;k++)
{
num=readnumber();
if(num%2==0){
printf("number from file is %d and process id is %d\n",num,pid);
num++;
writenumber(num);
}else{
sleep(1);
}
}
}
else{/* parent process */
int k;
for(k=0;k<=60;k++)
{
num=readnumber();
if(num%2!=0){
printf("number from file is %d and process id is %d\n",num,pid);
num++;
writenumber(num);
}else{
sleep(1);
//wait(NULL);
}
}
}
return 0;
}
int readnumber(){
int num;
FILE *fileptr;
fileptr =fopen("test.txt","r");
if(fileptr!=NULL){
fscanf(fileptr,"%d",&num);
fclose(fileptr);
}
else{
printf("Error!!,File not read");
}
return num;
}
void writenumber(int number){
int num;
num=number;
FILE *fileptr;
fileptr =fopen("test.txt","w");
if(fileptr!=NULL){
fprintf(fileptr,"%d",num);
fclose(fileptr);
}
else{
printf("Error!!,File not read");
}
}
Results:
Performance Improvement:
1. We can enhance the improvement of the copying either by increasing the buffer size. But
this does not work well for large file sizes.
2. Another improvement we can make is by breaking the file into small chunks and copying
in parallel can significantly decrease the copying time.
3. 3. A new concept of zero copying can be used to improve the copying. This can be done
by utilizing the zero-copy APIs.
Code in Java:
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyingfiletofileProg {
public static void main(String[] args)
{
int i=0;
FileInputStream instream = null;
FileOutputStream outstream = null;
String[] inputfilenames={ "details1.txt", "details2.txt", "details3.txt",
"details4.txt","details5.txt","details6.txt","details7.txt","details8.txt","details9.txt","details10.txt",
"details11.txt" };
long start,end,timeduration;
try{
start=System.currentTimeMillis();
for(i=0;i<=10;i++){
File infile =new File("FileFolder\\".concat(inputfilenames[i]));
File outfile =new File("FileFolder\\MyOutputFile.txt");
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}