initial
This commit is contained in:
commit
5f423d24e2
14
NumberServer/Makefile
Normal file
14
NumberServer/Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
CC=gcc
|
||||
CFLAGS=-std=gnu99
|
||||
LDFLAGS=-lpthread
|
||||
|
||||
lfs: lfs.o
|
||||
$(CC) -o $@ $(LDFLAGS) $^
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm -f *.o lfs
|
||||
|
123
NumberServer/lfs.c
Normal file
123
NumberServer/lfs.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
|
||||
pthread_mutex_t lock;
|
||||
volatile uint64_t value;
|
||||
|
||||
static void *linearFeedbackShiftRegisterHandler(void *v) {
|
||||
static uint64_t reg = 0x1;
|
||||
|
||||
while(1) {
|
||||
pthread_mutex_lock(&lock);
|
||||
value = reg;
|
||||
pthread_mutex_unlock(&lock);
|
||||
|
||||
uint8_t z = ((reg >> 64-1) & 1) ^ ((reg >> 63-1) & 1) ^
|
||||
((reg >> 61-1) & 1) ^ ((reg >> 60-1) & 1);
|
||||
reg <<= 1;
|
||||
reg |= z;
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void *readerHandler(void *v) {
|
||||
while(1) {
|
||||
uint64_t reg;
|
||||
|
||||
pthread_mutex_lock(&lock);
|
||||
reg = value;
|
||||
pthread_mutex_unlock(&lock);
|
||||
|
||||
printf("%" PRIx64 "\n", reg);
|
||||
|
||||
sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
static void *listenerHandler(void *v) {
|
||||
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverSocket == -1) {
|
||||
printf("Unable to create socket: %d\n", errno);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
struct sockaddr_in serverAddress;
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddress.sin_port = htons(15000);
|
||||
if (-1 == bind(serverSocket,
|
||||
(struct sockaddr*) &serverAddress,
|
||||
sizeof(serverAddress))) {
|
||||
printf("Unable to bind: %d\n", errno);
|
||||
}
|
||||
if (-1 == listen(serverSocket, 5)) {
|
||||
printf("Unable to listen: %d\n", errno);
|
||||
exit(4);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
struct sockaddr_in *clientAddr;
|
||||
socklen_t clientAddrLen;
|
||||
int clientSocket = accept(serverSocket,
|
||||
(struct sockaddr *) clientAddr,
|
||||
&clientAddrLen);
|
||||
if (clientSocket == -1) {
|
||||
printf("Failure when accepting: %d\n", errno);
|
||||
} else {
|
||||
uint64_t reg;
|
||||
pthread_mutex_lock(&lock);
|
||||
reg = value;
|
||||
pthread_mutex_unlock(&lock);
|
||||
char buf[128];
|
||||
sprintf(buf, "%" PRIx64 "\n", reg);
|
||||
write(clientSocket, buf, strlen(buf)+1);
|
||||
}
|
||||
close(clientSocket);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
pthread_mutex_init(&lock, NULL);
|
||||
|
||||
pthread_t linearFeedbackShiftRegister;
|
||||
int rc = pthread_create(&linearFeedbackShiftRegister, NULL,
|
||||
&linearFeedbackShiftRegisterHandler, NULL);
|
||||
if (rc != 0) {
|
||||
printf("Unable to start shifter thread\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pthread_t reader;
|
||||
rc = pthread_create(&reader, NULL,
|
||||
&readerHandler, NULL);
|
||||
if (rc != 0) {
|
||||
printf("Unable to start reader thread\n");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
pthread_t listener;
|
||||
rc = pthread_create(&listener, NULL,
|
||||
&listenerHandler, NULL);
|
||||
if (rc != 0) {
|
||||
printf("Unable to start listener thread\n");
|
||||
exit(6);
|
||||
}
|
||||
|
||||
pthread_join(linearFeedbackShiftRegister, NULL);
|
||||
pthread_join(reader, NULL);
|
||||
pthread_join(listener, NULL);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user