0% found this document useful (0 votes)
30 views9 pages

Assignment1 Os 2

Uploaded by

Vaibhav Shukla
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)
30 views9 pages

Assignment1 Os 2

Uploaded by

Vaibhav Shukla
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/ 9

ASSIGNMENT-2

NAME: - VAIbhAV ShuklA


Sub:- OpErATING SySTEM
STd: - Ty bSc (cS)
dIV: -A
rOll NO.: - 233331072
bATch: - b4
Set A

Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It

Accepts the command, tokenize the command line and execute it by creating the child process.

Also implement the additional command ‘count’ as

Myshell$ count c filename: It will display the number of characters in given file

Myshell$ count w filename: It will display the number of words in given file

Myshell$ count l filename: It will display the number of lines in given file

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#define MAX_INPUT_SIZE 1024

Void executeCommand(char *tokens[]) {

Pid_t pid;

Pid = fork();

If (pid < 0) {

Perror(“Fork failed”);

Exit(1);

} else if (pid == 0) {

// Child process

If (execvp(tokens[0], tokens) == -1) {

Perror(“Execution failed”);

Exit(1);
}

} else {

// Parent process

Wait(NULL);

Int main() {

Char input[MAX_INPUT_SIZE];

Char *tokens[MAX_INPUT_SIZE / 2];

While (1) {

Printf(“myshell$ “);

Fgets(input, sizeof(input), stdin);

// Tokenize the input

Char *token = strtok(input, “ \n”);

Int I = 0;

While (token != NULL) {

Tokens[i++] = token;

Token = strtok(NULL, “ \n”);

Tokens[i] = NULL;

If (I > 0) {

If (strcmp(tokens[0], “count”) == 0) {

// Handle ‘count’ command

If (I != 3) {

Printf(“Usage: count [c|w|l] filename\n”);

} else {

Char *command = tokens[1];


Char *filename = tokens[2];

FILE *file = fopen(filename, “r”);

If (file == NULL) {

Perror(“File opening failed”);

Exit(1);

Int count = 0;

Char c;

While ((c = fgetc(file)) != EOF) {

If (command[0] == ‘c’) {

Count++;

} else if (command[0] == ‘w’) {

If (c == ‘ ‘ || c == ‘\n’ || c == ‘\t’) {

Count++;

} else if (command[0] == ‘l’) {

If (c == ‘\n’) {

Count++;

Fclose(file);

Printf(“%s count: %d\n”, command, count);

} else {

// Execute other commands

executeCommand(tokens);

}
}

Return 0;

}
Set B

Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It

Accepts the command, tokenize the command line and execute it by creating the child process.

Also implement the additional command ‘list’ as

Myshell$ list f dirname: It will display filenames in a given directory.

Myshell$ list n dirname: It will count the number of entries in a given directory.

Myshell$ list I dirname: It will display filenames and their inode number for the files in a given
Directory.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <dirent.h>

#include <sys/stat.h>

#define MAX_INPUT_SIZE 1024

Void executeCommand(char *tokens[]) {

Pid_t pid;

Pid = fork();

If (pid < 0) {

Perror(“Fork failed”);

Exit(1);

} else if (pid == 0) {

// Child process

If (execvp(tokens[0], tokens) == -1) {

Perror(“Execution failed”);
Exit(1);

} else {

// Parent process

Wait(NULL);

Void listFiles(char *dirname, char *option) {

DIR *dir;

Struct dirent *entry;

Struct stat filestat;

Dir = opendir(dirname);

If (dir == NULL) {

Perror(“Directory opening failed”);

Exit(1);

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

Char path[1024];

Snprintf(path, sizeof(path), “%s/%s”, dirname, entry->d_name);

If (lstat(path, &filestat) < 0) {

Perror(“Failed to stat”);

Exit(1);

If (strcmp(option, “f”) == 0) {

If (S_ISREG(filestat.st_mode)) {

Printf(“%s\n”, entry->d_name);
}

} else if (strcmp(option, “n”) == 0) {

Printf(“%s\n”, entry->d_name);

} else if (strcmp(option, “I”) == 0) {

If (S_ISREG(filestat.st_mode)) {

Printf(“%s – Inode: %lu\n”, entry->d_name, (unsigned long)filestat.st_ino);

Closedir(dir);

Int main() {

Char input[MAX_INPUT_SIZE];

Char *tokens[MAX_INPUT_SIZE / 2];

While (1) {

Printf(“myshell$ “);

Fgets(input, sizeof(input), stdin);

// Tokenize the input

Char *token = strtok(input, “ \n”);

Int I = 0;

While (token != NULL) {

Tokens[i++] = token;

Token = strtok(NULL, “ \n”);

Tokens[i] = NULL;

If (I > 0) {

If (strcmp(tokens[0], “list”) == 0) {
// Handle ‘list’ command

If (I != 3) {

Printf(“Usage: list [f|n|i] dirname\n”);

} else {

Char *option = tokens[1];

Char *dirname = tokens[2];

If (strcmp(option, “f”) != 0 && strcmp(option, “n”) != 0 && strcmp(option, “I”) != 0) {

Printf(“Invalid option. Use [f|n|i]\n”);

} else {

listFiles(dirname, option);

} else {

// Execute other commands

executeCommand(tokens);

Return 0;

You might also like