Slip - 4...............
Slip - 4...............
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int nice_value = -10; // Higher priority value
pid = fork();
if (pid < 0) {
// Fork failed
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Child process (PID: %d) before nice():\n", getpid());
printf("Current nice value: %d\n", nice(0));
return 0;
}
Write a program to implement a toy shell (Command Interpreter). It has its own prompt say “MyShell $”. Any normal shell command
is executed from this shell (MyShell$) by starting a child process to execute the system program corresponding to the command. It
should additionally interpret the following commands:
list f dirname : To print names of all the files in current directory
list n dirname : To print the number of all entries in the current directory.
list i dirname : To print names and inodes of the files in the current directory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
if (dir == NULL) {
perror("opendir");
return;
}
if (dir == NULL) {
perror("opendir");
return;
}
if (dir == NULL) {
perror("opendir");
return;
}
if (fork() == 0) {
// Child process
if (execvp(args[0], args) == -1) {
perror("execvp");
exit(EXIT_FAILURE);
}
} else {
// Parent process
wait(NULL);
}
}
int main() {
char input[MAX_INPUT_SIZE];
while (1) {
printf("MyShell $ ");
if (fgets(input, sizeof(input), stdin) == NULL) {
break; // Exit on EOF
}
if (strcmp(command, "list") == 0) {
char option[2];
char dirname[MAX_INPUT_SIZE];
sscanf(input, "%s %s %s", command, option, dirname);
if (strcmp(option, "f") == 0) {
list_files(dirname);
} else if (strcmp(option, "n") == 0) {
list_count(dirname);
} else if (strcmp(option, "i") == 0) {
list_inodes(dirname);
} else {
printf("Invalid option. Use 'f', 'n', or 'i'.\n");
}
} else {
// Execute normal command
execute_command(input);
}
}
return 0;
}