1.编写一个程序,开启3个线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示,如ABCABC....….依次递推
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>
pthread_cond_t cond1,cond2,cond3;
pthread_mutex_t mutex;
int flag = 0;
void* callback1(void* arg)
{
for(int i=0;i<10;i++){
pthread_mutex_lock(&mutex);
if(flag!=0){
pthread_cond_wait(&cond1,&mutex);
}
printf("A");
flag = 1;
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callback2(void* arg)
{
for(int i=0;i<10;i++){
pthread_mutex_lock(&mutex);
if(flag!=1){
pthread_cond_wait(&cond2,&mutex);
}
printf("B");
flag = 2;
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callback3(void* arg)
{
for(int i=0;i<10;i++){
pthread_mutex_lock(&mutex);
if(flag!=2){
pthread_cond_wait(&cond3,&mutex);
}
printf("C");
flag = 3;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex,NULL);
if(pthread_cond_init(&cond1,NULL)!=0){
perror("cond");
return -1;
}
if(pthread_cond_init(&cond2,NULL)!=0){
perror("cond");
return -1;
}
if(pthread_cond_init(&cond3,NULL)!=0){
perror("cond");
return -1;
}
pthread_t A,B,C;
if(pthread_create(&A,NULL,callback1,NULL)!=0){
perror("pthread_create1");
return -1;
}
if(pthread_create(&B,NULL,callback2,NULL)!=0){
perror("pthread_create2");
return -1;
}
if(pthread_create(&C,NULL,callback3,NULL)!=0){
perror("pthread_create3");
return -1;
}
pthread_join(A,NULL);
// pthread_cancel(tid2);
pthread_join(B,NULL);
pthread_join(C,NULL);
printf("\n");
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
pthread_mutex_destroy(&mutex);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/8db6c5013e278a5a2118d0e7cbffb42d.png)
创建父子进程,实现父子进程的通话。
1)父进程先发送一句话给子进程,子进程接收打印。
2)子进程发送与句话给父进程,父进程接收后打印。
3)重复1)2)步骤即可。
4)当父进程或者子进程发送quit后,父子进程均要结束。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int fpd[2] = {0};
char arr[64] = "";
if(pipe(fpd)<0){
perror("pipe");
return -1;
}
pid_t pid = fork();
if(pid>0){
while(1){
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1] = '\0';
write(fpd[1],arr,strlen(arr));
if(arr[0]=='q'&&arr[1]=='u'&&arr[2]=='i'
&&arr[3]=='t'&&strlen(arr)==4){
//break;
exit(1);
}
printf("父进程写入管道:%s\n",arr);
sleep(1);
bzero(arr,sizeof(arr));
read(fpd[0],arr,sizeof(arr));
if(arr[0]=='q'&&arr[1]=='u'&&arr[2]=='i'
&&arr[3]=='t'&&strlen(arr)==4){
//break;
exit(1);
}
printf("父进程读取管道:%s\n",arr);
bzero(arr,sizeof(arr));
}
}else if(pid == 0){
while(1){
read(fpd[0],arr,sizeof(arr));
if(arr[0]=='q'&&arr[1]=='u'&&arr[2]=='i'
&&arr[3]=='t'&&strlen(arr)==4){
//break;
exit(1);
}
printf("子进程读取管道:%s\n",arr);
bzero(arr,sizeof(arr));
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1] = '\0';
write(fpd[1],arr,strlen(arr));
if(arr[0]=='q'&&arr[1]=='u'&&arr[2]=='i'
&&arr[3]=='t'&&strlen(arr)==4){
//break;
exit(1);
}
printf("子进程写入管道:%s\n",arr);
sleep(1);
}
}else{
perror("fork");
return -1;
}
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/ff86b84c5ee84327eefa7921d1deb2df.png)