Creating pipe,fork,exec, dup

Please help!
this is the assign: The purpose of this assignment is to make sure that the students can work effectively with the fork, pipe, and
dup, system calls. It should also grant some insight into how the shell enables input and output redirection
behind the scenes.

I am almost done with the code, but I don't know how to split the arguments when the user types it in the command line. For example, "ls -a", I have no idea what to do in order to split the text in the command line. This is what the suggestions are according to the assignment.

Possibilities include the C function strtok or the C++ istringstream
object.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
	// Creating command1 & command2
	string command1, command2;
	
	// quit = exit program
	char quit[] = "quit";

	// User entering command #1
	cout << "command1? ";
	cin >> command1;

	// if "quit" is not typed in command line, user will enter command #2
	while (command1 != "quit")
	{
		// command #2 
		cout << "command2? ";
		cin >> command2;

		// Creating my pipe 
		int pipefd[2];
		int rs = pipe(pipefd);

		// If pipe fails
		if (rs == -1)
		{
			perror("Piping has fail!");
			// Exit the program 
			exit(EXIT_FAILURE);

		}

		// Creating my fork to 2 processes
		rs = fork();


		// If fork fails 
		if (rs == -1)
		{
			perror("Piping has fail");
			// Exit the program
			exit(EXIT_FAILURE);

		}

		//Once fork, child is created 
		if (rs == 0)
		{

			//  Always close the read end of the pipe
			//  to avoid ForkBomb, the write end will 
			//  be left open
			close(pipefd[0]);

			// Closing stdout
			close(1);

			//  Creating a duplicate (dup) to place into
			//  the write end of the pipe to stdout(1)
			dup(pipefd[1]);

			//  Closing the wight end of the pipe
			close(pipefd[1]);

			//  Command1 will be run using execlp
			rs = execlp(command1.c_str(), command1.c_str(), (char*)NULL);

			// If execlp fails display message to user
			if (rs == -1)
			{
				perror("Execlp has failed!");
				// Exit the program
				exit(EXIT_FAILURE);

			}


		}


		// This is the parent process
		else
		{
			// Closing write end of the pipe and leaving 
			// read open
			close(pipefd[1]);
			
			// Close stdout
			close(0);

			// Creating a duplicate (dup) to place into 
			// the read end of the pipe to stdin(0)
			dup(pipefd[0]);

			// Closing the read end of the pipe
			close(pipefd[0]);

			// Forking into 2 processes again
			rs = fork();

			// If fork() fails display message to user
			if (rs == 1)
			{
				perror("Parent fork has failed!");
				exit(EXIT_FAILTURE);
			}


			// You are in child process #2
			// Here we will run command2 
			if (rs == 0)
			{
				//Running command2
				rs == execlp(command2.c_str(), command2.c_str(), (char*)NULL);

				// If execlp fails display message to user
				if (rs == -1)
				{
					perror("Child2 execlp has faild");
					exit(EXIT_FAILURE);
				}

			}


			// Inside the parent process
			else 
			{

				wait(NULL);
				wait(NULL);
                                break;
			}


		}

		
	}


	return 0;
}






Last edited on
Please edit your post to put code tags around the code.
https://www.cplusplus.com/articles/jEywvCM9/
Sorry, First time doing this! lol
Here's one example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

vector<string> splitter(const string &line) {
  vector<string> result;
  istringstream is(line);
  string word;
  while ( is >> word ) {
    result.push_back(word);
  }
  return result;
}

int main ( ) {
  string test = "ls -l";
  vector<string> words = splitter(test);
  cout << words[0] << endl;
}
Topic archived. No new replies allowed.