#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[], char **environ)
{
int fd[2];
pid_t pid;
int stat_val;
if (argc < 2) {
printf("wrong parameters \n");
exit(0);
}
if (pipe(fd)) {
perror("pipe failed");
exit(1);
}
pid = fork();
switch (pid) {
case -1:
perror("fork failed\n");
exit(1);
case 0:
close(0);
dup(fd[0]);
execve("ctrlprocess.exe", (void *)argv, environ);
exit(0);
default:
close(fd[0]);
write(fd[1], argv[1], strlen(argv[1]));
break;
}
wait(&stat_val);
exit(0);
}
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int arg, char *argv[])
{
char buf[1024];
ssize_t n;
while(1) {
if ((n = read(0, buf, 1024)) > 0) {
buf[n] = '\0';
printf("ctrlprocess receive: %s\n", buf);
if (!strcmp(buf, "exit"))
exit(0);
if (!strcmp(buf, "getpid")) {
printf("My pid: %d\n", getpid());
sleep(3);
exit(0);
}
}
}
}
beyes@linux-beyes:~/C/pipe> ./monitor.exe getpid
ctrlprocess receive: getpid
My pid: 9579
beyes@linux-beyes:~/C/pipe> ./monitor.exe exit
ctrlprocess receive: exit
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |