Advertisement
tsnaik

OS lab8

Sep 8th, 2015
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include <unistd.h>
  5. #include<errno.h>
  6.  
  7. #include<sys/types.h>
  8. #define BUFF 1024
  9.  
  10.    
  11. int main()
  12. {
  13.    
  14.  
  15.  
  16.         system("clear");
  17.  
  18. while(1)
  19. {  
  20.         char str[BUFF];
  21.         printf("\n>");
  22.        
  23.         gets(str);
  24.          char *array[50];
  25.          
  26.          for( int i = 0; i < 50; ++i )
  27.     array[i] = malloc( 256 * sizeof *array[i] );
  28.    
  29.             int i = 0;
  30.         char *pch;
  31.        
  32.        
  33.      /*  strcpy(array[i++],"sh");
  34.        strcpy(array[i++],"-c");
  35.        */
  36.        
  37.           pch = strtok (str," ");
  38.        
  39.        
  40.         while (pch != NULL)
  41.         {
  42.                 //printf ("%s\t",pch);
  43.                 strcpy(array[i++],pch);
  44.                 //array[i++] = pch;
  45.                 pch = strtok (NULL, " ");
  46.                
  47.         }
  48.        
  49.        
  50.        (array[i++]=NULL);
  51.        
  52.        
  53. execv(array[0],array);
  54.    
  55.     printf("%s",strerror(errno));
  56.  
  57.  
  58.    
  59. }
  60. return 0;
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*p3.c
  77.  
  78.  
  79.  
  80.  
  81. /*
  82. One example use would be I/O redirection. For this you fork a child process and close the stdin or stdout file descriptors (0 and 1) and then you do a dup() on another filedescriptor of your choice which will now be mapped to the lowest available file descriptor, which is in this case 0 or 1.
  83. Using this you can now exec any child process which is possibly unaware of your application and whenever the child writes on the stdout (or reads from stdin, whatever you configured) the data gets written on the provided filedescriptor instead.
  84. */
  85.  
  86. #include<stdio.h>
  87. #include<unistd.h>
  88. #include<sys/types.h>
  89. #include<string.h>
  90. main()
  91. {
  92. int fd[2],pid; // fd[0] read fd[1] write
  93. pipe(fd);
  94. pid=fork();
  95.  
  96. if(pid==0) // Child Process
  97. {
  98. close(fd[0]);
  99. dup2(fd[1],1);
  100. close(fd[1]);
  101. execl("/bin/ls","ls",(char *)0);
  102. //write(fd[1],m,strlen(m+1));
  103. }
  104. else// Parent Process
  105. {
  106. close(fd[1]);
  107. dup2(fd[0],0);
  108. close(fd[0]);
  109. execl("/usr/bin/wc","wc",(char *)0);
  110.  
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement