创建ABC三个线程,要求ABC线程顺序运行,不考虑退出条件。
#include <stdio.h>
#include <pthread.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;
void* handler_a(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* handler_b(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* handler_c(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 2)
{
pthread_cond_wait(&cond3, &mutex);
}
printf("C");
flag = 0;
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid_a, tid_b, tid_c;
pthread_create(&tid_a, NULL, handler_a, NULL);
pthread_create(&tid_b, NULL, handler_b, NULL);
pthread_create(&tid_c, NULL, handler_c, NULL);
pthread_join(tid_a, NULL);
pthread_join(tid_b, NULL);
pthread_join(tid_c, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}
> 创建父子进程,实现父子进程的通话。
>
> 1)父进程先发送一句话给子进程,子进程接收打印。
>
> 2)子进程发送与句话给父进程,父进程接收后打印。
>
> 3)重复1)2)步骤即可。
>
> 4)当父进程或者子进程发送quit后,父子进程均要结束。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define N 128
int main(int argc, const char *argv[])
{
int pfd1[2], pfd2[2];
if(pipe(pfd1) < 0)
{
perror("pipe");
return -1;
}
if(pipe(pfd2) < 0)
{
perror("pipe");
return -1;
}
ssize_t res = 0;
char str[N] = "";
pid_t pid = fork();
if(pid > 0)
{
close(pfd1[0]);
close(pfd2[1]);
while(1)
{
bzero(str,sizeof(str));
printf("父进程>>>");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1] = 0;
if(write(pfd1[1], str, sizeof(str)) < 0)
{
perror("write");
return -1;
}
if(strncasecmp(str, "quit", 4) == 0)
{
break;
}
bzero(str, sizeof(str));
res = read(pfd2[0], str, sizeof(str));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("子进程退出\n");
break;
}
printf("子进程说: %s\n", str);
if(strncasecmp(str, "quit", 4) == 0)
{
break;
}
}
wait(NULL);
close(pfd1[1]);
close(pfd2[0]);
}
else if(0 == pid)
{
close(pfd1[1]);
close(pfd2[0]);
while(1)
{
bzero(str, sizeof(str));
res = read(pfd1[0], str, sizeof(str));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("父进程退出\n");
break;
}
printf("父进程说: %s\n", str);
if(strncasecmp(str, "quit", 4) == 0)
{
break;
}
bzero(str, sizeof(str));
printf("子进程>>>");
fgets(str, sizeof(str), stdin);
str[strlen(str)-1] = 0;
if(write(pfd2[1], str, sizeof(str)) < 0)
{
perror("write");
return -1;
}
if(strncasecmp(str, "quit", 4) == 0)
{
break;
}
}
close(pfd1[0]);
close(pfd2[1]);
}
else
{
perror("fork");
return -1;
}
return 0;
}