CS 601p Assignment 1 Solution BC210410285
CS 601p Assignment 1 Solution BC210410285
BC210410285
Zaeem Farooq
Please carefully read the following instructions before attempting the assignment.
You should consult the recommended books to clarify your concepts, as handouts are insufficient.
Assignment Submission:
You are supposed to submit your assignment in Doc or Docx format.
Any other formats, such as scanned images, PDF, zip, rar, ppt, and BMP, will not be accepted.
You are required to send the Screenshot and C code of Question No. 1 in the same Word file.
Furthermore, Linux commands of Question No. 2 should also be pasted in the same Word file.
Assignment No. 1 covers Labs 1 to Labs 3
OBJECTIVE
The objective of this assignment is to provide hands-on experience in the:
Inter-Process Communication through the pipe System call
Linux File/Directory management commands.
System calls and their usage in Linux
NOTE
No assignment will be accepted after the due date via email in any case (whether it is due to load
shedding or internet malfunctioning, etc.). Hence, refrain from uploading assignments within the
last hour of the deadline. It is recommended that the solution file be uploaded at least two days
before its closing date.
Please consult your instructor before the deadline if you find any mistake or confusion in the
assignment (Question statement). After the deadline, no queries will be entertained in this
regard.
Questions No 01 15 marks
You are required to describe how the following code created in the C program works.
Execute the program in C first, then determine how it works from its output. Also, attach
screenshots of the output.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int pipefd[2];
pid_t pid;
char buffer[BUFFER_SIZE];
int nbytes;
// Create pipe
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
return 0;
}
Answer:
This program demonstrates inter-process communication using a pipe
between a parent and a child process.
Header Files and Macros:
Includes necessary header files for I/O operations, process management,
and pipe.
Defines a buffer size macro BUFFER_SIZE.
Pipe Creation:
pipe(pipefd): Creates a pipe with two file descriptors in pipefd[0] for
reading and pipefd[1] for writing.
Forking a Child Process:
pid = fork(): Forks a new child process.
If pid == -1, an error occurred.
If pid == 0, it's the child process.
If pid > 0, it's the parent process.
Child Process:
Read from Pipe:
close(pipefd[1]): Closes the write end of the pipe.
read(pipefd[0], buffer, BUFFER_SIZE): Reads data from the pipe into
buffer.
printf("Child received message: %s", buffer): Prints the received
message.
close(pipefd[0]): Closes the read end of the pipe.
exit(EXIT_SUCCESS): Exits the child process.
Parent Process:
Write to Pipe:
close(pipefd[0]): Closes the read end of the pipe.
fgets(buffer, BUFFER_SIZE, stdin): Reads a message from the user.
write(pipefd[1], buffer, BUFFER_SIZE): Writes the message to the pipe.
close(pipefd[1]): Closes the write end of the pipe.
wait(NULL): Waits for the child process to finish.
Solution
BC210410285
Zaeem Farooq
exit(EXIT_SUCCESS): Exits the parent process.
Expected Output:
Parent Process Output:
Prompts the user to enter a message.
Sends the message to the child process via the pipe.
Child Process Output:
Receives the message from the parent via the pipe.
Prints the received message.
ScreenShot:
Questions No 02 5 marks
In the following table, write the details for the given Linux commands.
Linux Details
Command
Ls List files and directories
Cd Change the current directory or move up the directories
Grep Searches in files or input streams
Cat Used to concatenate and Display the content of file
rm or rmdir Remove file or directories