0% found this document useful (0 votes)
93 views74 pages

Operating System: (Fall/ Spring 20 - )

The document is a lab manual for an Operating Systems course. It lists 12 experiments to be completed over the course, including experiments on socket programming in Java, file I/O in Java, multi-threading in Java, Linux commands, system administration commands, shell programming in Linux, information security techniques in Linux, concurrency control techniques in Java, process scheduling algorithms in Java, containers and dockers, page replacement algorithms in Java, and disk scheduling algorithms in Java. It provides instructions for the student to complete the experiments and includes their name and student ID.

Uploaded by

Azeem Talib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views74 pages

Operating System: (Fall/ Spring 20 - )

The document is a lab manual for an Operating Systems course. It lists 12 experiments to be completed over the course, including experiments on socket programming in Java, file I/O in Java, multi-threading in Java, Linux commands, system administration commands, shell programming in Linux, information security techniques in Linux, concurrency control techniques in Java, process scheduling algorithms in Java, containers and dockers, page replacement algorithms in Java, and disk scheduling algorithms in Java. It provides instructions for the student to complete the experiments and includes their name and student ID.

Uploaded by

Azeem Talib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

OPERATING SYSTEM

Lab Manual
[Fall/ Spring 20__]

Student Name: _______________


Student Id: _______________

Prepared By: Dr. Noman Islam

Instructor: Dr. Noman Islam

1
LIST OF EXPERIMENTS

S. No Date Experiment
1 __/__/__ To study and implement socket programming in Java
2 __/__/__ To study and implement file I/O in Java
3 __/__/__ To study and implement multi-threading in Java
4 __/__/__ To study and execute basic Linux commands on a terminal
5 __/__/__ To study and execute system administration commands on a
terminal
6 __/__/__ To study and implement shell programming in Linux
7 __/__/__ To study and implement information security techniques in
Linux
8 __/__/__ To study and implement concurrency control techniques in
Java
9 __/__/__ To study and implement process scheduling algorithms in
Java
10 __/__/__ To study and implement containers and dockers
11 __/__/__ To study and implement page replacement algorithms in Java
12 __/__/__ To study and implement disk scheduling algorithm in Java

2
To study and implement File I/O in Java
Instructions:

1. Try to compile the class FileTest. What goes wrong? This is because opening up a file
could throw an IOException, which is a checked exception. This means you have to tell
Java how to deal with it, or the program won't compile
ANSWER#01:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package filetest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
*
* @author Areeba Shahbaz
*/
public class FileTest {
public void fileWrite() throws IOException
{
File dstFile = new File("K:\\myOutput\\outputfile.txt");
PrintWriter out = new PrintWriter
(new BufferedWriter(new FileWriter(dstFile)));
out.print("Hello ");
out.println("world");
out.close();
}

3
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FileTest fileTest = new FileTest();
fileTest.fileWrite();
}

2. Run your program again. If all went successfully, open up "My Computer", and find your
FilePractice folder on your K drive. You should be able to find the file "outputfile.txt".
Double click on it, and take a look. What do you see?

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package file_test2;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;
4
import java.io.PrintWriter;

/**

* @author 7500

*/

public class File_Test2 {

public void fileWrite() throws IOException

try{

File dstFile = new File("outputfile.txt");

PrintWriter out = new PrintWriter

(new BufferedWriter(new FileWriter(dstFile)));

out.print("Hello ");

out.println("world");

out.close();

catch(Exception s){

/**

5
* @param args the command line arguments

*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

File_Test2 fileTest = new File_Test2();

fileTest.fileWrite();

3.Modify your program to write to the file five lines, each of which contains your name or a
friend's name, followed by a space and then an age, then another space and a gpa. For
example:

6
Arlene 19 3.8
Bill 22 3.5
Marilyn 15 3.9
Bryan 35 1.1
Buzz 6 4.0

ANSWER 3:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package filetest2;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.PrintWriter;

/**

* @author Areeba Shahbaz

*/

public class FileTest2 {

public void fileWrite()

{
7
try{

File dstFile = new File("FileTest2.txt");

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dstFile)));

out.println("Arlene 19 3.8\n");

out.println("Bill 22 3.5\n");

out.println("Marilyn 15 3.9\n");

out.println("Bryan 35 1.1\n");

out.println("Buzz 6 4.0 ");

out.close();

catch (Exception ex){

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

FileTest2 fileTest = new FileTest2();

fileTest.fileWrite();

8
}

4.Add the following method to your FileTest class:


public void consoleRead() throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("What is your first name? ");
String first = in.readLine();
System.out.print("What is your last name? ");
String last = in.readLine();
System.out.println("Your name is " + last + ", " + first + ".");
}
Compile it. Add "throws" statements as necessary. Modify your main to run the consoleRead
method, and recompile. Run your program. What does it do?

ANSWER 4:

/*

9
* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package filetest2;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

/**

* @author Areeba Shahbaz

*/

public class FileTest2 {

public void fileWrite()

try{

File dstFile = new File("FileTest2.txt");

10
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dstFile)));

out.println("Arlene 19 3.8\n");

out.println("Bill 22 3.5\n");

out.println("Marilyn 15 3.9\n");

out.println("Bryan 35 1.1\n");

out.println("Buzz 6 4.0 ");

out.close();

catch (Exception ex){

public void consoleRead() throws IOException

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("What is your first name? ");

String first = in.readLine();

System.out.print("What is your last name? ");

String last = in.readLine();

System.out.println("Your name is " + last + ", " + first + ".");

11
/**

* @param args the command line arguments

*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

FileTest2 fileTest = new FileTest2();

fileTest.fileWrite();

fileTest.consoleRead();

6.In reality, you would want to be able to separate each item on each line into different
variables, rather than keeping all the information on name, age, and gpa in one string. To
break it up, use a StringTokenizer.

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

12
*/

package filetest2;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import static java.lang.System.in;

import java.util.StringTokenizer;

/**

* @author Areeba Shahbaz

*/

public class FileTest2 {

public void fileWrite()

try{

File dstFile = new File("FileTest2.txt");

13
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dstFile)));

out.println("Arlene 19 3.8\n");

out.println("Bill 22 3.5\n");

out.println("Marilyn 15 3.9\n");

out.println("Bryan 35 1.1\n");

out.println("Buzz 6 4.0 ");

out.close();

catch (Exception ex){

/* public void consoleRead() throws IOException

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("What is your first name? ");

String first = in.readLine();

System.out.print("What is your last name? ");

String last = in.readLine();

System.out.println("Your name is " + last + ", " + first + ".");

}*/

public void fileRead() throws IOException

14
try{

File srcFile = new File("FileTest2.txt");

BufferedReader in = new BufferedReader(new FileReader(srcFile));

String text;

while((text=in.readLine()) != null){

StringTokenizer st= new StringTokenizer(text);

System.out.println("Name: "+ st.nextToken());

System.out.println("Age: "+ st.nextToken());

System.out.println("CGPA: "+ st.nextToken());

}}

catch(Exception s){

in.close();

/**

* @param args the command line arguments

15
*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

FileTest2 fileTest = new FileTest2();

fileTest.fileWrite();

//fileTest.consoleRead();

fileTest.fileRead();

16
Lab2 To study and implement socket programming in Java

Lab 2

To study and implement socket programming in Java


Sockets provide the communication mechanism between two computers using Transmission
Control Protocol (TCP) or User Datagram Protocol (UDP). This lab will demonstrate how to
implement TCP sockets using Java. Before starting the lab, download and install Java and
Eclipse IDE by following the instructions below:

1. Download and Install Java Development Kit (JDK)’s latest version

2. Download ‘Eclipse’ on your computer

3. Go to Eclipse folder and Run eclipse.exe file

4. The Eclipse environment will start. Now perform the lab tasks.

Lab Tasks:

1. Find the IP address of a local host using java program. Use the InetAddress class.

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package getip;

17
Lab2 To study and implement socket programming in Java

import java.net.InetAddress;

import java.net.UnknownHostException;

/**

* @author 7500

*/

public class Getip {

/**

* @param args the command line arguments

*/

public static void main(String[] args) throws UnknownHostException {

// TODO code application logic here

InetAddress myIP=InetAddress.getLocalHost();

/* public String getHostAddress(): Returns the IP

* address string in textual presentation.

*/

System.out.println("My IP Address is:");

System.out.println(myIP.getHostAddress());

18
Lab2 To study and implement socket programming in Java

2. Write a small port scanner application. The program usage is as follows:


E:\ >java PortScanner 132 137
Port not in use : 132
Port not in use : 133
Port not in use : 134
Port in use : 135
Port not in use : 136
Port not in use : 137

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package server;

19
Lab2 To study and implement socket programming in Java

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
*
* @author Areeba Shahbaz
*/
public class Server {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
for(int i=1;i<=65000;i++){
try{
Socket s=new Socket("127.0.0.1",i);
System.out.println("port in use "+i);
s.close();}
catch(IOException s){

}
System.out.println("port not in use "+i);
}
}
}

3. Write a small server that accepts socket connection on port 2020. Develop a client

20
Lab2 To study and implement socket programming in Java

application that connects to the server.

a. Using BufferedOutputStream, write to the server “Hello”

b. The server should respond with the word Hello

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package javaapplication4;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.*;

import java.*;

import java.net.Socket;

import java.io.OutputStream;

/**

* @author 7500
21
Lab2 To study and implement socket programming in Java

*/

public class Socketlist {

public static void main(String[] args) throws IOException {

Socket ss =new Socket("192.168.119.92",130);// server connectivity from client

String message1=("salam");

String message2=("salassssssssssm");

// comunication

OutputStream ostream = ss.getOutputStream();

DataOutputStream dos= new DataOutputStream(ostream);

dos.writeBytes(message1);

dos.writeBytes(message2);

System.out.println();

dos.close();

ostream.close();

ss.close();

3. Modify the Task 3 to develop an echo server


4. try

22
Lab2 To study and implement socket programming in Java

5. {
6. ServerSocket s=new ServerSocket(2020); //allowing port to connect
7. Socket ss=s.accept(); //accept client
8.
9. DataInputStream dis=new DataInputStream(ss.getInputStream());
10. DataOutputStream dos=new DataOutputStream(ss.getOutputStream());
11.
12. String msg=dis.readUTF();
13. System.out.println(msg);
14.
15. dos.writeUTF(msg);
16.
17. s.close();
18. ss.close();
19. }
20. catch(Exception e)
21. {
22. System.out.println(e);
23. }
24.
25. */
26. }
27.
28. }

23
Lab2 To study and implement socket programming in Java

24
Lab2 To study and implement socket programming in Java

To study and implement multi-threading in Java


Instructions:

1. A thread is an independent unit of execution.


2. In Java, the Runnable interface and Thread class of package java.lang are used for
implementation of thread
3. To implement a thread, the desired class must implement the Runnable interface and
provide the run() method.
public class MyThread implements Runnable {
public void run() {
//implementation of thread
}
}

4. The Thread class can then be used to start a thread as follows:


public class TestThread
{
public static void main( String[] args )
{
MyThread m = new MyThread();
Thread t = new Thread(m);
m.start();
}
}
Lab Tasks:
1. Write a class that implements Runnable. Define a constructor that takes the name of the
thread as argument. The thread upon execution will print the name of the thread in a
while loop. Define and run 5 thread objects. What output do you see?

import java.util.logging.Level;

import java.util.logging.Logger;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

25
Lab2 To study and implement socket programming in Java

/**

* @author 7500

*/

public class MyThread implements Runnable {

String name;

public MyThread(String n){

this.name=n;

@Override

public void run() {

while(true){

System.out.println("running"+ System.currentTimeMillis()+ name );

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

26
Lab2 To study and implement socket programming in Java

Main class code

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

/**

27
Lab2 To study and implement socket programming in Java

* @author 7500

*/

public class main {

public static void main (String[] args){

for(int i=0;i<5;i++){

MyThread m=new MyThread("Thread"+i);

Thread t=new Thread(m);

t.start();

}}}

TASKJ#03:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package multithreadserver;

import java.io.IOException;

import java.io.PrintStream;

import java.net.ServerSocket;

import java.net.Socket;

28
Lab2 To study and implement socket programming in Java

/**

* @author 7500

*/

public class MultiThreadServer implements Runnable{

Socket csocket;

MultiThreadServer(Socket csocket) {

this.csocket = csocket;

/**

* @param args the command line arguments

*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

ServerSocket ssock = new ServerSocket(1234);

System.out.println("Listening");

while (true) {

Socket sock = ssock.accept();

System.out.println("Connected");

new Thread(new MultiThreadServer(sock)).start();

29
Lab2 To study and implement socket programming in Java

@Override

public void run() {

try {

PrintStream pstream = new PrintStream(csocket.getOutputStream());

for (int i = 100; i >= 0; i--) {

pstream.println(i + " bottles of beer on the wall");

pstream.close();

csocket.close();

} catch (IOException e) {

System.out.println(e);

2. In task 1, modify the run method to randomly sleep the thread for few milliseconds.
Observe the output.

30
Lab2 To study and implement socket programming in Java

3. Create a multi-threaded client server application in Java.

Lab 4 To study and execute basic Linux commands on a terminal

To study and execute basic Linux commands on a terminal

Linux is a Unix-like and mostly POSIX-compliant computer operating system (OS) assembled
under the model of free and open-source software development and distribution. In this lab, we
will work on Ubuntu, one of the flavors of Linux. For this purpose, we will use virtualization
environment.

Lab Tasks:

1. Using ls command find out the contents of current directory

2. What are the permissions for normal user, group and world for each file

31
Lab2 To study and implement socket programming in Java

3. Find out the name of current working directory

32
Lab2 To study and implement socket programming in Java

4. Create a new folder named “lab os” using the mkdir command

5. Switch to the directory “lab os”

33
Lab2 To study and implement socket programming in Java

6. Create a file in the directory named “lab4.txt” using touch command

34
Lab2 To study and implement socket programming in Java

7. List down the contents of file using cat command. Try using “more” and “less” option

35
Lab2 To study and implement socket programming in Java

More:

36
Lab2 To study and implement socket programming in Java

8. Find out the space consumed by directory using “du” command

37
Lab2 To study and implement socket programming in Java

9. Copy the file to parent directory using cp command

38
Lab2 To study and implement socket programming in Java

10. Remove the file using rm command

11. Remove the directory using rmdir command

39
Lab2 To study and implement socket programming in Java

12. Check the free space on disk using df command

13. Change the password of the user using passwd command

40
Lab2 To study and implement socket programming in Java

14. Switch to super user, using the command “su”

15. Using the history command, list down the commands run on the terminal window

41
Lab2 To study and implement socket programming in Java

42
Lab2 To study and implement socket programming in Java

Lab 5 To study and execute system administration commands on a terminal

To study and execute system administration commands on a terminal


Instructions:

Linux comprises a set of commands for basic system administration. In this lab, we will study
these commands.

Lab Tasks:

1. Using the ‘uptime' command, since how long your system is running and the number of
users that are currently logged in.

2. Using the ‘w’, display the users currently logged in and their process along-with load
averages

43
Lab2 To study and implement socket programming in Java

3. Using the ‘users’ command, display the currently logged in users.

4. Using the ‘top’ command, display processor activity of your system and also displays
tasks managed by kernel in real-time.

44
Lab2 To study and implement socket programming in Java

5. Using ‘tar’ command, compress your home directory in Linux.

6. ‘lsof’ command to list all open files

45
Lab2 To study and implement socket programming in Java

7. Using the ‘last’ command, watch activity of ‘mint’ user in the system

46
Lab2 To study and implement socket programming in Java

8. Using the ‘env’ command, lists all the environment variables of your system. Use
‘echo’ command to print values of $HOME and $PATH

47
Lab2 To study and implement socket programming in Java

9. The ‘ps’ command displays about processes running in the system. Try option –ax, -u.

The ‘kill’ command can be used to terminate process. Using this command terminate some
processes of your system

48
Lab2 To study and implement socket programming in Java

10. ‘ifconfig’ command is used to show the configuration of internet on LINUX. Use this
command to find IP and MAC address of your computer

11. Using the ‘netstat’ command, show the status of your network

12. Using the ping command, to ping your localhost

49
Lab2 To study and implement socket programming in Java

13. Create a group named ‘student’ using groupadd

50
Lab2 To study and implement socket programming in Java

14. Create a file named ‘hello.txt’

15. Using the ‘useradd’ command create a user with your name in the group student

51
Lab2 To study and implement socket programming in Java

16. Change the owner of hello.txt to user you just created

17. Change the group owner of hello.txt to group student

52
Lab2 To study and implement socket programming in Java

Lab 6 To study and implement shell programming in Linux

To study and implement shell programming in Linux


Instructions:

1. A shell script is a computer program designed to be run by the Unix shell, a command-
line interpreter
2. The various dialects of shell scripts are considered to be scripting languages.
3. Typical operations performed by shell scripts include file manipulation, program
execution, and printing text.

Lab Tasks:

1. Write a script that backs itself up, that is, copies itself to a file named backup.sh.
Hint: Use the cat command

53
Lab2 To study and implement socket programming in Java

2. Write a script that echoes itself to stdout, but backwards.


Hint: Use the tac command

3. Perform a recursive directory listing on the user's home directory and save the
information to a file.

4. Write a script that reads each line of a target file, then writes the line back to stdout, but
with an extra blank line following. This has the effect of double-spacing the file.

54
Lab2 To study and implement socket programming in Java

55
Lab2 To study and implement socket programming in Java

5. Write a shell script that takes a command –line argument and reports on whether it is
directory, a file

6. Write a shell script program to display list of user currently logged in.

56
Lab2 To study and implement socket programming in Java

7. Shell script program to count number of files in a Directory.

57
Lab2 To study and implement socket programming in Java

Lab 7 To study and implement concurrency control techniques in Java

To study and implement concurrency control techniques in Java


Java provides the synchronized key word for implementing concurrency control while using
multi-threaded applications. In this lab, you will learn how to implement these techniques.

Instructions:

Create the following program in Java:

public class UnsynchronizedExample {


public static void main(String[] args) {
new PrintStringsThread("Hello ", "there.");
new PrintStringsThread("How are ", "you?");
new PrintStringsThread("Thank you ", "very much!");
}
}

public class PrintStringsThread implements Runnable {

Thread thread;
String str1, str2;

PrintStringsThread(String str1, String str2) {


this.str1 = str1;
this.str2 = str2;
thread = new Thread(this);
thread.start();
}

public void run() {


TwoStrings.print(str1, str2);
}

public class TwoStrings {

58
Lab2 To study and implement socket programming in Java

// This method is not synchronized


static void print(String str1, String str2) {
System.out.print(str1);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
System.out.println(str2);
}
}

Lab Tasks:

1. What output do you see? Explain the output.


2. ANSWER:
3. //unsynchronized class
4. /*
5. * To change this license header, choose License Headers in Project Properties.
6. * To change this template file, choose Tools | Templates
7. * and open the template in the editor.
8. */
9. package unsynchronizedexample;
10.
11. /**
12. *
13. * @author 7500
14. */
15. public class UnsynchronizedExample {
16.
17. /**
18. * @param args the command line arguments
19. */
20. public static void main(String[] args) {
21. // TODO code application logic here
22. new printstring("Hello ", "there.");
23. new printstring("How are ", "you?");
24. new printstring("Thank you ", "very much!");
25.
26. }
27.
28. }

59
Lab2 To study and implement socket programming in Java

29. //printstring class


30. /*
31. * To change this license header, choose License Headers in Project Properties.
32. * To change this template file, choose Tools | Templates
33. * and open the template in the editor.
34. */
35. package unsynchronizedexample;
36.
37. /**
38. *
39. * @author 7500
40. */
41. public class printstring implements Runnable{
42. Thread thread;
43. String str1, str2;
44.
45. printstring(String str1, String str2) {
46. this.str1 = str1;
47. this.str2 = str2;
48. thread = new Thread(this);
49. thread.start();
50. }
51.
52. @Override
53. public void run() {
54. two_strings.print(str1, str2);
55. }
56.
57.
58.
59. }
60. //two_strings class
61. /*
62. * To change this license header, choose License Headers in Project Properties.
63. * To change this template file, choose Tools | Templates
64. * and open the template in the editor.
65. */
66. package unsynchronizedexample;
67.
68. /**

60
Lab2 To study and implement socket programming in Java

69. *
70. * @author 7500
71. */
72. public class two_strings {
73. // This method is not synchronized
74. static void print(String str1, String str2) {
75. System.out.print(str1);
76. try {
77. Thread.sleep(500);
78. } catch (InterruptedException ie) {
79. }
80. System.out.println(str2);
81. }
82. }

83. Now use the synchronized methods to display the desired result.
84. //unsynchronized class:
85. /*
86. * To change this license header, choose License Headers in Project Properties.
87. * To change this template file, choose Tools | Templates
88. * and open the template in the editor.
89. */
90. package unsynchronizedexample;
91.
92. /**
93. *
94. * @author 7500
95. */

61
Lab2 To study and implement socket programming in Java

96. public class UnsynchronizedExample {


97.
98. /**
99. * @param args the command line arguments
100. */
101. public static void main(String[] args) {
102. // TODO code application logic here
103. new printstring("Hello ", "there.");
104. new printstring("How are ", "you?");
105. new printstring("Thank you ", "very much!");
106.
107. }
108.
109. }
110. //printstring class
111. /*
112. * To change this license header, choose License Headers in Project Properties.
113. * To change this template file, choose Tools | Templates
114. * and open the template in the editor.
115. */
116. package unsynchronizedexample;
117.
118. /**
119. *
120. * @author 7500
121. */
122. public class printstring implements Runnable{
123. Thread thread;
124. String str1, str2;
125.
126. printstring(String str1, String str2) {
127. this.str1 = str1;
128. this.str2 = str2;
129. thread = new Thread(this);
130. thread.start();
131. }
132.
133. @Override
134. public void run() {
135. two_strings.print(str1, str2);

62
Lab2 To study and implement socket programming in Java

136. }
137.
138.
139.
140. }
141. //two_string class
142. /*
143. * To change this license header, choose License Headers in Project Properties.
144. * To change this template file, choose Tools | Templates
145. * and open the template in the editor.
146. */
147. package unsynchronizedexample;
148.
149. /**
150. *
151. * @author 7500
152. */
153. public class two_strings {
154. // This method is not synchronized
155. static synchronized void print(String str1, String str2) {
156. System.out.print(str1);
157. try {
158. Thread.sleep(500);
159. } catch (InterruptedException ie) {
160. }
161. System.out.println(str2);
162. }
163. }

63
Lab2 To study and implement socket programming in Java

164. Now use the synchronized keyword on an object to synchronize.

ANSWER:

//UNSYNCHRONIZED CLASS

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package objectbased;

/**
*
* @author 7500
*/
public class UnsynchronizedExample {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new printstring("Hello ", "there.");
new printstring("How are ", "you?");
new printstring("Thank you ", "very much!");

//printstring class

/*
* To change this license header, choose License Headers in Project Properties.

64
Lab2 To study and implement socket programming in Java

* To change this template file, choose Tools | Templates


* and open the template in the editor.
*/
package objectbased;

/**
*
* @author 7500
*/
public class printstring implements Runnable{
Thread thread;
String str1, str2;
static two_strings ts=new two_strings();
printstring(String str1, String str2) {
this.str1 = str1;
this.str2 = str2;
thread = new Thread(this);
thread.start();
}

@Override
public void run() {

ts.print(str1,str2);

//two_strings class

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package objectbased;

/**
*
* @author 7500

65
Lab2 To study and implement socket programming in Java

*/
public class two_strings {
// This method is not synchronized
void print(String str1, String str2) {
synchronized(this){
System.out.print(str1);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
}
System.out.println(str2);
}
}
}

66
Lab 9 To study and implement process scheduling algorithms in
Java

Lab 9 To study and implement process scheduling algorithms in Java

To study and implement process scheduling algorithms in Java


Instructions:

In this lab, we will implement different CPU scheduling techniques.

Lab Tasks

1. Shortest Job First: The number of processes and burst time is input from the user. The
program should then print total access time, burst time and wait time for every process.
Also print the average wait time.
Hint: Sort the element based on their burst time

2. Simulate the First Come First Serve and Priority scheduling algorithm.

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package shotest_job_first;

import java.util.*;

import java.util.ArrayList;

import java.util.Scanner;

/**

* @author 7500

67
Lab 9 To study and implement process scheduling algorithms in
Java

*/

public class Shotest_job_first {

public int id;

public int bursttime ;

public int waittime;

public int priority;

public Shotest_job_first(int id, int bursttime, int priority){

this.id =id;

this.bursttime =bursttime;

this.priority =priority;

@Override

public String toString(){

return "Process : " + id + " Burst Time : " +this.bursttime + " Priority : " + this.priority + "
Wait Time : " + this.waittime;

/**

* @param args the command line arguments

*/

68
Lab 9 To study and implement process scheduling algorithms in
Java

//main class

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package shotest_job_first;

import java.util.ArrayList;

import java.util.Scanner;

/**

* @author 7500

*/

public class Process {

public static void main(String[] args) {

// TODO code application logic here

Scanner sc = new Scanner (System.in);

ArrayList< Shotest_job_first> al = new ArrayList< Shotest_job_first>();


69
Lab 9 To study and implement process scheduling algorithms in
Java

System.out.println("Enter the Number of Process : ");

int n = sc.nextInt();

for (int i=0;i<n ;i++){

System.out.printf("Enter the burst Time and Priority %d : " , i);

Shotest_job_first p = new Shotest_job_first(i,sc.nextInt(),sc.nextInt());

al.add(p);

System.out.println(al);

System.out.println("Based on SJF");

sjf(new ArrayList(al));

System.out.println("Based on priority");

priorityBased(new ArrayList(al));

System.out.println("Based on first come fist serve");

fcfs(new ArrayList(al));

public static void sjf (ArrayList al){

int currentTime = 0;

while (al.size()!= 0){

//Pick the shortest Job


70
Lab 9 To study and implement process scheduling algorithms in
Java

int s = 0;

for (int i = 1;i< al.size(); i++){

Shotest_job_first p = ( Shotest_job_first)al.get(i);

Shotest_job_first shortest = ( Shotest_job_first)al.get(s);

if(p.bursttime<shortest.bursttime){

s=i;

Shotest_job_first shortest = ( Shotest_job_first)al.get(s);

shortest.waittime=currentTime;

System.out.println(shortest);

currentTime += shortest.bursttime;

al.remove(s);

public static void priorityBased (ArrayList al){

int currentTime = 0;

while (al.size()!= 0)

//Pick the shortest Job

int s = 0;
71
Lab 9 To study and implement process scheduling algorithms in
Java

for (int i = 1;i< al.size(); i++){

Shotest_job_first p = ( Shotest_job_first)al.get(i);

Shotest_job_first highest = ( Shotest_job_first)al.get(s);

if(p.priority>highest.priority){

s=i;

Shotest_job_first shortest = ( Shotest_job_first)al.get(s);

shortest.waittime=currentTime;

System.out.println(shortest);

currentTime += shortest.bursttime;

al.remove(s);

public static void fcfs(ArrayList al)

int currentTime = 0;

for (int i = 1;i< al.size(); i++){

Shotest_job_first p = ( Shotest_job_first)al.get(i);

p.waittime=currentTime;
72
Lab 9 To study and implement process scheduling algorithms in
Java

System.out.println(p);

currentTime += p.bursttime;

73
74

You might also like