免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1694 | 回复: 1
打印 上一主题 下一主题

[Linux] 一道多线程编程题初解 [复制链接]

论坛徽章:
1
白羊座
日期:2013-08-22 17:30:33
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-30 17:22 |只看该作者 |倒序浏览
本帖最后由 cjdao 于 2013-05-30 17:23 编辑

请各位指点指点

/*
* 作者: cjdao@163.com
* 日期:2013-05-29
* 描述:
* 有四个线程1、2、3、4。线程1的功能就是输出A,线程2的功能就是输出B,以此类推.........
* 现在有四个文件file1, file2,file3, file4。初始都为空。现要让四个文件呈如下格式:
* file1:A B C D A B....
* file2:B C D A B C....
* file3:C D A B C D....
* file4:D A B C D A....
*/

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

pthread_t threads[4];
char writer_char[4] = {'A','B', 'C', 'D'};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

struct file_res{
        pthread_t *writer; /*当前文件可以被哪个线程写入*/
        int fd;            /*文件描述符*/
}file_res[4] =
{
        {.writer=&threads[0]},/*file1初始化可以被线程1写入'A'*/
        {.writer=&threads[1]},/*file2初始化可以被线程1写入'B'*/
        {.writer=&threads[2]},/*file3初始化可以被线程1写入'C'*/
        {.writer=&threads[3]},/*file4初始化可以被线程1写入'D'*/
};

void *writer_routine(void *arg)
{
        int index = (int)arg;
        int file_index[4] ;
        int can_write_file_cnt = 0;
        int i = 0;
        int next_index=0;
        printf("thread %d is running, and will write '%c' to files\n", index, writer_char[index]);
       
        while(1)
        {
                if (0!=pthread_mutex_lock(&mutex))
                        exit(-1);
                for( ;; ) {
                    /*下面的for语句查询本线程可以写的文件id*/
                        for(i=0; i<(sizeof(file_res)/sizeof(file_res[0])); i++) {
                                if (&threads[index]==file_res.writer) {
                                        file_index[can_write_file_cnt++] = i;
                                        next_index = index+1;
                                        if (next_index>3)next_index=0;
                                        file_res.writer = &threads[next_index];
                                }
                        }
                        /*找到本线程可以写的文件id,则退出for( ;; ), 执行写操作*/
                        if (can_write_file_cnt != 0)
                                break;
                        /*没有可以写的文件, 等待其他线程唤醒*/
                        pthread_cond_wait(&cond,&mutex);
                }               
               
                /*针对本线程可以写的文件,执行写操作*/
                for(i=0; i<can_write_file_cnt; i++) {
                        write(file_res[file_index].fd, &writer_char[index], sizeof(writer_char[index]));
                }
                can_write_file_cnt = 0;
               
                /*唤醒下一批线程*/
                pthread_cond_broadcast(&cond);
                if (0!=pthread_mutex_unlock(&mutex))
                        exit(-1);       
        }
}

int main(int argc, char* argv[])
{
        int i;
       
        for(i=0; i<4; i++)
        {
                char file_name[] = "filex";
                sprintf(file_name, "file%d", i+1);
                if ((file_res.fd = open(file_name, O_RDWR|O_CREAT|O_TRUNC, 0666))<0)
                {
                        printf("open %s error.\n", file_name);
                        exit(-1);
                }
        }
       
        for (i=0; i<(sizeof(threads)/sizeof(pthread_t)); i++)
        {
                if(pthread_create(&threads, NULL, writer_routine, (void *)i))
                {
                        printf("create writer thread error\n");
                        exit(-1);
                }
        }
        pthread_exit(NULL);
}

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
2 [报告]
发表于 2013-05-30 19:55 |只看该作者
初解??????
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP