Ejemplo n.º 1
0
void Producer(int bufSize, int itemCnt, int randSeed)
{
    srand(randSeed);

    // Write code here to produce itemCnt integer values in the range [0-100]
    // Use the functions provided below to get/set the values of shared variables "in" and "out"
    // Use the provided function WriteAtBufIndex() to write into the bounded buffer 	
	// Use the provided function GetRand() to generate a random number in the specified range
    // **Extremely Important: Remember to set the value of any shared variable you change locally
	// Use the following print statement to report the production of an item:
	// printf("Producing Item %d with value %d at Index %d\n", i, val, in);
	// where i is the item number, val is the item value, in is its index in the bounded buffer
    
	int i;
	for(i=0;i < GetItemCnt(); i++){
		while(((GetIn()+1) % GetBufSize())==GetOut())
			;
		int rando = GetRand(0,100);
		WriteAtBufIndex(GetIn(),rando);
		printf("Producing Item %d with value %d at Index %d\n", i+1, rando, GetIn());
		SetIn((GetIn()+1) % GetBufSize());
	}

	printf("Producer Completed\n");
}
Ejemplo n.º 2
0
int main()
{
	const char *name = "OS_HW1"; // Name of shared memory object to be passed to shm_open
	int bufSize; // Bounded buffer size
	int itemCnt; // Number of items to be consumed

	int shm_fd; // Shared Memory File Descriptor

	// Write code here to create a shared memory block and map it to gShmPtr
	// Use the above name
	// **Extremely Important: map the shared memory block for both reading and writing 
	// Use PROT_READ | PROT_WRITE
	shm_fd = shm_open(name, O_RDWR , 0666);
	gShmPtr = mmap(0,SHM_SIZE,PROT_READ | PROT_WRITE,MAP_SHARED,shm_fd,0);

	// Write code here to read the four integers from the header of the shared memory block 
	// These are: bufSize, itemCnt, in, out
	// Just call the functions provided below like this:
	bufSize = GetBufSize();
	itemCnt = GetItemCnt();

	// Write code here to check that the consumer has read the right values: 
	printf("Consumer reading: bufSize = %d\n",bufSize);
	printf("Consumer reading: itemCnt = %d\n",itemCnt);
	printf("Consumer reading: in = %d\n",GetIn());
	printf("Consumer reading: out = %d\n",GetOut());

	// Write code here to consume all the items produced by the producer
	// Use the functions provided below to get/set the values of shared variables in, out, bufSize
	// Use the provided function ReadAtBufIndex() to read from the bounded buffer 	
	// **Extremely Important: Remember to set the value of any shared variable you change locally
	// Use the following print statement to report the consumption of an item:
	// printf("Consuming Item %d with value %d at Index %d\n", i, val, out);
	// where i is the item number, val is the item value, out is its index in the bounded buffer
	int i;
	for(i=0;i < GetItemCnt(); i++){
		while(GetIn() == GetOut())
			;
		printf("Consuming Item %d with value %d at Index %d\n", i+1, ReadAtBufIndex(GetOut()), GetOut()); //i+1 to show human readable count
		SetOut((GetOut()+1) % GetBufSize());
	}


	// remove the shared memory segment 
	if (shm_unlink(name) == -1) {
		printf("Error removing %s\n",name);
		exit(-1);
	}

	return 0;
}
Ejemplo n.º 3
0
bool
MemBuffer::Compare(const SharedMemBufferPtr compTo)
{
    if (compTo->GetBufSize() != GetBufSize()) {
        throw FrmwkEx(HERE, "Compare buffers not same size: %d != %d",
                      compTo->GetBufSize(), GetBufSize());
    }

    if (memcmp(compTo->GetBuffer(), GetBuffer(), GetBufSize()) != 0) {
        LOG_ERR("Detected data miscompare");
        return false;
    }
    return true;
}
Ejemplo n.º 4
0
bool
MemBuffer::Compare(const vector<uint8_t> &compTo)
{
    if (compTo.size() != GetBufSize()) {
        throw FrmwkEx(HERE, "Compare buffers not same size: %d != %d",
                      compTo.size(), GetBufSize());
    }

    vector<uint8_t>::const_iterator iterCompTo = compTo.begin();
    uint8_t *iterThis = GetBuffer();
    for (size_t i = 0; i < GetBufSize(); i++, iterCompTo++, iterThis++) {
        if (*iterCompTo != *iterThis) {
            LOG_ERR("Detected data miscompare @ index = %ld(0x%08lX)", i, i);
            return false;
        }
    }
    return true;
}
Ejemplo n.º 5
0
FX_FLOAT* CPDF_ColorSpace::CreateBuf()
{
    int size = GetBufSize();
    uint8_t* pBuf = FX_Alloc(uint8_t, size);
    return (FX_FLOAT*)pBuf;
}
Ejemplo n.º 6
0
FX_FLOAT* CPDF_ColorSpace::CreateBuf()
{
    int size = GetBufSize();
    FX_BYTE* pBuf = FX_Alloc(FX_BYTE, size);
    return (FX_FLOAT*)pBuf;
}
Ejemplo n.º 7
0
static char *SendRecvCommand(char cid, char *par, int psz) {

CmdDesc *cmdsc;
int i, bi, cm, tmo, ibinc, wexit;
char chr, cks;

   tcflush(amp, TCIOFLUSH);

   for (cm=0; cm<COMMANDS; cm++) {
      cmdsc = &(cmdtb[cm]);
      if (cid == cmdsc->Id) {

	 Pkt.Head = STX;
	 Pkt.Tail = ETX;

	 Pkt.DeviceId[0] = Id[0];
	 Pkt.DeviceId[1] = Id[1];

	 Pkt.CmdId = cid;

	 if (par) Pkt.Payload = par;
	 else     Pkt.Payload = NULL;

	 Pkt.Checksum = CalcCheckSum(PktGeneric,&Pkt,psz);

	 Serialize(&Pkt,psz);
	 OBufSize = GetBufSize(OBuf,OBUF_SIZE);
	 EscapeOBuf();

	 if (OBufSize != write(amp,OBuf,OBufSize)) {
	    fprintf(stderr,"libamp:Error:Cant write to amp\n");
	    perror("write");
	 }
	 //tcflush(amp, TCOFLUSH);
	 tcdrain(amp);

	 if (slow) usleep(slow);

	 tmo = 0; IBufSize = 0; wexit = 0;
	 do {

	    usleep(100000); // It slow give it 100 ms between reads (10^5 us)

	    ibinc = read(amp,&IBuf[IBufSize],(IBUF_SIZE - IBufSize));
	    if (ibinc > 0) {
	       IBufSize += ibinc;
	       wexit++;
	    } else if (wexit) break;

	    if (tmo++ > 20) {
	       fprintf(stderr,"libamp:Error:Amplifier has not responed after 2 seconds\n");
	       perror("read");

	       amp = AmpRecover();
	       if (amp == 0) {
		  fprintf(stderr,"libamp:Error:Can't recover from amplifier error\n");
		  perror("open");
	       }
	       return NULL;
	    }

	 } while ((errno == EAGAIN) && (IBufSize < IBUF_SIZE)); // Wait for data

	 IBufSize = GetBufSize(IBuf,IBufSize);

	 UnEscapeIBuf();

	 bi = 0;

	 Pkt.Head        = IBuf[bi++];
	 Pkt.DeviceId[0] = IBuf[bi++];
	 Pkt.DeviceId[1] = IBuf[bi++];
	 Pkt.CmdId       = IBuf[bi++];
	 Pkt.ModleId[0]  = IBuf[bi++];
	 Pkt.ModleId[1]  = IBuf[bi++];

	 bzero((void *) Payload, PAYLOAD_SIZE);
	 for (i=0; bi<IBufSize-2; bi++) Payload[i++] = IBuf[bi];
	 PayloadSize = i;

	 cks = IBuf[IBufSize-2];
	 chr = IBuf[IBufSize-1];

	 if (i) Pkt.Payload = Payload;
	 else   Pkt.Payload = NULL;

	 Pkt.Checksum = cks;
	 cks = CalcCheckSum(PktReply,&Pkt,i);
	 if (cks != Pkt.Checksum) {
	    printf("Reply:Checksum error\n");
	 }

	 Pkt.Tail = chr;
	 if (chr != ETX) {
	    printf("libamp:Reply:Not terminated by ETX\n");
	 }
	 return Payload;
      }
   }
   return NULL;
}
Ejemplo n.º 8
0
void
MemBuffer::SetDataPattern(DataPattern dataPat, uint64_t initVal,
                          uint32_t offset, uint32_t length)
{
    LOG_NRM("Write data pattern: initial value = 0x%016llX",
            (long long unsigned int)initVal);

    if (mRealBaseAddr == NULL)
        return;

    length = (length == UINT32_MAX) ? GetBufSize() : length;
    if ((length + offset) > GetBufSize())
        throw FrmwkEx(HERE, "Length exceeds total buffer size");

    switch (dataPat)
    {
    case DATAPAT_CONST_8BIT:
    {
        LOG_NRM("Write data pattern: constant 8 bit");
        uint8_t *rawPtr = (uint8_t *)(GetBuffer() + offset);
        for (uint64_t i = 0; i < length; i++)
            *rawPtr++ = (uint8_t)initVal;
    }
    break;

    case DATAPAT_CONST_16BIT:
    {
        LOG_NRM("Write data pattern: constant 16 bit");
        uint16_t *rawPtr = (uint16_t *)(GetBuffer() + offset);
        for (uint64_t i = 0; i < (length / sizeof(uint16_t)); i++)
            *rawPtr++ = (uint16_t)initVal;
    }
    break;

    case DATAPAT_CONST_32BIT:
    {
        LOG_NRM("Write data pattern: constant 32 bit");
        uint32_t *rawPtr = (uint32_t *)(GetBuffer() + offset);
        for (uint64_t i = 0; i < (length / sizeof(uint32_t)); i++)
            *rawPtr++ = (uint32_t)initVal;
    }
    break;

    case DATAPAT_INC_8BIT:
    {
        LOG_NRM("Write data pattern: incrementing 8 bit");
        uint8_t *rawPtr = (uint8_t *)(GetBuffer() + offset);
        for (uint64_t i = 0; i < length; i++)
            *rawPtr++ = (uint8_t)initVal++;
    }
    break;

    case DATAPAT_INC_16BIT:
    {
        LOG_NRM("Write data pattern: incrementing 16 bit");
        uint16_t *rawPtr = (uint16_t *)(GetBuffer() + offset);
        LOG_NRM("Length = %d GetBufSize = %d, offset = %d", length,
                GetBufSize(), offset);
        for (uint64_t i = 0; i < (length / sizeof(uint16_t)); i++)
            *rawPtr++ = (uint16_t)initVal++;
    }
    break;

    case DATAPAT_INC_32BIT:
    {
        LOG_NRM("Write data pattern: incrementing 32 bit");
        uint32_t *rawPtr = (uint32_t *)(GetBuffer() + offset);
        for (uint64_t i = 0; i < (length / sizeof(uint32_t)); i++)
            *rawPtr++ = (uint32_t)initVal++;
    }
    break;

    default:
        throw FrmwkEx(HERE, "Unsupported data pattern %d", dataPat);
    }
}
Ejemplo n.º 9
0
void
MemBuffer::Dump(DumpFilename filename, string fileHdr)
{
    Buffers::Dump(filename, GetBuffer(), 0, ULONG_MAX, GetBufSize(), fileHdr);

}
Ejemplo n.º 10
0
void
MemBuffer::Log(uint32_t bufOffset, unsigned long length)
{
    Buffers::Log(GetBuffer(), bufOffset, length, GetBufSize(), "MemBuffer");
}