#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define N 10
#define MAX 100
int child_read_pipe(int fd)
{
char buf[N]
int n = 0
while(1)
{
n = read(fd,buf,sizeof(buf))
buf[n] = '\0'
printf("Read %d bytes : %s.\n",n,buf)
if(strncmp(buf,"quit",4) == 0)
break
}
return 0
}
int father_write_pipe(int fd)
{
char buf[MAX] = {0}
while(1)
{
printf(">")
fgets(buf,sizeof(buf),stdin)
buf[strlen(buf)-1] = '\0'
write(fd,buf,strlen(buf))
usleep(500)
if(strncmp(buf,"quit",4) == 0)
break
}
return 0
}
int main()
{
int pid
int fd[2]
if(pipe(fd) <0)
{
perror("Fail to pipe")
exit(EXIT_FAILURE)
}
if((pid = fork()) <0)
{
perror("Fail to fork")
exit(EXIT_FAILURE)
}else if(pid == 0){
close(fd[1])
child_read_pipe(fd[0])
}else{
close(fd[0])
father_write_pipe(fd[1])
}
exit(EXIT_SUCCESS)
}
fork()应该是Linux操作系统的函数。从#include <sys/signal.h>和fork()上判断,这个程序应该是Linux操作系统的应用程序。
Windows上应该用CreateProcess创建进程
获取进程ID用
DWORD GetWindowThreadProcessld(HWND hwnd,LPDWORD lpdwProcessld)
hWnd:窗口句柄。
lpdwProcessld:接收进程标识的32位值的地址。如果这个参数不为NULL,GetWindwThreadProcessld将进程标识拷贝到这个32位值中,否则不拷贝。
返回值:返回值为创建窗口的线程标识。