COMP 3000 2026F Carleton University Assignment 3 Solutions ---------------------- 1. [2] The 3000cp programs copy a file from source to destination. The current versions overwrite the destination file if it already exists. How could you make 3000cp-fifo generate an error if the destination already exists? A: All we need to do is replace to call to creat() on line 79 in write_file() with a call to open() that specifies O_EXCL. creat() is just a call to open() but with the flags O_CREAT|O_WRONLY|O_TRUNC, and instead of the O_TRUNC we want O_EXCL, so that instead of zeroing out the file if it already exists it generates an error. The new line would be: out_fd = open(fn, O_WRONLY|O_CREAT|O_EXCL, 0666); (1 pt for using open, 1 pt for using the right flags) 2. [2] On lines 128 and 131, 3000cp-fifo calls close. Are these calls important? Why? A: Yes they are important. Those calls to close are needed so the pipe terminates when the other side is closed, thus allowing the producer to terminate the consumer and vice versa. Without these closes, when the producer terminates the consumer will keep waiting for input, and when the consumer terminates early the producer will keep waiting for the pipe to be cleared. (1 pt for yes, 1 pt for explanation) 3. [2] How could you change 3000cp-fifo so the destination file has the same permissions as the source file, always? A: This solution only changes the permissions, not the user and group, as that would require the program to run as root. Also note that this solution is a bit more complicated because write_file() doesn't have any information originally about the source file. While we can set the permissions with the open command, those permissions go through the user's umask. So we either need to change umask or we can just separately change the permissions with chmod after getting the permissions with stat. In general you don't want to mess with umask, as that is the user's preference for how permissions are set on new files, so here we're just calling fchmod (so we can use the existing file descriptor). Here we set the permissions before putting any data into the file so the security properties are the same, although many copy commands set the permissions after all the data has been transferred. First, we get the permissions in main() before the fork(), by adding the following declarations to the beginning of main: struct stat statbuf; And then adding the following code to stat the source file (around original line 124, before fork): if (stat(source_fn, &statbuf)) { report_error("Couldn't check the status of the source file"); exit(-1); } statbuf is then passed to write_file() as a new argument (original line 132): write_file(dest_fn, &statbuf, pipefd[0]); We then change the arguments of write_file() to take a new argument: void write_file(char *fn, struct stat *statbufp, int in_fd) And then we extract and use the new mode to change the file permissions after it has been successfully opened (around original line 86): int mode = statbufp->st_mode; /** at top of function **/ if (fchmod(out_fd, mode) != 0) { report_error("Couldn't change permissions on output file\n"); close(in_fd); exit(-1); } (1 pt for stat functionality, 1 pt for fchmod/chmod. If only changed permissions in open/creat, 1 pt only) 4. [4] How could you make 3000cp-fifo, with the -u option, make the destination file's owner and group be the same as the original file, no matter what those were for the original file? How could you make this version work properly if called by any user? A: To solve this, we need to 1) process the -u argument (0.5 pt), 2) get the stat info for the source file in main, (1 pt) 3) pass it to write_file() along with whether -u is set (0.5 pt), 4) have write_file() change the ownership (1 pt). Process -u argument, in main() just before setting source_fn: int preserve_owner = 0; /* at top of main() */ if (argc == 4) { if (strcmp(argv[1], "-u") == 0) { preserve_owner = 1; argv[1] = argv[2]; argv[2] = argv[3]; } else { report_error("incorrect command line"); usage_exit(argv[0]); } } Get the stat buff just before fork() in main(): struct stat statbuf; /* at top of main() */ if (stat(source_fn, &statbuf)) { report_error("Couldn't check the status of the source file"); exit(-1); } Pass stat_buf and preserve_owner to write_file() (near end of main as before): write_file(dest_fn, pipefd[0], &statbuf, preserve_owner); Change write_file() to change the permissions: /* new declaration */ void write_file(char *fn, int in_fd, struct stat *statbufp, int preserve_owner) /* before the while loop in write_file() */ if (preserve_owner) { if (fchown(out_fd, statbufp->st_uid, statbufp->st_gid) != 0) { report_error("Couldn't change ownership"); close(in_fd); exit(-1); } } Note by using fchown() rather than chown(), we can assume that the file already exists and so we don't have to check. To make this version work for any user, you'd need to make it setuid root, i.e., "sudo chown root:root 3000cp-fifo; sudo chmod u+s 3000cp-fifo". In the code then you'd need the following at the start of write_file() so all the resulting files won't end up owned as root: if (!preserve_owner) { seteuid(getuid()); } (1 pt) 5. [4] How could you change 3000cp-fifo so that with the -x option it only copies a file if the destination is a different filesystem than the source? Why does your solution work? How did you test it? A: We can do the same thing as Q4, although if we want to support both -u and -x we'd probably want to just use getopt(3). For simplicity here we'll assume just -x is needed, allowing this solution to be very similar to that of Q4. To solve this, we need to 1) process the -x argument (0.5 pt), 2) get the stat info for the source file in main, (0.5 pt) 3) pass it to write_file() along with whether -x is set (0.5 pt), 4) have write_file() stat the destination file (0.5 pt), 5) check whether the devices for the source and destination are the same (0.5 pt), and 6) unlink the dest file if it is on the same filesystem (0.5 pt). (So 3 points for the code.) Process -x argument, in main() just before setting source_fn: int different_filesystem = 0; /* at top of main() */ if (argc >= 4) { if (strcmp(argv[1], "-x") == 0) { different_filesystem = 1; argv[1] = argv[2]; argv[2] = argv[3]; } else { report_error("incorrect command line"); usage_exit(argv[0]); } } Get the stat of the source file, just before fork(): struct stat statbuf; /* at top of main() */ if (stat(source_fn, &statbuf)) { report_error("Couldn't check the status of the source file"); exit(-1); } Change the call to write_file() so it passes on the new info: write_file(dest_fn, pipefd[0], &statbuf, different_filesystem); Change write_file()'s arguments: void write_file(char *fn, int in_fd, struct stat *source_statbufp, int different_filesystem) Then, after the destination file is open: struct stat dest_statbuf; /* at top of write_file() */ if (different_filesystem) { if (fstat(out_fd, &dest_statbuf)) { report_error("Error on status of the dest file"); exit(-1); } if (source_statbufp->st_dev == dest_statbuf.st_dev) { report_error("Same filesystem with -x, terminating"); close(out_fd); unlink(fn); exit(-1); } } This solution works because stat can tell what device a file is on, and a filesystem is per block device. (0.5 pt) Easiest way to test is to copy a to or from /tmp, as that's a different filesystem on standard Ubuntu 26.04. You could also test by copying to a new loopback filesystem that you created and mounted. (0.5 pt) 6. [2] Does 3000cp-rendezvous work consistently? How did you test it? Note that you can make arbitrarily sized random files using the dd command. For example, dd if=/dev/urandom of=testfile bs=4096 count=4 will produce a file with exactly 16,384 random bytes. A: If 3000cp-rendezvous were to work inconsistently, it would do so on larger files because they involve the most coordination between the producer and consumer. The class VM has over 6G of free storage, so we can make a 3G file and then copy it: dd if=/dev/urandom of=source bs=4096 count=786432 time ./3000cp-rendezvous source dest The first time I ran this, it deadlocked. It might work sometimes but it is not reliable. (Honestly I assumed that with file copying 3000cp-rendezvous would be reliable in practice, but it isn't!) (1 pt for inconsistent, 1 pt for testing) 7. [4] How could you modify 3000cp-rendezvous so it used the same timeout strategy as 3000pc-rendezvous-timeout? Does the modified version behave significantly differently from the original? At minimum, present timing test results. A: To make 3000cp-rendezvous-timeout, we just copy the key changes from 3000pc-rendezvous-timeout. First, we add a declaration for including time.h to the top: #include Then we change the shared struct, replacing pthread_mutex_t cond_mutex; with pthread_mutex_t nonempty_mutex; pthread_mutex_t nonfull_mutex; We then copy the entirety of wait_for_producer() and wait_for_consumer() with the versions from 3000pc-rendezvous-timeout.c We change queue_buffer() to match queue_word() as follows. Add the wait_count variables at the top: int wait_count = 0; int wait_count_max = 2000; /* 2000 1-millisecond waits */ Replace the first "if (e->count != 0)" with the following, making it a while loop waiting for the consumer: while (e->word[0] == '\0') { wait_count++; sem_post(&e->lock); usleep(1); wait_for_consumer(s); sem_wait(&e->lock); if (wait_count >= wait_count_max) { break; } } Delete the existing call to pthread_cont_signal() in the else clause of the second "if (e->count != 0)" if/else statement Add the following before the return retval at the end: pthread_mutex_lock(&s->nonempty_mutex); pthread_cond_signal(&s->queue_nonempty); pthread_mutex_unlock(&s->nonempty_mutex); We do the same to get_next_buffer() taking code from get_next_word() (which is essentially the same code, as we're making sure they don't interfere with each other). We change init_shared, deleting the existing calls to pthread_() at the start and replacwing them with: pthread_mutexattr_t mattr; pthread_condattr_t cattr; pthread_mutexattr_init(&mattr); pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK_NP); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(&s->nonempty_mutex, &mattr); pthread_mutex_init(&s->nonfull_mutex, &mattr); pthread_condattr_init(&cattr); pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED); pthread_cond_init(&s->queue_nonempty, &cattr); pthread_cond_init(&s->queue_nonfull, &cattr); And that's it! The modified version reliably completes (in around 40s on the class VMs for me) while the original doesn't. Note that 3000cp-rendezvous-timeout takes about 40s to finish while the fifo version takes around 20s. The producer finishes way before the consumer in the rendezvous-timeout version, while with the fifo version they finish around the same time. (1 pt for copying wait_for_producer() and wait_for_consumer() 1 pt for changes to queue_buffer()/get_next_buffer() 1 pt init_shared() changes 1 pt for testing that shows the new version finishes while the old one didn't.)