22.11.15 IO day 6

1. 编写一个程序,开启 3 个 线程,这 3 个线程的 ID 分别为 ABC ,每个线程将自己的 ID 在屏幕上打
10 遍,要求输出结果必须按 ABC 的顺序显示,如 ABCABC…… 依次递推
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3
^[[A^[[Ac^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3

ubuntu@ubuntu:~/yuyu/yu/8$ cat 16.c
#if 0
 编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,
 每个线程将自己的ID在屏幕上打印10遍,
要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推
#endif



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
//创建互斥锁
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
//创建条件变量
pthread_cond_t cond1=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2=PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3=PTHREAD_COND_INITIALIZER;

int flag=0;
int i=0;
void *callBack1(void *arg)//线程1
{
	while(i<10)
	{
#if 0
		if(i==10)
		{
			break;
		}
#endif
		//上锁
		pthread_mutex_lock(&mutex);
		//休眠
		if(0!=flag)
		{
			pthread_cond_wait(&cond1,&mutex);
		}
		printf("%d,tip1\n",i);
		sleep(1);
		flag=1;
		pthread_cond_signal(&cond2);
		//解锁
		pthread_mutex_unlock(&mutex);
		i++;
#if 0
		if(i==10)
		{
			putchar(10);
			break;
		}
#endif
	}
	

}
void *callBack2(void *arg)//线程1
{
	while(i<10)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//休眠
		if(1!=flag)
		{
			pthread_cond_wait(&cond2,&mutex);
		}
		printf("tip2\n");
	//	sleep(1);
		flag=2;
		pthread_cond_signal(&cond3);
		//解锁
		pthread_mutex_unlock(&mutex);
	}

}
void *callBack3(void *arg)//线程1
{
	while(i<10)
	{
		//上锁
		pthread_mutex_lock(&mutex);
		//休眠
		if(2!=flag)
		{
			pthread_cond_wait(&cond3,&mutex);
		}
		printf("tip3\n");
	//	sleep(1);
		flag=1;
		pthread_cond_signal(&cond1);
		//解锁
		pthread_mutex_unlock(&mutex);
#if 1
		if(i==10)
		{
			putchar(10);
			break;
		}
#endif
	}
}

int main(int argc, const char *argv[])
{
	//创建线程1
	pthread_t tid1;
	if(pthread_create(&tid1,NULL,callBack1,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	//创建线程2
	pthread_t tid2;
	if(pthread_create(&tid2,NULL,callBack2,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	//创建线程3
	pthread_t tid3;
	if(pthread_create(&tid3,NULL,callBack3,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	//阻塞
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_join(tid3,NULL);
	//销毁锁
	pthread_mutex_destroy(&mutex);
	return 0;
}


ubuntu@ubuntu:~/yuyu/yu/8$ 
ubuntu@ubuntu:~/yuyu/yu/8$ 
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
16.c:17:46: error: expected ‘,’ or ‘;’ before ‘:’ token
 pthread_cond_t cond1=PTHREAD_COND_INITIALIZER:
                                              ^
16.c:18:46: error: expected identifier or ‘(’ before ‘:’ token
 pthread_cond_t cond2=PTHREAD_COND_INITIALIZER:
                                              ^
16.c:19:46: error: expected identifier or ‘(’ before ‘:’ token
 pthread_cond_t cond3=PTHREAD_COND_INITIALIZER:
                                              ^
16.c: In function ‘callBack1’:
16.c:29:9: error: ‘flag’ undeclared (first use in this function); did you mean ‘float’?
   if(0!=flag)
         ^~~~
         float
16.c:29:9: note: each undeclared identifier is reported only once for each function it appears in
16.c:36:24: error: ‘cond2’ undeclared (first use in this function); did you mean ‘cond1’?
   pthread_cond_signal(&cond2);
                        ^~~~~
                        cond1
16.c: In function ‘callBack2’:
16.c:49:9: error: ‘flag’ undeclared (first use in this function); did you mean ‘float’?
   if(1!=flag)
         ^~~~
         float
16.c:51:23: error: ‘cond2’ undeclared (first use in this function); did you mean ‘cond1’?
    pthread_cond_wait(&cond2,&mutex);
                       ^~~~~
                       cond1
16.c:56:24: error: ‘cond3’ undeclared (first use in this function); did you mean ‘cond2’?
   pthread_cond_signal(&cond3);
                        ^~~~~
                        cond2
16.c: In function ‘callBack3’:
16.c:68:9: error: ‘flag’ undeclared (first use in this function); did you mean ‘float’?
   if(2!=flag)
         ^~~~
         float
16.c:70:23: error: ‘cond3’ undeclared (first use in this function); did you mean ‘cond1’?
    pthread_cond_wait(&cond3,&mutex);
                       ^~~~~
                       cond1
16.c: In function ‘main’:
16.c:108:25: error: ‘nutex’ undeclared (first use in this function); did you mean ‘mutex’?
  pthread_mutex_destroy(&nutex);
                         ^~~~~
                         mutex
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
16.c: In function ‘main’:
16.c:108:25: error: ‘nutex’ undeclared (first use in this function); did you mean ‘mutex’?
  pthread_mutex_destroy(&nutex);
                         ^~~~~
                         mutex
16.c:108:25: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
tip2
tip3
tip1
^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
0,tip1
tip2
tip3
0,tip1
tip2
tip3
0,tip1
tip2
^[tip3
0,tip1
^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
16.c:23:1: error: expected identifier or ‘(’ before ‘while’
 while(i<10)
 ^~~~~
16.c: In function ‘main’:
16.c:89:31: error: ‘callBack1’ undeclared (first use in this function); did you mean ‘calloc’?
  if(pthread_create(&tid1,NULL,callBack1,NULL)!=0)
                               ^~~~~~~~~
                               calloc
16.c:89:31: note: each undeclared identifier is reported only once for each function it appears in
16.c:96:31: error: ‘callBack2’ undeclared (first use in this function); did you mean ‘callBack1’?
  if(pthread_create(&tid2,NULL,callBack2,NULL)!=0)
                               ^~~~~~~~~
                               callBack1
16.c:103:31: error: ‘callBack3’ undeclared (first use in this function); did you mean ‘callBack2’?
  if(pthread_create(&tid3,NULL,callBack3,NULL)!=0)
                               ^~~~~~~~~
                               callBack2
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
0,tip1
tip2
tip3
0,tip1
tip2
^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3



^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3

^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1

tip2
tip3
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3
^[[A^[[Ac^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 16.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
0,tip1
tip2
tip3
1,tip1
tip2
tip3
2,tip1
tip2
tip3
3,tip1
tip2
tip3
4,tip1
tip2
tip3
5,tip1
tip2
tip3
6,tip1
tip2
tip3
7,tip1
tip2
tip3
8,tip1
tip2
tip3
9,tip1
tip2
tip3

ubuntu@ubuntu:~/yuyu/yu/8$ cat 16.c
#if 0
 编写一个程序,开启3个 线程,这3个线程的ID分别为ABC,
 每个线程将自己的ID在屏幕上打印10遍,
要求输出结果必须按ABC的顺序显示,如ABCABC……依次递推
#endif


 

 

创建父子进程,实现父子进程的通话。
1 )父进程先发送一句话给子进程,子进程接收打印。
2 )子进程发送与句话给父进程,父进程接收后打印。
3 )重复 1 2 )步骤即可。
4 )当父进程或者子进程发送 quit 后,父子进程均要结束。
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:你好
子进程写入管道成功:128
父进程打印:你好
父进程对子进程说:quit
父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:你好
父进程写入管道成功:128
子进程打印:你好
子进程对父进程说:qu    


^C
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:你好
父进程写入管道成功:128
子进程打印:你好
子进程对父进程说:quit
子进程写入管道成功:128
父进程打印:quit
ubuntu@ubuntu:~/yuyu/yu/8$ cat 15.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
	//创建管道1
	int pfd1[2];
	if(pipe(pfd1)<0)
	{
		perror("pipe");
		return -1;
	}
	printf("无名管道1创建成功 r=%d w=%d\n",pfd1[0],pfd1[1]);
	
	//创建管道2
	int pfd2[2];
	if(pipe(pfd2)<0)
	{
		perror("pipe");
		return -1;
	}
	printf("无名管道2创建成功 r=%d w=%d\n",pfd2[0],pfd2[1]);

	ssize_t ret1;
	ssize_t ret2;
	char buf1[128]="";
	char buf2[128]="";
	//创建子进程
	pid_t pid=fork();
	while(1)
	{
	//父进程
	if(pid>0)
	{
		while(1)
		{
		//	bzero(buf1,sizeof(buf1));
			printf("父进程对子进程说:");
			scanf("%s",buf1);
#if 0
		fgets(buf1,sizeof(buf1),stdin);

			buf1[sizeof(buf1)-1]='\0';
#endif
		  //  gets(buf1);
			getchar();

			//父进程写数据到管道1
			ret1=write(pfd1[1],buf1,sizeof(buf1));
			if(ret1<0)
			{
				perror("write");
				return -1;
			}
			printf("父进程写入管道成功:%ld\n",ret1);
			if(strcmp(buf1,"quit")==0)
			{
				break;
			}
			//父进程从管道2读取数据
	      //  bzero(buf2,sizeof(buf2));//置0有问题,行缓冲有问题
			ret2=read(pfd2[0],buf2,sizeof(buf2));
			if(ret2<0)
			{
				perror("read");
				return -1;
			}
			//sleep(1);
			printf("父进程打印:%s\n",buf2);
			if(strcmp(buf2,"quit")==0);
			{
				break;
			}
	        //printf("父进程写入成功\n");
		//sleep(3);
		}
	}
	//子进程
	
	else if(pid==0)
	{
	//	sleep(3);
		while(1)
		{
			//子进程从管道1读取数据
			bzero(buf1,sizeof(buf1));//刷新行缓冲
			ret1=read(pfd1[0],buf1,sizeof(buf1));
			if(ret1<0)
			{
				perror("read");
				return -1;
			}
			if(strcmp(buf1,"quit")==0)
			{
				break;
			}
		//	sleep(1);
			printf("子进程打印:%s\n",buf1);

			
		//子进程写数据到管道2
			bzero(buf2,sizeof(buf2));
			printf("子进程对父进程说:");
			scanf("%s",buf2);
#if 0
		fgets(buf2,sizeof(buf2),stdin);
			buf2[sizeof(buf2)-1]='\0';
#endif
		//gets(buf2);
			getchar();
			ret2=write(pfd2[1],buf2,sizeof(buf2));
			if(ret2<0)
			{
				perror("write");
				return -1;
			}
			printf("子进程写入管道成功:%ld\n",ret2);
			if(strcmp(buf2,"quit")==0)
			{
				break;
			}
         	//printf("子进程写入成功\n");
		}
	//	exit(0);
#if 0 	
		close(pfd2[0]);
		close(pfd2[1]);
		close(pfd1[0]);
		close(pfd1[1]);
	//	wait(0);
		exit(0);
#endif  //在这关闭刷新不了
	}
#if 0
			else if(strcmp(buf2,"quit")==0)
			{
				break;
			}
		else if(strcmp(buf1,"quit")==0)
			{
				break;
			}
#endif
	else
	{
		perror("fork");
		return -1;
	}
			 if(strcmp(buf2,"quit")==0)
			{
				break;
			}
		else if(strcmp(buf1,"quit")==0)
			{
				break;
			}
	}
#if 1
		close(pfd2[0]);
		close(pfd2[1]);
		close(pfd1[0]);
		close(pfd1[1]);
		//wait(NULL);//没啥用死循环
#endif
	
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/8$ 

缺陷:只能输入连续的,空格会换行

15.c:47:14: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
子进程打印:hello
子进程说:yes

^C
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
子进程打印:hello
子进程说:hello
hello
quit
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
父进程写入成功
子进程打印:hello
子进程说:world
子进程写入成功
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
父进程写入成功
子进程打印:hello
子进程说:world
子进程写入成功
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:112:5: error: break statement not within loop or switch
     break;
     ^~~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:112:5: error: break statement not within loop or switch
     break;
     ^~~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:112:5: error: break statement not within loop or switch
     break;
     ^~~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
父进程写入成功
父进程:子进程打印:hello
子进程说:world
父进程写入成功
父进程:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
父进程:子进程打印:hello
子进程说:world
父进程:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
父进程:子进程打印:hello
子进程说:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:44:4: error: too many arguments to function ‘getchar’
    getchar(10);
    ^~~~~~~
In file included from 15.c:1:0:
/usr/include/stdio.h:484:12: note: declared here
 extern int getchar (void);
            ^~~~~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:44:4: error: too many arguments to function ‘getchar’
    getchar('\0');
    ^~~~~~~
In file included from 15.c:1:0:
/usr/include/stdio.h:484:12: note: declared here
 extern int getchar (void);
            ^~~~~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:he      
^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello

父进程:子进程打印:hello
子进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程:hello
父进程:子进程打印:hello
子进程说:world
父进程:hello
父进程:world
子进程打印:world
子进程说:hello
子进程打印:hello
子进程说:hello
父进程:1
父进程:2
子进程打印:hello
子进程说:3
子进程打印:1
子进程说:4
父进程:5
父进程:6
子进程打印:4
子进程说:7
子进程打印:5
子进程说:1
父进程:2
父进程:3
子进程打印:1
子进程说:4
子进程打印:2
子进程说:5
父进程:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:1
写入管道成功:128
父进程对子进程说:子进程打印:1
子进程说:2
写入管道成功:128
父进程对子进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
父进程对子进程说:子进程打印:hello
子进程对父进程说:world
父进程写入管道成功:128
父进程对子进程说:hello
父进程写入管道成功:128
父进程对子进程说:hello
子进程写入管道成功:128
子进程打印:world
子进程对父进程说:hello
父进程写入管道成功:128
父进程对子进程说:hello
父进程写入管道成功:128
父进程对子进程说:hello
子进程写入管道成功:128
子进程打印:hello
子进程对父进程说:hello
父进程写入管道成功:128
父进程对子进程说:quit
父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
子进程打印:hello
子进程对父进程说:子进程写入管道成功:128
子进程打印:hello
子进程对父进程说:子进程写入管道成功:128
子进程打印:hello
子进程对父进程说:子进程写入管道成功:128
父进程打印:hello
./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:quit
父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
quit

Command 'quit' not found, but can be installed with:

sudo snap install quit

ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
gcc 15.c -pthread
15.c: In function ‘main’:
15.c:77:2: error: ‘else’ without a previous ‘if’
  else if(pid==0)
  ^~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:77:2: error: ‘else’ without a previous ‘if’
  else if(pid==0)
  ^~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:quit
父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:1
父进程写入管道成功:128
子进程打印:1
子进程对父进程说:2
父进程打印:2
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:1
父进程打印:1
子进程写入管道成功:128
1
1


2

3
2
13
3
313

^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
子进程写入管道成功:128
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
子进程写入管道成功:128
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
gcc 15.c -pthread
15.c: In function ‘main’:
15.c:134:8: warning: passing argument 1 of ‘wait’ makes pointer from integer without a cast [-Wint-conversion]
   wait(1);
        ^
In file included from 15.c:8:0:
/usr/include/x86_64-linux-gnu/sys/wait.h:77:16: note: expected ‘int *’ but argument is of type ‘int’
 extern __pid_t wait (int *__stat_loc);
                ^~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:134:8: warning: passing argument 1 of ‘wait’ makes pointer from integer without a cast [-Wint-conversion]
   wait(1);
        ^
In file included from 15.c:8:0:
/usr/include/x86_64-linux-gnu/sys/wait.h:77:16: note: expected ‘int *’ but argument is of type ‘int’
 extern __pid_t wait (int *__stat_loc);
                ^~~~
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
子进程写入管道成功:128






^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128
gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
父进程对子进程说:quit
父进程写入管道成功:128
父进程对子进程说:quit
父进程写入管道成功:128
父进程对子进程说:quit
父进程写入管道成功:128
父进程对子进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
fork: Success
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:quit
父进程写入管道成功:128
fork: Success
ubuntu@ubuntu:~/yuyu/yu/8$ fork: Success
gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
父进程对子进程说:子进程写入管道成功:128
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
父进程对子进程说:quit
write: Bad file descriptor
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:1
父进程写入管道成功:128
子进程打印:1
子进程对父进程说:2
父进程打印:2
父进程对子进程说:子进程写入管道成功:128

^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
父进程打印:world
父进程对子进程说:子进程写入管道成功:128
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
父进程对子进程说:quit
父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:world
子进程写入管道成功:128
父进程打印:world
父进程对子进程说:you are pretty
父进程写入管道成功:128
子进程打印:you
子进程对父进程说:thank you!
子进程写入管道成功:128
父进程打印:thank
父进程对子进程说:父进程写入管道成功:128
子进程打印:are
子进程对父进程说:子进程写入管道成功:128
父进程打印:you!
父进程对子进程说:父进程写入管道成功:128
子进程打印:pretty
子进程对父进程说:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:46:7: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
       gets(buf1);
       ^~~~
       fgets
/tmp/ccLvSU7m.o: In function `main':
15.c:(.text+0x147): warning: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:you are pretty!

父进程写入管道成功:128
子进程打印:you are pretty!
子进程对父进程说:thank you!

父进程打印:thank you!
父进程对子进程说:子进程写入管道成功:128


父进程写入管道成功:128
子进程打印:
子进程对父进程说:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
15.c: In function ‘main’:
15.c:47:7: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
       gets(buf1);
       ^~~~
       fgets
/tmp/ccT4Qsru.o: In function `main':
15.c:(.text+0x162): warning: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:you are pretty!

父进程写入管道成功:128
子进程打印:you are pretty!
子进程对父进程说:thank you!

子进程写入管道成功:128
父进程打印:thank you!
父进程对子进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:you are pretty!

父进程写入管道成功:128
子进程打印:you are pretty!
子进程对父进程说:thank you!

父进程打印:thank you!
父进程对子进程说:子进程写入管道成功:128


父进程写入管道成功:128
子进程打印:
子进程对父进程说:quit

父进程打印:quit
ubuntu@ubuntu:~/yuyu/yu/8$ 子进程写入管道成功:128

ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:quit

父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:you are pretty!

父进程写入管道成功:128
子进程打印:you are pretty!

子进程对父进程说:thank you!

子进程写入管道成功:128
父进程打印:thank you!

父进程对子进程说:quit

父进程写入管道成功:128
子进程打印:quit

子进程对父进程说:quit

子进程写入管道成功:128
父进程打印:quit

父进程对子进程说:hello

父进程写入管道成功:128
子进程打印:hello

子进程对父进程说:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:you are

父进程写入管道成功:128
子进程打印:you are

子进程对父进程说:1 2

父进程打印:1 2

父进程对子进程说:子进程写入管道成功:128


父进程写入管道成功:128
子进程打印:

子进程对父进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:1 2

父进程写入管道成功:1
子进程打印:1
子进程对父进程说:2 1

子进程写入管道成功:1
父进程打印:2
父进程对子进程说:
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:12

父进程写入管道成功:128
子进程打印:12

子进程对父进程说:^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:1 2

父进程写入管道成功:128
子进程打印:1 2

子进程对父进程说:
21
子进程写入管道成功:128
父进程打印:

父进程对子进程说:12

父进程写入管道成功:128
子进程打印:12

子进程对父进程说:1
子进程写入管道成功:128
父进程打印:1

父进程对子进程说:2

父进程写入管道成功:128
子进程打印:2

子进程对父进程说:12
子进程写入管道成功:128
父进程打印:

父进程对子进程说:^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:你好 
父进程打印:你好
父进程对子进程说:子进程写入管道成功:128


^C
ubuntu@ubuntu:~/yuyu/yu/8$ gcc 15.c -pthread
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:hello
父进程写入管道成功:128
子进程打印:hello
子进程对父进程说:你好
子进程写入管道成功:128
父进程打印:你好
父进程对子进程说:quit
父进程写入管道成功:128
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:你好
父进程写入管道成功:128
子进程打印:你好
子进程对父进程说:qu    


^C
ubuntu@ubuntu:~/yuyu/yu/8$ ./a.out
无名管道1创建成功 r=3 w=4
无名管道2创建成功 r=5 w=6
父进程对子进程说:你好
父进程写入管道成功:128
子进程打印:你好
子进程对父进程说:quit
子进程写入管道成功:128
父进程打印:quit

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值