feat: lab7 done
This commit is contained in:
15
OSs/lab7/Makefile
Normal file
15
OSs/lab7/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
targets = server client
|
||||
all: $(targets)
|
||||
|
||||
server: server.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
client: client.o
|
||||
$(CC) -o $@ $^ $(CFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(targets)
|
||||
rm -f *.o
|
||||
65
OSs/lab7/client.c
Normal file
65
OSs/lab7/client.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include "common.h"
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FAIL_TO_CLEANUP(cond, jmp_to) \
|
||||
if (cond) { \
|
||||
perror(#cond); \
|
||||
ret = EXIT_FAILURE; \
|
||||
goto jmp_to; \
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ret = 0;
|
||||
int shm_fd = -1;
|
||||
void *shm_ptr = MAP_FAILED;
|
||||
sem_t *sem_c2s = SEM_FAILED;
|
||||
sem_t *sem_s2c = SEM_FAILED;
|
||||
|
||||
FAIL_TO_CLEANUP((shm_fd = shm_open(SHM_NAME, O_RDWR, 0)) == -1, exit);
|
||||
FAIL_TO_CLEANUP(
|
||||
(shm_ptr = mmap(NULL, SHM_SIZE,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, shm_fd, 0)) == MAP_FAILED,
|
||||
unmap_shared_mem);
|
||||
|
||||
FAIL_TO_CLEANUP((sem_c2s = sem_open(SEM_C2S_NAME, 0)) == SEM_FAILED,
|
||||
close_sem_c2s);
|
||||
FAIL_TO_CLEANUP((sem_s2c = sem_open(SEM_S2C_NAME, 0)) == SEM_FAILED,
|
||||
close_sem_s2c);
|
||||
|
||||
char filename[SHM_SIZE];
|
||||
FAIL_TO_CLEANUP(fgets(filename, sizeof(filename), stdin) == NULL, close_sem_s2c);
|
||||
|
||||
size_t ln = strlen(filename);
|
||||
if (ln > 0 && filename[ln - 1] == '\n')
|
||||
filename[ln - 1] = '\0';
|
||||
|
||||
memset(shm_ptr, 0, SHM_SIZE);
|
||||
strncpy((char *)shm_ptr, filename, SHM_SIZE - 1);
|
||||
|
||||
FAIL_TO_CLEANUP(sem_post(sem_c2s) == -1, close_sem_s2c);
|
||||
|
||||
FAIL_TO_CLEANUP(sem_wait(sem_s2c) == -1, close_sem_s2c);
|
||||
|
||||
size_t len = strnlen((char *)shm_ptr, SHM_SIZE);
|
||||
FAIL_TO_CLEANUP(fwrite(shm_ptr, sizeof(char), len, stdout) != len, close_sem_s2c);
|
||||
fflush(stdout); // don't really care if fails
|
||||
|
||||
close_sem_s2c:
|
||||
sem_close(sem_s2c);
|
||||
close_sem_c2s:
|
||||
sem_close(sem_c2s);
|
||||
unmap_shared_mem:
|
||||
munmap(shm_ptr, SHM_SIZE);
|
||||
close_shared_fd:
|
||||
close(shm_fd);
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
12
OSs/lab7/common.h
Normal file
12
OSs/lab7/common.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#define SHM_NAME "/lab7_shm"
|
||||
#define SEM_C2S_NAME "/sem_c2s_lab7"
|
||||
#define SEM_S2C_NAME "/sem_s2c_lab7"
|
||||
#define SHM_SIZE (64 * 1024)
|
||||
|
||||
#define FAIL_ON(cond) if (cond) return EXIT_FAILURE
|
||||
|
||||
#endif /* COMMON_H */
|
||||
|
||||
136
OSs/lab7/server.c
Normal file
136
OSs/lab7/server.c
Normal file
@ -0,0 +1,136 @@
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define FAIL_TO_CLEANUP(cond, jmp_to) \
|
||||
if (cond) { \
|
||||
perror(#cond); \
|
||||
ret = EXIT_FAILURE; \
|
||||
goto jmp_to; \
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int ret;
|
||||
int shm_fd = -1;
|
||||
void *shm_ptr = MAP_FAILED;
|
||||
sem_t *sem_c2s = SEM_FAILED;
|
||||
sem_t *sem_s2c = SEM_FAILED;
|
||||
|
||||
/* Create or open shared memory */
|
||||
FAIL_TO_CLEANUP((shm_fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0600)), cleanup);
|
||||
if (shm_fd == -1) {
|
||||
perror("shm_open");
|
||||
goto cleanup;
|
||||
}
|
||||
if (ftruncate(shm_fd, SHM_SIZE) == -1) {
|
||||
perror("ftruncate");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
shm_ptr = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED, shm_fd, 0);
|
||||
if (shm_ptr == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Create semaphores (initialize to 0) */
|
||||
sem_c2s = sem_open(SEM_C2S_NAME, O_CREAT, 0600, 0);
|
||||
if (sem_c2s == SEM_FAILED) {
|
||||
perror("sem_open c2s");
|
||||
goto cleanup;
|
||||
}
|
||||
sem_s2c = sem_open(SEM_S2C_NAME, O_CREAT, 0600, 0);
|
||||
if (sem_s2c == SEM_FAILED) {
|
||||
perror("sem_open s2c");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
printf("Server: waiting for client...\n");
|
||||
|
||||
while (1) {
|
||||
/* Wait for client to write filename */
|
||||
if (sem_wait(sem_c2s) == -1) {
|
||||
perror("sem_wait c2s");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Read filename from shared memory (ensure NUL) */
|
||||
char filename[SHM_SIZE];
|
||||
memset(filename, 0, sizeof(filename));
|
||||
memcpy(filename, shm_ptr, sizeof(filename) - 1);
|
||||
|
||||
/* If client sent special "EXIT" filename, break loop and cleanup */
|
||||
if (strcmp(filename, "EXIT") == 0) {
|
||||
/* Notify client and exit */
|
||||
const char *msg = "Server shutting down.";
|
||||
size_t len = strlen(msg) + 1;
|
||||
if (len > SHM_SIZE)
|
||||
len = SHM_SIZE;
|
||||
memcpy(shm_ptr, msg, len);
|
||||
sem_post(sem_s2c);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Try open file */
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
char errbuf[SHM_SIZE];
|
||||
snprintf(errbuf, sizeof(errbuf), "Error opening file '%s'\n", filename);
|
||||
size_t len = strlen(errbuf) + 1;
|
||||
if (len > SHM_SIZE)
|
||||
len = SHM_SIZE;
|
||||
memcpy(shm_ptr, errbuf, len);
|
||||
sem_post(sem_s2c);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Read file content and write to shared memory (truncate if too large) */
|
||||
size_t total = 0;
|
||||
while (!feof(f) && total < SHM_SIZE - 1) {
|
||||
size_t toread = SHM_SIZE - 1 - total;
|
||||
size_t r = fread((char *)shm_ptr + total, 1, toread, f);
|
||||
total += r;
|
||||
if (r == 0)
|
||||
break;
|
||||
}
|
||||
((char *)shm_ptr)[total] = '\0'; /* ensure NUL */
|
||||
|
||||
if (!feof(f) && total == SHM_SIZE - 1) {
|
||||
/* truncated */
|
||||
const char *note =
|
||||
"\n[Output truncated: file larger than shared memory]\n";
|
||||
size_t note_len = strlen(note);
|
||||
if (note_len + 1 < SHM_SIZE) {
|
||||
size_t copy_pos = SHM_SIZE - 1 - note_len;
|
||||
memcpy((char *)shm_ptr + copy_pos, note, note_len);
|
||||
((char *)shm_ptr)[SHM_SIZE - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
/* Signal client that data is ready */
|
||||
if (sem_post(sem_s2c) == -1) {
|
||||
perror("sem_post s2c");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup:
|
||||
if (sem_c2s != SEM_FAILED)
|
||||
sem_close(sem_c2s);
|
||||
if (sem_s2c != SEM_FAILED)
|
||||
sem_close(sem_s2c);
|
||||
if (shm_ptr != MAP_FAILED) munmap(shm_ptr, SHM_SIZE); if (shm_fd != -1)
|
||||
close(shm_fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user