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;
}
gcc parent_child_pipe.c -o parent_child_pipe
./parent_child_pipe
******************************************************
Slip No 13
Q1 . Write a C program that illustrates suspending and resuming processes using signals
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
volatile sig_atomic_t child_suspended = 0;
// Signal handler for the SIGUSR1 signal (suspend)
void suspend_handler(int signum) {
(void) signum;
child_suspended = 1;
printf("Child process suspended.\n");
}
// Signal handler for the SIGUSR2 signal (resume)
void resume_handler(int signum) {
(void) signum;
child_suspended = 0;
printf("Child process resumed.\n");
}
int main() {
pid_t child_pid;
// Set up signal handlers
signal(SIGUSR1, suspend_handler);
signal(SIGUSR2, resume_handler);
// Create a child process
if ((child_pid = fork()) == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (child_pid == 0) { // Child process
while (1) {
// Child process is doing some work
printf("Child process is working...\n");
sleep(1);
}
} else { // Parent process
sleep(2); // Give the child some time to start
// Send SIGUSR1 to the child process to suspend it
kill(child_pid, SIGUSR1);
// Wait for a moment
sleep(3);
// Send SIGUSR2 to the child process to resume it
kill(child_pid, SIGUSR2);
// Wait for the child process to complete
wait(NULL);
}
return 0;
}
Q2. Write a C program that a string as an argument and return all the files that begins with that name
in the current directory. For example > ./a.out foo will return all file names that begins with foo
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
void listFiles(const char *prefix) {
DIR *dir;
struct dirent *entry;
// Open the current directory
if ((dir = opendir(".")) == NULL) {
perror("Error opening directory");
exit(EXIT_FAILURE);
}
// Iterate over the directory entries
while ((entry = readdir(dir)) != NULL) {
// Check if the entry is a regular file and starts with the given prefix
if (entry->d_type == DT_REG && strncmp(entry->d_name, prefix, strlen(prefix)) == 0) {
printf("%s\n", entry->d_name);
}
}
// Close the directory
closedir(dir);
}
int main(int argc, char *argv[]) {
// Check if the correct number of arguments is provided
if (argc != 2) {
fprintf(stderr, "Usage: %s <prefix>\n", argv[0]);
exit(EXIT_FAILURE);
}
// Get the prefix from the command line argument
const char *prefix = argv[1];
// List files in the current directory with the specified prefix
listFiles(prefix);
return 0;
}
**************************************
Slip No 15
Q1. Display all the files from current directory whose size is greater that n Bytes Where n is accept
from user
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
void listFilesWithSize(const char *directoryPath, long sizeThreshold) {
DIR *dir;
struct dirent *entry;
struct stat fileStat;
// Open the specified directory
if ((dir = opendir(directoryPath)) == NULL) {
perror("Error opening directory");
exit(EXIT_FAILURE);
}
// Iterate over the directory entries
while ((entry = readdir(dir)) != NULL) {
// Build the full path of the file
char filePath[PATH_MAX];
snprintf(filePath, sizeof(filePath), "%s/%s", directoryPath, entry->d_name);
// Get information about the file
if (stat(filePath, &fileStat) == -1) {
perror("Error getting file information");
exit(EXIT_FAILURE);
}
// Check if the entry is a regular file and its size is greater than the threshold
if (S_ISREG(fileStat.st_mode) && fileStat.st_size > sizeThreshold) {
printf("%s - %ld bytes\n", entry->d_name, fileStat.st_size);
}
}
// Close the directory
closedir(dir);
}
int main() {
long sizeThreshold;
// Get the size threshold from the user
printf("Enter the size threshold in bytes: ");
scanf("%ld", &sizeThreshold);
// List files in the current directory with size greater than the specified threshold
listFilesWithSize(".", sizeThreshold);
return 0;
}
Q2. Write a C program which creates a child process to run linux/ unix command or any user defined
program. The parent process set the signal handler for death of child signal and Alarm signal. If
a child process does not complete its execution in 5 second then parent process kills child process
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void childHandler(int signum) {
(void)signum;
printf("Child process terminated.\n");
exit(EXIT_SUCCESS);
}
void alarmHandler(int signum) {
(void)signum;
printf("Timeout: Child process exceeded 5 seconds. Killing...\n");
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
pid_t child_pid;
int status;
// Check if a command is provided as an argument
if (argc < 2) {
fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
exit(EXIT_FAILURE);
}
// Set signal handlers
signal(SIGCHLD, childHandler);
signal(SIGALRM, alarmHandler);
// Create a child process
if ((child_pid = fork()) == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
}
if (child_pid == 0) { // Child process
// Execute the command provided in the arguments
execvp(argv[1], &argv[1]);
// If execvp fails
perror("execvp failed");
exit(EXIT_FAILURE);
} else { // Parent process
// Set an alarm for 5 seconds
alarm(5);
// Wait for the child process to complete
waitpid(child_pid, &status, 0);
// Disable the alarm
alarm(0);
// Check if the child process terminated normally
if (WIFEXITED(status)) {
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child process terminated abnormally.\n");
}
}
return 0;
}