Assignment IPC
Assignment IPC
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
if (argc != 3) {
char buffer[BUFFER_SIZE];
if (source_fd == -1) {
exit(EXIT_FAILURE);
if (dest_fd == -1) {
exit(EXIT_FAILURE);
// Copy data
if (bytes_written != bytes_read) {
exit(EXIT_FAILURE);
if (bytes_read == -1) {
exit(EXIT_FAILURE);
// Close files
if (close(source_fd) == -1) {
exit(EXIT_FAILURE);
if (close(dest_fd) == -1) {
exit(EXIT_FAILURE);
1. Save and exit the text editor: In nano, press Ctrl + O to save the file and Ctrl + X
to exit.
mkdir -p ~/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:
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>
if (argc != 3) {
char buffer[BUFFER_SIZE];
if (source_fd == -1) {
exit(EXIT_FAILURE);
if (dest_fd == -1) {
exit(EXIT_FAILURE);
if (bytes_written <= 0) {
exit(EXIT_FAILURE);
bytes_to_write -= bytes_written;
ptr += bytes_written;
if (bytes_read == -1) {
exit(EXIT_FAILURE);
// Close files
if (close(source_fd) == -1) {
exit(EXIT_FAILURE);
}
if (close(dest_fd) == -1) {
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
1. Save and exit the text editor: In nano, press Ctrl + O to save the file and Ctrl + X
to exit.
mkdir -p ~/bin
p ~/bin
mkdir -p ~/bin
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:
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.
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>
FILE *file;
file = stdin;
} else {
if (file == NULL) {
exit(EXIT_FAILURE);
int ch;
putchar(ch);
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.
Move the executable to ~/bin: If ~/bin directory doesn't exist, create it.
mkdir -p ~/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:
my_cat
This will allow you to type input, and it will echo it back until you press Ctrl + D to
signal EOF.
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.