SAVITIBAI PHULE UNIVERSITY M.Sc.(Computer Science) Sem-I Practical Examination (From 2023-2024) SUBJECT: CS-504-MJP: Lab Course on CS-501-MJ (Advanced Operating System) Answers is here -
Time: 3 Hours Max. Marks: 35
____________________________________________________________________
-------------------------- Slip 10 --------------------
Q.1) Write a program that illustrates how to execute two commands concurrently with a pipe. [10 Marks ]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipe_fd[2];
pid_t child_pid;
// Create a pipe
if (pipe(pipe_fd) == -1) {
perror("Pipe creation failed");
exit(EXIT_FAILURE);
}
// Create a child process
if ((child_pid = fork()) == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (child_pid == 0) { // Child process
// Close the write end of the pipe
close(pipe_fd[1]);
// Redirect standard input to read from the pipe
dup2(pipe_fd[0], STDIN_FILENO);
// Close the unused end of the pipe
close(pipe_fd[0]);
// Execute the second command (e.g., 'wc -w' as an example)
execlp("wc", "wc", "-w", NULL);
// If execlp fails
perror("execlp failed");
exit(EXIT_FAILURE);
} else { // Parent process
// Close the read end of the pipe
close(pipe_fd[0]);
// Redirect standard output to write to the pipe
dup2(pipe_fd[1], STDOUT_FILENO);
// Close the unused end of the pipe
close(pipe_fd[1]);
// Execute the first command (e.g., 'ls' as an example)
execlp("ls", "ls", NULL);
// If execlp fails
perror("execlp failed");
exit(EXIT_FAILURE);
}
// To run this program on a Linux machine, compile it using a C compiler (e.g., gcc):
// gcc concurrent_commands.c -o concurrent_commands
// Then, execute the compiled program:
// ./concurrent_commands
return 0;
}
Q.2) Generate parent process to write unnamed pipe and will write into it. Also generate child process which will read from pipe
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFER_SIZE 256
int main() {
int pipe_fd[2];
pid_t child_pid;
// Create a pipe
if (pipe(pipe_fd) == -1) {
perror("Pipe creation failed");
exit(EXIT_FAILURE);
}
// Create a child process
if ((child_pid = fork()) == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (child_pid == 0) { // Child process
// Close the write end of the pipe
close(pipe_fd[1]);
char buffer[BUFFER_SIZE];
ssize_t read_bytes;
// Read from the pipe and print the received data
printf("Child process reading from the pipe:\n");
while ((read_bytes = read(pipe_fd[0], buffer, BUFFER_SIZE)) > 0) {
write(STDOUT_FILENO, buffer, read_bytes);
}
if (read_bytes == -1) {
perror("Read from pipe failed");
exit(EXIT_FAILURE);
}
// Close the read end of the pipe
close(pipe_fd[0]);
} else { // Parent process
// Close the read end of the pipe
close(pipe_fd[0]);
// Data to be written into the pipe
const char *message = "Hello, Child!";
// Write data into the pipe
write(pipe_fd[1], message, strlen(message));
// Close the write end of the pipe
close(pipe_fd[1]);
// Wait for the child process to complete
wait(NULL);
}
return 0;
}