您的位置:首页 > 运维架构 > Linux

几个关于多线程笔试题(linux)

2015-09-05 16:39 627 查看
子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
pthread_attr_t  attr;
pthread_mutex_t  mutex;
pthread_cond_t cond;

pthread_t pid;

int  flag;
void *func(void *arg) {
int i, k = 0;
while( 1) {
pthread_mutex_lock(&mutex);
flag = 1;
for(i = 1; i <= 10; i++ )
printf("%d ", i);
printf("\n");
pthread_cond_signal(&cond);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
k++;
if(k ==4)
pthread_exit(NULL);
}
}
int main() {
int i, k = 0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_attr_init( &attr);                      /*属性*/
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED);
pthread_create(&pid, &attr, func, NULL );
while(1) {
pthread_mutex_lock(&mutex);
while(flag != 1) {
pthread_cond_wait(&cond, &mutex);
}
flag = 0;
for(i = 0; i < 100; i++) {
printf("%d ",i+1);
}
printf("\n");
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);

k++;
if(k == 4) {
exit(0);
}
}
exit(0);
}


编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC...依次递推。

该例使用互斥量和条件锁

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
pthread_attr_t  attr;
pthread_mutex_t  mutex;
pthread_cond_t condA2B, condB2C, condC2A;

pthread_t tid_A, tid_B;

int flagAB, flagBC;
void *tfnA(void *arg) {
int k = 0;
while(1) {
pthread_mutex_lock(&mutex);
printf("A");
flagAB = 1;
pthread_cond_signal(&condA2B);
pthread_cond_wait(&condC2A, &mutex);
pthread_mutex_unlock(&mutex);
k++;
if(k == 10)
pthread_exit(NULL);
}
}
void *tfnB(void *arg) {
int k = 0;
while(1) {
pthread_mutex_lock(&mutex);
while(flagAB != 1) {
pthread_cond_wait(&condA2B, &mutex);
}
flagAB = 0;
printf("B");
flagBC = 1;
pthread_cond_signal(&condB2C);
pthread_mutex_unlock(&mutex);
k++;
if(k == 10)
pthread_exit(NULL);
}
}
int main() {
int k = 0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&condA2B, NULL);
pthread_cond_init(&condB2C, NULL);
pthread_cond_init(&condC2A, NULL);
pthread_attr_init( &attr);                      /*属性*/
pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid_A, &attr, tfnA, NULL );
pthread_create(&tid_B, &attr, tfnB, NULL );
while(1) {
pthread_mutex_lock(&mutex);
while(flagBC != 1) {
pthread_cond_wait(&condB2C, &mutex);
}
flagBC = 0;
printf("C");
pthread_cond_signal(&condC2A);
pthread_mutex_unlock(&mutex);
k++;
if(k == 10) {
exit(0);
}
}
exit(0);
}


该例使用信号灯

include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
//#include "unpipc.h"
#include <semaphore.h>
pthread_t  tidA, tidB, tidC;
sem_t semA, semB, semC;

void *funcA(void *arg);
void *funcB(void *arg);
void *funcC(void *arg);
int main( ) {
sem_init(&semA, 0, 1);
sem_init(&semB, 0, 0);
sem_init(&semC, 0, 0);
pthread_create(&tidA, NULL, funcA, (void *)&tidA );
pthread_create(&tidB, NULL, funcB, (void *)&tidB );
pthread_create(&tidC, NULL, funcC, (void *)&tidC );
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
pthread_join(tidC, NULL);
sem_destroy(&semA);
sem_destroy(&semB);
sem_destroy(&semC);
exit(0);
}
void *funcA(void *arg) {
int i;
for(i = 0; i< 10; i++){
sem_wait(&semA);
printf("A");
fflush(stdout);
sem_post(&semB);
}
return NULL;
}
void *funcB(void *arg) {
int i;
for(i = 0; i< 10; i++){
sem_wait(&semB);
printf("B");
fflush(stdout);
sem_post(&semC);
}
return NULL;
}
void *funcC(void *arg) {
int i;
for(i = 0; i< 10; i++){
sem_wait(&semC);
printf("C");
fflush(stdout);
sem_post(&semA);
}
return NULL;
}


有四个线程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....

程序例1,使用信号量,但该例的多线程1、2、3、4 只能串行执行

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
FILE *file[4];
pthread_t  tidA, tidB, tidC, tidD;
sem_t semA, semB, semC, semD;
void *tfnA(void *arg);
void *tfnB(void *arg);
void *tfnC(void *arg);
void *tfnD(void *arg);
int main(int argc, char *argv[])
{
file[0] = fopen("A", "a");
file[1] = fopen("B", "a");
file[2] = fopen("C", "a");
file[3] = fopen("D", "a");
sem_init(&semA, 0, 1);
sem_init(&semB, 0, 0);
sem_init(&semC, 0, 0);
sem_init(&semD, 0, 0);
pthread_create(&tidA, NULL, tfnA, NULL );
pthread_create(&tidB, NULL, tfnB, NULL );
pthread_create(&tidC, NULL, tfnC, NULL );
pthread_create(&tidD, NULL, tfnD, NULL );
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
pthread_join(tidC, NULL);
pthread_join(tidD, NULL);
fclose(file[0]);
fclose(file[1]);
fclose(file[2]);
fclose(file[3]);
sem_destroy(&semA);
sem_destroy(&semB);
sem_destroy(&semC);
sem_destroy(&semD);
exit(0);
}
void *tfnA(void *arg){
int count=0;
int i=0;
while(count < 12){
sem_wait(&semA);
fprintf(file[i],"1");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&semB);
}
return NULL;
}
void *tfnB(void *arg){
int count=0;
int i=1;
while(count < 12){
sem_wait(&semB);
fprintf(file[i],"2");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&semC);
}
return NULL;
}
void *tfnC(void *arg){
int count=0;
int i=2;
while(count < 12){
sem_wait(&semC);
fprintf(file[i],"3");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&semD);
}
return NULL;
}
void *tfnD(void *arg){
int count=0;
int i=3;
while(count < 12){
sem_wait(&semD);
fprintf(file[i],"4");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&semA);
}
return NULL;
}


程序2,该例也是使用信号量,但多线程1、2、3、4 是并发执行

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
FILE *file[4];
pthread_t  tidA, tidB, tidC, tidD;
sem_t sem, semA, semB, semC, semD;
void *tfnA(void *arg);
void *tfnB(void *arg);
void *tfnC(void *arg);
void *tfnD(void *arg);
int main(int argc, char *argv[])
{
file[0] = fopen("A", "a");
file[1] = fopen("B", "a");
file[2] = fopen("C", "a");
file[3] = fopen("D", "a");
sem_init(&sem, 0, 0);
sem_init(&semA, 0, 1);
sem_init(&semB, 0, 1);
sem_init(&semC, 0, 1);
sem_init(&semD, 0, 1);
pthread_create(&tidA, NULL, tfnA, NULL );
pthread_create(&tidB, NULL, tfnB, NULL );
pthread_create(&tidC, NULL, tfnC, NULL );
pthread_create(&tidD, NULL, tfnD, NULL );
int count=0;
while (count < 12) {
sem_wait(&sem);
sem_wait(&sem);
sem_wait(&sem);
sem_wait(&sem);
sem_post(&semA);
sem_post(&semB);
sem_post(&semC);
sem_post(&semD);
count ++;
}
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
pthread_join(tidC, NULL);
pthread_join(tidD, NULL);
fclose(file[0]);
fclose(file[1]);
fclose(file[2]);
fclose(file[3]);
sem_destroy(&sem);
sem_destroy(&semA);
sem_destroy(&semB);
sem_destroy(&semC);
sem_destroy(&semD);
exit(0);
}
void *tfnA(void *arg){
int count=0;
int i=0;
while(count < 12){
sem_wait(&semA);
fprintf(file[i],"1");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&sem);
}
return NULL;
}
void *tfnB(void *arg){
int count=0;
int i=1;
while(count < 12){
sem_wait(&semB);
fprintf(file[i],"2");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&sem);
}
return NULL;
}
void *tfnC(void *arg){
int count=0;
int i=2;
while(count < 12){
sem_wait(&semC);
fprintf(file[i],"3");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&sem);
}
return NULL;
}
void *tfnD(void *arg){
int count=0;
int i=3;
while(count < 12){
sem_wait(&semD);
fprintf(file[i],"4");
fflush(file[i]);
i --;
if (i < 0) {
i = 3;
}
count ++;
sem_post(&sem);
}
return NULL;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: