parent 에서 실행하는 bash의 출력을 child로 보내기인데요
글쓴이: study / 작성시간: 목, 2017/03/02 - 10:41오후
여기서 해보려고 하는건요
child process에서 "shell"이라고 입력하면 parent process에서
system("/usr/bin/bash")를 실행하고 그 출력을 child process에게
전해주는 거에요.
그리고 child process에서 shell을 exit할때까지는 계속 shell을
쓰고 있는 것처럼하고 싶은데요.
아래 code는 이제 시작하고 있는 중인데요
parent()에서 bash를 수행하면 어떻게 그 출력을 child로 보낼 수 있을지
모르겠네요.
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <error.h>
void child(int socket)
{
char buf[1024];
int err;
int n;
const char hello[] = "test message from child";
write(socket, hello, sizeof(hello));
while(1)
{
printf("in child\n");
n = read(socket, buf, sizeof(buf));
if (n > 0)
printf("child received '%.*s'\n",n, buf);
sleep(5);
}
}
void parent(int socket)
{
char buf[1024];
const char hello[] = "test message from parent";
int n = read(socket, buf, sizeof(buf));
printf("parent received '%.*s'\n",n, buf);
system("/usr/bin/bash");
write(socket, hello, sizeof(hello));
while(1)
{
printf("in parent\n");
sleep(5);
}
}
int main(void)
{
int fd[2];
static const int parentsocket = 0;
static const int childsocket = 1;
pid_t pid;
socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
pid = fork();
if (pid == 0)
{
close(fd[parentsocket]);
child(fd[childsocket]);
}
else
{
close(fd[childsocket]);
parent(fd[parentsocket]);
}
exit(0);
}Forums:


댓글 달기