1、有三个线程,ID号分别为ABC,且每个线程中都在循环打印自己的ID。要求打印的结果为ABC.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
int flag = 0;//0:A, 1:B, 2:C
pthread_cond_t cond1, cond2, cond3;
pthread_mutex_t mutex;
void *func1(void *arg)
{
while(1)
{
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 *func2(void *arg)
{
while(1)
{
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 *func3(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 2)
pthread_cond_wait(&cond3, &mutex);
printf("C\n");
flag = 0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond1, NULL);
pthread_cond_init(&cond2, NULL);
pthread_cond_init(&cond3, NULL);
pthread_t tid1, tid2, tid3;
pthread_create(&tid1, NULL, func1, NULL);
pthread_create(&tid2, NULL, func2, NULL);
pthread_create(&tid3, NULL, func3, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
2、实现AB进程对话。
a.A进程发送一句话后,B进程接收到打印。然后B进程发送一句话,A进程接收后打印
b.重复上述步骤。直到AB接收或者发送完quit后,结束AB进程
A代码
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
umask(0);
if(mkfifo("./fifo1", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
if(mkfifo("./fifo2", 0664) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
ssize_t fd1 = open("./fifo1", O_RDONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
ssize_t fd2 = open("./fifo2", O_WRONLY);
if(fd2 < 0)
{
perror("open2");
return -1;
}
char buf1[32]="";
char buf2[32]="";
int res1 = 0;
while(1)
{
res1 = read(fd1, buf1, sizeof(buf1));
if(res1 < 0)
{
perror("read");
return -1;
}
else if(0 == res1)
{
printf("进程退出\n");
}
printf("B:%s\n", buf1);
if(strcmp(buf1, "quit") == 0)
break;
bzero(buf2, sizeof(buf2));
fgets(buf2, sizeof(buf2), stdin);
buf2[strlen(buf2)-1] = '\0';
write(fd2, buf2, sizeof(buf2));
if(strcmp(buf2, "quit") == 0)
break;
}
close(fd1);
close(fd2);
return 0;
}
B代码
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
umask(0);
if(mkfifo("./fifo1", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
if(mkfifo("./fifo2", 0664) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
ssize_t fd1 = open("./fifo1", O_WRONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
ssize_t fd2 = open("./fifo2", O_RDONLY);
if(fd2 < 0)
{
perror("open2");
return -1;
}
char buf1[32]="";
char buf2[32]="";
int res1 = 0;
while(1)
{
bzero(buf2, sizeof(buf2));
fgets(buf2, sizeof(buf2), stdin);
buf2[strlen(buf2)-1] = '\0';
write(fd1, buf2, sizeof(buf2));
if(strcmp(buf2, "quit") == 0)
break;
res1 = read(fd2, buf1, sizeof(buf1));
if(res1 < 0)
{
perror("read");
return -1;
}
else if(0 == res1)
{
printf("进程退出\n");
}
printf("A:%s\n", buf1);
if(strcmp(buf1, "quit") == 0)
break;
}
close(fd1);
close(fd2);
return 0;
}