0% found this document useful (0 votes)
99 views4 pages

CS 601p Assignment 1 Solution BC210410285

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views4 pages

CS 601p Assignment 1 Solution BC210410285

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Solution

BC210410285
Zaeem Farooq

Operating System (CS604) Total marks = 20


Assignment # 01 Deadline Date

Spring 2024 May 2, 2024

Please carefully read the following instructions before attempting the assignment.

RULES FOR MARKING


It should be clear that your assignment would not get any credit if:
 The assignment is submitted after the due date.
 The submitted assignment does not open, or the file is corrupted.
 Strict action will be taken if the submitted solution is copied from any other student or the
internet.

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.

For any query, feel free to email me at:


[email protected]
Solution
BC210410285
Zaeem Farooq

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>

#define BUFFER_SIZE 256

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);
}

// Fork a child process


pid = fork();

if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}

if (pid == 0) { // Child process


// Close the write end of the pipe
close(pipefd[1]);

// Read from the pipe


nbytes = read(pipefd[0], buffer, BUFFER_SIZE);
if (nbytes == -1) {
perror("read");
exit(EXIT_FAILURE);
}

printf("Child received message: %s", buffer);

// Close the read end of the pipe


close(pipefd[0]);
exit(EXIT_SUCCESS);
} else { // Parent process
// Close the read end of the pipe
close(pipefd[0]);
Solution
BC210410285
Zaeem Farooq
// Write to the pipe
printf("Enter message to send to child: ");
fgets(buffer, BUFFER_SIZE, stdin);
write(pipefd[1], buffer, BUFFER_SIZE);

// Close the write end of the pipe


close(pipefd[1]);

// Wait for the child to finish


wait(NULL);
exit(EXIT_SUCCESS);
}

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

You might also like