Operating System: (Fall/ Spring 20 - )
Operating System: (Fall/ Spring 20 - )
Lab Manual
[Fall/ Spring 20__]
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?
/*
*/
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
*/
try{
out.print("Hello ");
out.println("world");
out.close();
catch(Exception s){
/**
5
* @param args the command line arguments
*/
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:
/*
*/
package filetest2;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
*/
{
7
try{
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.close();
/**
*/
fileTest.fileWrite();
8
}
ANSWER 4:
/*
9
* To change this license header, choose License Headers in Project Properties.
*/
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;
/**
*/
try{
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.close();
11
/**
*/
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.
/*
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 java.util.StringTokenizer;
/**
*/
try{
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.close();
}*/
14
try{
String text;
while((text=in.readLine()) != null){
}}
catch(Exception s){
in.close();
/**
15
*/
fileTest.fileWrite();
//fileTest.consoleRead();
fileTest.fileRead();
16
Lab2 To study and implement socket programming in Java
Lab 2
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.
/*
*/
package getip;
17
Lab2 To study and implement socket programming in Java
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author 7500
*/
/**
*/
InetAddress myIP=InetAddress.getLocalHost();
*/
System.out.println(myIP.getHostAddress());
18
Lab2 To study and implement socket programming in Java
/*
* 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
/*
*/
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
*/
String message1=("salam");
String message2=("salassssssssssm");
// comunication
dos.writeBytes(message1);
dos.writeBytes(message2);
System.out.println();
dos.close();
ostream.close();
ss.close();
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
import java.util.logging.Level;
import java.util.logging.Logger;
/*
*/
25
Lab2 To study and implement socket programming in Java
/**
* @author 7500
*/
String name;
this.name=n;
@Override
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
26
Lab2 To study and implement socket programming in Java
/*
*/
/**
27
Lab2 To study and implement socket programming in Java
* @author 7500
*/
for(int i=0;i<5;i++){
t.start();
}}}
TASKJ#03:
/*
*/
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
*/
Socket csocket;
MultiThreadServer(Socket csocket) {
this.csocket = csocket;
/**
*/
System.out.println("Listening");
while (true) {
System.out.println("Connected");
29
Lab2 To study and implement socket programming in Java
@Override
try {
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
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:
2. What are the permissions for normal user, group and world for each file
31
Lab2 To study and implement socket programming in Java
32
Lab2 To study and implement socket programming in Java
4. Create a new folder named “lab os” using the mkdir command
33
Lab2 To study and implement socket programming in Java
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
37
Lab2 To study and implement socket programming in Java
38
Lab2 To study and implement socket programming in Java
39
Lab2 To study and implement socket programming in Java
40
Lab2 To study and implement socket programming in Java
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
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
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
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
49
Lab2 To study and implement socket programming in Java
50
Lab2 To study and implement socket programming in Java
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
52
Lab2 To study and implement socket programming in Java
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
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
57
Lab2 To study and implement socket programming in Java
Instructions:
Thread thread;
String str1, str2;
58
Lab2 To study and implement socket programming in Java
Lab Tasks:
59
Lab2 To study and implement socket programming in Java
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
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
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
/**
*
* @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 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.
/*
*/
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
*/
this.id =id;
this.bursttime =bursttime;
this.priority =priority;
@Override
return "Process : " + id + " Burst Time : " +this.bursttime + " Priority : " + this.priority + "
Wait Time : " + this.waittime;
/**
*/
68
Lab 9 To study and implement process scheduling algorithms in
Java
//main class
/*
*/
package shotest_job_first;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @author 7500
*/
int n = 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));
fcfs(new ArrayList(al));
int currentTime = 0;
int s = 0;
Shotest_job_first p = ( Shotest_job_first)al.get(i);
if(p.bursttime<shortest.bursttime){
s=i;
shortest.waittime=currentTime;
System.out.println(shortest);
currentTime += shortest.bursttime;
al.remove(s);
int currentTime = 0;
while (al.size()!= 0)
int s = 0;
71
Lab 9 To study and implement process scheduling algorithms in
Java
Shotest_job_first p = ( Shotest_job_first)al.get(i);
if(p.priority>highest.priority){
s=i;
shortest.waittime=currentTime;
System.out.println(shortest);
currentTime += shortest.bursttime;
al.remove(s);
int currentTime = 0;
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