0% found this document useful (0 votes)
6 views3 pages

BC220413631 - CS604P - A1

ASS

Uploaded by

tihob66222
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)
6 views3 pages

BC220413631 - CS604P - A1

ASS

Uploaded by

tihob66222
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/ 3

BC220413631

Muhammad Inam Ul Haq


CS604P
Assignment 1

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

// 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;
}
Output:

Working:
 The purpose of the given program is to use a pipe for a parent process and then
send a message to the child process
 At the top there are header files and a constant named BUFFER_SIZE.
 Main function declares variables.
 A pipe is created with pipe() function failing which prints an error and exits the
program. Fork() function is used to fork child process and if it fails, prints error
and exits.
 Child Process:
o Closed write end.
o Reads only and stores message and then prints the message.
o Reading end closed after reading and then exit
 Parent Process:
o Closed read end.
o Writes only and sendts it to the child. Closes write end.
o Exits.

Questions No 02 5 marks

In the following table, write the details for the given Linux commands.

Linux Command Details


Ls Stands for list files and it lists files and
directories
Cd Stands for change directory and allows to
change current working directory.
Grep Stands for global regular expression print
and it is used to seartch and match text
patterns in files.
Cat Stands for concatenate and reads files
sequentially. Displays contents without
actually opening the file for editing.
rm or rmdir Both kinda remove stuff. Rm command
removes directories and files while rmdir
command removes empty directories.

The End

You might also like