C 多线程题目
来源:互联网 发布:宋慧乔长相 知乎 编辑:程序博客网 时间:2024/06/04 21:46
题目:
有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:
A:1 2 3 4 1 2....
B:2 3 4 1 2 3....
C:3 4 1 2 3 4....
D:4 1 2 3 4 1....
请设计程序。
程序:
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
FILE *f1;
FILE *f2;
FILE *f3;
FILE *f4;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condA_B = PTHREAD_COND_INITIALIZER;
pthread_cond_t condB_C = PTHREAD_COND_INITIALIZER;
pthread_cond_t condC_D = PTHREAD_COND_INITIALIZER;
pthread_cond_t condD_A = PTHREAD_COND_INITIALIZER;
int flaga,flagb,flagc,flagd;
void *th_fun_a(void *arg)
{
int i = 0;
while(i < 10)
{
pthread_mutex_lock(&mutex);
flaga = 1;
pthread_cond_wait(&condD_A,&mutex);
flaga = 0;
f1 = fopen("A.txt","a+");
fseek(f1,0,SEEK_END);
fputc('1',f1);
fclose(f1);
f2 = fopen("B.txt","a+");
fseek(f2,0,SEEK_END);
fputc('2',f2);
fclose(f2);
f3 = fopen("C.txt","a+");
fseek(f3,0,SEEK_END);
fputc('3',f3);
fclose(f3);
f4 = fopen("D.txt","a+");
fseek(f4,0,SEEK_END);
fputc('4',f4);
fclose(f4);
while(!flagb)
{
pthread_mutex_unlock(&mutex);
usleep(50);
pthread_mutex_lock(&mutex);
}
pthread_cond_broadcast(&condA_B);
pthread_mutex_unlock(&mutex);
i++;
usleep(50);
}
flaga = 1;
}
void *th_fun_b(void *arg)
{
int i = 0;
while(i < 10)
{
pthread_mutex_lock(&mutex);
flagb = 1;
pthread_cond_wait(&condA_B,&mutex);
flagb = 0;
f1 = fopen("A.txt","a+");
fseek(f1,0,SEEK_END);
fputc('2',f1);
fclose(f1);
f2 = fopen("B.txt","a+");
fseek(f2,0,SEEK_END);
fputc('3',f2);
fclose(f2);
f3 = fopen("C.txt","a+");
fseek(f3,0,SEEK_END);
fputc('4',f3);
fclose(f3);
f4 = fopen("D.txt","a+");
fseek(f4,0,SEEK_END);
fputc('1',f4);
fclose(f4);
while(!flagc)
{
pthread_mutex_unlock(&mutex);
usleep(50);
pthread_mutex_lock(&mutex);
}
pthread_cond_broadcast(&condB_C);
pthread_mutex_unlock(&mutex);
i++;
usleep(50);
}
}
void *th_fun_c(void *arg)
{
int i = 0;
while(i < 10)
{
pthread_mutex_lock(&mutex);
flagc = 1;
pthread_cond_wait(&condB_C,&mutex);
flagc = 0;
f1 = fopen("A.txt","a+");
fseek(f1,0,SEEK_END);
fputc('3',f1);
fclose(f1);
f2 = fopen("B.txt","a+");
fseek(f2,0,SEEK_END);
fputc('4',f2);
fclose(f2);
f3 = fopen("C.txt","a+");
fseek(f3,0,SEEK_END);
fputc('1',f3);
fclose(f3);
f4 = fopen("D.txt","a+");
fseek(f4,0,SEEK_END);
fputc('2',f4);
fclose(f4);
while(!flagd)
{
pthread_mutex_unlock(&mutex);
usleep(50);
pthread_mutex_lock(&mutex);
}
pthread_cond_broadcast(&condC_D);
pthread_mutex_unlock(&mutex);
i++;
usleep(50);
}
}
void *th_fun_d(void *arg)
{
int i = 0;
while(i < 10)
{
pthread_mutex_lock(&mutex);
flagd = 1;
pthread_cond_wait(&condC_D,&mutex);
flagd = 0;
f1 = fopen("A.txt","a+");
fseek(f1,0,SEEK_END);
fputc('4',f1);
fclose(f1);
f2 = fopen("B.txt","a+");
fseek(f2,0,SEEK_END);
fputc('1',f2);
fclose(f2);
f3 = fopen("C.txt","a+");
fseek(f3,0,SEEK_END);
fputc('2',f3);
fclose(f3);
f4 = fopen("D.txt","a+");
fseek(f4,0,SEEK_END);
fputc('3',f4);
fclose(f4);
while(!flaga)
{
pthread_mutex_unlock(&mutex);
usleep(50);
pthread_mutex_lock(&mutex);
}
pthread_cond_broadcast(&condD_A);
pthread_mutex_unlock(&mutex);
i++;
usleep(50);
}
}
int main()
{
pthread_t th[4];
void *(*th_funs[])(void *) =
{
th_fun_a,
th_fun_b,
th_fun_c,
th_fun_d,
};
int i;
for(i = 0; i < 4; i++)
{
if(pthread_create(th+i,NULL,th_funs[i],(void *)i) < 0)
{
fprintf(stderr,"creat:%s\n",strerror(errno));
exit(-1);
}
}
pthread_mutex_lock(&mutex);
while(!flagd)
{
pthread_mutex_unlock(&mutex);
usleep(50);
pthread_mutex_lock(&mutex);
}
printf("hi\n");
pthread_cond_broadcast(&condD_A);
pthread_mutex_unlock(&mutex);
for(i = 0; i < 4; i++)
{
pthread_join(th[i],NULL);
printf("process %d finished!\n",i);
}
return 0;
}
题目
- C 多线程题目
- 多线程题目
- 多线程题目
- C题目
- C题目
- C 题目
- c 题目
- C题目
- 多线程并发题目
- 实验题目:多线程
- 多线程经典题目
- 多线程:常见题目
- java多线程经典题目
- 多线程基础题目
- 经典的多线程题目
- Java多线程题目
- 多线程题目解答
- Java多线程的题目
- Android05
- 两个链表的第一个公共节点
- RTMP基础学习
- Linux静态库和动态库区别
- Android06
- C 多线程题目
- ufei启动+GPT安装win7
- 读书笔记:《计算机系统概论》——第七章:汇编语言
- POJ 1265 Area 已被翻译
- 关于shell中函数的返回值
- python 遍历目录所有文件
- jQuery字符串函数
- Java线程系列(2)——线程有限等待状态分析
- 第十五周项目1(3)——验证希尔排序