Professional Documents
Culture Documents
Assignment No. 2
Q 1: Including the initial parent process, how many processes are created by the following program? Draw a
tree like diagram to show the number of processes.
#include<stdio.h> #include<unistd.h>
int main()
{
int i;
for(i=0;i<4;i++)
fork(); return
0; }
Solution
No of processes = 2^n – 1
=2^4 – 1
= 15
Q 2: Using the following program, assume that the actual pids of the parent and child are 2600 and 2603,
respectively. Identify the values of pid at lines A, B, C, and D. (3 Marks)
#include<sys/types.h>
#include<stdio.h> #include<unistd.h>
int main()
{
pidt pid, pid1;
/* fork a child process */ pid
= fork();
if (pid < 0){ /* error occurred */
fprintf(stderr, "Fork Failed"); return
1;
} else if (pid ==
0)
{ /* child process */ pid1
= getpid();
printf("child: pid = %d",pid); /* A */ printf("child:
pid1 = %d",pid1); /* B */
}
else{ /* parent process */
pid1 = getpid(); printf("parent: pid
= %d",pid); /* C */ printf("parent:
pid1 = %d",pid1); /* D */ wait(NULL);
} return
0;
}
Solution
A=0
B = 2603
C = 2603
D = 2600
Q 3: What is the difference between user level thread and kernel level thread? Given scenarios below,
identify the user level thread and kernel level thread. (3 Marks)
a. Whenever you create a new tab in a web browser (i.e. Chrome, Firefox etc.)
b. A process sends a request for printing.
c. Some user enables a spelling checking in a MS-Word document.
d. A server daemon for providing some service to client on a server.
Solution
USER LEVEL THREAD KERNEL LEVEL THREAD
User thread are implemented by users. kernel threads are implemented by OS.
OS doesn’t recognized user level threads. Kernel threads are recognized by OS.
operation then entire process will be If one kernel thread perform blocking operation
a) Kernel thread
b) User thread
c) User thread
d) Kernel thread
Q 4: We discussed a paradigm for cooperating processes, producer process produces information that is
consumed by a consumer process. Change the below code in such a way that producer process produce an
item and do nothing (wait) until the consumer process consume the item. Write an algorithm, no need of
coding. (3 Marks)
Bounded-Buffer – Producer Bounded Buffer – Consumer
item next_produced; while (true) { item
/* produce an item in next produced */ next_consumed;
while (((in + 1) % BUFFER_SIZE) == out) while (true) {
; /* do nothing */ while (in == out)
buffer[in] = next_produced; ; /* do nothing */
in = (in + 1) % BUFFER_SIZE; next_consumed = buffer[out];
} out = (out + 1) % BUFFER_SIZE;
Solution
The only problem with the above mentioned code is that the
maximum number of items which can be placed into buffer is
buffer size -1. One slot is unavailable because there always
has to be a gap between producer and consumer.so we will try
to overcome this deficiency by introducing a counter variable.
Q 5: There are different methods (i.e. sockets, RPC and pipes) used for inter-process communication in a
distributed environment or client-server environment. Describe the client-server process communication
using Remote Procedure Call (RPC). (4 Marks)
Solution
Client–server model is a distributed application structure that partitions tasks or workloads between the
providers of a resource or service, called servers, and service requesters, called clients. Often clients and
servers communicate over a computer network on separate hardware, but both client and server may reside
in the same system. A server host runs one or more server programs, which share their resources with
clients.
A remote procedure call is an interprocess communication technique that is used for client-server based
applications. It is also known as a subroutine call or a function call.
A client has a request message that the RPC translates and sends to the server. This request may be a procedure
or a function call to a remote server. When the server receives the request, it sends the required response back
to the client. The client is blocked while the server is processing the call and only resumed execution after the
server is finished.
90 81 78 95 79 72 85
The variables representing the average, minimum, and maximum values will be stored globally. The worker
threads will set these values, and the parent thread will output the values once the workers have exited. The
program will report
#include<stdio.h>
#include<pthread.h>
int arr[50],n,i;
void *th()
{
int sum=0;
float average;
printf("enter your number :=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
average=sum/n;
printf("The average value is:%f",average);
}
void *th1()
{
int temp=arr[0];
for(int i=1;i<n;i++)
{
if(temp>arr[i])
{
temp=arr[i];
}
}
printf("\nThe Minimum value is:=%d",temp);
}
void *th2()
{
int temp=arr[0];
for(int i=1;i<n;i++)
{
if(temp<arr[i])
{
temp=arr[i];
}
}
printf("\nThe Maximum value is:=%d",temp);
}
int main()
{
int n,i;
pthread_t t1;
pthread_t t2;
pthread_t t3;
n=pthread_create(&t1,NULL,&th,NULL);
pthread_join(t1,NULL);
//printf("\n done and my value is %d",n);
n=pthread_create(&t2,NULL,&th1,NULL);
pthread_join(t2,NULL);
//printf("\n done and my value is %d",n);
n=pthread_create(&t3,NULL,&th2,NULL);
pthread_join(t3,NULL);
//printf("\n done and my value is %d",n);
----------The End----------