×

Loading...

@Vancouver

Topic

  • 工作学习 / IT技术讨论 / Computer Jobs need true skills----see these assignments, please
    本文发表在 rolia.net 枫下论坛Friends,

    Attached is a set of assignments that one of my friends was asked to implement in C++ and Java. Our forum abounds in gurus. Please comment on the following questions. (Frankly speaking, I feel they are very thorny, though I have implemented 3 in Java quickly and 4 elegantly in Java 2D). Thanks.
    ---------------------------------

    >Here are the programming questions that I would like you to do:
    >
    >1. Write a program using shared memory:
    >a. one process writing "CooperNeff"
    >b. the other process reading this in, and print out to the standard
    >output.
    >
    >2. Folking process:
    >printout "CooperNeff" character by character, and each one is done by one
    >single process
    >
    >3. write a program with two threads. One thread is doing n+=1. Every 50
    >times, this thread will
    >push the result onto a second thread.
    >The second thread will print out the result pushed by thread one.
    >
    >4. a simple Java GUI to plot two series, with (x,y1) and (x,y2) variables.
    >(eg, (x,y1) = (1,10), (2,15), (3.3, -10)
    >(x,y2) = (1,-3), (2, 9), (3.3, -8) )
    >
    >5 write a class derived from both iostream and priority queue? This class
    >should be able to handle
    >a. take a couple stock objects into stream. (overloads << operator)
    >b. when flushing, it will print out to standard output in the order of
    >either ticker (a string type) or price (a double type) within the stock
    >object. This choice can be set by the user.
    >The stock objects are:
    >class Stock {
    >string ticker;
    >double price;
    >};
    >with {"IBM", 120.0}, {"INTC", 60}, {"AMAT", 75}更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • These look like my Operating Systems course assignments. What kind of app they are developping?
      • Maybe easy to you.
        My friend is a C++ guy. The guy who gave him these assignments is a Taiwanese. My friend told me that these questions looks easy, but it is by no means some simple exercises. Though it is an open test (for this weekend), C++ experts can judge your level from your code.

        I am Java-oriented, so I don't sell my C++ story. But I have some comments on 3 and 4. The best solution for 3 is to use java.io.PipedInputStream and java.io.PipedOutputStream ... But people can also create the wheels themselves. The simplest solution for 4 is to use java.awt.Graphics to draw lines. If you can use Java 2D to implement it, you may give people a deeper impression. Even for these simple examples, people can judge how good you are at programming from the following aspects: Is the scope of variables in your code reasonable? If your code flexible? .....
    • let me answer question 1/2/3
      本文发表在 rolia.net 枫下论坛Q1:
      there 3 cpp file: for sharemem.dll
      //sharemem.cpp
      #include "windows.h"
      #define BUFSIZE 256
      #pragma data_seg(".shared")
      volatile char internal_buffer[BUFSIZE]="";
      volatile int status = 0;
      #pragma data_seg()
      #pragma comment(linker, "/SECTION:.shared,RWS")

      __declspec(dllexport) bool writestr(char* str)
      {
      status=0;
      if(strlen(str)>BUFSIZE)
      return false;
      strcpy((char*)internal_buffer,str);
      status=1;
      return true;
      }


      __declspec(dllexport) bool readstr(char* buf,unsigned len)
      {
      while(status<1)
      Sleep(10);
      if(strlen((const char*)internal_buffer)>=len)
      return false;
      strcpy(buf,(const char*)internal_buffer);
      return true;
      }

      //p1.cpp for writer

      #pragma comment(lib,"../sharemem")
      __declspec(dllimport) bool writestr(char* str);
      void main()
      {
      writestr("CooperNeff");
      }

      //p2.cpp for reader
      #pragma comment(lib,"../sharemem")
      #define BUFFERSIZE 256
      __declspec(dllexport) bool readstr(char* buf,unsigned len);
      #include "stdio.h"
      void main()
      {
      char buf[BUFFERSIZE];

      readstr(buf,BUFFERSIZE);
      printf(buf);
      }


      Q2:
      same as Q1,using share memory, each process wait the status
      //processn.cpp
      while(status!=n)
      {
      Sleep(10);
      }
      printf(myChar); //myChar is the nth in the string
      status++;

      Q3:
      #include "stdio.h"
      #include "windows.h"
      #define QUEUESIZE 100
      struct ValueQueue
      {
      int current;
      int counter;
      int value[QUEUESIZE];
      CRITICAL_SECTION section;
      int getvalue()
      {
      int result;
      while(counter==0)
      {
      Sleep(10);
      }
      EnterCriticalSection(&section);
      result=value[current];
      current++;
      current%=QUEUESIZE;
      counter--;
      LeaveCriticalSection(&section);
      return result;
      }
      void putvalue(int i)
      {
      while(counter>=QUEUESIZE)
      {
      Sleep(10);
      }
      EnterCriticalSection(&section);
      value[(current+counter)%QUEUESIZE]=i;
      counter++;
      LeaveCriticalSection(&section);
      }
      ValueQueue()
      {
      current=counter=0;
      InitializeCriticalSection(&section);
      }
      ~ValueQueue()
      {
      DeleteCriticalSection(&section);
      }

      };

      DWORD WINAPI Thread1(LPVOID p)
      {
      ValueQueue* q=(ValueQueue*)p;
      int n=0;
      while(1)
      {
      n++;
      if(!(n%50))
      q->putvalue(n);

      }
      return 0;
      }
      DWORD WINAPI Thread2(LPVOID p)
      {
      ValueQueue* q=(ValueQueue*)p;
      int n=0;
      while(1)
      {
      int i=q->getvalue();
      printf("%d\n",i);

      }
      return 0;
      }

      void main()
      {
      DWORD dummy;
      HANDLE thread[2];
      ValueQueue q;
      thread[0]=CreateThread(0,1024*8,Thread2,(void*)&q,0,&dummy);
      thread[1]=CreateThread(0,1024*8,Thread1,(void*)&q,0,&dummy);

      while(1);
      // WaitForMultipleObjects(2,thread,true,0);
      CloseHandle(thread[0]);
      CloseHandle(thread[1]);
      }
      I don't know why the WaitForMultipleObjects don't work, so I had to put a while(1); there. It's a shame.更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Quick hand! The last parameter of WaitForMultipleObjects() is set to zero. It causes WaitForMultipleObjects() to return immediately. Just a dicussion.
        • HH, how long no see.
          • Jabber, I am a reader of most of your postings.
            • I always remember your helped me with my Javascript coding...Now I feel much better about Javascript.
              • Rien. (learned from you)
                • De rien. Hahaha.... You made a mistake again.
                  • De......
        • oh, u r right, I misstaken it as infinate.
          • it's better to replace sleep() by singlelock. for putvalue: inputlock[in].lock;value[in] = i;ouputlock[in].unlock;in=(in+1)%QUEUESIZE. for getvalue: outputlock[out].lock;result = value[out];inputlock[out].unlock;out=(out+1)%QUEUESIZE.
          • I think the type of "n" in ...
            I think the type of "n" in your Thread1 should be unsigned or add some codes to limit its increasement (n++) in case it becomes negative,
            and the shared memory should be synchronized. I am not very sure if it is no problem in practice of this specific implementation to access the shared memory without synchronization.
            • in case there is only one reader and one write, it's safe.
              • On my understanding of this matter, the reason for “safety” in this specific example could not be that there are only one reader and one writer. The reason could be that the statement which access “status”,
                e.g. “status=0;” or “status=1;”, is usually, if not always, compiled into a machine code instruction which is “atomic” when the machine's bits >= 8 * sizeof(int).
      • Thank you so much. I will study your code. I post my Java solution to 3 as my complements. Comments are welcome.
        本文发表在 rolia.net 枫下论坛import java.util.*;
        import java.io.*;

        public class ThreadDemo {
        PipedOutputStream pos = null;
        PipedInputStream pis = null;
        DataInputStream dis = null;
        DataOutputStream dos =null;
        boolean shouldRun=true;

        //use the so-called inner class mechanism
        Thread t1= new Thread(){
        int n=0;
        public void run() {
        while(shouldRun){
        n++;
        if(n%50 ==0) {
        try{
        dos.writeInt(n);
        } catch(IOException e) {};
        }
        Thread.yield();
        }
        }
        };

        //use the so-called inner class mechanism
        Thread t2= new Thread(){
        public void run() {
        while(shouldRun){
        try{
        System.out.println(dis.readInt());
        } catch(IOException e) {};
        Thread.yield();
        }
        }
        };

        public void stop() {
        shouldRun=false;
        }

        public ThreadDemo() throws IOException{
        //Piping IO Streams between two Threads
        pos = new PipedOutputStream();
        pis = new PipedInputStream(pos);
        dis = new DataInputStream(pis);
        dos = new DataOutputStream(pos);
        t1.start();
        t2.start();
        }

        public static void main(String[] args) throws Exception {
        ThreadDemo demo = new ThreadDemo();
        }


        }更多精彩文章及讨论,请光临枫下论坛 rolia.net