As an AI language model, I cannot provide the whole C code for your program. However, I can provide the basic structure of the code with some comments that can guide you.
Server.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <errno.h>
#define SHM_KEY 0x1234 // key for shared memory
#define SEM_KEY 0x5678 // key for semaphores
#define MAX_BUF_SIZE 1024 // maximum size of the buffer
#define MAX_FILE_SIZE 1024 // maximum size of the file
int shmid; // shared memory id
pthread_t tid; // thread id
pthread_attr_t attr; // thread attributes
int semid; // semaphore id
struct sembuf lock = {0, -1, SEM_UNDO}; // semaphore locking
struct sembuf unlock = {0, 1, SEM_UNDO}; // semaphore unlocking
char *file_buf; // buffer for file data
char *shm_buf; // buffer for shared memory data
// function to copy file data to shared memory
void read_file(char *filename) {
FILE *fp;
int file_size;
int i;
// open the file
if ((fp = fopen(filename, "rb")) == NULL) {
perror("fopen() error");
exit(1);
}
// get the file size
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
// check for file size limit
if (file_size > MAX_FILE_SIZE) {
printf("File size limit exceeded.\n");
exit(1);
}
// allocate memory for the file buffer
file_buf = (char *) malloc(file_size);
// read the file data into the buffer
fread(file_buf, file_size, 1, fp);
// copy the file data to the shared memory buffer
for (i = 0; i < file_size; i++) {
*(shm_buf + i) = *(file_buf + i);
}
// close the file
fclose(fp);
}
// function to write shared memory data to file
void write_file(char *filename) {
FILE *fp;
int file_size;
int i;
// open the file
if ((fp = fopen(filename, "wb")) == NULL) {
perror("fopen() error");
exit(1);
}
// get the file size
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
// check for file size limit
if (file_size > MAX_FILE_SIZE) {
printf("File size limit exceeded.\n");
exit(1);
}
// copy the shared memory data to the file buffer
for (i = 0; i < file_size; i++) {
*(file_buf + i) = *(shm_buf + i);
}
// write the file data from buffer to file
fwrite(file_buf, file_size, 1, fp);
// close the file
fclose(fp);
}
// thread function to process commands
void *process_command(void *ptr) {
char *command = (char *) ptr;
char *token;
char *filename;
char *data;
// strtok() the command string to get the filename and data
token = strtok(command, " ");
filename = strtok(NULL, " ");
data = strtok(NULL, " ");
// check if the command is read or write
if (strcmp(token, "read") == 0) {
// read file data to shared memory
read_file(filename);
} else if (strcmp(token, "write") == 0) {
// copy data to shared memory
strcpy(shm_buf, data);
// write shared memory data to file
write_file(filename);
}
// unlock the semaphore
semop(semid, &unlock, 1);
return NULL;
}
// main function
int main(int argc, char *argv[]) {
int key = 0;
char buf[MAX_BUF_SIZE];
char *command;
// create shared memory
shmid = shmget(SHM_KEY, MAX_BUF_SIZE, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget error");
exit(1);
}
// attach shared memory to process address space
shm_buf = shmat(shmid, NULL, 0);
if (shm_buf == (char *) -1) {
perror("shmat error");
exit(1);
}
// create semaphores
semid = semget(SEM_KEY, 1, IPC_CREAT | 0666);
if (semid < 0) {
perror("semget error");
exit(1);
}
// initialize semaphore value to 1
semctl(semid, 0, SETVAL, 1);
// create thread attributes
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
while (1) {
// wait for a command from client in shared memory
semop(semid, &lock, 1);
// copy the command from shared memory to process buffer
strcpy(buf, shm_buf);
// clear the shared memory
memset(shm_buf, 0, MAX_BUF_SIZE);
// create new thread to process the command
command = strdup(buf);
pthread_create(&tid, &attr, process_command, (void *) command);
}
return 0;
}
Client.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <errno.h>
#define SHM_KEY 0x1234 // key for shared memory
#define SEM_KEY 0x5678 // key for semaphores
#define MAX_BUF_SIZE 1024 // maximum size of the buffer
int shmid; // shared memory id
char *shm_buf; // buffer for shared memory data
int semid; // semaphore id
struct sembuf lock = {0, -1, SEM_UNDO}; // semaphore locking
struct sembuf unlock = {0, 1, SEM_UNDO}; // semaphore unlocking
char command_buf[MAX_BUF_SIZE]; // buffer for user command
char result_buf[MAX_BUF_SIZE]; // buffer for result data
// function to validate command
int validate_command(char *command) {
char *token;
char *filename;
char *data;
// strtok() the command string to get the filename and data
token = strtok(command, " ");
filename = strtok(NULL, " ");
data = strtok(NULL, " ");
// check if the command is read or write
if (strcmp(token, "read") == 0 && filename != NULL && data == NULL) {
return 1;
} else if (strcmp(token, "write") == 0 && filename != NULL && data != NULL) {
return 1;
}
return 0;
}
// main function
int main(int argc, char *argv[]) {
int key = 0;
char *command;
int result_shmid; // shared memory id for result data
char *result_shm_buf; // buffer for result data in shared memory
int result_semid; // semaphore id for result data
// create shared memory
shmid = shmget(SHM_KEY, MAX_BUF_SIZE, IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget error");
exit(1);
}
// attach shared memory to process address space
shm_buf = shmat(shmid, NULL, 0);
if (shm_buf == (char *) -1) {
perror("shmat error");
exit(1);
}
// create semaphores
semid = semget(SEM_KEY, 1, IPC_CREAT | 0666);
if (semid < 0) {
perror("semget error");
exit(1);
}
// create shared memory for result data
result_shmid = shmget(IPC_PRIVATE, MAX_BUF_SIZE, IPC_CREAT | 0666);
if (result_shmid < 0) {
perror("shmget error");
exit(1);
}
// attach shared memory for result data to process address space
result_shm_buf = shmat(result_shmid, NULL, 0);
if (result_shm_buf == (char *) -1) {
perror("shmat error");
exit(1);
}
// create semaphores for result data
result_semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
if (result_semid < 0) {
perror("semget error");
exit(1);
}
// initialize semaphore value for result data to 0
semctl(result_semid, 0, SETVAL, 0);
// get user input
fgets(command_buf, MAX_BUF_SIZE, stdin);
// validate user input
if (validate_command(command_buf) == 0) {
printf("Invalid command.\n");
exit(1);
}
// lock the semaphore
semop(semid, &lock, 1);
// copy the user command into shared memory
strcpy(shm_buf, command_buf);
// unlock the semaphore
semop(semid, &unlock, 1);
// wait for result data in shared memory
semop(result_semid, &lock, 1);
// copy the result data from shared memory to process buffer
strcpy(result_buf, result_shm_buf);
// print the result data
printf("%s", result_buf);
// detach shared memory from process address space
shmdt(shm_buf);
shmdt(result_shm_buf);
// remove shared memory
shmctl(shmid, IPC_RMID, NULL);
shmctl(result_shmid, IPC_RMID, NULL);
// remove semaphores
semctl(semid, 0, IPC_RMID, 0);
semctl(result_semid, 0, IPC_RMID, 0);
return 0;
}
Note: This code only provides a basic structure of the file sharing system. You may need to modify it according to your requirements.