Posts

Showing posts from July, 2012

Remort Shell

The client program running on the client computer should connect to the server program running on the server computer. Then the client program should prompt for commands from the user, make them run on the server computer and print results back on the client computer's screen. This command-result sequence should run in a loop so that a number of commands can get run on the server. When the user enters the text "exit" on the client program the system should terminate. Implementation in C client.c /* compile Command : gcc -o client client.c                   ./client */ #include  <sys/types.h> #include  <sys/socket.h> #include  <sys/un.h> #include  <stdio.h> #include  <netinet/in.h> #include  <netdb.h> #include <stdlib.h> #define  PORT  1030    /* addr to connect */ #define  HOST  "localhost" #define BUFSIZE 256 /*#define  HOST  "10.16.48.56"*/ main() {     char buffer[BUFSIZE];  

Simple pipe implementation

#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(){     int pfds[2], status;     char *com1[]={"ls","-a",NULL};     char *com2[]={"sort",NULL};     switch(fork()){         case -1:              printf("Error has been occured");             exit(1);         case 0:             break;//Run the child         default:             wait(&status);//Parent is waiting until child exit             return (status);          }     if(pipe(pfds)<0)         exit(1);     switch(fork()){         case -1:             exit(1);         case 0:             close(1);//Close the stdout             dup(pfds[1]);//Duplicate the pipe write end             close(pfds[0]);//Close pipe read end             close(pfds[1]);//Close pipe write end             execvp(com1[0], com1);//Execute the command             exit(1);         default:             close(0);//Close the stdin             dup(pfds[0]);