i got this exercise and i wrote this code.
[IMG]http://img42.imageshack.us/img42/8331/37255827.png[/IMG]
i have a problem with the bubblesort part becouse it thinkes it's void pointer. i don't think i nedd to write the program 8 times there must be a way
tnx a million

# include <fcntl.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/ipc.h>
# include <sys/stat.h>
# include <sys/shm.h>
# include <sys/sem.h>
# include <sys/wait.h>
# include <sys/types.h>
# include <unistd.h>
# include <string.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)

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[]) {
	void *shared; // the shared memory will be attached to it
	int i, j, semid, size, fd, proc = atoi(argv[2]), num_of_byte =
			atoi(argv[3]), shmid, type = 0, read_test, fdfork;
	struct sembuf sops[1]; // for the semaphore
	/* semaphore initialization */
	semid = semget(IPC_PRIVATE, 1, 0600);
	semarg.val = 1;
	semctl(semid, 0, SETVAL, semarg);
	sops->sem_num = 0;
	sops->sem_flg = 0;

	if (argc < 4) {
		perror("not enough arguments");
		exit(-1);
	}
	if (strcmp(argv[4], "signed") == 0) {
		type = 1;
	}
	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 + 1), 0600 | IPC_CREAT)) < 0) {
		perror("shmget error");
		exit(1);
	}
	// attach the shared memory segment to the right shared memory type

	if ((shared = shmat(shmid, NULL, 0)) == (void*) -1) {
		perror("shmat error");
		exit(1);
	}
	read_test = read(fd, shared, st.st_size);
	if (read_test == -1) {
		perror("open");
		exit(-1);
	}
	switch (num_of_byte) {
	case 8:
		if (strcmp(argv[4], "signed"))
			shared = (long long int*) &shared;
		else
			shared = (unsigned long long int*) &shared;
		break;
	case 4:
		if (strcmp(argv[4], "signed"))
			shared = (int*) &shared;
		else
			shared = (unsigned int*) &shared;
		break;
	case 2:
		if (strcmp(argv[4], "signed"))
			shared = (short int*) &shared;
		else
			shared = (unsigned short int*) &shared;
		break;
	case 1:
		if (strcmp(argv[4], "signed"))
			shared = (char*) shared;
		else
			shared = (unsigned char*) &shared;
		break;
	}

	if ((shmdt(shared)) == -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
			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
					}
				}
			}
			exit(0);
		}
	}
	//father
	for (i = 1; i < proc; i++) // father waits for all child processes to die
		wait(NULL);
	write(1, shared, st.st_size);
	//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);
	}
	return 0;
}

Recommended Answers

All 2 Replies

You already know the size of each element in the form of num_of_byte. First alter your swap macro to accept a byte count which will be passed to memcpy. Then replace your comparison (which is comparing the address of two pointers) with a predicate function that compares values. Look to qsort for an example of how this kind of interface works.

Member Avatar for rafi1082

thanks for your reply
but even if know the number of byte doesn't the signed\unsigned makes it much more complicated?
how will the function be able to compare? it has 8 options.
about the swap i'l try to change it.
if you can help me in anything else...
tnx again

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.