Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include <unistd.h>
- #include<errno.h>
- #include<sys/types.h>
- #define BUFF 1024
- int main()
- {
- system("clear");
- while(1)
- {
- char str[BUFF];
- printf("\n>");
- gets(str);
- char *array[50];
- for( int i = 0; i < 50; ++i )
- array[i] = malloc( 256 * sizeof *array[i] );
- int i = 0;
- char *pch;
- /* strcpy(array[i++],"sh");
- strcpy(array[i++],"-c");
- */
- pch = strtok (str," ");
- while (pch != NULL)
- {
- //printf ("%s\t",pch);
- strcpy(array[i++],pch);
- //array[i++] = pch;
- pch = strtok (NULL, " ");
- }
- (array[i++]=NULL);
- execv(array[0],array);
- printf("%s",strerror(errno));
- }
- return 0;
- }
- /*p3.c
- /*
- 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.
- 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.
- */
- #include<stdio.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<string.h>
- main()
- {
- int fd[2],pid; // fd[0] read fd[1] write
- pipe(fd);
- pid=fork();
- if(pid==0) // Child Process
- {
- close(fd[0]);
- dup2(fd[1],1);
- close(fd[1]);
- execl("/bin/ls","ls",(char *)0);
- //write(fd[1],m,strlen(m+1));
- }
- else// Parent Process
- {
- close(fd[1]);
- dup2(fd[0],0);
- close(fd[0]);
- execl("/usr/bin/wc","wc",(char *)0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement