Commit 53c953a7 authored by Son Pham's avatar Son Pham

Lab 11.4 Completed

parent e0cd25ec
......@@ -22,12 +22,18 @@
# compiler
CC = gcc
CFLAGS = -g -Wall
CFLAGS = -g -Wall -lpthread
EXE = file_stat read_dir fdump
EXE = file_stat read_dir fdump sender receiver
all: $(EXE)
receiver: receiver.c
$(CC) $(CFLAGS) receiver.c -o receiver
sender: sender.c
$(CC) $(CFLAGS) sender.c -o sender
fdump: fdump.c hexdump.o
$(CC) $(CFLAGS) fdump.c hexdump.o -o fdump
......
HellllllllllloHellllllHellllllHelllllllllllo
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* receiver.c
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main(int argc, char* argv[]) {
long offset; // This variable tells where is the file position indicator
char buf[1000];
/* Recreate the channel.txt file (this is optional) by uncomment the two lines
if we don't want the file to be messy
*/
FILE* file;
// file = fopen("channel.txt", "w"); // Create a new file
// fclose(file);
file = fopen("channel.txt", "r");
fseek(file, 0, SEEK_END);
offset = ftell(file); // Store the current offset of the file
/* A binary named semaphore to control what is going on */
sem_unlink("semaphore");
sem_unlink("sem_read");
sem_t* sem_channel = sem_open("semaphore", O_CREAT, S_IRUSR | S_IWUSR, 1);
sem_t* sem_read = sem_open("sem_read", O_CREAT, S_IRUSR | S_IWUSR, 0);
/* While loop to check if new message is received */
long temp_offset;
long num_char;
while(1) {
// Call sem_wait to ask for permission to write
sem_wait(sem_read);
// Call sem_wait to "request access" to the channel file
sem_wait(sem_channel);
// Compare the file position indicator to see if there are new stuffs
fseek(file,0,SEEK_END);
temp_offset = ftell(file);
num_char = temp_offset - offset;
// If current offset is not end of file, print whatever available
if (num_char > 0) {
fseek(file,-num_char,SEEK_END); // Move to current offset
fread(buf, num_char, 1, file);
buf[num_char] = 0; // NULL terminated to limit the read message
printf("receiver [meg arrival]: %s\n", buf);
offset = temp_offset; // Update the new offset
}
// Unlock the semaphores
sem_post(sem_channel);
}
}
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* receiver.c
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main(int argc, char* argv[]) {
long offset; // This variable tells where is the file position indicator
char buf[1000];
/* Recreate the channel.txt file (this is optional) by uncomment the two lines
if we don't want the file to be messy
*/
FILE* file;
file = fopen("channel.txt", "w"); // Create a new file
fclose(file);
file = fopen("channel.txt", "r");
fseek(file, 0, SEEK_END);
offset = ftell(file); // Store the current offset of the file
/* A binary named semaphore to control what is going on */
sem_unlink("semaphore");
sem_unlink("sem_read");
sem_t* sem_channel = sem_open("semaphore", O_CREAT, S_IRUSR | S_IWUSR, 1);
sem_t* sem_read = sem_open("sem_read", O_CREAT, S_IRUSR | S_IWUSR, 0);
/* While loop to check if new message is received */
long temp_offset;
long num_char;
while(1) {
// Call sem_wait to ask for permission to write
sem_wait(sem_read);
// Call sem_wait to "request access" to the channel file
sem_wait(sem_channel);
// Compare the file position indicator to see if there are new stuffs
fseek(file,0,SEEK_END);
temp_offset = ftell(file);
num_char = temp_offset - offset;
// If current offset is not end of file, print whatever available
if (num_char > 0) {
fseek(file,-num_char,SEEK_END); // Move to current offset
fread(buf, num_char, 1, file);
buf[num_char] = 0; // NULL terminated to limit the read message
printf("receiver [meg arrival]: %s\n", buf);
offset = temp_offset; // Update the new offset
}
// Unlock the semaphores
sem_post(sem_channel);
}
}
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* sender.c
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main(int argc, char* argv[]) {
// Check for the number of arguments
if (argc != 2) {
printf("Command: sender [message]\n");
exit(-1);
}
// Open the named semaphore
sem_t* sem_channel = sem_open("semaphore", O_CREAT, S_IRUSR | S_IWUSR, 1);
sem_t* sem_read = sem_open("sem_read", O_CREAT, S_IRUSR | S_IWUSR, 0);
// Call sem_wait to "request access"
sem_wait(sem_channel);
// Append the message to the file
char* message = argv[1];
FILE* file = fopen("channel.txt", "a"); // "a" indicates append mode
fprintf(file, message);
fclose(file);
// Release the lock
sem_post(sem_channel);
sem_post(sem_read); // Allow the receiver to read the content
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment