Professional Documents
Culture Documents
OS Lab - Record 2
OS Lab - Record 2
Record Work
BONAFIDECERTIFI-
CATE
Technology, Kattankulathur.
Examiner-1 Examiner-2
INDEX SHEET
4
fi
fi
EXPERIMENT 1
AIM:
Software Used:
Procedure:
5
3) Click New and make partition in storage device.
6
5) Allocate the Ram and Processor Core for the Ubuntu
operating system.
7
6) Start the Virtual Machine.
8
8) Wait till the installation of Ubuntu in the system.
9
Result:
10
EXPERIMENT 2
AIM:
Commands:
11
ff
fi
fi
fi
fi
fi
fi
fi
ff
fi
fi
fi
fi
fi
fi
19) wc lename: To nd number of lines in the le
20) zcat Filename: Used to view the zip le without unzipping it
21) gunzip: To uncompress the compressed le
22) gzip lename: To create a zip le and to reduce the space
used
23) more lename: It shows the rst part of the le
24) cal: To display the calendar in command line interface
25) hostname: To display the hostname
26) ping website: To display the ping of URL
27) banner display text: To display text as a banner
28) cat lename: Used to display the content of a le
29) date: To display present date and time
30) whoamiTo display the current user name
31) who: To display current users details
32) last -1: To display the last login time and details
33) nger: Get more details about the user
34) clear: To clear the terminal window
35) History: To display the history of all the commands
12
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
OUTPUT:
13
fi
fi
4) touch: To create a new le
14
fi
fi
6) echo:“Hello, World!” Displays any message given in the brackets
15
8) cp lename1 lename2 : To copy les
9) man: To learn about any linux command
16
fi
fi
fi
10) ls: To list all the directory
11) mv: To more le using command line
17
fi
12) nano: To use inbuilt text directory
13) df -m: To check all the available disk space in the system
18
14) sudo: To use SuperUser do mode
15) tar cvf lename1 *.txt:To create a tar le using command line inter-
face
19
fi
fi
16) du Directory: To nd the disk used using command line interface
17) uname -a: To show information about the system
20
fi
18) di : To nd the di erence between two les
19) wc lename: To nd number of lines in the le
21
ff
fi
fi
fi
ff
fi
fi
20) zcat Filename: Used to view the zip le without unzipping it
22
fi
fi
22) gzip lename: To create a zip le and to reduce the space used
23
fi
fi
fi
fi
fi
24) cal: To display the calendar in command line interface
25) hostname: To display the hostname
24
26) ping website: To display the ping of URL
25
28) cat lename: Used to display the content of a le
26
fi
fi
30) whoamiTo display the current user name
31) who: To display current users details
27
32) last -1: To display the last login time and details
33) nger: Get more details about the user
28
fi
34) clear: To clear the terminal window
29
EXPERIMENT - 3
AIM:
To write programs using shell scripting covering data types, con-
ditional, and looping and decision statements.
CODE:
Ex-1:
echo “HELLO"
Ex-2
for((i=1;i<=99;i=i+2))
do
echo $i
done
Ex-3
read NAME
echo "Welcome $NAME”
Ex-4
read NUM1
read NUM2
echo `expr $NUM1 + $NUM2`
echo `expr $NUM1 - $NUM2`
echo `expr $NUM1 \* $NUM2`
echo `expr $NUM1 / $NUM2`
30
Ex-5
read X
read Y
if (( $X > $Y ))
then
echo "X is greater than Y"
if (( $X == $Y))
then
echo "X is equal to Y"
if(( $X < $Y))
then
echo "X is less than Y"
Ex-6
read word
if [[($word == 'y') || ($word == 'Y')]]
then
echo "YES"
elif [[($word == 'n') || ($word == 'N')]]
then
echo "NO"
Ex-7
read x
printf "%.3f\n" `echo "$x" | bc -l`
Ex-8
read num
ctr=$num
sum=0
while [ $ctr -gt 0 ]
do
read x
sum=$((sum + x))
ctr=$((ctr - 1))
done
printf "%.3f\n" `echo "$sum/$num" | bc -l`
31
fi
fi
fi
fi
HACKER RANK SCREENSHOT:
RESULT:
We have successfully executed Linux shell questions.
32
EXPERIMENT 4.A
AIM:
To Execute Round robin scheduling algorithm.
CODE:
#include<iostream>
int t = 0;
while (1)
{
bool done = true;
rem_bt[i] -= quantum;
}
else
33
{
t = t + rem_bt[i];
wt[i] = t - bt[i];
/
rem_bt[i] = 0;
}
}
}
if (done == true)
break;
}
}
34
f
f
f
f
int main()
{
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int quantum = 2;
indavgTime(processes, n, burst_time, quantum);
return 0;
}
35
f
f
f
f
OUTPUT:
RESULT:
36
EXPERIMENT - 4.B
AIM:
To execute priority scheduling algorithm.
CODE:
#include<iostream>
int main() {
cin >> n;
p[i] = i + 1;
pos = i;
pos = j;
temp = pr[i];
pr[i] = pr[pos];
pr[pos] = temp;
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
temp = p[i];
p[i] = p[pos];
p[pos] = temp;
37
}
wt[0] = 0;
wt[i] = 0;
wt[i] += bt[j];
total += wt[i];
avg_wt = total / n;
total = 0;
total += tat[i];
cout << "\nP[" << p[i] << "]\t\t " << bt[i] << "\t\t " << wt[i] <<
"\t\t\t" << tat[i];
avg_tat = total / n;
return 0;
38
OUTPUT:
RESULT:
We have successfully executed priority scheduling.
39
EXPERIMENT - 4.C
AIM:
To execute rst come rst serve.
CODE:
#include<iostream>
wt[0] = 0;
<< " Waiting time " << " Turn around time\n";
40
fi
fi
fi
fi
fi
fi
fi
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
int main()
ndavgTime(processes, n, burst_time);
return 0;
41
fi
fl
fl
fl
fl
OUTPUT:
RESULT:
We have successfully implemented rst come rst serve
scheduling.
42
fi
fi
EXPERIMENT 5
AIM:
Write a program in C to implement reader-writer problem using
monitors. (pthread).
CODE:
#include <iostream>
#include <pthread.h>
#include <unistd.h>
class monitor
private:
// no. of readers
int rcnt;
// no. of writers
int wcnt;
int waitr;
int waitw;
pthread_cond_t canread;
pthread_cond_t canwrite;
pthread_mutex_t condlock;
public:
monitor()
43
rcnt = 0;
wcnt = 0;
waitr = 0;
waitw = 0;
pthread_cond_init(&canread, NULL);
pthread_cond_init(&canwrite, NULL);
pthread_mutex_init(&condlock, NULL);
void beginread(int i)
pthread_mutex_lock(&condlock);
waitr++;
// reader suspended
pthread_cond_wait(&canread, &condlock);
waitr--;
rcnt++;
pthread_mutex_unlock(&condlock);
pthread_cond_broadcast(&canread);
void endread(int i)
pthread_mutex_lock(&condlock);
if (--rcnt == 0)
pthread_cond_signal(&canwrite);
44
pthread_mutex_unlock(&condlock);
void beginwrite(int i)
pthread_mutex_lock(&condlock);
++waitw;
pthread_cond_wait(&canwrite, &condlock);
--waitw;
wcnt = 1;
pthread_mutex_unlock(&condlock);
void endwrite(int i)
pthread_mutex_lock(&condlock);
wcnt = 0;
if (waitr > 0)
pthread_cond_signal(&canread);
else
pthread_cond_signal(&canwrite);
pthread_mutex_unlock(&condlock);
M;
int c = 0;
45
// each reader attempts to read 5 times
while (c < 5)
usleep(1);
M.beginread(i);
M.endread(i);
c++;
int c = 0;
while (c < 5)
usleep(1);
M.beginwrite(i);
M.endwrite(i);
c++;
int main()
int id[5];
id[i] = i;
pthread_join(r[i], NULL);
46
}
pthread_join(w[i], NULL);
OUTPUT:
RESULT:
We have successfully implemented Write a program in C to
implement reader-writer problem using monitors. (pthread)
47
EXPERIMENT - 6
AIM:
Write a program in C to implement dinning philosopher’s problem
using semaphore.
CODE:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#de ne N 5
#de ne THINKING 2
#de ne HUNGRY 1
#de ne EATING 0
int state[N];
sem_t mutex;
sem_t S[N];
state[num_of_philosopher] = EATING;
sleep(2);
48
fi
fi
fi
fi
fi
fi
fi
fi
printf("Philosopher %d is Eating\n", num_of_philosopher + 1);
during takefork
during putfork */
sem_post(&S[num_of_philosopher]);
// take up Forks
sem_wait(&mutex);
state[num_of_philosopher] = HUNGRY;
test(num_of_philosopher);
sem_post(&mutex);
sem_wait(&S[num_of_philosopher]);
sleep(1);
sem_wait(&mutex);
state[num_of_philosopher] = THINKING;
49
ff
printf("Philosopher %d is thinking\n", num_of_philosopher + 1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
while (1)
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
int main()
int i;
pthread_t thread_id[N];
sem_init(&mutex, 0, 1);
sem_init(&S[i], 0, 0);
50
printf("Philosopher %d is thinking\n", i + 1);
pthread_join(thread_id[i], NULL);
51
OUTPUT:
RESULT:
52
EXPERIMENT - 7
AIM:
Create process using fork () system call and use getpid (), getppid
() functions along with wait () and exit () using C programming.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void)
if (pid == 0)
exit(EXIT_SUCCESS);
wait(NULL);
else
return EXIT_SUCCESS;
53
fi
fi
OUTPUT:
RESULT:
We have successfully implemented OS functions.
54
EXPERIMENT - 8
AIM:
To Write a program in C to implement shared memory using IPC.
CODE:
1) Reading
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
shmdt(str);
return 0;
55
fi
fi
2) Writing
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
int main()
shmdt(str);
return 0;
56
fi
OUTPUT:
RESULT:
We have successfully implement shared memory using IPC.
57
EXPERIMENT - 9
AIM:
To write a program in C to implement message queue using IPC.
Code:
// C Program for Message Queue (Writer Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#de ne MAX 10
struct mesg_bu er
long mesg_type;
char mesg_text[100];
} message;
int main()
key_t key;
int msgid;
message.mesg_type = 1;
return 0;
58
fi
ff
fi
fi
// C Program for Message Queue (Reader Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesg_bu er
long mesg_type;
char mesg_text[100];
} message;
int main()
key_t key;
int msgid;
message.mesg_text);
return 0;
59
ff
fi
fi
OUTPUT:
RESULT:
We have successfully implemented message passing IPC.
60
EXPERIMENT 10
AIM:
To write program to implement unidirectional pipe under IPC us-
ing C programming.
CODE:
#include <stdio.h>
#include <unistd.h>
int main()
int pipefds[2];
int returnstatus;
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1)
return 1;
return 0;
61
OUTPUT:
RESULT:
We have successfully implemented Unidirectional PIPE using IPC.
62
EXPERIMENT - 9
AIM:
To understand the overlay concepts and practice how to overlay
the current process to new process in Linux using C.
CODE:
Step 1 : hello #include<stdio.h> void main()
printf("Hello World\n”);
return;
int value = 1;
printf("%d\t", value);
Value++;
printf("\n");
Return;
63
Step 3 : Overlay 1
void
main()
return;
Step 4 : Overlay 2
void
main()
int pid;
pid = fork();
64
else
{ /* Parent process */
sleep(3);
return;
Step 5 Overlay 2
void
int pid;
int err;
int num_times;
char num_times_str[5];
65
/* In no command line arguments are passed, then loop maximum count taken as
10 */ if (argc == 1)
num_times = 10;
else
strcpy(num_times_str, argv[1]);
pid = fork();
66
else
{ /* Parent process */
sleep(3);
return;
OUTPUT:
67
68
RESULT:
69
EXPERIMENT - 12
AIM:
Implement the C program in which the child process calculates
the sum of odd numbers and the parent process calculate the
sum of even numbers up to the number ‘n’.
CODE:
#include <iostream>
#include <unistd.h>
int main()
n = fork();
if (n > 0)
if (a[i] % 2 == 0)
cout << "Sum of even no. is " << sumEven << endl;
else
if (a[i] % 2 != 0)
cout << "\nSum of odd no. is " << sumOdd << endl;
return 0;
70
OUTPUT:
RESULT:
We have successfully implemented the C program in which
the child process calculates the sum of odd numbers and the
parent process calculate the sum of even numbers up to the
number ‘n’.
71
EXPERIMENT - 13
AIM:
Implement the C program in which main program accepts the in-
tegers to be sorted Main program uses the fork system call to
create a new process called a child process. Parent process sorts
the integers using insertion sort and waits for child process using
wait system call to sort the integers using selection sort.
CODE:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int i, j, temp;
temp = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = temp;
72
{
min = i;
min = j;
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
void fork1()
scanf("%d", &n);
scanf("%d", &arr[i]);
if (pid == 0)
sleep(10);
printf("\nchild process\n");
73
selectionSort(arr, n);
printf("\n");
printf("%d,", arr[i]);
printf("\b");
system("ps -x");
else
printf("\nparent process\n");
insertionSort(arr, n);
printf("\n");
printf("%d,", arr[i]);
printf("\n\n\n");
int main()
fork1();
return 0;
74
OUTPUT:
RESULT:
We have successfully implemented the process sort program.
75
EXPERIMENT - 14
AIM:
To perform bindshell using Nasm in Linux 64bit shell code.
Procedure:
Step 2: First Check whether nasm is install or not. If not then in-
as - $nasm -h
Step 4: For running the bindshell, write the code and save it
76
Step 6: Now open new terminal and type
$netstat –nlt //to check whether 4444 port is open after executing
bindshell
of directory
section wise.
PROGRAM:
BITS 64
;bind
77
push 0x5c110102 mov [rsp+1],al mov rsi,rsp
;listen
;accept
;dup2
inc al
inc al
;exec
xor rdx,rdx
push rbx mov rdi,rsp xor rax,rax push rax push rdi mov rsi,rsp mov al,0x3b
syscall
78
OUTPUT:
RESULT:
79
fi
EXPERIMENT - 15
AIM:
To Analyze binary le in Linux.
ANALYSIS:
• File This will be your starting point for binary analysis. We
work with les daily. Not everything is an executable type;
there is a whole wide range of le types out there. Before you
start, you need to understand the type of le that is being an-
alyzed. Is it a binary le, a library le, an ASCII text le, a
video le, a picture le, a PDF, a data le, etc.?
virtual-machine:~$ le /bin/ls
• ldd command comes into the picture. Running it against a
dynamically linked binary shows all its dependent libraries
and their paths.
virtual-machine:~$ldd /bin/ls
• ltrace command displays all the functions that are being
called at run time from the library. In the below example, you
can see the function names being called, along with the ar-
guments being passed to that function. You can also see
what was returned by those functions on the far right side of
the output.
virtual-machine:~$ltrace ls
• Hexdump helps you see what exactly the le contains. You
can also choose to see the ASCII representation of the data
present in the le using some command-line options.
virtual-machine:~$hexdump -C /bin/ls | head
• When software is being developed, a variety of text/ASCII
messages are added to it, like printing info messages, de-
bugging info, help messages, errors, and so on. Provided all
this information is present in the binary, it will be dumped to
screen using strings.
80
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
virtual-machine:~$strings /bin/ls
• ELF (Executable and Linkable File Format) is the dominant
le format for executable or binaries, not just on Linux but a
variety of UNIX systems as well. If you have utilized tools like
le command, which tells you that the le is in ELF format,
the next logical step will be to use the readelf command and
its various options to analyze the le further.
virtual-machine:~$readelf -h /bin/ls
• objdump utility reads the binary or executable le and
dumps the assembly language instructions on the screen.
Knowledge of assembly is critical to understand the output of
the objdump command.
virtual-machine:~/OS$ objdump -d /bin/ls | head
• The strace utility traces system calls. System calls are how
you interface with the kernel to get work done
virtual-machine:~$strace -f /bin/ls
• The nm command will provide you with the valuable informa-
tion that was embedded in the binary during
compilation. nm can help you identify variables and functions
from the binary
jvirtual-machine:~/OS$ nm pipe | tail
• gdb is the defacto debugger. It helps you load a program, set
breakpoints at speci c places, analyze memory and CPU reg-
ister, and do much more. It complements the other tools men-
tioned above and allows you to do much more runtime analy-
sis.
RESULT:
82
fi