2 Replies - 4345 Views - Last Post: 08 February 2010 - 05:30 PM Rate Topic: -----

#1 namkcuR  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Attempting to implement a basic pipe in C++ using system calls(fork,du

Posted 08 February 2010 - 01:05 AM

I am doing an assignment in which we(students in the class) are given the c++ source code for a bare-bones replica unix shell. When I say 'bare-bones', I mean it implements 'ls' and a few functions dealing with creating virtual file systems and virtual disks(aka the working directory of the c++ project is looked at as the root of an imaginary drive).

The assignment is to implement a basic pipe in this shell, using system calls(this is an OS class). The test case is ls|cat, where the output of our shell's ls function is passed to the real cat function(since there is no cat function in our shell) as input.

The input on the command line of our shell is taken as a char array. I've written a function isPipe(char[]) that checks said array for the '|' character and returns the index of said character or -1 if it is not found. If that function returns >0, another function, processPipe(char[],int) is called, and that is the function I will focus on. Here is that function:

void processPipe(char buf[], int pos)
{
	char first[1024];
	char second[1024];
	*first = 0;
	*second = 0;
	memset(first,NULL,1024);
	memset(second,NULL,1024);
	for(int i=0; i<pos; i++)
	{
		first[i] = buf[i];
	}
	for(int i=pos+1; buf[i]!=NULL; i++)
	{
		second[i-(pos+1)] = buf[i];
	}


	int fds[2];
	pipe(fds);
	pid_t processID;
	processID = fork();
	if(processID==0)
	{
		dup2(fds[0],STDIN_FILENO);  //Duplicate process
		close(fds[1]); //close file descriptor
		char line[1024];  //create char array
		memset(line,NULL,1024);  //clear char array
		read(fds[0],line,1024);  //read in output of one command as input for the other
		write(fds[1],line,1024);  //write char line as input of the second command
		system(second);  //call second command via system function
		exit(0);
	}
	else
	{
		dup2(fds[1],STDOUT_FILENO);
		close(fds[0]);
                /*The enclosed block of code wasn't written by me, it is c&p-ed from the main method of the initial                          
                    source code we were given, and it executes the functions entered in our shell.
                ------
		setArgsGiven(first, arg, types, nArgsMax);
		int k = findCmd(first, types);
		if (k >= 0)
			invokeCmd(k, arg);
		else
			usage();
               ------*/
		
               wait(NULL);
	}



Basically, two char arrays are created, two for loops populate them with the commands to be piped. So, char first would be 'ls' and char second would be 'cat'.

After this, the actual pipe implementation starts. I've based it on the notes from class as well as various things I've found in google, but it's not working. If I execute the code as it is written above, the program just hangs and does nothing after I press enter(after having typed ls|cat). I am not certain that I am using the read/write system calls properly, nor am I certain that I am using the exit/wait calls properly. They resemble some examples in my notes, but it's not working, so chances are I've done something wrong. Any assistance would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Attempting to implement a basic pipe in C++ using system calls(fork,du

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Attempting to implement a basic pipe in C++ using system calls(fork,du

Posted 08 February 2010 - 06:41 AM

You seem to fancy ignoring important return values. dup2 returns the duplicated file descriptor, which you just throw away. read returns the number of bytes read, seems you should take that into account when you call write immedialtely thereafter. Perhaps you might even like to check that these function calls succeeded.

Read your man pages, dude!
Was This Post Helpful? 0
  • +
  • -

#3 namkcuR  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 27-October 09

Re: Attempting to implement a basic pipe in C++ using system calls(fork,du

Posted 08 February 2010 - 05:30 PM

I have read man pages. I've also read several tutorials online about how to do this kind of thing, and none of them says anything about storing the values returned by dup2. I am still struggling.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1