You are on page 1of 6

1

Birla Institute of Technology and Science, Pilani - K.K. Birla Goa Campus
CS F372 Test-I Max. Marks Date Time
Operating Semester-I (Closed Book) = 90 13-10-2017 4:00 - 5:30 PM
Systems

Q1. Using the program in below, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the
parent and child are 2600 and 2603, respectively.) [10]

#include <sys/types.h> pid1 = getpid();


#include <stdio.h> printf("child: pid = %d",pid); /* A */
#include <unistd.h> printf("child: pid1 = %d",pid1); /* B */
int main() }
{ else { /* parent process */
pid t pid, pid1; pid1 = getpid();
pid = fork(); printf("parent: pid = %d",pid); /* C */
if (pid < 0) { printf("parent: pid1 = %d",pid1); /* D */
fprintf(stderr, "Fork Failed"); wait(NULL);
return 1; }
} return 0;
else if (pid == 0) { }

Solutions: For reference, the solutions are 0, 2603, 2603, and 2600.

Q2. The program shown in below uses the Pthreads API. What would be the output from the program at LINE C and
LINE P? [10]

#include <pthread.h> pthread join(tid,NULL);


#include <stdio.h> printf("CHILD: value = %d",value); /* LINE C
#include <types.h> */
int value = 0; }
void *runner(void *param); /* the thread */ else if (pid > 0) { /* parent process */
int main(int argc, char *argv[]) wait(NULL);
{ printf("PARENT: value = %d",value); /* LINE P
pid t pid; */
pthread t tid; }
pthread attr t attr; }
pid = fork(); void *runner(void *param) {
if (pid == 0) { /* child process */ value = 5;
pthread attr init(&attr); pthread exit(0);
pthread create(&tid,&attr,runner,NULL); }

Solutions:
Line C: CHILD: value = 5 since the thread changes the value of the global variable.
Line P: PARENT: value = 0 no change to the global variable is made in the parent,
changes made in the child process does not affect
the global variables in the parent process.

Q3. Java Program to Send a Message from Client to Server and Receive a Response Back Using Socket
Programming
What this program does?
This Java Program
1. Make Use of Java Socket Programming
2

2. It starts a server which will be always running listening to a port 25000 (Server.java)
3. Client (Client.java) sends a number (message) to the server
4. Server receives this number and multiplies it by 2
5. Server (Server.java) sends back the result (message) to the client (Client.java)
6. In case the number sent by the client was not a proper number, server (Server.java) sends back the message
“Please send a proper number” to the client (Client.java)

Note: Please run Server.java because server should be ready before client sends the message to it.
[10]

Solutions:
Server.Java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server


{

private static Socket socket;

public static void main(String[] args)


{
try
{

int port = 25000;


ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");

//Server is running always. This is done using this while(true) loop


while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);

//Multiplying the number by 2 and forming the return message


String returnMessage;
try
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + "\n";
}
catch(NumberFormatException e)
{
3

//Input was not a number. Sending proper message back to client.


returnMessage = "Please send a proper number\n";
}

//Sending the response back to the client.


OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}

Clint. Java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client


{

private static Socket socket;

public static void main(String args[])


{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);

//Send the message to the server


OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);

String number = "2";


4

String sendMessage = number + "\n";


bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);

//Get the return message from the server


InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}

Q4. The following state transition table is a simplified model of process management, with the labels representing
transitions between states of READY, RUN, BLOCKED, and NONRESIDENT. [10]

Give an example of an event that can cause each of the above transitions, namely 1, 2, 3, 4, 5 and 6. Draw a State
Transition Diagram (STD) for each example.
5

Answer:

1. READY to RUN occurs only if a process is allocated the CPU. This is the job of the scheduler or dispatcher
2. RUN to READY can be caused by a time-quantum expiration (running process has reached the maximum allowable
time for uninterrupted execution).
3. RUN to BLOCKED can occur if there is a request from the operating systems process, or I/O or other kernel request.
4. BLOCKED to READY occurs if the awaited event completes the event it is been waiting for (perhaps I/O completion).
5. READY to NONRESIDENT occurs if memory is overcommitted, and a process is temporarily swapped out of memory.
6. BLOCKED to NONRESIDENT - occurs if memory is overcommitted, and a process is temporarily swapped out of
memory same just as ready to nonresident

[10 Marks]
Q6. Consider 4 processes A, B, C and D in a Uniprocessor system. xPy represents process P goes for x units of I/O
operation after y units of execution. Arrival time, execution time and xPy of all processes are given in table below.
Consider time quantum of 4 units. [20 Marks]
a. Draw the resultant schedule and queues using Virtual Round Robin scheduling algorithm.
b. Find the waiting period of A and B processes.
c. Find the Turnaround Time of C and D processes.
d. List the time units when the process is preempted and find the number of preemptions.
Process Arrival Time Execution Time xPy
A 0 8 2A6
B 2 5 5B2
C 3 5 CPU bound
D 6 7 4D4

Ans: a. safe sequence: <P0, P2, P1, P3, P4>


b. yes, <P0, P2, P1, P3, P4>

Q7. Consider the following snapshot of the system with five processes P0 toP4 and 4 resource types A, B, C and D with
instances 3, 14, 12 and 8 respectively.
Using Banker’s algorithm
6

a. Check whether the system is safe or not. If the system is safe give a safe sequence. If not, name all the processes
in unsafe state.
b. If a request from P1 arrives (0,4,2,0) can the request be granted? Give reasons.

Process MAX ALLOCATION


A B C D A B C D
P0 0 0 1 2 0 0 1 2
P1 1 7 5 0 1 0 0 0
P2 2 3 5 6 1 3 5 4
P3 3 6 5 2 0 6 3 2
P4 0 6 5 7 0 0 1 0

You might also like