main1.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd[2];
int ret;
char buff1[1024];
char buff2[1024];
ret = pipe(fd);
if (ret !=0) {
printf("create pipe failed!
");
exit(1);
}
strcpy(buff1, "Hello!");
write(fd[1], buff1, strlen(buff1));
printf("send information:%s
", buff1);
bzero(buff2, sizeof(buff2));
read(fd[0], buff2, sizeof(buff2));
printf("received information:%s
", buff2);
return 0;
}
main2.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd[2];
int ret;
char buff1[1024];
char buff2[1024];
pid_t pd;
ret = pipe(fd);
if (ret !=0) {
printf("create pipe failed!
");
exit(1);
}
pd = fork();
if (pd == -1) {
printf("fork error!
");
exit(1);
} else if (pd == 0) {
bzero(buff2, sizeof(buff2));
read(fd[0], buff2, sizeof(buff2));
printf("process(%d) received information:%s
", getpid(), buff2);
} else {
strcpy(buff1, "Hello!");
write(fd[1], buff1, strlen(buff1));
printf("process(%d) send information:%s
", getpid(), buff1);
}
if (pd > 0) {
wait();
}
return 0;
}
main3.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd[2];
int ret;
char buff1[1024];
char buff2[1024];
pid_t pd;
ret = pipe(fd);
if (ret !=0) {
printf("create pipe failed!
");
exit(1);
}
pd = fork();
if (pd == -1) {
printf("fork error!
");
exit(1);
} else if (pd == 0) {
//bzero(buff2, sizeof(buff2));
sprintf(buff2, "%d", fd[0]);
execl("main3_2", "main3_2", buff2, 0);
printf("execl error!
");
exit(1);
} else {
strcpy(buff1, "Hello!");
write(fd[1], buff1, strlen(buff1));
printf("process(%d) send information:%s
", getpid(), buff1);
}
if (pd > 0) {
wait();
}
return 0;
}
main4.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd[2];
int ret;
char buff1[1024];
char buff2[1024];
pid_t pd;
ret = pipe(fd);
if (ret !=0) {
printf("create pipe failed!
");
exit(1);
}
pd = fork();
if (pd == -1) {
printf("fork error!
");
exit(1);
} else if (pd == 0) {
close(fd[1]);
bzero(buff2, sizeof(buff2));
read(fd[0], buff2, sizeof(buff2));
printf("process(%d) received information:%s
", getpid(), buff2);
} else {
strcpy(buff1, "Hello!");
close (fd[0]);
write(fd[1], buff1, strlen(buff1));
printf("process(%d) send information:%s
", getpid(), buff1);
close (fd[1]);
}
if (pd > 0) {
wait();
}
return 0;
}
main5.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define CMD_EXIT "exit"
int main(void)
{
int fd[2];
int ret;
char buff[80];
pid_t pd;
ret = pipe(fd);
if (ret !=0) {
printf("create pipe failed!
");
exit(1);
}
pd = fork();
if (pd == -1) {
printf("fork error!
");
exit(1);
} else if (pd == 0) {
close(fd[1]);
while (1) {
bzero(buff, sizeof(buff));
if (read(fd[0], buff, sizeof(buff)) == 0) {
break;
}
printf("received information:%s
", buff);
}
printf("receive end!
");
close(fd[0]);
} else {
close (fd[0]);
while(1) {
scanf("%s", buff);
if (strcmp(buff, CMD_EXIT) == 0) {
break;
}
write(fd[1], buff, strlen(buff));
}
close (fd[1]);
}
if (pd > 0) {
wait();
}
return 0;
}
main6.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
int fd[2];
int ret;
char buff1[1024];
char buff2[1024];
pid_t pd;
ret = pipe(fd);
if (ret !=0) {
printf("create pipe failed!
");
exit(1);
}
pd = fork();
if (pd == -1) {
printf("fork error!
");
exit(1);
} else if (pd == 0) {
//bzero(buff2, sizeof(buff2));
//sprintf(buff2, "%d", fd[0]);
close(fd[1]);
close(0);
dup(fd[0]);
close(fd[0]);
execlp("od", "od", "-c", 0);
printf("execl error!
");
exit(1);
} else {
close(fd[0]);
strcpy(buff1, "Hello!");
write(fd[1], buff1, strlen(buff1));
close(fd[1]);
}
return 0;
}
main7.c
#include <stdio.h>
#include <stdlib.h>
#define BUFF_SIZE 1024
int main(void)
{
FILE * file;
char buff[BUFF_SIZE+1];
int cnt;
// system("ls -l > result.txt");
file = popen("ls -l", "r");
if (!file) {
printf("fopen failed!
");
exit(1);
}
cnt = fread(buff, sizeof(char), BUFF_SIZE, file);
if (cnt > 0) {
buff[cnt] = ' ';
printf("%s", buff);
}
pclose(file);
return 0;
}
main8.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFF_SIZE 1024
int main(void)
{
FILE * file;
char buff[BUFF_SIZE+1];
int cnt;
file = popen("./p2", "w");
if (!file) {
printf("fopen failed!
");
exit(1);
}
strcpy(buff, "hello world!");
cnt = fwrite(buff, sizeof(char), strlen(buff), file);
pclose(file);
return 0;
}