cjdao 发表于 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;
char writer_char = {'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 =
{
        {.writer=&threads},/*file1初始化可以被线程1写入'A'*/
        {.writer=&threads},/*file2初始化可以被线程1写入'B'*/
        {.writer=&threads},/*file3初始化可以被线程1写入'C'*/
        {.writer=&threads},/*file4初始化可以被线程1写入'D'*/
};

void *writer_routine(void *arg)
{
        int index = (int)arg;
        int file_index ;
        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);
       
        while(1)
        {
                if (0!=pthread_mutex_lock(&mutex))
                        exit(-1);
                for( ;; ) {
                  /*下面的for语句查询本线程可以写的文件id*/
                        for(i=0; i<(sizeof(file_res)/sizeof(file_res)); i++) {
                                if (&threads==file_res.writer) {
                                        file_index = i;
                                        next_index = index+1;
                                        if (next_index>3)next_index=0;
                                        file_res.writer = &threads;
                                }
                        }
                        /*找到本线程可以写的文件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].fd, &writer_char, sizeof(writer_char));
                }
                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);
}

井蛙夏虫 发表于 2013-05-30 19:55

初解??????
页: [1]
查看完整版本: 一道多线程编程题初解