多线程编程题

举例一:子线程循环 3 次,接着主线程循环 6 次,接着又回到子线程循环 3 次,接着再回到主线程又循环次,如此循环50次,试写出代码。

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

int flag = 0;
int count = 0;

void *function(void *arg)
{
    while (1)
    {
        if (flag == 0)
        {
            for (int i = 0; i < 3; i++)
            {
                printf("pthread circ 3 time: %d\n",++count);

                if (count == 50)
                {
                    goto sign;
                }
            }

            flag = 1;
        }

        if(count == 50)
        {
            sign:
            break;    
        }
        
    }

    return NULL;
}

int main()
{
    pthread_t pid;
    int ret;
    void *return_val = NULL;

    if ((ret = pthread_create(&pid, NULL, (void *)function, NULL)) != 0)
    {
        perror("pthread create error!");
        exit(EXIT_FAILURE);
    }

    while (1)
    {
        sleep(1);
        if (flag == 1)
        {
            for (int i = 0; i < 6; i++)
            {
                printf("main circ 6 time: %d\n",++count);

                if (count == 50)
                {
                    goto sign;
                }
            }

            flag = 0;
        }

        if (count == 50)
        {
            sign:
            break;
        }
        
              
    }
    
    pthread_join(pid, &return_val);
    

    return 0;
}

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

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


pthread_mutex_t lock;
pthread_cond_t A;
pthread_cond_t B;
pthread_cond_t C;

int flag = 1;

void *function1(void *arg)
{
    
    
    for (int i = 0; i < 10; i++)
    {
        pthread_mutex_lock(&lock);
        if (flag != 1)
        {
            pthread_cond_wait(&A, &lock);
        }

        //sleep(1);
        printf("pthread1 id is: %ld\n", pthread_self());

        flag = 2;
        pthread_cond_signal(&B);
        pthread_mutex_unlock(&lock);
    }
    

    return NULL;
}

void *function2(void *arg)
{
    

    for (int i = 0; i < 10; i++)
    {
        pthread_mutex_lock(&lock);
        if(flag != 2)
        {
            pthread_cond_wait(&B, &lock);
        }

        //sleep(1);
        printf("pthread2 id is: %ld\n", pthread_self());
        
        flag = 3;
        pthread_cond_signal(&C);
        pthread_mutex_unlock(&lock);
    }
    

    return NULL;
}

void *function3(void *arg)
{
    
    for (int i = 0; i < 10; i++)
    {
        pthread_mutex_lock(&lock);
        if (flag != 3)
        {
            pthread_cond_wait(&C, &lock);
        }
        
        //sleep(1);
        printf("pthread3 id is: %ld\n", pthread_self());

        flag = 1;
        pthread_cond_signal(&A);
        pthread_mutex_unlock(&lock);
    }   
   
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t pid1;
    pthread_t pid2;
    pthread_t pid3;
    int ret;
    void *return_val;

    pthread_mutex_init(&lock, NULL);

    pthread_cond_init(&A, NULL);
    pthread_cond_init(&B, NULL);
    pthread_cond_init(&C, NULL);


    if ((ret = pthread_create(&pid1, NULL, (void *)function1, NULL)) != 0)
    {
        perror("pthread create error!");
        exit(EXIT_FAILURE);
    }
    pthread_create(&pid2, NULL, (void *)function2, NULL);
    pthread_create(&pid3, NULL, (void *)function3, NULL);

    
    if ((ret = pthread_join(pid1, &return_val)) != 0)
    {
        perror("pthread join error!");
        exit(EXIT_FAILURE);        
    }
    pthread_join(pid2, &return_val);
    pthread_join(pid3, &return_val);

    pthread_mutex_destroy(&lock);

    pthread_cond_destroy(&A);
    pthread_cond_destroy(&B);
    pthread_cond_destroy(&C);
    
    return 0;
}

举例三:

有四个线程1234。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:

A1 2 3 4 1 2....

B2 3 4 1 2 3....

C3 4 1 2 3 4....

D4 1 2 3 4 1....

请设计程序。

将代码中 全局变量 改为 1 和 文件名改为:A.txt ;运行程序得到:A .txt  (A1 2 3 4 1 2....) 

将代码中 全局变量 改为 2 和 文件名改为:B.txt ;运行程序得到:B .txt  (B2 3 4 1 2 3....)

以此类推:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
 
pthread_mutex_t lock;
pthread_cond_t A;
pthread_cond_t B;
pthread_cond_t C;
pthread_cond_t D;
int fd;
 
int n = 4;
 
void *thread_func1(void *arg)
{
    char k;
    for (int i = 0; i < 3; i++)
    {
        pthread_mutex_lock(&lock);
	    while(n != 1)
	    {
		    pthread_cond_wait(&D,&lock);
	    }
   
	    printf("%d ", n);

        fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
        lseek(fd, 0, SEEK_END);
        k = n + '0';
        write(fd, &k, sizeof(k));
        close(fd);

        pthread_cond_signal(&A);
	    pthread_mutex_unlock(&lock);
        n = 2;
    }
  

    return NULL;	

}

void *thread_func2(void *arg)
{
    char k;
    for (int i = 0; i < 3; i++)
    {
        pthread_mutex_lock(&lock);
	    while(n != 2)
	    {
		    pthread_cond_wait(&A,&lock);
	    }
   
	    printf("%d ", n);

	    fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
        lseek(fd, 0, SEEK_END);
        k = n + '0';
        write(fd, &k, sizeof(k));
        close(fd);

        pthread_cond_signal(&B);
	    pthread_mutex_unlock(&lock);
        n =3;
    }
    
    return NULL;	

}

void *thread_func3(void *arg)
{
    char k;
    for (int i = 0; i < 3; i++)
    {
        pthread_mutex_lock(&lock);
	    while(n != 3)
	    {
		    pthread_cond_wait(&B,&lock);
	    }
   
	    printf("%d ", n);

        fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
        lseek(fd, 0, SEEK_END);
        k = n + '0';
        write(fd, &k, sizeof(k));
	    close(fd);

        pthread_cond_signal(&C);
	    pthread_mutex_unlock(&lock);	
        n = 4;
    }   

    return NULL;	
}

void *thread_func4(void *arg)
{
    char k;
    for (int i = 0; i < 3; i++)
    {
        pthread_mutex_lock(&lock);
	    while(n != 4)
	    {
		    pthread_cond_wait(&C,&lock);
	    }
    
	    printf("%d ", n);

        fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
        lseek(fd, 0, SEEK_END);
        k = n + '0';
        write(fd, &k, sizeof(k));
	    close(fd);

        pthread_cond_signal(&D);
	    pthread_mutex_unlock(&lock);
        n = 1;
    }
  
    return NULL;

}

int main()
{
	pthread_t pid1;
	pthread_t pid2;
	pthread_t pid3;
	pthread_t pid4;

	int ret;
	int i;
 
	pthread_mutex_init(&lock,NULL);
	pthread_cond_init(&A,NULL);
 
	
	ret = pthread_create(&pid1,NULL,thread_func1,NULL);
	if(ret == -1)
	{
	    printf("pthread_create error!\n");
		exit(-1);
	}
	ret = pthread_create(&pid2,NULL,thread_func2,NULL);
	ret = pthread_create(&pid3,NULL,thread_func3,NULL);
	ret = pthread_create(&pid4,NULL,thread_func4,NULL);

	
	ret = pthread_join(pid1,NULL);
	if(ret == -1)
	{
		printf("pthread_join error!\n");
		exit(-1);
	}
    ret = pthread_join(pid2,NULL);
    ret = pthread_join(pid3,NULL);
    ret = pthread_join(pid4,NULL);

    pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&A);
    pthread_cond_destroy(&B);
    pthread_cond_destroy(&C);
    pthread_cond_destroy(&D);

    printf("\n");
	
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值