0% found this document useful (0 votes)
2 views

Assignment IPC

This document provides step-by-step instructions for creating two C programs: my_cp, which copies files byte by byte or chunk by chunk using system calls, and my_cat, which displays file contents or reads from standard input. It includes details on writing the code, compiling the programs, moving them to the ~/bin directory, and ensuring that ~/bin is in the PATH variable. The document emphasizes testing the programs after implementation to ensure they function as intended.

Uploaded by

naincy kumari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment IPC

This document provides step-by-step instructions for creating two C programs: my_cp, which copies files byte by byte or chunk by chunk using system calls, and my_cat, which displays file contents or reads from standard input. It includes details on writing the code, compiling the programs, moving them to the ~/bin directory, and ensuring that ~/bin is in the PATH variable. The document emphasizes testing the programs after implementation to ensure they function as intended.

Uploaded by

naincy kumari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment

1.write a program to copy files(Byte by byte) using open, read, write and close system
calls. The implementation should work like cp command. The executable should be
named as my_cp that should be linked in ~/bin directory (make sure ~/bin is set in
PATH variable). Usage:$ my_cp<source_file_path> (destination_file_path> write code
in ubuntu step by step?

Sure, here's a step-by-step guide to create a program named my_cp that copies files
byte by byte using system calls like open, read, write, and close in Ubuntu:

1. Create a new C file: Open a terminal and navigate to the directory where you want
to create the C file. Then, use a text editor like nano or vim to create a new file
named my_cp.c.

nano my_cp.c

Copy and paste the following code into my_cp.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#define BUFFER_SIZE 4096

int main(int argc, char *argv[]) {

if (argc != 3) {

printf("Usage: %s <source_file> <destination_file>\n", argv[0]);


exit(EXIT_FAILURE);

int source_fd, dest_fd;

ssize_t bytes_read, bytes_written;

char buffer[BUFFER_SIZE];

// Open source file

source_fd = open(argv[1], O_RDONLY);

if (source_fd == -1) {

perror("Error opening source file");

exit(EXIT_FAILURE);

// Create or truncate destination file

dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

if (dest_fd == -1) {

perror("Error opening destination file");

exit(EXIT_FAILURE);

// Copy data

while ((bytes_read = read(source_fd, buffer, BUFFER_SIZE)) > 0) {


bytes_written = write(dest_fd, buffer, bytes_read);

if (bytes_written != bytes_read) {

perror("Error writing to destination file");

exit(EXIT_FAILURE);

if (bytes_read == -1) {

perror("Error reading source file");

exit(EXIT_FAILURE);

// Close files

if (close(source_fd) == -1) {

perror("Error closing source file");

exit(EXIT_FAILURE);

if (close(dest_fd) == -1) {

perror("Error closing destination file");

exit(EXIT_FAILURE);

printf("File copied successfully.\n");


exit(EXIT_SUCCESS);

1. Save and exit the text editor: In nano, press Ctrl + O to save the file and Ctrl + X
to exit.

2. Compile the program: Compile the my_cp.c file using gcc:

gcc -o my_cp my_cp.c

Move the executable to ~/bin: If ~/bin directory doesn't exist, create it

mkdir -p ~/bin

Then, move the my_cp executable to ~/bin:

mv my_cp ~/bin/

Ensure ~/bin is in PATH: If ~/bin is not already in your PATH, you can add it by
editing your ~/.bashrc or ~/.profile file and appending the following line:

export PATH="$PATH:~/bin"

1. Then, either reopen your terminal or run source ~/.bashrc (or source ~/.profile)
to apply the changes.

2. Test the program: Now you can use my_cp from any directory. For example:

my_cp source_file.txt destination_file.txt

1. This will copy source_file.txt to destination_file.txt.

That's it! You've created a simple file copy program my_cp and made it available in
your ~/bin directory for easy access.
Modify copy command assignment a to copy files(chunk by chunk) using
open,read,write and close system calls. The implementation should work like cp
command. The executable should be named as my_cp that should be linked in
~/bin directory (make sure ~/bin is set in PATH variable). tell me all the process
step by step?

Sure, let's modify the my_cp program to copy files chunk by chunk instead of byte by
byte. Here's the step-by-step process:

1. Create a new C file: Open a terminal and navigate to the directory where you want
to create the C file. Then, use a text editor like nano or vim to create a new file
named my_cp.c

nano my_cp.c

2. Write the C code: Copy and paste the following modified code into my_cp.c:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>

#define BUFFER_SIZE 4096

int main(int argc, char *argv[]) {

if (argc != 3) {

printf("Usage: %s <source_file> <destination_file>\n", argv[0]);


exit(EXIT_FAILURE);

int source_fd, dest_fd;

ssize_t bytes_read, bytes_written;

char buffer[BUFFER_SIZE];

// Open source file

source_fd = open(argv[1], O_RDONLY);

if (source_fd == -1) {

perror("Error opening source file");

exit(EXIT_FAILURE);

// Create or truncate destination file

dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);

if (dest_fd == -1) {

perror("Error opening destination file");

exit(EXIT_FAILURE);

// Copy data chunk by chunk

while ((bytes_read = read(source_fd, buffer, BUFFER_SIZE)) > 0) {


char *ptr = buffer;

ssize_t bytes_to_write = bytes_read;

while (bytes_to_write > 0) {

bytes_written = write(dest_fd, ptr, bytes_to_write);

if (bytes_written <= 0) {

perror("Error writing to destination file");

exit(EXIT_FAILURE);

bytes_to_write -= bytes_written;

ptr += bytes_written;

if (bytes_read == -1) {

perror("Error reading source file");

exit(EXIT_FAILURE);

// Close files

if (close(source_fd) == -1) {

perror("Error closing source file");

exit(EXIT_FAILURE);

}
if (close(dest_fd) == -1) {

perror("Error closing destination file");

exit(EXIT_FAILURE);

printf("File copied successfully.\n");

exit(EXIT_SUCCESS);

1. Save and exit the text editor: In nano, press Ctrl + O to save the file and Ctrl + X
to exit.

2. Compile the program: Compile the my_cp.c file using gcc:

gcc -o my_cp my_cp.c

Move the executable to ~/bin:

3. If ~/bin directory doesn't exist, create

mkdir -p ~/bin

p ~/bin

Then, move the my_cp executable to ~/bin:

mkdir -p ~/bin

Ensure ~/bin is in PATH:

If ~/bin is not already in your PATH, you can add it by editing your ~/.bashrc or
~/.profile file and appending the following line:

export PATH="$PATH:~/bin"

Then, either reopen your terminal or run source ~/.bashrc (or source ~/.profile)
to apply the changes.
Test the program: Now you can use my_cp from any directory. For example:

my_cp source_file.txt destination_file.txt

This will copy source_file.txt to destination_file.txt chunk by chunk.

That's it! You've modified the my_cp program to copy files chunk by chunk and made
it available in your ~/bin directory for easy access.

3. write a program to display file contents on console . The implementation


should work like cat command . The executable should be named as my_cat
that should be linked in ~/bin directory (make sure ~/bin is set in PATH
variable). Usage: $ my_cat The above command should read user input and
echo back on stdout unless and until the user hits EOF. $ my_cat <file_path>
The above command should display the file content on stdout. note: You may
try cat command to understand the behaviors tell me this step by step?
ChatGPT
ChatGPT
Certainly! Here's a step-by-step guide to create a program named my_cat that
displays file contents on the console similar to the cat command:

1. Create a new C file: Open a terminal and navigate to the directory where you want
to create the C file. Then, use a text editor like nano or vim to create a new file
named my_cat.c.

nano my_cat.c

Write the C code: Copy and paste the following code into my_cat.c:

#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[]) {

FILE *file;

// If no command-line argument is provided, read from stdin


if (argc == 1) {

file = stdin;

} else {

// Open the file for reading

file = fopen(argv[1], "r");

if (file == NULL) {

perror("Error opening file");

exit(EXIT_FAILURE);

// Read and display contents until EOF

int ch;

while ((ch = fgetc(file)) != EOF) {

putchar(ch);

// Close the file if it was opened

if (argc > 1) {

fclose(file);

exit(EXIT_SUCCESS);
}

1. Save and exit the text editor: In nano, press Ctrl + O to save the file and Ctrl + X
to exit.

2. Compile the program: Compile the my_cat.c file using gcc:

gcc -o my_cat my_cat.c

Move the executable to ~/bin: If ~/bin directory doesn't exist, create it.

mkdir -p ~/bin

Then, move the my_cat executable to ~/bin:

mv my_cat ~/bin/

Ensure ~/bin is in PATH: If ~/bin is not already in your PATH, you can add it by editing
your ~/.bashrc or ~/.profile file and appending the following line:

export PATH="$PATH:~/bin"

1. Then, either reopen your terminal or run source ~/.bashrc (or source ~/.profile)
to apply the changes.

2. Test the program: Now you can use my_cat from any directory. For example:

 To read from stdin:

my_cat

 This will allow you to type input, and it will echo it back until you press Ctrl + D to
signal EOF.

 To display a file's contents:

my_cat <file_path>

1.

 Replace <file_path> with the path to the file whose contents you want to display.

That's it! You've created a custom my_cat command and made it available in your
~/bin directory for easy access.

You might also like