(written in C) OS Purpose: The programming portion of this assignment covers exe
ID: 3748935 • Letter: #
Question
(written in C) OS
Purpose: The programming portion of this assignment covers exec.
Build a minishell. Start with a copy of /net/326/minishell.c. This contains code that parses a line into a char* narg[] array.
The program has an infinite loop. You will exit with a ctrl+c. Inside the loop:
Remove the part that prints the parsed line.
Insert the following.
1) fork a child
2 parent) wait3 for the child to complete After the child completes the parent goes back and prints another
prompt. (Note: you will need to include sys/wait.h as in homework 3 to suppress a compiler warning).
2 child) exec what was entered. If you examine what the parser generates you will notice that you want to use the execvp. (Note: execvp may also require an include to suppress a warning, see its man page to know which).
Discussion:
You get the prompt “Your command please: ”
You type “ls -la”
You see the output of the ls.
When the ls is done, you see the prompt (“Your command please”).
Examination of existing code:
Read the manual entry on strtok (string tokenize). Make sure you understand how that part of the code works.
The exploration portion of this assignment has you look at the Linux kernel.
Which modules are loaded by the kernel of the machine you are using?
You must use man -k to find out the commands which pertain to modules. Then use the correct command, of course it wouldn’t hurt to read the entry for the command before you use it. Hint: the command shows the status of the modules in the Linux Kernel.
To simplify, if there are a lot of sound modules loaded, you can just report how many of those there are rather than listing them all.
The Linux kernel. We mentioned that Linux makes everything look like a file, including information going to and from the kernel. We stated that Linux presents this system information in the directory called /proc. Remember /proc isn’t really a directory, it just looks like one. The files aren’t really files either. They are just information coming from (and going to) the kernel.
Now for the questions. Go to the /proc directory and see if you can find the file that has information on interrupts. Examine that file and answer the following questions:
What interrupt number is used by the timer?
How many timer interrupts have there been? (The number you see is how many since the last reboot.) What interrupt number is used by the first usb controller (usb1)?
How many usb1 interrupts have there been?
Explanation / Answer
//main.cpp
#include "BookStore.h"
using namespace std;
int main(){
const string s1 = "Bookdata";
Bookstore bookstore = Bookstore(s1);
bookstore.print();
return 0;
}
----------------------------------------------------------------------
//Book.cpp
#include <cstring>
#include <iostream>
#include <iomanip>
#include "Book.h"
using namespace std;
// default constructor
Book::Book() {
ISBN[0] = '';
title[0] = '';
setPrice(0);
setQuantity(0);
}
//Constructor(char array new ISBN, char array new Title, double variable new Price, int new Price
Book::Book(char *newISBN, char *newTitle, double newPrice, int newQuantity) {
strcpy(ISBN, newISBN);
strcpy(title,newTitle);
setPrice(newPrice);
setQuantity(newQuantity);
}
//Accessor method declaration returns ISBN data member
char* Book::getISBN() {
return ISBN;
}
//Accessor method declaration returns Title data member
char* Book::getTitle() {
return title;
}
//Accessor method declaration returns Price data member as type double
double Book::getPrice() {
return price;
}
//Accessor method declaration returns Quantity as type int
int Book::getQuantity() {
return quantity;
}
//Method declaration that takes a double argument and newPrice
void Book::setPrice(double newPrice) {
if (newPrice >= 0) {
price = newPrice;
} else {
price = 0;
}
}
//Method declaration that takes an int newQuantity
void Book::setQuantity(int newQuantity) {
if (newQuantity >= 0) {
quantity = newQuantity;
} else {
quantity = 0;
}
}
//Accessor method that takes an int which is the quantity of the book and returns the quantity available
int Book::fulfillOrder(int orders) {
int current_quantity = getQuantity();
if (orders < 0) {
return 0;
} else if (orders <= getQuantity()) {
setQuantity(current_quantity - orders);
return current_quantity;
} else {
int current_quantity = getQuantity();
setQuantity(0);
return current_quantity;
}
}
//Method prints the ISBN, title, price, and quantity members in custom format
void Book::print(){
left( cout << ISBN << setw(14) << endl );
left( cout << title << setw(44) << endl );
right( cout << price << setw(5) << endl );
right( cout << quantity << setw(6) << endl );
}
------------------------------------------------------------------
//Book.h
#ifndef ASSIGNMENT_2_BOOK_H
#define ASSIGNMENT_2_BOOK_H
class Book
{
private:
// Char array with room for 10 chars + null
char ISBN[11];
//Char array with room for 40 chars + null
char title[41];
//Double variable used for storing price value
double price;
//Integer used for storing stock quantity
int quantity;
public:
Book(); //Default Constructor
//Alt Constructor(char array new ISBN, char array new Title, double variable new Price, int new Price
Book(char *newISBN, char *newTitle, double newPrice, int newQuantity);
char * getISBN(); //Accessor method declaration returns ISBN data member
char * getTitle(); //Accessor method declaration returns Title data member
double getPrice(); //Accessor method declaration returns Price data member as type double
int getQuantity(); //Accessor method declaration returns Quantity as type int
void setPrice(double newPrice); //Method declaration that takes a double argument and newPrice
void setQuantity(int newQuantity); //Method declaration that takes an int and newQuantity
int fulfillOrder(int orders); //Accessor method that takes an int which is the quantity of the book and returns the quantity available
void print(); //Method prints the ISBN, title, price, and quantity members in custom format
};
#endif //ASSIGNMENT_2_BOOK_H
----------------------------------------------------------------------------
//BookStore.cpp
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "BookStore.h"
using namespace std;
// default constructor
Bookstore::Bookstore() {
booksSize = 0;
}
// alternate constructor
Bookstore::Bookstore(const std::string& str) {
ifstream inFile;
inFile.open(str);
if (!inFile.is_open()) {
perror("could not open file!");
} else {
inFile.read((char*) this, sizeof(Bookstore));
inFile.close();
}
}
void Bookstore::print(){
cout << "Book Inventory Listing ";
cout << "----------------------- ";
for (int i = 0; i < (int) sizeof(inventory); i++) {
inventory[i].print();
}
}
----------------------------------------------------------------------
//BookStore.h
#ifndef ASSIGNMENT_2_BOOKSTORE_H
#define ASSIGNMENT_2_BOOKSTORE_H
#include "Book.h"
#include <string>
class Bookstore {
private:
Book inventory[30];
int booksSize; // = sizeof(books);
public:
// default constructor
Bookstore();
// alternate constructor
Bookstore(const std::string&);
// print
void print();
};
#endif //ASSIGNMENT_2_BOOKSTORE_H
-------------------------------------------------------------------------------
//main.cpp
#include <iostream>
#include "Book.h"
using std::cout;
using std::endl;
int main()
{
char isbn1[11] = "1111111111";
char title1[41] = "Learn C++ Now";
char isbn2[11] = "2222222222";
char title2[41] = "Learn Java Later";
int numShipped;
// Test default constructor
Book book1;
// Test alternate constructor
Book book2(isbn1, title1, 39.99, 5);
// Test data validation
Book book3(isbn2, title2, -22.99, -6);
// Test print() method and whether constructors
// properly initialized objects
cout << "Printing book1 ";
book1.print();
cout << endl << endl;
cout << "Printing book2 ";
book2.print();
cout << endl << endl;
cout << "Printing book3 ";
book3.print();
cout << endl << endl;
// Test accessor methods
cout << book2.getISBN() << endl;
cout << book2.getTitle() << endl;
cout << book2.getPrice() << endl;
cout << book2.getQuantity() << endl;
// Test the fulfillOrder() method
numShipped = book2.fulfillOrder(-5);
cout << " Shipped " << numShipped << endl;
cout << "Quantity now " << book2.getQuantity() << endl;
numShipped = book2.fulfillOrder(3);
cout << "Shipped " << numShipped << endl;
cout << "Quantity now " << book2.getQuantity() << endl;
numShipped = book2.fulfillOrder(4);
cout << "Shipped " << numShipped << endl;
cout << "Quantity now " << book2.getQuantity() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.