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

Slip - 4...............

The document contains two C programs: the first demonstrates the use of the nice() system call to assign higher priority to a child process created with fork(), while the second implements a toy shell that executes commands and interprets specific 'list' commands to manage directory contents. The toy shell provides functionalities to list files, count entries, and display file inodes in a specified directory. Both programs utilize standard libraries for process management and directory operations.

Uploaded by

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

Slip - 4...............

The document contains two C programs: the first demonstrates the use of the nice() system call to assign higher priority to a child process created with fork(), while the second implements a toy shell that executes commands and interprets specific 'list' commands to manage directory contents. The toy shell provides functionalities to list files, count entries, and display file inodes in a specified directory. Both programs utilize standard libraries for process management and directory operations.

Uploaded by

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

Write a program that demonstrates the use of nice() system call.

After a child process is started using fork(), assign higher priority to


the child using nice() system call.

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

// Set higher priority (lower nice value)


if (nice(nice_value) == -1) {
perror("nice failed");
exit(1);
}

printf("Child process (PID: %d) after nice():\n", getpid());


printf("New nice value: %d\n", nice(0));
// Simulate work
for (int i = 0; i < 5; i++) {
printf("Child is running... (%d)\n", i);
sleep(1);
}
} else {
// Parent process
wait(NULL); // Wait for the child process to finish
printf("Parent process (PID: %d) finished waiting for child.\n", getpid());
}

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>

#define MAX_INPUT_SIZE 1024


#define MAX_ARG_SIZE 100

// Function to list files in a directory


void list_files(const char *dirname) {
DIR *dir = opendir(dirname);
struct dirent *entry;

if (dir == NULL) {
perror("opendir");
return;
}

while ((entry = readdir(dir)) != NULL) {


// Skip the "." and ".." entries
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
}

// Function to count entries in a directory


void list_count(const char *dirname) {
DIR *dir = opendir(dirname);
struct dirent *entry;
int count = 0;

if (dir == NULL) {
perror("opendir");
return;
}

while ((entry = readdir(dir)) != NULL) {


count++;
}
closedir(dir);

// Subtract 2 for "." and ".."


printf("%d\n", count - 2);
}

// Function to list files with their inodes


void list_inodes(const char *dirname) {
DIR *dir = opendir(dirname);
struct dirent *entry;
struct stat fileStat;

if (dir == NULL) {
perror("opendir");
return;
}

while ((entry = readdir(dir)) != NULL) {


if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
char path[MAX_INPUT_SIZE];
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
if (stat(path, &fileStat) == 0) {
printf("%s - Inode: %lu\n", entry->d_name, fileStat.st_ino);
} else {
perror("stat");
}
}
}
closedir(dir);
}

// Function to execute normal shell commands


void execute_command(char *input) {
char *args[MAX_ARG_SIZE];
char *token;
int i = 0;

token = strtok(input, " \n");


while (token != NULL && i < MAX_ARG_SIZE - 1) {
args[i++] = token;
token = strtok(NULL, " \n");
}
args[i] = NULL;

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
}

// Check for exit command


if (strncmp(input, "exit", 4) == 0 || strncmp(input, "quit", 4) == 0) {
break;
}

// Parse the input for "list" commands


char command[MAX_INPUT_SIZE];
sscanf(input, "%s", command);

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

You might also like