none
pthread + C/C++ 谷歌笔试题 RRS feed

  • 问题

  • 题目:

    ---------------------------------------------------------------------------

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

    请教高手解题思路!!!

    2010年11月27日 13:34

答案

  • 您好,

    我想知道输出的文件时什么结果,是格式问题还是值有问题,我这边只测试了一些语句没有全部测试。

     

    Regards!

    Jesse

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • 已标记为答案 Daneill 2010年12月22日 11:58
    2010年11月30日 3:52
    版主

全部回复

  • #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<string>
    #include<pthread.h>
    
    #pragma comment(lib,"pthreadVC2.lib")
    
    #define ID_WORKER_1 1
    #define ID_WORKER_2 2
    #define ID_WORKER_3 3
    #define ID_WORKER_4 4
    
    #define MAX_ID 4
    
    using namespace std;
    
    class OutStream;
    
    class Worker{
    public:
    	Worker(int id, char * str):workerID(id){
    		memcpy(buffer, str, 4);
    	}
    	~Worker(){
    		free(buffer);
    	}
    	void doWork(OutStream *outStream);
    	char * getBuffer(){ return buffer; }
    	int getWorkerID(){ return workerID; }
    private:
    	char buffer[3];
    	int workerID;
    };
    
    class OutStream{
    public:
    	OutStream(int id, string filename):filename(filename){
    		workerID = id;
    		pthread_mutex_init(&lock, NULL);
    		outFile = fopen(filename.c_str(),"a");
    		if(outFile == NULL){
    			printf("creating file %s failed!", filename.c_str());
    			exit(1);
    		}
    	}
    	~OutStream(){
    		fclose(outFile);
    		pthread_mutex_unlock(&lock);
    	}
    	FILE * getFilePtr(){
    		return outFile;
    	}
    	string getFilename(){
    		return filename;
    	}
    	bool captureToken(Worker *worker){
    		if(worker->getWorkerID() != this->workerID)
    			return false;
    		if(pthread_mutex_trylock(&lock))
    			return false;
    		return true;
    	}
    	
    	// let next worker to write
    	void releaseToken(){
    		workerID++;
    		if(workerID > ID_WORKER_4)
    			workerID = ID_WORKER_1;
    		pthread_mutex_unlock(&lock);
    	}
    private:
    	FILE * outFile;
    	int workerID;
    	string filename;
    	pthread_mutex_t lock;
    };
    
    void Worker::doWork(OutStream* outStream){
    	int cnt = fprintf(outStream->getFilePtr(),"%s",buffer);
    	printf("Worker %d write to %s\n", this->workerID, (outStream->getFilename()).c_str());
    }
    
    // declare workers
    Worker worker_1(ID_WORKER_1, "1 ");
    Worker worker_2(ID_WORKER_2, "2 ");
    Worker worker_3(ID_WORKER_3, "3 ");
    Worker worker_4(ID_WORKER_4, "4 ");
      
    // declare out streams
    OutStream streamA(ID_WORKER_1, "C:/A.txt");
    OutStream streamB(ID_WORKER_2, "C:/B.txt");
    OutStream streamC(ID_WORKER_3, "C:/C.txt");
    OutStream streamD(ID_WORKER_4, "C:/D.txt");
    
    void* work_thread(void* worker_arg){
    	Worker* worker = (Worker*)worker_arg;
    	pthread_t threadID;
    	threadID = pthread_self();
    	pthread_detach(threadID);
    	while(1){
    		if ( streamA.captureToken(worker) ) {
    			worker->doWork(&streamA);
          streamA.releaseToken();
        }
        
        if ( streamB.captureToken(worker) ) {
    			worker->doWork(&streamB);
          streamB.releaseToken();
        }
        
        if ( streamC.captureToken(worker) ) {
    			worker->doWork(&streamC);
          streamC.releaseToken();
        }
        if ( streamD.captureToken(worker) ) {
    			worker->doWork(&streamD);
          streamD.releaseToken();
        }
    	}
    	return NULL;
    }
    
    int main(int argc, char *argv[]){
    	pthread_t tid;
      pthread_create(&tid, NULL, work_thread, &worker_1);
      pthread_create(&tid, NULL, work_thread, &worker_2);
      pthread_create(&tid, NULL, work_thread, &worker_3);
      pthread_create(&tid, NULL, work_thread, &worker_4);
    	getchar();
    	return 0;
    }
    这是我参照网上给的答案写的,出现的问题是:能正确输出到屏幕上,但是却不能正确输出到文件上,不知道为什么?如何解决??呼唤高手啊!!!
    2010年11月27日 13:40
  • // declare workers
    Worker worker_1(ID_WORKER_1, "1 ");
    Worker worker_2(ID_WORKER_2, "2 ");
    Worker worker_3(ID_WORKER_3, "3 ");
    Worker worker_4(ID_WORKER_4, "4 ");

    改成

    // declare workers
    Worker worker_1(ID_WORKER_1, "1");
    Worker worker_2(ID_WORKER_2, "2");
    Worker worker_3(ID_WORKER_3, "3");
    Worker worker_4(ID_WORKER_4, "4");//把空格去掉就行了,不知道为什么?

     

    2010年11月28日 12:05
  • 您好,

    我想知道输出的文件时什么结果,是格式问题还是值有问题,我这边只测试了一些语句没有全部测试。

     

    Regards!

    Jesse

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • 已标记为答案 Daneill 2010年12月22日 11:58
    2010年11月30日 3:52
    版主
  • 正确的输出结果是这样的

    A.txt的结果是这样的:  1 2 3 4 1 2....

    B.txt的结果是这样的:  2 3 4 1 2 3....

    C.txt的结果是这样的:  3 4 1 2 3 4....

    D.txt的结果是这样的:  4 1 2 3 4 1....

     

    但是二楼那个程序的结果是这四个文件空白,但输出到屏幕上的东西是正确的,输出到屏幕上的东西就是输出到文件上的,不知道为什么文件就是写不进去

     

    非常谢谢Jesse Jiang!

    2010年12月11日 13:36