Skip to content

Commit

Permalink
Some cleanup before 1.0 release (#9)
Browse files Browse the repository at this point in the history
* Add buffer length flag, improve crash report

* Remove printf
  • Loading branch information
LorenzoRuffati authored Sep 30, 2022
1 parent d7a5d8d commit 8f36866
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/help_str.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Options:
For the receiver this will be the (non pre-existing) file into which
to copy the contents received from the sender

--length-buff <size in kb>, -l <size in kb>
Use this option with a shared memory sender to specify the size (in kB)
of each chunk that will be copied. The total shared memory usage will be
slightly more than twice the size of the chunk

Role, method and filename can also be passed as naked arguments in this
order

Expand Down
9 changes: 7 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char **argv)
{"file", required_argument, NULL, 'f'},
{"role", required_argument, NULL, 'r'},
{"method", required_argument, NULL, 'm'},
{"max_pages", required_argument, NULL, 'l'},
{"length-buff", required_argument, NULL, 'l'},
{"help", no_argument, NULL, 'h'},
{0, 0, NULL, 0}
};
Expand Down Expand Up @@ -84,7 +84,7 @@ int main(int argc, char **argv)
break;
case 'l':
if (flag_max_pages){
err_and_leave("Too many definitions of max_pages", 1);
err_and_leave("Too many definitions of buffer length", 1);
} else {
flag_max_pages = 1;
}
Expand Down Expand Up @@ -155,6 +155,11 @@ int main(int argc, char **argv)
all_flags &= 0;
}

if ((settings.method==SHARED) && (settings.role == SENDER) && !flag_max_pages){
printf("Setting default buffer size to 2 kb, use the -l flag to set manually\n");
settings.max_pages = 2;
}

if (!all_flags){
printf("Use --help to get more informations on how to use the program\n");
return 3;
Expand Down
11 changes: 5 additions & 6 deletions src/pipe/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define BUFFSZ 256

int use_pipe(setting_t settings){
printf("usp %s\n", settings.password);
//printf("usp %s\n", settings.password);
switch (settings.role) {
case SENDER:
return pipe_sender(settings);
Expand All @@ -25,7 +25,7 @@ int use_pipe(setting_t settings){
}

char* fifo_name(char* passwd){
printf("ffn %s\n", passwd);
//printf("ffn %s\n", passwd);
char* myfifo = "/tmp/myfifo_";
// We have passwd
// We have myfifo
Expand All @@ -39,9 +39,9 @@ char* fifo_name(char* passwd){
}

char* create_fifo(char* passwd){
printf("ffn %s\n", passwd);
//printf("ffn %s\n", passwd);
char* new_string = fifo_name(passwd);
printf("%s\n", new_string);
//printf("%s\n", new_string);
int ret = mkfifo(new_string, 0666);
if (ret == 0){
} else if (ret == -1) {
Expand All @@ -50,8 +50,7 @@ char* create_fifo(char* passwd){
case EEXIST:
break;
default:

printf("%d\n", errno);
printf("%d\n", errno);
err_and_leave("Failed to create pipe", 4);
break;
}
Expand Down
26 changes: 19 additions & 7 deletions src/shm/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ mmap_creat_ret_t access_or_create_coord(char* passwd){
strcat(path, passwd);

int fdm = shm_open(path, O_CREAT | O_EXCL | O_RDWR, 0660);
printf("opn_sync %d %d\n", fdm, errno);
//printf("opn_sync %d %d\n", fdm, errno);
if (fdm == -1){
if (errno == EEXIST){
fdm = shm_open(path, O_RDWR, 0);
coord_struct* str = (coord_struct*) mmap(NULL, sizeof(coord_struct), PROT_READ | PROT_WRITE, MAP_SHARED, fdm, 0);
printf("mmp sync %p %d\n", str, errno);
//printf("mmp sync %p %d\n", str, errno);
return (mmap_creat_ret_t){.mem_region=str, .fd_shared=fdm, .path=path};
} else {
err_and_leave("Error when accessing shared memory", 5);
Expand Down Expand Up @@ -49,17 +49,18 @@ mmap_creat_ret_t init_copy_area(char* passwd, size_t width, size_t* mmap_size){
char *path = path_copy(passwd);
{ // Create file for shared_memory
fdm = shm_open(path, O_CREAT | O_EXCL | O_RDWR, 0660);
printf("opn_cp_shm %d %d\n", fdm, errno);
//printf("opn_cp_shm %d %d\n", fdm, errno);
if (fdm == -1){
err_and_leave("Error when creating file-specific sharedmemory", 5);
}
}
size_t m_sz = sizeof(copy_struct) + (2*width);
//printf("Size of copy area: %ld + 2*%ld = %ld\n", sizeof(copy_struct), width, m_sz);
*mmap_size = m_sz;
ftruncate(fdm, m_sz);
copy_struct* copy_mem = (copy_struct*) mmap(NULL, m_sz, PROT_READ | PROT_WRITE, MAP_SHARED, fdm, 0);

printf("cmm %p %d\n", copy_mem, errno);
//printf("cmm %p %d\n", copy_mem, errno);
init_cond_pack(&(copy_mem->signal_wrtr));


Expand Down Expand Up @@ -103,10 +104,18 @@ int shared_sender(setting_t settings, int lockfd, mmap_creat_ret_t mmap_info){
pthread_mutex_lock(&(coord->lock)); // Prevent any other thread from joining
pthread_mutex_lock(&(coord->writer_ready.lock));
if (coord->writer_ready.v != 0){
char *cpath = path_copy(settings.password);
printf("A writer seems to be already using this memory area, try to use\n"
"a different password. If the previous program crashed manually\n"
"delete the files:\n"
"- /dev/shm%s\n"
"- /dev/shm%s\n", mmap_info.path, cpath);
return 1;
}

mmap_creat_ret_t ret_copy_init = init_copy_area(settings.password, DEFAULT_WIDTH, &(coord->mmap_size));
size_t width = settings.max_pages * 1024;

mmap_creat_ret_t ret_copy_init = init_copy_area(settings.password, width, &(coord->mmap_size));
copy_struct* copy = (copy_struct*) ret_copy_init.mem_region;

// Lock the first active area, now it's ready to accept readers
Expand Down Expand Up @@ -217,7 +226,7 @@ int shared_receiver(setting_t settings, int lockfd, mmap_creat_ret_t mmap_info){
int fdm;
{ // Create file for shared_memory
fdm = shm_open(path, O_RDWR , 0660);
printf("opn_cp_shm %d %d\n", fdm, errno);
//printf("opn_cp_shm %d %d\n", fdm, errno);
if (fdm == -1){
err_and_leave("Error when creating file-specific sharedmemory", 5);
}
Expand All @@ -237,7 +246,7 @@ int shared_receiver(setting_t settings, int lockfd, mmap_creat_ret_t mmap_info){
// Here I'm only holding copy->leaving[1]
FILE* fstr = fopen(settings.filename, "w");
ftruncate(fileno(fstr), 0);
printf("Opened file\n");
//printf("Opened file\n");

int to_read = 1;
int idx = 0;
Expand All @@ -251,6 +260,9 @@ int shared_receiver(setting_t settings, int lockfd, mmap_creat_ret_t mmap_info){
err_and_leave("Error", 5);
}
to_read = (n_wr > 0);

//printf("Read %ld bytes\n", n_wr);

// Lock the leaving lock
pthread_mutex_lock(&(copy->leaving[idx]));
// Release active
Expand Down
2 changes: 0 additions & 2 deletions src/shm/shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "../shared/types.h"
#include "../shared/hash.h"
#include "sync_util.h"
#define NUM_READERS (2)
#define DEFAULT_WIDTH (2048)
#define BASEPATHSHM "/tmp/shm_ocp/"
#define SHMEMBASE "/ocp_sync"

Expand Down

0 comments on commit 8f36866

Please sign in to comment.