Member Avatar for rafi1082

hello again
i got an assignment i thought i understand it but the every time i read it i understand it differently. my teacher won't explain the exercise.
here is the exercise
[IMG]http://img42.imageshack.us/img42/8331/37255827.png[/IMG]
he also gave an input output example located here
http://rapidshare.com/files/434579925/sorted.zip
i'v learned using shmat & shmget and using semaphore.
i have a few questions
1)if the size given to me is 8 what should i do? am i suppose to use long long?
2)if the size is given to me is 1 one can i use reguar int?
3)i want to do bubblesort and before the swap to lock the semaphore am i right?
4)i started writing 4 cases how does it look

if ((shmid = shmget(IPC_PRIVATE, st.st_size, 0600 | IPC_CREAT)) < 0) {
		perror("shmget error");
		exit(1);
	}
	if (num_of_bit != 8) {
		if (argv[4] == "signed") {
			if ((share1 = (int*) shmat(shmid, NULL, 0)) == (int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 1;
		} else {
			if ((share2 = (unsigned int*) shmat(shmid, NULL, 0))
					== (unsigned int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 2;
		}
	} else {
		if (argv[4] == "signed") {
			if ((share3 = (long long int*) shmat(shmid, NULL, 0))
					== (long long int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 3;
		} else {
			if ((share4 = (long long unsigned int*) shmat(shmid, NULL, 0))
					== (long long unsigned int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 4;
		}
	}
}

what am i missing?
am just suppose to read the file into the shared memory and then attach a diffrent pointer every time?
tnx allot

Recommended Answers

All 4 Replies

hello again
i got an assignment i thought i understand it but the every time i read it i understand it differently. my teacher won't explain the exercise.
here is the exercise
[IMG]http://img42.imageshack.us/img42/8331/37255827.png[/IMG]
he also gave an input output example located here
http://rapidshare.com/files/434579925/sorted.zip
i'v learned using shmat & shmget and using semaphore.
i have a few questions
1)if the size given to me is 8 what should i do? am i suppose to use long long?
2)if the size is given to me is 1 one can i use reguar int?
3)i want to do bubblesort and before the swap to lock the semaphore am i right?
4)i started writing 4 cases how does it look

if ((shmid = shmget(IPC_PRIVATE, st.st_size, 0600 | IPC_CREAT)) < 0) {
		perror("shmget error");
		exit(1);
	}
	if (num_of_bit != 8) {
		if (argv[4] == "signed") {
			if ((share1 = (int*) shmat(shmid, NULL, 0)) == (int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 1;
		} else {
			if ((share2 = (unsigned int*) shmat(shmid, NULL, 0))
					== (unsigned int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 2;
		}
	} else {
		if (argv[4] == "signed") {
			if ((share3 = (long long int*) shmat(shmid, NULL, 0))
					== (long long int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 3;
		} else {
			if ((share4 = (long long unsigned int*) shmat(shmid, NULL, 0))
					== (long long unsigned int*) -1) {
				perror("shmat error");
				exit(1);
			}
			kind = 4;
		}
	}
}

what am i missing?
am just suppose to read the file into the shared memory and then attach a diffrent pointer every time?
tnx allot

No offense, but your program looks like something the cat drug in. The forum has very limited width, and you're wasting yours with huge indentations. Instead of using tabs (with maybe 8 char's width), use the space bar, and make it just 2 or 3 spaces for each level of indentation. THEN we can start talking turkey much easier about the code.

I would use #defines like:

#define LONG 8
#define INT 4
#define SHORT 2

like that, but be sure the numbers on the end of the line, match the sizeof(long), etc., on your system:

printf("My system has sizes of: Long: %d  Int: %d  short: %d\n", sizeof(long),sizeof(int),sizeof(short));

If long long's are a possibility, add them to the defines, as well.

You need to use strcmp() to compare strings in C. Not this:
if (argv[4] == "signed")


but this:

if(strcmp(argv4, "signed")==0)  //testing for string equality requires string.h
  //etc.

I can't help you with shared memory - it wasn't in my C books. Since you need to make an array for the data, (after the sizeof the data type is determinded), I believe you need to use dynamic memory arrays, with calloc() or malloc(). Include the stdlib.h header file and I can help you with questions on that part of it. You do NOT need to cast the pointer that malloc() or calloc() returns.

Try to keep this assignment as simple as possible. Simple is GOOD. ;)

@Adak

Why are you recommending

#define LONG 8
#define INT 4
#define SHORT 2

instead of the compile time sizeof operator?

Member Avatar for rafi1082

i wrote this for now it's ha a few cooments a i'l be happy to hear comments

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/shm.h>
# include <sys/sem.h>
# include <fcntl.h>
# include <sys/stat.h>
# include <unistd.h>
#define swap(x,y) do \
   { unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
     memcpy(swap_temp,&y,sizeof(x)); \
     memcpy(&y,&x,       sizeof(x)); \
     memcpy(&x,swap_temp,sizeof(x)); \
    } while(0)

void bubblesort(void *point, int num, long int size) {
	union semun semarg;
	int i, j;
	int semid; // semaphore id
	/* semaphore initialization */
		semid = semget(IPC_PRIVATE, 1, 0600);
		semarg.val = 1;
		semctl(semid, 0, SETVAL, semarg);
		sops->sem_num = 0;
		sops->sem_flg = 0;
	struct sembuf sops[1]; // for the semaphore
	//if (num == 1){
	char *shared = (char*) &point;//}
	if (num == 10) {
		unsigned char *shared = (unsigned char*) point;
	}
	if (num == 20) {
		short int *shared = (short int*) point;
	}
	if (num == 4) {
		short unsigned int *shared = (short unsigned int*) point;
	}
	if (num == 40) {
		int *shared = (int*) point;
	}
	if (num == 8) {
		unsigned int *shared = (unsigned int*) point;
	}
	if (num == 80) {
		long long int *shared = (long long int*) point;
	}
	if (num == 16) {
		long long unsigned int *shared = (long long unsigned int*) point;
	}

	for (i = 0; i < size - 1; i++) {
		for (j = 0; j < i; j++) {
			if (shared[j] > shared[j + 1]) {
				sops->sem_op = -1; // set semaphore to lock other processes
				semop(semid, sops, 1); // LOCK the critical section
				swap(shared[j], shared[j + 1]);
				sops->sem_op = 1; // set the semaphore to allow other processes to enter the critical section
				semop(semid, sops, 1); // UNLOCK the critical section
			}
		}
	}
}
union semun {
	int val; /* Value for SETVAL */
	struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
	unsigned short *array; /* Array for GETALL, SETALL */
	struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};

union semun semarg;

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

	char *shared1; // the shared memory will be attached to it
	unsigned char *shared2; // the shared memory will be attached to it
	short int *shared3;// the shared memory will be attached to it
	unsigned short int *shared4;// the shared memory will be attached to it
	int *shared5; // the shared memory will be attached to it
	unsigned int *shared6; // the shared memory will be attached to it
	long long int *shared7, size; // the shared memory will be attached to it
	long long unsigned int *shared8; // the shared memory will be attached to it
	int i, j, fd, proc = atoi(argv[2]), num_of_byte = atoi(argv[3]), shmid,
			type = 2, read_test, fdfork;
	char buffer[4096];

	if (argc < 4) {
		perror("not enough arguments");
		exit(-1);
	}
	if (strcmp(argv[4], "signed") == 0) {
		type = 10;
	}

	fd = open(argv[1], O_RDONLY, 0600);
	if (fd == -1) {
		perror("open");
		exit(-1);
	}

	struct stat st;
	stat(argv[1], &st);
	size = st.st_size / num_of_byte;
	//create IPC memory
	if ((shmid = shmget(IPC_PRIVATE, st.st_size, 0600 | IPC_CREAT)) < 0) {
		perror("shmget error");
		exit(1);
	}
	// attach the shared memory segment to the right shared memory type

	if ((shared1 = shmat(shmid, NULL, 0)) == (char*) -1) {
		perror("shmat error");
		exit(1);
	}
	read_test = read(fd, buffer, sizeof(buffer));
	if (read_test == -1) {
		perror("open");
		exit(-1);
	}
	//coping the file to shared memory
	while (read_test > 0) {
		strcpy(shared1, buffer);
		read_test = read(fd, buffer, sizeof(buffer));
		if (read_test == -1) {
			perror("open");
			exit(-1);
		}
	}

	if ((shmdt(shared1)) == -1) {
		perror("shmdt error");
		exit(1);
	}
	for (i = 0; i < proc; i++) {
		fdfork = fork();
		if (fdfork == -1) {
			perror("fork");
			exit(-1);
		}
		if (fdfork == 0) {//son
			bubblesort(shared1, type * num_of_byte, size_t);
			exit(0);
		}
	}
	//father
	for (i = 1; i < proc; i++) // father waits for all child processes to die
		wait(NULL);
	//the father merges the sorts
	// remove the shared memory segment and check if the remove was successful. if not -> exit(1)
	if ((shmctl(shmid, IPC_RMID, NULL)) == -1) {
		perror("shmctl error");
		exit(1);
	}

	//cleanup
	// detach shared memory segment from 'shared_memory' and check if the detachment was successful. if not -> exit(1)

	// delete the semaphore
	if ((semctl(semid, 0, IPC_RMID, semarg)) == -1) {
		perror("semctl error");
		exit(1);
	}

	return 0;
}

@Adak

Why are you recommending

#define LONG 8
#define INT 4
#define SHORT 2

instead of the compile time sizeof operator?

I wanted him to use the sizeof operator to get these numbers right. THEN put them into defines to simplify his thinking on this assignment.

When I first thought about this, I wrote out some code idea's that used sizeof(), but then I thought "Let's Arch Linux" this bad boy assignment, and simplify/clarify it a bit more.

I'm unsure if it was helpful or not. In general, I like and recommend using the built in operator's for a program, but sometimes a few simple defines are really helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.