Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Xv6-OS-with-custom-modifications/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
*~
_*
*.o
Expand Down
6 changes: 0 additions & 6 deletions Xv6-OS-with-custom-modifications/.vscode/settings.json

This file was deleted.

10 changes: 10 additions & 0 deletions Xv6-OS-with-custom-modifications/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ void clearpteu(pde_t *pgdir, char *uva);
// kshutdown.c
void shutdown(void);

/// @brief This fuction acquires a sleep lock for the given file.
/// @param fp a valid file pointer
/// @return 0 upon success and -1 upon error
int filelock(struct file* fp);

/// @brief This fuction releases a sleep lock for the given file.
/// @param fp a valid file pointer
/// @return 0 upon success and -1 upon error
int fileunlock(struct file* fp);

// number of elements in fixed-size array
#define NELEM(x) (sizeof(x) / sizeof((x)[0]))

Expand Down
1 change: 1 addition & 0 deletions Xv6-OS-with-custom-modifications/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct inode {
uint32_t dev; // Device number
uint32_t inum; // Inode number
int ref; // Reference count
struct sleeplock flock; // protects everything below hereNDIRECT
struct sleeplock lock; // protects everything below hereNDIRECT
int valid; // inode has been read from disk?

Expand Down
2 changes: 1 addition & 1 deletion Xv6-OS-with-custom-modifications/kkernel/dhello.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static int s_cnputs(char *outbuffer, int n, const char* string)
int
helloread(struct inode *ip, char *dst, int n)
{
return s_cnputs(dst, n, "Hello, World!");
return s_cnputs(dst, n, "Hello, World!\n");
}

int
Expand Down
24 changes: 23 additions & 1 deletion Xv6-OS-with-custom-modifications/kkernel/kfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// File descriptors
//

#include "file.h"
#include "defs.h"
#include "file.h"
#include "fs.h"
#include "param.h"
#include "sleeplock.h"
Expand All @@ -16,6 +16,28 @@ struct {
struct file file[NFILE];
} ftable;

int filelock(struct file *fp) {
int status = 0;
if (fp->ip == 0 || fp->ip->ref < 1) {
status = -1;
} else {
acquiresleep(&fp->ip->flock);
}

return status;
}

int fileunlock(struct file *fp) {
int status = -1;

if (fp->ip != 0 && holdingsleep(&fp->ip->flock) && !(fp->ip->ref < 1)) {
releasesleep(&fp->ip->flock);
status = 0;
}

return status;
}

void fileinit(void) { initlock(&ftable.lock, "ftable"); }

// Allocate a file structure.
Expand Down
3 changes: 3 additions & 0 deletions Xv6-OS-with-custom-modifications/kkernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ extern int sys_write(void);
extern int sys_uptime(void);

extern int sys_shutdown(void);
extern int sys_flock(void);
extern int sys_funlock(void);

static int (*syscalls[])(void) = {
[SYS_fork] = sys_fork, [SYS_exit] = sys_exit,
Expand All @@ -115,6 +117,7 @@ static int (*syscalls[])(void) = {
[SYS_mknod] = sys_mknod, [SYS_unlink] = sys_unlink,
[SYS_link] = sys_link, [SYS_mkdir] = sys_mkdir,
[SYS_close] = sys_close, [SYS_shutdown] = sys_shutdown,
[SYS_flock] = sys_flock, [SYS_funlock] = sys_funlock,
};

void syscall(void) {
Expand Down
21 changes: 21 additions & 0 deletions Xv6-OS-with-custom-modifications/kkernel/sysfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,24 @@ int sys_pipe(void) {
fd[1] = fd1;
return 0;
}


int
sys_flock(void) {
struct file *fp;
if(argfd(0, 0, &fp) >= 0) {
return filelock(fp);
}
cprintf("***** %s\n", "sys_flock FAILED");
return -1;
}

int
sys_funlock(void) {
struct file *fp;
if(argfd(0, 0, &fp) >= 0) {
return fileunlock(fp);
}
cprintf("***** %s\n", "sys_funlock FAILED");
return -1;
}
2 changes: 1 addition & 1 deletion Xv6-OS-with-custom-modifications/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
#define MAXOPBLOCKS 10 // max # of blocks any FS op writes
#define LOGSIZE (MAXOPBLOCKS * 3) // max data blocks in on-disk log
#define NBUF (MAXOPBLOCKS * 3) // size of disk block cache
#define FSSIZE 1000 // size of file system in blocks
#define FSSIZE 2000 // size of file system in blocks

#endif /* A4559DE9_C220_44C9_96A3_C431CF69CB63 */
2 changes: 2 additions & 0 deletions Xv6-OS-with-custom-modifications/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
#define SYS_close 21

#define SYS_shutdown 22
#define SYS_flock 23
#define SYS_funlock 24

#endif /* D403047C_65A6_4017_9DFB_514095C998F7 */
15 changes: 15 additions & 0 deletions Xv6-OS-with-custom-modifications/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ int uptime(void);

int shutdown(void);

/// @brief call this function pasing the file descriptor for an open file in
/// order to acquire a sleep lock associated with the open file. This function
/// blocks (a.k.a. sleeps) until the lock is acquired.
/// @param[in] fd descriptor for a file opened with open()
/// @return zero if lock is acquired and a negative number if any error is detected.
int flock(int fd);

/// @brief call this function pasing the file descriptor for an open file
/// that is currently locked via flock(). This function unlocks the lock
/// associated with the file.
/// @param fd descriptor for a file opened with open() and locked with flock()
/// @return zero if lock is released and a negative number if any error is
/// detected.
int funlock(int fd);

// ulib.c
int stat(const char *, struct stat *);
char *strcpy(char *, const char *);
Expand Down
4 changes: 3 additions & 1 deletion Xv6-OS-with-custom-modifications/user/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ULIB = ../ulib.o ../usys.o ../printf.o ../malloc.o
.PRECIOUS: %.o

UPROGS=\
_countUD\
_cat\
_cp\
_echo\
Expand All @@ -74,7 +75,8 @@ UPROGS=\
_testfs\
_testuser\
_testdevhello\
_ex\
_stuff\
_testflock\

all: $(UPROGS)

Expand Down
18 changes: 18 additions & 0 deletions Xv6-OS-with-custom-modifications/user/stuff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "stat.h"
#include "types.h"
#include "user.h"
#include "fcntl.h" // For O_ constants

int main(int argc, const char *argv[]) {
int consoleLock = open("consoleLock", O_CREATE|O_RDWR);

const char* message = "Stuff\n";
for (int i = 0; i < 1000; ++i) {
flock(consoleLock);
for(int j = 0; j < 7; ++j) {
write(1, &message[j], 1);
}
funlock(consoleLock);
}
close(consoleLock);
}
15 changes: 15 additions & 0 deletions Xv6-OS-with-custom-modifications/user/testflock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h" // For O_CREATE

int
main(int argc, char *argv[])
{
int lockFd = open("consoleLock", O_CREATE|O_RDWR);
printf(1, "lockFd is %d\n", lockFd);
flock(lockFd);
flock(lockFd);
funlock(lockFd);
exit();
}
2 changes: 2 additions & 0 deletions Xv6-OS-with-custom-modifications/usys.S
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ SYSCALL(sleep)
SYSCALL(uptime)

SYSCALL(shutdown)
SYSCALL(flock)
SYSCALL(funlock)