Unix Lab Manual Bca 5
Unix Lab Manual Bca 5
Lab Manual
BCA 5th SEM
I. SHELL commands in UNIX
sh
ls # Basic list of files in the current directory
ls -l # Detailed list, including permissions, size, and modification time
ls -a # Show hidden files (files starting with .)
Output
2. Change Directory
sh
cd Documents # Change to the "Documents" directory
cd .. # Go up one directory level
cd ~ # Go to the user's home directory
Output
sh
pwd # Shows the full path of the current directory
Output
sh
Output
5. Remove an Empty Directory
sh
Output
sh
touch example.txt # Creates an empty file named "example.txt" or updates the timestamp if it exists
Output
sh
cp file1.txt file2.txt # Copy "file1.txt" to a new file named "file2.txt"
cp -r folder1 folder2 # Copy "folder1" and its contents to "folder2"
Output
sh
Output
sh
rm unwanted_file.txt # Delete "unwanted_file.txt"
rm -r unwanted_folder # Delete "unwanted_folder" and all its contents recursively
Output
Viewing File Contents
sh
cat file.txt # Show the contents of "file.txt"
Output
sh
more file.txt # View "file.txt" one screen at a time
less file.txt # View "file.txt" with navigation options
Output
sh
head file.txt # Display the first 10 lines of "file.txt"
head -n 5 file.txt # Show the first 5 lines of "file.txt"
tail file.txt # Display the last 10 lines of "file.txt"
tail -n 5 file.txt # Show the last 5 lines of "file.txt"
Output
System Information
sh
whoami # Display the current user's name
Output
2. System Information
sh
uname -a # Detailed information about the system
Output
3. Disk Space
sh
df # Show available disk space on file systems
df -h # Disk space in human-readable format (KB, MB, etc.)
Output
sh
top # Display running processes and system usage in real-time
Output
5. List Processes
sh
ps # List processes running in the current shell
ps aux # Show all running processes with details
Output
sh
chmod 755 script.sh # Set permissions to read/write/execute for the owner, read/execute for others
Output
sh
chown user:group file.txt # Change owner and group of "file.txt" to "user" and "group"
Output
Text Print and Manipulation Commands
sh
echo "Hello, Unix!" # Print "Hello, Unix!" to the terminal
echo "User: $USER" # Print the value of the environment variable $USER
Output
2. printf – Similar to echo, but allows formatted text (like C’s printf).
sh
printf "Name: %s, Age: %d\n" John 25 # Print formatted output: "Name: John, Age: 25"
Output
sh
cat file.txt # Print the contents of "file.txt"
cat file1.txt file2.txt # Concatenate and display contents of both files
Output
sh
tac file.txt # Display "file.txt" contents in reverse order
Output
sh
nl file.txt # Display "file.txt" with line numbers
Output
6. head – Display the first few lines of a file.
sh
head -n 5 file.txt # Print the first 5 lines of "file.txt"
Output
sh
tail -n 5 file.txt # Print the last 5 lines of "file.txt"
Output
sh
more file.txt # Display "file.txt" with pagination
Output
sh
less file.txt # View "file.txt" with more navigation options
Output
10. awk – A powerful text processing command, often used for parsing and printing specific parts of a file.
sh
awk '{print $1}' file.txt # Print the first field of each line in "file.txt"
Output
sh
sed 's/old/new/g' file.txt # Replace "old" with "new" in "file.txt"
Output
12. grep – Search for patterns in text and print matching lines.
sh
grep "pattern" file.txt # Print lines in "file.txt" that match "pattern"
Output
sh
echo -e "Line1\nLine2\nLine3" # Print multiple lines
Output
Output
sh
cat file.txt | grep "error" # Display lines with "error" from "file.txt"
Output
sh
echo "Hello, World!" # Print "Hello, World!" to the terminal
Output
II. Programs using Inter Process Communication (IPC)
To execute C programs that use IPC mechanisms in UNIX, you need to follow these steps:
1. Write the Code in a File: Save each program in a separate .c file, e.g., pipe_example.c,
message_queue_example.c, etc.
2. Compile the Program Using GCC: Use the GNU Compiler Collection (gcc) to compile the C
programs. You can run each program using the following command in your terminal:
bash
gcc -o program_name file_name.c
3. Run the Executable: After successful compilation, run the program by typing:
bash
./program_name
1. Pipes
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
int main() {
int pipefds[2];
char buffer[5];
if (fork() == 0) {
close(pipefds[0]); // close reading end
write(pipefds[1], "Hello", 5);
close(pipefds[1]); // close writing end
} else {
close(pipefds[1]); // close writing end
read(pipefds[0], buffer, 5);
printf("Received: %s\n", buffer);
close(pipefds[0]); // close reading end
}
return 0;
}
Compile the Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
struct msgbuf
{
long mtype;
char mtext[100];
};
int main()
{
int msgid;
struct msgbuf message;
{
perror("msgget");
exit(1);
// prepare message
message.mtype = 1;
// Message type
// Clean up
msgctl(msgid, IPC_RMID, NULL); // Remove the message queue
return 0;
}
Create Receiver Program - Save the file as msg_receiver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
struct msgbuf
{
long mtype;
char mtext[100];
};
int main()
{
int msgid;
struct msgbuf message;
// Create or connect to the message queue (you can use the same key as in sender)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
int main()
{
int shmid;
char *data;
shmdt(data);
return 0;
}
Create the Reader Program - Save the file as shm_reader.c.
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
int main()
{
int shmid;
char *data;
// Create or connect to the shared memory segment (use the same key as in writer)
shmid = shmget(IPC_PRIVATE, SHM_SIZE, 0666 | IPC_CREAT);
if (shmid < 0) {
perror("shmget");
exit(1);
}
// Attach the shared memory segment
data = (char *)shmat(shmid, NULL, 0);
if (data == (char *)(-1))
{
perror("shmat");
exit(1);
}
// Read from the shared memory
printf("Reader: Data read from shared memory: %s\n", data);
// Detach from shared memory
shmdt(data);
// Remove the shared memory segment
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
int main()
{
int shmid, semid;
char *data;
// Create semaphore
semid = semget(SEM_KEY, 1, 0666 | IPC_CREAT);
if (semid < 0) {
perror("semget");
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <string.h>
#define SHM_SIZE 1024 // Size of the shared memory
#define SEM_KEY 1234 // Semaphore key
void P(int semid)
{
// Wait (decrement)
struct sembuf sb = {0, -1, 0};
semop(semid, &sb, 1);
}
void V(int semid)
{
// Signal (increment)
struct sembuf sb = {0, 1, 0};
semop(semid, &sb, 1);
}
int main()
{
int shmid, semid;
char *data;
// Create shared memory
shmid = shmget(IPC_PRIVATE, SHM_SIZE, 0666 | IPC_CREAT);
if (shmid < 0) {
perror("shmget");
exit(1);
}
// Get semaphore
semid = semget(SEM_KEY, 1, 0666);
if (semid < 0) {
perror("semget");
exit(1);
}
// Attach the shared memory
data = (char *)shmat(shmid, NULL, 0);
if (data == (char *)(-1)) {
perror("shmat");
exit(1);
}
// Consume data
P(semid); // Wait
printf("Consumer: %s\n", data);
V(semid); // Signal
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
// Accept a connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
// Read data from the client
read(new_socket, buffer, 1024);
printf("Received: %s\n", buffer);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080
int main()
{
int sock = 0;
struct sockaddr_in serv_addr;
char *message = "Hello from client";
char buffer[1024] = {0};
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
return 0;
}
Compile the Programs: gcc -o tcp_server tcp_server.c gcc -o tcp_client tcp_client.c
Execute the Programs
1. First, run the server in one terminal window: ./tcp_server
2. In another terminal window, run the client: ./tcp_client