Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Help for programming a UNIX Shell in C++

Homework & Coursework Questions


Tags
c++, shell, systemcalls, unix

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-14-2015
DarkDan DarkDan is offline
Registered User
 
Join Date: Nov 2015
Last Activity: 14 November 2015, 4:51 AM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Help for programming a UNIX Shell in C++

1. The problem statement, all variables and given/known data:

Hello! Linux

I currently got the task of programming a UNIX Shell for practice. The functionality is as follows:

1. Entering commands with the keyboard. Enter stops the input and creates a process which should start any program
2. the shell waits for termination of each command before another command can be accepted
3. parameters should be extracted and correctly delivered to the specific programm
4. "logout" ends the shell (more of that later)
5. commands which have an '&' appended should be executed in the background. new commands can be entered thereafter

-> commands are not to be hardcoded

6. entering CTRL-C should deliver the currently running foreground process the signal SIGINT (not the shell)
7. entering CTRL-Z should deliver the currently running foregruond process the signal SIGSTP
8. implement a command "fg" that brings back a process back to the foreground which has been stopped by CTRL-Z
9. implement a command "bg" that lets a stopped process (by CTRL-Z) run again in the background
10. the shell can only be terminated if all background processes have been terminated, else give an error message, and make sure there should not be any zombie processes

I already started doing some of the tasks but I'm terribly unsure of how to exactly handle processes, it's very new to me. I would be delighted if someone could lend me a hand, maybe some pseudocode, so I can get the picture of it.

2. Relevant commands, code, scripts, algorithms:

Relevant commands that should be executed (for example):
- firefox
- gedit
- ls
- not cd
- CTRL-C
- CTRL-Z
- fg
- bg

Relevant systemcalls that should be used:
- fork
- waitpid
- execvp or a variant
- not system
- signal
- sigaction
- setpgid

3. The attempts at a solution (include all code and scripts):

My code so far:


Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <vector>

using namespace std;

void getArguments();
string input = "";
vector<char*> arguments;

int main(int argc, char** argv) {

    cout << "Welcome to my shell. Exit with “logout“.0" << endl;

    while (input != "logout") {
        getline(cin, input);
        //getArguments();
        size_t space = input.find(" ");
        string command = input.substr(0, space);
        int end = input.length() - space;
        string text = input.substr(space + 1, ende);

        string path = "/usr/bin/" + command;

        pid_t pid = fork();
        if (pid == 0)
        {
            cout << "Process " << command << " wird ausgeführt: \n";
            execlp(path.c_str(), NULL);
        }
        if (text != "&")
            waitpid(pid, 0, 0);
    }
    return 0;
}


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):


University: University of Applied Sciences in Darmstadt, Germany
Professor: Lars-Olof Burchard
Homepage: https://www.fbi.h-da.de/organisation...bssysteme.html

Best regards,
Dan

Last edited by DarkDan; 11-14-2015 at 04:51 AM.. Reason: formatting error
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help for programming a UNIX Shell in C++ DarkDan Shell Programming and Scripting 1 11-13-2015 04:24 PM
UNIX shell programming ashokkrishna063 Shell Programming and Scripting 2 06-29-2011 07:48 AM
How does unix system administration, unix programming, unix network programming differ? thulasidharan2k UNIX for Dummies Questions & Answers 0 06-09-2011 09:40 AM
Shell Programming in unix nivas Shell Programming and Scripting 3 02-14-2008 01:00 AM
shell programming in unix nivas Shell Programming and Scripting 2 02-14-2008 12:44 AM



All times are GMT -4. The time now is 03:26 AM.