All pastes #970954 Raw Edit

Mine

public text v1 · immutable
·published 2008-04-04 12:33 UTC
rendered paste body
/*

** consumer.c -- reads 27 integers from a shared memory segment

** 		     of size equal to a single integer

*/



#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>



int main(int argc, char *argv[])

{

    const int SIGNAL = -4;
    const int ENDSIGNAL = -5;

    key_t key;

    int shmid;

    int* shared_data;

    int data[27];



    /* !!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!

    

    ALWAYS check the return values of shmget, shmat and shmdt

    AND implement appropriate Error Handling.

    For examples see producer.c

    

    */

    

    

     if ((key = ftok("producer.c", 'R')) == -1) {
        perror("ftok");
        exit(1);
     }





    if ((shmid = shmget(key, sizeof(int), 0666 | IPC_CREAT)) == -1) {
        perror("shmget");
        exit(1);
    }



    

    shared_data = (int*)shmat(shmid, (void *)0, 0);
    if (shared_data == (int *)(-1)) {
        perror("shmat");
        exit(1);
    }

    



    printf("Consumer STARTS\n\n");



    // read produced integers into this array

    int readint[27];



    // start reading integers from shared segment

    int i=0;

    for (i=0;i<27;i++){
	while(*shared_data == SIGNAL);

        readint[i] = *shared_data;

	printf("Consumer reads:%d\n", readint[i]);

 	*shared_data = SIGNAL;

    }



    /* detach from the segment: */

         if (shmdt(shared_data) == -1) {
        perror("shmdt");
        exit(1);
    }





    // DO NOT EDIT AFTER THIS LINE

    //

	

    char argstr[82];

    argstr[0] = '\0';

    i=0;

    int charcount=0;

    for (i=0;i<27;i++){

	char strtemp1[3];

	strtemp1[0]=' ';

	strtemp1[1]=' ';

	strtemp1[2]=' ';

	sprintf(strtemp1,"%d",readint[i]);

	if (readint[i]>9 && readint[i]>0){

	    strtemp1[2]=' ';

	}

	else if (readint[i]<=9 && readint[i]>=0){

		strtemp1[1]=' ';

		strtemp1[2]=' ';

	}

	else {

		printf("Value at position %i smaller than '0', something is wrong\n", i);

		exit(1);

	}

	strncat(argstr, strtemp1, 3);

    }



    char finalarg[91];

    strcpy(finalarg,"./tester ");

    strcat(finalarg,argstr);

    system(finalarg);