23,116
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/select.h>
#include <errno.h>
int fds[2];
void do_shell();
void do_ioredirect();
void sig_chld(int signo)
{
printf("receive a SIGCHLD\n");
return;
}
int main(int argc,char **argv)
{
char buf[256];
int maxfd,nread;
signal(SIGCHLD,sig_chld);
if(pipe(fds) < 0)
{
perror("pipe");
exit(-1);
}
do_shell();
for(;;)
{
fd_set rset;
FD_ZERO(&rset);
FD_SET(0,&rset);
FD_SET(fds[0],&rset);
maxfd = fds[0] + 1;
memset(buf,0x00,sizeof(buf));
int n = select(maxfd,&rset,NULL,NULL,NULL);
if( -1 == n && errno == EINTR)
continue;
else if(-1 == n)
{
perror("select");
exit(-1);
}
if(FD_ISSET(0,&rset))
{
nread = read(0,buf,256);
write(fds[1],buf,nread);
}
if(FD_ISSET(fds[0],&rset))
{
nread = read(fds[0],buf,256);
printf("%s",buf);
}
}
return 0;
}
void do_shell()
{
int pid;
switch((pid = fork()))
{
case -1:
perror("fork");
exit(-1) ;
case 0:
do_ioredirect();
if(execl("/bin/bash","sh",(char *)0) < 0)
{
perror("execl");
exit(-1);
}
default:
break;
}
return;
}
void do_ioredirect()
{
if(dup2(fds[0],STDIN_FILENO) < 0)
{
perror("fd[0] dup2");
exit(-1);
}
if(dup2(fds[1],STDOUT_FILENO) < 0)
{
perror("fd[1] dup2");
exit(-1);
}
}
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/select.h>
#include <errno.h>
int fds1[2];
int fds2[2];
void do_shell();
void do_ioredirect();
void sig_chld(int signo)
{
printf("receive a SIGCHLD\n");
return;
}
int main(int argc,char **argv)
{
char buf[256];
int maxfd,nread;
signal(SIGCHLD,sig_chld);
if(pipe(fds1) < 0)
{
perror("pipe");
exit(-1);
}
if(pipe(fds2) < 0)
{
perror("pipe");
exit(-1);
}
do_shell();
for(;;)
{
fd_set rset;
FD_ZERO(&rset);
FD_SET(0,&rset);
FD_SET(fds2[0],&rset);
maxfd = fds2[0] + 1;
memset(buf,0x00,sizeof(buf));
int n = select(maxfd,&rset,NULL,NULL,NULL);
if( -1 == n && errno == EINTR)
continue;
else if(-1 == n)
{
perror("select");
exit(-1);
}
if(FD_ISSET(0,&rset))
{
nread = read(0,buf,256);
write(fds1[1],buf,nread);
}
if(FD_ISSET(fds2[0],&rset))
{
nread = read(fds2[0],buf,256);
printf("%s",buf);
}
}
return 0;
}
void do_shell()
{
int pid;
switch((pid = fork()))
{
case -1:
perror("fork");
exit(-1) ;
case 0:
do_ioredirect();
if(execl("/bin/sh","/bin/sh","-i",(char *)0) < 0)
{
perror("execl");
exit(-1);
}
default:
close(fds1[0]);
close(fds2[1]);
break;
}
return;
}
void do_ioredirect()
{
if(dup2(fds1[0],STDIN_FILENO) < 0)
{
perror("fd[0] dup2");
exit(-1);
}
if(dup2(fds2[1],STDOUT_FILENO) < 0)
{
perror("fd[1] dup2");
exit(-1);
}
close(fds1[1]);
close(fds2[0]);
}
charlesye@charlesye-desktop:~/Program/salira_shell$ ./test
$ who
charlesye tty7 Aug 4 19:46 (:0)
charlesye pts/0 Aug 4 19:46 (192.168.108.1)
$ ls
[3]+ Stopped ./test
charlesye@charlesye-desktop:~/Program/salira_shell$ ps aux | grep test
1000 5626 0.0 0.1 2784 724 pts/0 T 20:01 0:00 ./test
1000 5648 0.0 0.1 2784 724 pts/0 T 20:08 0:00 ./test
1000 5682 0.0 0.1 2784 724 pts/0 T 20:09 0:00 ./test
1000 5691 0.0 0.1 2020 568 pts/0 S+ 20:10 0:00 grep test
void do_ioredirect()
{
if(dup2(fds[0],STDIN_FILENO) < 0)
{
perror("fd[0] dup2");
exit(-1);
}
/* 注释掉这一段就没问题了
if(dup2(fds[1],STDOUT_FILENO) < 0)
{
perror("fd[1] dup2");
exit(-1);
}
*/
}