Vous êtes sur la page 1sur 10

Assignment 1

1. Briefly define the following terms:


a) Multiprogramming
b) Response Time
c) Job Scheduling
d) Process Scheduling
Solution:
Multiprogramming:
Multiprogramming is a form of processing in which the various system resources (for example,
CPU, memory, and peripheral devices) are utilized effectively, but they do not provide for user
interaction with the computer system. The CPU executes multiple jobs by switching among
them, but switching occurs so fast that it appears that all programs are executing at the same
time.
Response Time:
Response time, is the time it takes from the submission of a request until the first response is
produced or system starts responding, not the time it takes to output the response.
Job Scheduling:
In OS, Time sharing and multiprogramming require that several jobs be kept simultaneously in
memory. If several jobs are ready to be brought into memory, and if there is not enough room for
all of them, then the system must choose among them. Making this decision is called as Job
scheduling.
Process Scheduling:
Process scheduling is the activity if process scheduler, which selects an available process
(possibly from several processes) for execution on the CPU so that some process is running at all
times on CPU to maximize the CPU utilization. As processes enter the system, they are put into a
job queue, which consists of all processes in the system. The processes that are residing in main
memory and are ready and waiting to execute are kept on a list called the ready queue.

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

Speed of signals= 80% of speed of light => 80/100(3 10 m/sec )


10
7
Distance= speed * clock cycle => 1 10 24 10 m

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:

number from file is 30 and process id is 0


number from file is 31 and process id is 2592
number from file is 32 and process id is 0
number from file is 33 and process id is 2592
number from file is 34 and process id is 0
number from file is 35 and process id is 2592
number from file is 36 and process id is 0
number from file is 37 and process id is 2592
number from file is 38 and process id is 0
number from file is 39 and process id is 2592
number from file is 40 and process id is 0
number from file is 41 and process id is 2592
number from file is 42 and process id is 0
number from file is 43 and process id is 2592
number from file is 44 and process id is 0
number from file is 45 and process id is 2592
number from file is 46 and process id is 0
number from file is 47 and process id is 2592
number from file is 48 and process id is 0
number from file is 49 and process id is 2592
number from file is 50 and process id is 0
number from file is 51 and process id is 2592
number from file is 52 and process id is 0
number from file is 53 and process id is 2592
number from file is 54 and process id is 0
number from file is 55 and process id is 2592

number from file is 56 and process id is 0


number from file is 57 and process id is 2592
number from file is 58 and process id is 0
number from file is 59 and process id is 2592
number from file is 60 and process id is 0
number from file is 61 and process id is 2592
number from file is 62 and process id is 0
number from file is 63 and process id is 2592
number from file is 64 and process id is 0
number from file is 65 and process id is 2592
number from file is 66 and process id is 0
number from file is 67 and process id is 2592
number from file is 68 and process id is 0
number from file is 69 and process id is 2592
number from file is 70 and process id is 0
number from file is 71 and process id is 2592
number from file is 72 and process id is 0
number from file is 73 and process id is 2592
number from file is 74 and process id is 0
number from file is 75 and process id is 2592
number from file is 76 and process id is 0
number from file is 77 and process id is 2592
number from file is 78 and process id is 0
number from file is 79 and process id is 2592
number from file is 80 and process id is 0
number from file is 81 and process id is 2592
number from file is 82 and process id is 0
number from file is 83 and process id is 2592
number from file is 84 and process id is 0
number from file is 85 and process id is 2592

number from file is 86 and process id is 0


number from file is 87 and process id is 2592
number from file is 88 and process id is 0
number from file is 89 and process id is 2592
number from file is 90 and process id is 0
5. Write a small program that copies data from a file A to a file B. For different file sizes
20KB 20MB (doubling the file size in each step) record the time your program
requires to complete the copy when using non-blocking I/O (using read and write
statements). Generate a graph that depicts the program performance. Briefly describe
what you observe and investigate if you can improve the programs performance.
Solution:
A small program was written in Java to file one file to another using buffer size of 512 using
non-blocking IO operation. We observe that as file size increases the time taken to copy files of
different sizes increases exponentially for fixed buffer size but remains close for smaller file
sizes. A graph is plotted for various file sizes and can be seen as below.

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");

instream = new FileInputStream(infile);


outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[512];
int length;
/*copying the contents from input stream to
* output stream using read and write methods*/
while ((length = instream.read(buffer)) > 0){
outstream.write(buffer, 0, length);
}

//Closing the input/output file streams


instream.close();
outstream.close();
end=System.currentTimeMillis();
timeduration=end-start;
System.out.println(timeduration);

}
}catch(IOException ioe){
ioe.printStackTrace();
}
}

Vous aimerez peut-être aussi