Skip to content
Commits on Source (2)
......@@ -6,7 +6,7 @@ FIND_SRC = ./scripts/find_src.sh
OSS_TARGET = oss
USER_TARGET = user
OSS_REDQUIRED = oss.c
OSS_REDQUIRED = oss.c new_process.c shared_memory.c logical_clock.c
USER_REQUIRED = user.c
......@@ -54,7 +54,7 @@ debug:
.PHONY: help
help:
@echo "makefile for project 4"
@echo "makefile for project 5"
@echo "make all - builds the project"
@echo "make rebuild - cleans and builds the project"
@echo "make clean - removes the build directory"
......
......@@ -3,4 +3,7 @@
#define PATH_TO_USER "./build/user"
#define SHM_CLOCK_ID 0
#define SHM_CLOCK_PATH "./build/oss"
#endif
\ No newline at end of file
#include <stdio.h>
#include "logical_clock.h"
#include "shared_memory.h"
/* create_shared_clock
*
* Returns a pointer to a shared logical clock.
* If the clock does not exist, it is created.
* If an error occurs, prints an error message and returns NULL.
*
* path: the path to the file to use for ftok
* id: the id to use for ftok
*
* returns: a pointer to a shared logical clock
*/
logical_clock_t *create_shared_clock(const char *path, int id)
{
static logical_clock_t *clock = NULL;
if (clock == NULL) {
key_t key = get_key(path, id);
if (key == -1) {
return NULL;
}
int shmid = get_shared_memory(key, sizeof(logical_clock_t), CREATE_FLAG);
if (shmid == -1) {
return NULL;
}
clock = attach_shared_memory(shmid);
}
return clock;
}
\ No newline at end of file
#ifndef LOGICAL_CLOCK_H
#define LOGICAL_CLOCK_H
typedef struct logical_clock_s {
unsigned int seconds;
unsigned int nanoseconds;
} logical_clock_t;
logical_clock_t *creat_shared_clock(const char *path, int id);
#endif
\ No newline at end of file
......@@ -4,6 +4,11 @@
#include <sys/types.h>
#include "new_process/new_process.h"
#include "config.h"
#include "shared_memory/shared_memory.h"
#include "logical_clock/logical_clock.h"
// globals
logical_clock_t *clock;
// function prototypes
int run_user();
......@@ -11,7 +16,8 @@ int run_user();
int main(void)
{
printf("Hello, world from oss!\n");
run_user();
printf("key: %d\n", get_key(SHM_CLOCK_PATH, SHM_CLOCK_ID));
// run_user();
}
int run_user()
......
#include <stdio.h>
#include "shared_memory.h"
const int CREATE_FLAG = 0666 | IPC_CREAT; // use if shared memory does not exist
const int EXIST_FLAG = 0; // use if shared memory already exists
/* get_key
*
* Returns a key_t for use with shared memory.
* If ftok fails, prints an error message and returns -1.
*
* path: the path to the file to use for ftok
* id: the id to use for ftok
*
* returns: a key_t for use with shared memory
*/
key_t get_key(const char *path, int id) {
key_t key = ftok(path, id);
if (key == -1) {
perror("ftok");
return -1;
}
return key;
}
/* get_shared_memory
*
* Returns a shared memory id.
* If shmget fails, prints an error message and returns -1.
*
* key: the key to use for shmget
* size: the size to use for shmget
* flags: the flags to use for shmget
*
* returns: a shared memory id
*/
int get_shared_memory(key_t key, size_t size, int flags) {
int shmid = shmget(key, size, flags);
if (shmid == -1) {
perror("shmget");
return -1;
}
return shmid;
}
/* attach_shared_memory
*
* Returns a pointer to the shared memory.
* If shmat fails, prints an error message and returns NULL.
*
* shmid: the shared memory id to use for shmat
*
* returns: a pointer to the shared memory
*/
void *attach_shared_memory(int shmid) {
void *shmaddr = shmat(shmid, NULL, 0);
if (shmaddr == (void *) -1) {
perror("shmat");
return NULL;
}
return shmaddr;
}
/* detach_shared_memory
*
* Returns 0 if successful.
* If shmdt fails, prints an error message and returns -1.
*
* shmaddr: the shared memory address to use for shmdt
*
* returns: 0 if successful, -1 otherwise
*/
int detach_shared_memory(const void *shmaddr) {
int status = shmdt(shmaddr);
if (status == -1) {
perror("shmdt");
return -1;
}
return 0;
}
/* remove_shared_memory
*
* Returns 0 if successful.
* If shmctl fails, prints an error message and returns -1.
*
* shmid: the shared memory id to use for shmctl
*
* returns: 0 if successful, -1 otherwise
*/
int remove_shared_memory(int shmid) {
int status = shmctl(shmid, IPC_RMID, NULL);
if (status == -1) {
perror("shmctl");
return -1;
}
return 0;
}
\ No newline at end of file
#ifndef SHARED_MEMORY_H
#define SHARED_MEMORY_H
#include <stdio.h>
#include <sys/ipc.h>
extern const int CREATE_FLAG;
extern const int EXIST_FLAG;
key_t get_key(const char *path, int id);
int get_shared_memory(key_t key, size_t size, int flags);
void *attach_shared_memory(int shmid);
int detach_shared_memory(const void *shmaddr);
int remove_shared_memory(int shmid);
#endif
\ No newline at end of file