Unix Network Programming Assignment-4

Winter Semester,Department of Computer Science

University of Hyderabad

Note : Precise Answers Will Be Appreciated.					Deadline : 07-03-05

1. Writing a Micro-Shell
A shell is a program which reads commands from the keyboard and creates the appropriate processes to execute them. 
Whenever you type a command in a Unix system, the program which reads and starts your command is a shell.  
Usually, shells have a multitude of additional functions, such as interpreting scripts, manipulating environment variables etc.
The goal of this exercise is to write a minimalistic shell, which only reads commands from the keyboard and executes them.
Attention: For all this exercise, the use of the system() function is forbidden. 
It is also forbidden to call another shell (such as /bin/sh) to do the work for you. . .

1. Start by writing a program called mysh1 which reads a program name from the keyboard. 
When a program name is read, the shell creates a new  process to execute the requested program. 
The shell waits for the new process to terminate before accepting another command. 
For example, entering "ls" to your program should list the content of the current directory. 
Entering "exit" should terminate the shell.

2. Extend mysh1 to accept a number of parameters in addition to the program name. 
For example, your shell should interpret correctly commands like "ls -l /tmp". 
Call the extended program mysh2.

3. Extend mysh2 to accept piped commands such as "ls /tmp | wc -l".   
Call the new program mysh3. You can assume that typed commands will contain at most one pipe: 
for example, you do not need to support  "chained pipes" like "sort foo | uniq -c | wc -l".

Problem 1(a):  Implement the above MicroShell and how many processes must your shell create 
when receiving a piped command? 
Hint: For this exercise, you will need to use the dup or dup2 functions.

Problem 1(b): Can you realize a shell program which only utilizes threads (instead of processes )? Why? 
If you think it is possible, then write a thread-based version of mysh1.



 2 . What does the following program do?
       #include <stdio.h>
      #include <unistd.h>
      void display(char *str) {
        char *tmp;
        for (tmp=str;*tmp;tmp++) {
          write(1,tmp,1);
          usleep(100);
        }
      }

int main() {
        int i;
           if (fork()) {
            for (i=0;i<10;i++) display("Hello Neelakanta\n");
            wait(NULL);
          }
          else {
            for (i=0;i<10;i++) display("Hi  Jagadish\n");
          }
          return 0;
      }

The goal of this exercise is to prevent the two messages to intersperse each other (without changing the display() function!).

 For example, this output is correct:
	      Hello Neelakanta
      	      Hello Neelakanta
      	      Hi Jagadish
      	      Hello Neelakanta
      	      Hi Jagadish
      	      Hi Jagadish  ..etc

But this output is not correct:
  	HHeil  lJoa  gNaedeilsahk
  	aHnit aJ
...something like this..

Problem (2) : What type of synchronization is required? Why?  
Modify the above program such that it gives correct output.   Call this program syn1.


3. What does the following program do?
  #include <stdio.h>
  #include <unistd.h>
  void display(char *str) {
    char *tmp;
    for (tmp=str;*tmp;tmp++) {
      write(1,tmp,1);
      usleep(100);
    }
  }

  int main() {
    int i;
      if (fork()) {
        for (i=0;i<10;i++) display("sylvia++");
        wait(NULL);
      }
      else {
        for (i=0;i<10;i++) display("sumalatha\n");
      }
      return 0;
  }
  The goal of this exercise is to make sure the output is exactly the following:
  	sylvia++sumalatha
  	sylvia++sumalatha
  	sylvia++sumalatha
  	...
  and not something like:
	ssyulmvailaa+t+hsay
	lsvuimaa+l+astyhlav
	isau+m+aslyaltvhiaa
	sylvia++sylvia++
           ....etc
Note:   Again, changing the display function is forbidden.

Problem (3) : What is the difference with the previous exercise(Problem2)? Which type of synchronization is required? Why? 
Modify the above program such that it gives correct output.  Call this program syn2.


 Problem ( 4 & 5 ) :Transform the programs syn1 and syn2 to use pthreads instead of processes. 
Use thread synchronization primitives as well. Call the new programs synthread1 and synthread2.
Here :  implementing synthread1 program is Problem 4.
     	   and  synthread2  program is Problem 5.	

Group No:		Roll No: 		Problem No.     
	1		(T33, T31, T23)		5
	2		(T17, T15, I08)		 1
	3		(T05, T06, T07)		3
	4		(T13, T03, T09)		4
	5		(T08, B04, I05)		 2
	6		(B08, B07, B10)		1
	7		(I10, T28, B01)		 2
	8		(C11, C31, C33)		3
	9		(C09, C35)		    4
	10		(T10, T12, I14)		 2
	11		(B06, B13, B12)		5
	12		(C01, C22, C38)		4
	13		(C04, C05, C08)		3
	14		(I01, I15, I19)		    2
	15		(I18, T26, T34)		  5


-- end --