int send(const char* filename) { int flagid = 0; int master_sender = is_sender(&flagid); if (!master_sender) exit(EXIT_SUCCESS); int shmemid = 0; void* smbuf = get_memptr( FILE_NAME_SHMEM_ATTACH, BUF_SIZE + sizeof(long), &shmemid); CHECK(smbuf != (void*)-1, "Failed to get shared memory pointer"); long* nbytes_to_save = smbuf + BUF_SIZE; *nbytes_to_save = 0; CHECK(smbuf != (void*)-1, "Failed to get memory pointer"); int semid = get_sems( FILE_NAME_SHMEM_ATTACH, 5); CHECK(semid != -1, "Failed to get semaphors id"); int cond = snd_protect_connection(semid); CHECK(cond == 0, "Failed to set up protected connection"); int fileid = open(filename, O_RDONLY); CHECK(fileid != -1, "Failed to open file to send"); struct sembuf rcv_activate[2] = { {RCV_MUTEX, 1, 0}, {RCV_CONNECT, 1, 0} }; cond = semop(semid, rcv_activate, 2); CHECK(cond != -1, "Failed to connect to receiver"); struct sembuf mutexes[3] = { {SND_MUTEX, 1, 0}, {RCV_MUTEX, -1, 0}, {SND_MUTEX, 0, 0} }; int nbytes = 0; while ((nbytes = read(fileid, smbuf, BUF_SIZE)) > 0) { *nbytes_to_save = nbytes; OUT("# Sending\n"); cond = semop(semid, &mutexes[0], 1); cond = semop(semid, &mutexes[1], 1); cond = semop(semid, &mutexes[2], 1); CHECK(cond == 0, "# Failed to set semaphors"); OUT("# Package sent\n"); } OUT("# Finishing...\n"); *nbytes_to_save = -1; cond = semop(semid, &mutexes[0], 1); cond = semop(semid, &mutexes[1], 1); cond = semop(semid, &mutexes[2], 1); OUT("# Finished!\n"); cond = snd_clean(semid, fileid, shmemid, flagid); exit(EXIT_SUCCESS); }
int receive() { int flagid = 0; int master_receiver = is_receiver(&flagid); if (!master_receiver) exit(EXIT_SUCCESS); int semid = get_sems( FILE_NAME_SHMEM_ATTACH, 5); CHECK(semid != -1, "Failed to get semaphors id"); int cond = rcv_protect_connection(semid); CHECK(cond == 0, "Failed to set up protected connection"); int shmemid = 0; void* smbuf = get_memptr( FILE_NAME_SHMEM_ATTACH, BUF_SIZE + sizeof(long), &shmemid); CHECK(smbuf != (void*)-1, "Failed to get shared memory pointer"); struct sembuf activate = {RCV_CONNECT, -1, 0}; cond = semop(semid, &activate, 1); OUT("# Activated\n"); struct sembuf mutexes[3] = { {SND_MUTEX, -1, 0}, {RCV_MUTEX, 1, 0}, {RCV_MUTEX, 0, 0} }; long* nbytes_to_save = smbuf + BUF_SIZE; cond = semop(semid, &mutexes[2], 1); CHECK(cond == 0, "Failed to set semaphors"); while (*nbytes_to_save != -1) { OUT1("# Printing %ld bytes\n", *nbytes_to_save); cond = write(STDOUT_FILENO, smbuf, *nbytes_to_save); if (cond != *nbytes_to_save) fprintf(stderr, "Printed = %d\nExpected = %ld\n", cond, *nbytes_to_save); CHECK(cond == *nbytes_to_save, "Printed ammount of bytes isn't valid"); OUT("# Getting...\n"); cond = semop(semid, &mutexes[1], 1); cond = semop(semid, &mutexes[0], 1); OUT1("# Current SND_MUTEX is %d\n", semctl(semid, SND_MUTEX, GETVAL)); OUT1("# Current RCV_MUTEX is %d\n", semctl(semid, RCV_MUTEX, GETVAL)); cond = semop(semid, &mutexes[2], 1); OUT("# Got package\n"); CHECK(cond == 0, "Failed to set semaphors"); }; OUT("# Finishing receiving\n"); semop(semid, &mutexes[0], 1); unlink(RCV_FLAG); semctl(flagid, 0, IPC_RMID); exit(EXIT_SUCCESS); }
int main() { int shmid = 0; void* smbuf = get_memptr("read.c", BUF + sizeof(long), &shmid); long* nbytes_to_save = smbuf + BUF; key_t key = ftok("read.c", 'b'); int sid = semget(key, 4, IPC_CREAT|0660); struct sembuf sem_undo_zero[2] = { {RCV_DIED, 1, 0}, {RCV_DIED, -1, SEM_UNDO}}; semop(sid, &sem_undo_zero[0], 1); // ERROR("ERROR IN RCV_DIED[0]"); semop(sid, &sem_undo_zero[1], 1); // ERROR("ERROR IN RCV_DIED[1]"); struct sembuf unlock_mutex_ifdead[2] = { {SND_MUTEX, 1, SEM_UNDO}, {SND_MUTEX, -1, 0} }; if(semop(sid, unlock_mutex_ifdead, 2) == -1); // ERROR("ERROR IN SND_MUTEX"); //char* segm = shmat(shmid, 0, 0); struct sembuf mutexes[5] = { {SND_MUTEX, -1, 0}, {RCV_MUTEX, 1, 0}, {SND_DIED, 0, IPC_NOWAIT}, {RCV_MUTEX, 0, 0}, {SND_CONNECT,-1, 0}, }; //memset(shm_ptr, BUF, 0); //long* nbytes_to_save = 1; //if(semop(sid, &mutexes[4], 1) == -1); // ERROR("ERROR TO CONNECT WITH SENDER"); semop(sid, &mutexes[2], 2); char str[10] = {}; while (*nbytes_to_save != -1) { if (write(STDOUT_FILENO, str, *nbytes_to_save) != *nbytes_to_save) fprintf(stderr, "%ld\n", *nbytes_to_save); semop(sid, &mutexes[0], 2); if (semop(sid, &mutexes[2], 2) == -1) { printf("Sender dead\n"); exit(1); } }; semop(sid, &mutexes[0], 1); }