#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#define MAXLINE 50
main(void)
{
int n;
int fd[2];
pid_t pid;
char line[MAXLINE];
if (pipe(fd) < 0) printf("pipe error");
if ((pid = fork()) < 0) printf("fork error");
else if (pid > 0)
{ /* parent */
close(fd[0]);
sleep(10);
write(fd[1], "hello world\n", 12);
}
else
{ /* child */
close(fd[1]);
n = read(fd[0], line, MAXLINE);
printf("n=%d,line:%s",n,line);
}
exit(0);
}
static int pfd1[2], pfd2[2];
void TELL_WAIT(void)
{
if (pipe(pfd1) < 0 || pipe(pfd2) < 0) err_sys("pipe error");
}
void TELL_PARENT(pid_t pid)
{
if (write(pfd2[1], "c", 1) != 1)
err_sys("write error");
}
void WAIT_PARENT(void)
{
char c;
if (read(pfd1[0], &c, 1) != 1) err_sys("read error");
if (c != 'p') err_quit("WAIT_PARENT: incorrect data");
}
void TELL_CHILD(pid_t pid)
{
if (write(pfd1[1], "p", 1) != 1) err_sys("write error");
}
void WAIT_CHILD(void)
{
char c;
if (read(pfd2[0], &c, 1) != 1) err_sys("read error");
if (c != 'c') err_quit("WAIT_CHILD: incorrect data");
}
int pclose(FILE *stream);
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
int main()
{
char buf[128];
FILE *pp;
if( (pp = popen("ls -l", "r")) == NULL )
{
printf("popen() error!\n");
exit(1);
}
system("ls -l");
while(fgets(buf, sizeof(buf), pp))
{
printf("%s", buf);
}
pclose(pp);
return 0;
}
int main()
{
sleep(10);
printf("tttttttttttttttttttttttttttt\n");
}
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
FILE * ret;
ret = popen("./t","r");
//system("./t");
//while(!feof(ret)) printf("%c",fgetc(ret));
printf("mmmmmmmmmmmmmmmmmmmmm\n");
pclose(ret);
return 0;
}