HW 2
HW 2
(Home Work-2)
D.Harikrishna(MT2010029)
N.Sandesh(MT2010130)
03 february 2011
1
1. Look up the standard C rand() function. Using the same, create a mod-
ified function zrand() which does not have the srand feature at all, but
internally uses srand to seed the random function with the pid of the call-
ing process. Use this function in a test program and submit the code and
results of the same.
Ans:
#include<stdio.h>
void zrand(void);
main()
{
zrand();
}
void zrand(void)
{
int r,i;
int k=getpid();
printf("%d\t",k);
srand(getpid());
for(i=0;i<5;i++)
{
r=rand();
printf("%d\t",r);
}
}
2. Write a C procedure getTime() that can be used to return the exe- cution
time of code segments by calling it before the code segment is executed
and again after the code segment is executed.
A. Experiment with your routine to find out the smallest clock resolution
that can be obtained on your machine, and report the results (including
the values obtained). What happens when an even smaller resolution is
attempted?
Ans:
#include<stdio.h>
#include<sys/time.h>
#include<math.h>
double getTime(int);
main()
1
{
double start,stop,elapsedTime;
int i,j,n;
printf("\nenter precision value");
scanf("%d",&n);
start=getTime(n);
for(i=0;i<1000000000;i++);
stop=getTime(n);
elapsedTime=stop-start;
printf("%lf\n",elapsedTime);
}
double getTime(int x)
{
double time;
struct timeval tv;
gettimeofday(&tv,NULL);
time=tv.tv_sec*pow(10,x);
return time;
}
output :
getTime(n). Time(sec).
-3 0.003000
-4 0.000300
-5 0.000030
-6 0.000003
-7 0.000000
2
static long int prime[max],p=2;
start=getTime(-6);
printf("%lf\n",start);
prime[0]=prime[1]=1;
for(c2 = 2;c2 <= i+1;c2++){
if(prime[c2] == 0){
c1=c2;
for(c3 = 2*c1;c3 <= i+1; c3 = c3+c1){
prime[c3] = 1;
}
}
}
for(p=2;p<i+1;p++)
{
if(prime[p]==0)
{
count++;
}
}
stop=getTime(-6);
elapsedTime=stop-start;
printf("%lf\n",elapsedTime);
printf("%ld\n",count);
}
double getTime(int n)
{
double time;
struct timeval tv;
gettimeofday(&tv,NULL);
printf("%d\n",n);
time=(tv.tv_sec*pow(10,6)+tv.tv_usec)/pow(10,6+(-6));
return time;
}
output :
3
sample No. Time(micro sec).
1 10869613
2 10743310
3 10740737
4 10769959
5 10781596
6 10742054
7 10748027
8 10752064
9 10719711
10 10741844
11 10746528
12 10737779
13 10753873
14 10755936
15 10757903
16 10755027
17 10762762
18 10755191
19 10763881
20 10745067
Statistics Value
Mean 10757143.00
Variance 828772992
Standard Deviation 28788.417969
Maximum value 10869613 Microsec.
Minimum value 10719711 Microsec.
4
fork() is a system call. When it is invoked it will create a new child
process of the same process. A seperate address space will be created for
that child process.The return value of fork() is 0 if it is executing in child
process, whereas in the parent process return value will be pid of the child
process.
Here, when ever the code is executed if it encounters the systemcall
fork(), it will create the new copy of the process, and this is called as
child process.So, execution of both processes(child and parent) will con-
tinue.Now in the execution part if parent executes the return value of fork
will be non-zero pid of the child process,so it will print x . Whenever child
process exeutes ,since the return value of fork is 0 it will print y value.
Thus both child and parent process will executes parallel so printing of x
and y will be in an arbitary fashion.
#include <stdio.h>
int main()
{
int i;
pthread_t tid[2];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid[0],&attr,runner1,NULL);
pthread_create(&tid[1],&attr,runner2,NULL);
for(i=0;i<2;i++)
pthread_join(tid[i],NULL);
5
Pthreads are POSIX standard for thread creation. here instead of cre-
ating a seperate process a part of executing process will be created,so the
same resources can be accessed.thread creation() starts a seperate thread
which will be intiated from main().
In the program, thread creation() is called twice to create two pro-
cesses, which will be controlled by runner1 and runner2 functions.So threads
while executing there will be a context-switch which will leads to print x
sometimes intially then y afterwards or arbitarilly in between x or the vice
verse.
Ans:
6
to other process,therefore thereis no chance of starvation,all processes will
executes in a round-robin fashion.
Progress-Condition:
This program doesn’t ensure Progress-condition, since by setting the
turn to next processes turn ,even though its not willing to enter ,the pro-
gram doesnt allow any other process unless the next process comes.
Ans:
Global variables:
turn : ← intially set to 0, checks for its turn or not;
count : ← intially set to -1, specifies the number of processes in critical
section;
present : ← intially set to 0, stores the latest turn value;
while true do
if count <= L − 1 then
count ← count+1; if count >= 0 then
while turn! = j do
end
present ←j;
if present = l − 1 then
turn ← 0;
else
turn ← present+1;
end
critical section();
count ←count-1;
end
else
wait(pj );
end
end
Algorithm 2: L- Exclusion for process pj
7
This program ensures Bounded-wait ,since when a l-number of process
completes execution it cannot excute the same processes again because it is
setting turn to one more than maximum of all current processes ,therefore
there is no chance of starvation,all processes will executes in a round-robin
fashion.
Progress-Condition:
This program doesn’t ensure Progress-condition, since by setting the
turn to next processes turn ,even though its not willing to enter ,the pro-
gram doesnt allow any other process unless the next process comes.
Ans:
count : Stores the sum of all pids that are accessing the file currently.
getpid() : Gets the process-id of the corresponding process.
if count < n then
if count + getpid() < n then
count ← count + getpid();
shared lock(pi );
file access();
shared unlock(pi );
end
if process end( pi ) then
count=count-getpid();
end
end
Algorithm 3: Psuedo code for controller to access file.
8
A. Give na pseudocode (without semaphores or other synchronization
primitives) of the code for producer i, and similar pseudocode for con-
sumer j.
Ans:
9
while true do
for i ←0 to m − 1 do
if Buf f er(i) > 0 then
break;
end
end
if i¿m-1 then
for k ←1 to n do
wakeup(producerk );
end
sleep();
end
Buf f eri [OUT] ← Buf f eri [OUT]-item;
OUT←(OUT+1)MOD SIZE;
if Buf f er(i)=0 then
wakeup(produceri );
end
if Buf f er(i)=FULL-1 then
wakeup(produceri );
end
end
Algorithm 5: consumerj
B. Now carefully analyze all the problems that can occur with the above.
(Note that there are at least three new problems that did not arise with
the single-producer/single-consumer case.) Give a sketch of an improved
solution that addresses these problems.
Ans:
1.when there are m producers and n consumers ,there is a chance that
two producers will produce into the same buffer ,so first updated value will
be lost.hence leads to inconsistency. To control such situation we will as-
sume two Counting semaphore No Empty Buffers and No Full Buffers and
Binary Semaphore mutex . where Counting semaphores , No Empty Buffers
-Number of empty buffers avaliable.assume intially m. No Full Buffers -
Number of Full Buffers Available. assume intially 0.
10
while true do
if Buf f er(i) = FULL then
for k ←0 to n − 1 do
wakeup( consumerk );
end
sleep();
end
down(No Empty Buffers);
down(mutex);
Buf f eri [IN]← Buf f eri [IN]+item;
IN←(IN+1)MOD SIZE;
up(mutex);
up(No Full BUffers);
if Buf f er(i) = 1 then
for k ←1 to n do
wakeup( consumerk );
end
end
end
Algorithm 6: produceri
11
while true do
for i ←0 to m − 1 do
if Buf f er(i) > 0 then
break;
end
end
if i¿m-1 then
for k ←1 to n do
wakeup(producerk );
end
sleep();
end
down(No Full BUffers);
down(mutex);
Buf f eri [OUT] ← Buf f eri [OUT]-item;
OUT←(OUT+1)MOD SIZE;
down(mutex);
down(No Empty Buffers);
if Buf f er(i)=0 then
wakeup(produceri );
end
if Buf f er(i)=FULL-1 then
wakeup(produceri );
end
end
Algorithm 7: consumerj
12