Linux Lab
Linux Lab
A R T I F I CI A L I N T E L L I G E N C
E AND
D A T A S C I E N CE
PRACTICAL MANUAL
Year/Semester/Sec:
Dr. J. Madhusudanan
1
Department Artificial Intelligence and Data Programme: B.Tech.
Science
Semester IV Course Category: ES End Semester Exam Type:
TE
Periods Credit Maximum Marks
Course Code U23ADB401 / Week
L T P C CAM ESE
Course Name Linux Internals 2 - 2 3 50 50
(Common to all Branches)
Prerequisite Any Programming Knowledge
On completion of the course, the students will be able to
2
Implement shared memory form of IPC.
Write a C program, using sockets create client and server socket programs. Write
a TCP iterative server program, in server program take user input for port number
and bind the port address. Server waits for
clients to connect. When client connects, communication can happen using recv
and sent functions.
TABLE OF CONTENTS
3
TABLE OF CONTENTS
4
UNIT - IV
5
EX.NO: Various Linux system and file management
DATE : commands
AIM
To learn and understand the usage of various Linux system and file management commands such as man,
passwd, tty, script,etc.
PROCEDURE
Set up a Linux environment using a virtual machine, WSL, or a live Linux distribution.
Open the terminal and execute each command with different options to explore its behavior.
Use the manual (man) command to understand the options available for each command.
Record the commands and their outputs to analyze the results.
PROGRAM
1) man
2) passwd
3) tty
4) script
5) clear
6
6) date
7) cal
8) cp
9) mv
10) ln
11) rm
12) unlink
13) mkdir
14) rmdir
7
Usage: Remove empty directories.
Command: rmdir mydir
Output: Deletes the mydir directory if it is empty.
15) du
16) df
17) mount
18) umount
19) find
20) umask
21) ulimit
22)
8
Usage: Display process status.
Command: ps
Output: Lists currently running processes with details like PID and TTY.
23) who
RESULT:
9
EX.NO: Various Linux commands for file
DATE: manipulation, text processing, and data
archiving
AIM:
To study and practice various Linux commands for file manipulation, text processing, and data archiving,
such as cat, tail, head, sort, nl, uniq, grep, awk, tar, and others.
PROCEDURE
1. Set up a Linux environment using a virtual machine, WSL, or a live Linux distribution.
2. Open the terminal and execute each command individually with relevant files to explore its functionality.
3. Use the man command to read the manual and understand the available options for each command.
4. Create sample text files to experiment with file management and text processing commands.
5. Use multiple options for each command to analyze and understand their variations.
6. Record the commands, their usage, and the output generated for each operation.
7. For commands like tar and cpio, create multiple files to experiment with archiving and compression
functionalities.
PROGRAM:
1) cat
2) tail
3) head
4) sort
10
5) nl
6) uniq
7) grep
8) egrep
9) fgrep
10) cut
11) paste
12) join
11
13) tee
14) pg
15) comm
16) cmp
17) diff
18) tr
19) awk
20) tar
21) cpio
RESULT:
13
EX.NO: Shell script that prints the names of all
DATE: .txt and .c files
AIM:
To write a shell script that prints the names of all .txt and .c files in the current
directory.
PROCEDURE
PROGRAM:
#!/bin/bash
ls *.txt 2>/dev/null
INPUT:
file1.txt
file2.txt
program.c
example.c
$ ./list_files.sh
14
OUTPUT:
Listing all .txt files:
file1.txt
file2.txt
Listing all .c files:
program.c
example.c
RESULT:
15
EX.NO: shell script to move a set of files
DATE:
AIM:
PROCEDURE
PROGRAM:
#!/bin/bash
# Shell script to move a set of files to a specified directory
echo "Enter the names of the files to move (separated by space):"
read -a files
16
OUTPUT:
Assume the current directory contains file1.txt and file2.txt, and the destination directory is
/home/user/documents.
$ ./move_files.sh
file1.txt file2.txt
/home/user/documents
2) Non-existent file:
$ ./move_files.sh
file3.txt
/home/user/documents
RESULT:
17
EX.NO:
DATE:
Shell script that displays all the users
AIM:
To write a shell script that displays all the users who are currently logged in after a specified time.
PROCEDURE
PROGRAM:
#!/bin/bash
# Shell script to display all the users who are currently logged in after a specified time
echo "Enter the time (in seconds) after which you want to display the logged-in users:"
read delay_time
sleep $delay_time
who
18
OUTPUT:
$ ./logged_in_users.sh
Enter the time (in seconds) after which you want to display the logged-in users:
10
$ ./logged_in_users.sh
Enter the time (in seconds) after which you want to display the logged-in users:
RESULT:
19
EX.NO:
DATE:
Shell script that wishes the user
based on the login time
AIM:
To write a shell script that wishes the user based on the login time (Good Morning, Good Afternoon, or Good
Evening).
PROCEDURE:
PROGRAM:
#!/bin/bash
current_hour=$(date +%H)
else
fi
20
OUTPUT
$ ./wish_user.sh
Good Morning, user!
RESULT:
21
EX.NO:
DATE: Shell programs to simulate the functionality of the
cat and cp commands
AIM
To write shell programs to simulate the functionality of the cat and cp commands.
PROCEDURE
--> Open the file and display its contents on the terminal.
5. Save the scripts with .sh extensions and make them executable using the chmod command.
6. Execute the scripts to simulate the cat and cp commands.
PROGRAM:
#!/bin/bash
if [ $# -ne 1 ]; then
exit 1
fi
if [ ! -f $1 ]; then
22
echo "File not found!"
exit
fi
cat $1
2) Simulate cp Command:
#!/bin/bash
if [ $# -ne 2 ]; then
exit 1
fi
if [ ! -f $1 ]; then
exit 1
fi
cp $1 $2
23
OUTPUT:
$ ./simulate_cat.sh testfile.txt
$ ./simulate_cat.sh non_existent_file.txt
RESULT:
24
EX.NO:
DATE:
Simulate the functionality of the head and tail
commands
AIM:
To write shell programs that simulate the functionality of the head and tail commands.
PROCEDURE:
5. Save the scripts with .sh extensions and make them executable using the chmod command.
6. Execute the scripts to simulate the head and tail commands.
PROGRAM:
#!/bin/bash
if [ $# -ne 1 ]; then
exit 1
fi
25
if [ ! -f $1 ]; then
exit 1
fi
head -n 10 $1
#!/bin/bash
if [ $# -ne 1 ]; then
exit 1
fi
if [ ! -f $1 ]; then
exit 1
fi
tail -n 10 $1
26
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
27
Unit
UNIT V
28
EX.NO: Handles the signals SIGINT, SIGQUIT, and
DATE:
SIGFPE in C
AIM:
To write a program that handles the signals SIGINT, SIGQUIT, and SIGFPE.
PROCEDURE:
PROGRAM:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
exit(0);
exit(0);
29
}
exit(0);
int main() {
signal(SIGINT, handle_sigint);
signal(SIGQUIT, handle_sigquit);
signal(SIGFPE, handle_sigfpe);
int x = 0;
return 0;
OUTPUT:
$ ./signal_handler
30
Program started. Waiting for signals.
^C
^\
RESULT:
EX.NO:
31
DATE: IPC MECHANISMS USING FIFO AND PIPE
AIM:
To implement IPC Mechanism using FIFO and PIPE.
PROCEDURE
I) Create a named pipe using the mkfifo() system call or mknod command.
II) Write one program to write data into the FIFO and another program to read data from it.
I) Use the pipe() system call to create an unnamed pipe for communication between parent and child processes.
3) Compile the programs using GCC and execute them as per the specified roles (reader/writer).
PROGRAMS
(a) FIFO (Named Pipe):
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
const char *fifo = "/tmp/myfifo"; // FIFO file path
char message[100];
// Create a FIFO
if (mkfifo(fifo, 0666) == -1) {
perror("mkfifo");
exit(EXIT_FAILURE);
32
int fd = open(fifo, O_WRONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
close(fd);
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
pid_t pid;
char read_msg[100];
if (pipe(pipefd) == -1) {
33
perror("pipe");
exit(EXIT_FAILURE);
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
return 0;
34
OUTPUT:
Terminal 1 (Reader):
$ ./fifo_reader
$ ./pipe_example
RESULT:
35
EX.NO: Implement message queue form of IPC
DATE:
AIM
To implement Inter-Process Communication (IPC) using the message queue mechanism in Linux.
PROCEDURE
PROGRAMS
Sender Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main() {
int msg_id;
// Send a message
message.msg_type = 1; // Message type
printf("Enter a message: ");
fgets(message.msg_text, sizeof(message.msg_text), stdin);
36
Receiver Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int main() {
int msg_id;
// Receive a message
if (msgrcv(msg_id, &message, sizeof(message.msg_text), 1, 0) == -1) {
perror("msgrcv");
exit(EXIT_FAILURE);
}
return 0;
}
Execution Steps:
./receiver
./sender
OUTPUT:
Sender Program Output:
$ ./sender
Enter a message: Hello, Message Queue!
Message sent: Hello, Message Queue!
$ ./receiver
Message received: Hello, Message Queue!
RESULT:
38
EX.NO: Inter-Process Communication (IPC) using the
DATE:
shared memory mechanism
AIM:
To implement Inter-Process Communication (IPC) using the shared memory mechanism in
Linux.
PROCEDURE
PROGRAMS
Writer Program:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main() {
int shmid;
char *shared_memory;
return 0;
}
Reader Program:
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main() {
int shmid;
char *shared_memory;
return 0;
}
Execution Steps
./writer
./reader
OUTPUT:
41
RESULT:
42
EX.NO: TCP iterative server and client
DATE:
socket program
AIM:
To implement a TCP iterative server and client socket program in C, where the server takes user input for
the port number, binds to the specified port, and waits for client connections.
PROCEDURE
1) Server Program:
2) Client Program:
PROGRAM:
Server Program (TCP Iterative Server):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int server_fd, client_fd, port;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;
char buffer[BUFFER_SIZE];
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
43
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
while (1) {
client_len = sizeof(client_addr);
// Accept client connection
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd == -1) {
perror("Accept failed");
continue;
}
Client Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
char server_ip[16];
int port;
// Create socket
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Connect to server
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Connection failed");
close(client_fd);
exit(EXIT_FAILURE);
}
printf("Connected to server.\n");
if (strcmp(buffer, "exit") == 0) {
printf("Exiting...\n");
break;
}
Execution Steps
./server
46
Run the client program in another terminal:
./client
Communicate:
OUTPUT:
Server Output:
Enter the port number to bind: 8080
Server is listening on port 8080...
Connection established with client.
Client: Hello, Server!
Client Output:
Enter server IP address: 127.0.0.1
Enter server port number: 8080
Connected to server.
Enter message to send (type 'exit' to quit): Hello, Server!
Server: Hello, Server!
Enter message to send (type 'exit' to quit): exit
Exiting...
47
` MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
48
EX.NO:
DATE: TCP iterative server-client program
AIM:
To implement a TCP iterative server-client program in C, where the server
accepts client connections, communicates using recv() and send() functions, and
handles the communication iteratively.
Procedure
Setup Environment:
1. Create a client program that connects to the server using its IP address and port number.
2. Use the socket() and connect() functions to establish a connection to the server.
3. Send and receive messages using send() and recv() functions.
4. Close the socket upon user request or termination.
1. Use the GCC compiler to compile the server and client programs separately.
Validate Communication:
1. Send multiple messages from the client to the server and verify that the server echoes responses.
2. Ensure that the connection terminates gracefully when the client sends an exit message.
Document Results:
49
1. Record the input, output, and observed behavior of the server and client during communication.
2. Note any errors or edge cases encountered and how they were resolved.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int server_fd, client_fd, port;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;
char buffer[BUFFER_SIZE];
while (1) {
// Step 6: Accept a client connection
client_len = sizeof(client_addr);
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd == -1) {
perror("Accept failed");
continue; // Continue accepting other clients
}
while (1) {
// Step 7: Receive data from the client
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (bytes_received == -1) {
perror("Receive failed");
break;
} else if (bytes_received == 0) {
printf("Client disconnected.\n");
break;
}
// Step 10: Close the server socket (Unreachable in this loop but added for
completeness)
51
close(server_fd);
return 0;
}
Client Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
char server_ip[16];
int port;
printf("Connected to server.\n");
if (strcmp(buffer, "exit") == 0) {
printf("Exiting...\n");
break;
}
memset(buffer, 0, BUFFER_SIZE);
recv(client_fd, buffer, BUFFER_SIZE, 0);
printf("Server: %s\n", buffer);
}
53
Execution Steps
./server
./client
Test Communication:
· In the client terminal, type messages to send to the server.
· Observe the server's response echoing the received messages.
OUTPUT
Server Output:
Client Output:
54
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
55