多线程试题汇总

一.概念题

1.线程的基本概念、线程的基本状态及状态之间的关系?

      线程是进程中某个单一顺序的控制流,是程序执行流的最小单位。线程由线程ID、当前指令指针、寄存器集合和堆栈组成。线程是进程的一个实体,是被系统调度和分配的基本单位,线程与同一进程中的其他线程共享进程的全部资源。

      线程有五种基本状态:新生状态,就绪状态,运行状态,阻塞状态,死亡状态。状态间关系如下图:

2.线程与进程的区别?

      (1)进程是系统资源分配和调度的独立单位;线程是进程的一个实体,是CPU调度和分派的基本单位。      
      (2)系统资源分配给进程;线程与资源分配无关,线程与同一进程内的其他线程共享进程的全部资源。
      (3)不同的进程拥有不同的虚拟地址;同一进程下的不同线程共享同一地址空间。

3.在Windows编程中互斥量与临界区比较类似,请分析一下二者的主要区别。

      (1)临界区不是内核对象,只能用于进程内的线程间同步,速度快;
      (2)互斥量是内核对象,可以用于进程间同步,在核心态进行锁操作,速度慢;
      (3)Linux中只有互斥量,用来保护临界区,保证同一时间只由一个线程访问临界区的共享资源。

4.多线程同步和互斥有何异同?多线程同步和互斥有几种实现方法?在什么情况下分别使用他们?

      同步指线程间的一种制约关系,一个线程的执行依赖于另一个线程的消息,消息未到达就等待,到达就被唤醒;
      互斥指对于一个进程的共享资源,任何时候只有一个线程可以访问,其他要访问的线程必须等待,直到该线程释放资源;
      实现方法分为用户模式和内核模式。用户模式:临界区;内核模式:互斥量,信号量,事件。
      (信号量允许多个线程同时使用共享资源,规定了同时访问共享资源的线程最大数目。
      事件可以实现不同进程中的线程同步操作,并且可以方便的实现多个线程的优先比较等待操作。)

 

二.选择题

1.以下多线程对int型变量x的操作,哪几个不需要进行同步:

       A. x=y;      B. x++;    C. ++x;    D. x=1;

2.多线程中栈与堆是公有的还是私有的

      A:栈公有,堆私有
      B:栈公有,堆公有
      C:栈私有,堆公有
      D:栈私有,堆私有

 

三.编程题

1.子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码。 

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<pthread.h>
#include<unistd.h>

int num=1;  //全局变量

static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void *func();

int main()
{
	pthread_t tid;
	int ret=0,i,j;
	if((ret=pthread_create(&tid,NULL,func,NULL))!=0)
		printf("create thread error!\n");
	for(i=0;i<3;i++)
	{

		pthread_mutex_lock(&mutex);
        if(num!=0)
			pthread_cond_wait(&cond,&mutex);
		num=1;
		for(j=0;j<10;j++)
    	{
	    	printf("0");
		}
    	printf("\n");
		pthread_mutex_unlock(&mutex);
		pthread_cond_signal(&cond);
	}
	return 0;
}

void *func()
{
	int i,j;
	for(i=0;i<3;i++)
	{
		pthread_mutex_lock(&mutex);
		if(num!=1)
			pthread_cond_wait(&cond,&mutex);
		num=0;
		for(j=0;j<5;j++)
    	{
	    	printf("1");
		}
		printf("\n");
		pthread_mutex_unlock(&mutex);
		pthread_cond_signal(&cond);
	}
	pthread_exit(0);
}

 

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

#include<stdio.h>
#include<stdlib.h>
#include<error.h>
#include<unistd.h>
#include<pthread.h>

int num=0;

static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void *func(void *);

int main()
{
	pthread_t tid[3];
	int ret=0,i;
	for(i=0;i<3;i++)
		if((ret=pthread_create(&tid[i],NULL,func,(void*)i))!=0)
			printf("create thread_%c error\n",i+'A');
   	for(i=0;i<3;i++)
		pthread_join(tid[i],NULL);
	printf("\n");
	return 0;
}

void *func(void *argc)
{
	int i;
	for(i=0;i<10;i++)
	{
		pthread_mutex_lock(&mutex);
	    while(num!=(int)argc)
			pthread_cond_wait(&cond,&mutex);
		printf("%c",num+'A');
		num=(num+1)%3;
		pthread_mutex_unlock(&mutex);
		pthread_cond_broadcast(&cond);
	}
	pthread_exit(0);
}

 

3. 有一int型全局变量g_Flag初始值为0;
在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
线程序1需要在线程2退出后才能退出
主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出

#include<stdio.h>
#include<stdlib.h>
#include<error.h>
#include<pthread.h>
#include<unistd.h>

int flag=0;

void *thread1(void*);
void *thread2(void*);

static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

int main()
{
	printf("enter main\n");
	pthread_t tid1,tid2;
	int ret=0;
	if((ret=pthread_create(&tid1,NULL,thread1,&tid2))!=0)
		printf("create thread1 error!\n");
	if((ret=pthread_create(&tid2,NULL,thread2,NULL))!=0)
		printf("create thread2 error!\n");
	pthread_cond_wait(&cond,&mutex);
	printf("leave main\n");
	return 0;
}

void *thread1(void *argc)
{
	printf("this is thread1\n");
	pthread_mutex_lock(&mutex);
	if(flag==2)
		pthread_cond_signal(&cond);
	flag=1;
	pthread_mutex_unlock(&mutex);
	pthread_join(*(pthread_t*)argc,NULL);
	pthread_exit(0);
}

void *thread2(void *argc)
{
	printf("this is thread2\n");
	pthread_mutex_lock(&mutex);
	if(flag==1)
		pthread_cond_signal(&cond);
	flag=2;
	pthread_mutex_unlock(&mutex);
	pthread_exit(0);
}

 

 

 

题目摘自:

1.http://blog.csdn.net/morewindows/article/details/7392749

2.http://www.cnblogs.com/skynet/archive/2010/10/30/1865267.html

转载于:https://www.cnblogs.com/Rosanna/p/3581835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值