pingpong finished

This commit is contained in:
2025-02-25 22:25:11 +08:00
parent 5bb955df94
commit b5dcfc539f
8 changed files with 325 additions and 1110 deletions

View File

@ -86,7 +86,7 @@ LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2 CFLAGS = -Wall -Werror -O2 -fno-omit-frame-pointer -ggdb -gdwarf-2
ifdef LAB ifdef LAB
LABUPPER = $(shell echo $(LAB) | tr a-z A-Z) LABUPPER = $(shell echo $(LAB) | tr a-z A-Z)
@ -189,6 +189,7 @@ UPROGS=\
$U/_wc\ $U/_wc\
$U/_zombie\ $U/_zombie\
$U/_sleep\ $U/_sleep\
$U/_pingpong\

File diff suppressed because it is too large Load Diff

View File

@ -11,11 +11,11 @@ struct superblock;
// bio.c // bio.c
void binit(void); void binit(void);
struct buf* bread(uint, uint); struct buf *bread(uint, uint);
void brelse(struct buf*); void brelse(struct buf *);
void bwrite(struct buf*); void bwrite(struct buf *);
void bpin(struct buf*); void bpin(struct buf *);
void bunpin(struct buf*); void bunpin(struct buf *);
// console.c // console.c
void consoleinit(void); void consoleinit(void);
@ -23,62 +23,62 @@ void consoleintr(int);
void consputc(int); void consputc(int);
// exec.c // exec.c
int exec(char*, char**); int exec(char *, char **);
// file.c // file.c
struct file* filealloc(void); struct file *filealloc(void);
void fileclose(struct file*); void fileclose(struct file *);
struct file* filedup(struct file*); struct file *filedup(struct file *);
void fileinit(void); void fileinit(void);
int fileread(struct file*, uint64, int n); int fileread(struct file *, uint64, int n);
int filestat(struct file*, uint64 addr); int filestat(struct file *, uint64 addr);
int filewrite(struct file*, uint64, int n); int filewrite(struct file *, uint64, int n);
// fs.c // fs.c
void fsinit(int); void fsinit(int);
int dirlink(struct inode*, char*, uint); int dirlink(struct inode *, char *, uint);
struct inode* dirlookup(struct inode*, char*, uint*); struct inode *dirlookup(struct inode *, char *, uint *);
struct inode* ialloc(uint, short); struct inode *ialloc(uint, short);
struct inode* idup(struct inode*); struct inode *idup(struct inode *);
void iinit(); void iinit();
void ilock(struct inode*); void ilock(struct inode *);
void iput(struct inode*); void iput(struct inode *);
void iunlock(struct inode*); void iunlock(struct inode *);
void iunlockput(struct inode*); void iunlockput(struct inode *);
void iupdate(struct inode*); void iupdate(struct inode *);
int namecmp(const char*, const char*); int namecmp(const char *, const char *);
struct inode* namei(char*); struct inode *namei(char *);
struct inode* nameiparent(char*, char*); struct inode *nameiparent(char *, char *);
int readi(struct inode*, int, uint64, uint, uint); int readi(struct inode *, int, uint64, uint, uint);
void stati(struct inode*, struct stat*); void stati(struct inode *, struct stat *);
int writei(struct inode*, int, uint64, uint, uint); int writei(struct inode *, int, uint64, uint, uint);
void itrunc(struct inode*); void itrunc(struct inode *);
// ramdisk.c // ramdisk.c
void ramdiskinit(void); void ramdiskinit(void);
void ramdiskintr(void); void ramdiskintr(void);
void ramdiskrw(struct buf*); void ramdiskrw(struct buf *);
// kalloc.c // kalloc.c
void* kalloc(void); void *kalloc(void);
void kfree(void *); void kfree(void *);
void kinit(void); void kinit(void);
// log.c // log.c
void initlog(int, struct superblock*); void initlog(int, struct superblock *);
void log_write(struct buf*); void log_write(struct buf *);
void begin_op(void); void begin_op(void);
void end_op(void); void end_op(void);
// pipe.c // pipe.c
int pipealloc(struct file**, struct file**); int pipealloc(struct file **, struct file **);
void pipeclose(struct pipe*, int); void pipeclose(struct pipe *, int);
int piperead(struct pipe*, uint64, int); int piperead(struct pipe *, uint64, int);
int pipewrite(struct pipe*, uint64, int); int pipewrite(struct pipe *, uint64, int);
// printf.c // printf.c
void printf(char*, ...); void printf(char *, ...);
void panic(char*) __attribute__((noreturn)); void panic(char *) __attribute__((noreturn));
void printfinit(void); void printfinit(void);
// proc.c // proc.c
@ -90,55 +90,55 @@ void proc_mapstacks(pagetable_t);
pagetable_t proc_pagetable(struct proc *); pagetable_t proc_pagetable(struct proc *);
void proc_freepagetable(pagetable_t, uint64); void proc_freepagetable(pagetable_t, uint64);
int kill(int); int kill(int);
int killed(struct proc*); int killed(struct proc *);
void setkilled(struct proc*); void setkilled(struct proc *);
struct cpu* mycpu(void); struct cpu *mycpu(void);
struct cpu* getmycpu(void); struct cpu *getmycpu(void);
struct proc* myproc(); struct proc *myproc();
void procinit(void); void procinit(void);
void scheduler(void) __attribute__((noreturn)); void scheduler(void) __attribute__((noreturn));
void sched(void); void sched(void);
void sleep(void*, struct spinlock*); void sleep(void *, struct spinlock *);
void userinit(void); void userinit(void);
int wait(uint64); int wait(uint64);
void wakeup(void*); void wakeup(void *);
void yield(void); void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len); int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len); int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void); void procdump(void);
// swtch.S // swtch.S
void swtch(struct context*, struct context*); void swtch(struct context *, struct context *);
// spinlock.c // spinlock.c
void acquire(struct spinlock*); void acquire(struct spinlock *);
int holding(struct spinlock*); int holding(struct spinlock *);
void initlock(struct spinlock*, char*); void initlock(struct spinlock *, char *);
void release(struct spinlock*); void release(struct spinlock *);
void push_off(void); void push_off(void);
void pop_off(void); void pop_off(void);
// sleeplock.c // sleeplock.c
void acquiresleep(struct sleeplock*); void acquiresleep(struct sleeplock *);
void releasesleep(struct sleeplock*); void releasesleep(struct sleeplock *);
int holdingsleep(struct sleeplock*); int holdingsleep(struct sleeplock *);
void initsleeplock(struct sleeplock*, char*); void initsleeplock(struct sleeplock *, char *);
// string.c // string.c
int memcmp(const void*, const void*, uint); int memcmp(const void *, const void *, uint);
void* memmove(void*, const void*, uint); void *memmove(void *, const void *, uint);
void* memset(void*, int, uint); void *memset(void *, int, uint);
char* safestrcpy(char*, const char*, int); char *safestrcpy(char *, const char *, int);
int strlen(const char*); int strlen(const char *);
int strncmp(const char*, const char*, uint); int strncmp(const char *, const char *, uint);
char* strncpy(char*, const char*, int); char *strncpy(char *, const char *, int);
// syscall.c // syscall.c
void argint(int, int*); void argint(int, int *);
int argstr(int, char*, int); int argstr(int, char *, int);
void argaddr(int, uint64 *); void argaddr(int, uint64 *);
int fetchstr(uint64, char*, int); int fetchstr(uint64, char *, int);
int fetchaddr(uint64, uint64*); int fetchaddr(uint64, uint64 *);
void syscall(); void syscall();
// trap.c // trap.c
@ -168,7 +168,7 @@ int uvmcopy(pagetable_t, pagetable_t, uint64);
void uvmfree(pagetable_t, uint64); void uvmfree(pagetable_t, uint64);
void uvmunmap(pagetable_t, uint64, uint64, int); void uvmunmap(pagetable_t, uint64, uint64, int);
void uvmclear(pagetable_t, uint64); void uvmclear(pagetable_t, uint64);
pte_t * walk(pagetable_t, uint64, int); pte_t *walk(pagetable_t, uint64, int);
uint64 walkaddr(pagetable_t, uint64); uint64 walkaddr(pagetable_t, uint64);
int copyout(pagetable_t, uint64, char *, uint64); int copyout(pagetable_t, uint64, char *, uint64);
int copyin(pagetable_t, char *, uint64, uint64); int copyin(pagetable_t, char *, uint64, uint64);
@ -186,4 +186,4 @@ void virtio_disk_rw(struct buf *, int);
void virtio_disk_intr(void); void virtio_disk_intr(void);
// number of elements in fixed-size array // number of elements in fixed-size array
#define NELEM(x) (sizeof(x)/sizeof((x)[0])) #define NELEM(x) (sizeof(x) / sizeof((x)[0]))

BIN
mkfs/mkfs Normal file

Binary file not shown.

21
user/pingpong.c Normal file
View File

@ -0,0 +1,21 @@
#include "kernel/types.h"
#include "user/user.h"
int main(int argc, char *argv[]) {
int proc_f2s[2], proc_s2f[2];
char buffer[8];
pipe(proc_f2s);
pipe(proc_s2f);
if (fork() == 0) {
read(proc_s2f[0], buffer, 4);
printf("%d: received %s\n", getpid(), buffer);
write(proc_f2s[1], "pong", strlen("pong"));
} else {
write(proc_s2f[1], "ping", strlen("ping"));
read(proc_f2s[0], buffer, 4);
printf("%d: received %s\n", getpid(), buffer);
}
exit(0);
}

View File

@ -1,134 +1,112 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fcntl.h" #include "kernel/fcntl.h"
#include "kernel/stat.h"
#include "kernel/types.h"
#include "user/user.h" #include "user/user.h"
// //
// wrapper so that it's OK if main() does not call exit(). // wrapper so that it's OK if main() does not call exit().
// //
void void _main() {
_main()
{
extern int main(); extern int main();
main(); main();
exit(0); exit(0);
} }
char* char *strcpy(char *s, const char *t) {
strcpy(char *s, const char *t)
{
char *os; char *os;
os = s; os = s;
while((*s++ = *t++) != 0) while ((*s++ = *t++) != 0)
; ;
return os; return os;
} }
int int strcmp(const char *p, const char *q) {
strcmp(const char *p, const char *q) while (*p && *p == *q)
{
while(*p && *p == *q)
p++, q++; p++, q++;
return (uchar)*p - (uchar)*q; return (uchar)*p - (uchar)*q;
} }
uint uint strlen(const char *s) {
strlen(const char *s)
{
int n; int n;
for(n = 0; s[n]; n++) for (n = 0; s[n]; n++)
; ;
return n; return n;
} }
void* void *memset(void *dst, int c, uint n) {
memset(void *dst, int c, uint n) char *cdst = (char *)dst;
{
char *cdst = (char *) dst;
int i; int i;
for(i = 0; i < n; i++){ for (i = 0; i < n; i++) {
cdst[i] = c; cdst[i] = c;
} }
return dst; return dst;
} }
char* char *strchr(const char *s, char c) {
strchr(const char *s, char c) for (; *s; s++)
{ if (*s == c)
for(; *s; s++) return (char *)s;
if(*s == c)
return (char*)s;
return 0; return 0;
} }
char* char *gets(char *buf, int max) {
gets(char *buf, int max)
{
int i, cc; int i, cc;
char c; char c;
for(i=0; i+1 < max; ){ for (i = 0; i + 1 < max;) {
cc = read(0, &c, 1); cc = read(0, &c, 1);
if(cc < 1) if (cc < 1)
break; break;
buf[i++] = c; buf[i++] = c;
if(c == '\n' || c == '\r') if (c == '\n' || c == '\r')
break; break;
} }
buf[i] = '\0'; buf[i] = '\0';
return buf; return buf;
} }
int int stat(const char *n, struct stat *st) {
stat(const char *n, struct stat *st)
{
int fd; int fd;
int r; int r;
fd = open(n, O_RDONLY); fd = open(n, O_RDONLY);
if(fd < 0) if (fd < 0)
return -1; return -1;
r = fstat(fd, st); r = fstat(fd, st);
close(fd); close(fd);
return r; return r;
} }
int int atoi(const char *s) {
atoi(const char *s)
{
int n; int n;
n = 0; n = 0;
while('0' <= *s && *s <= '9') while ('0' <= *s && *s <= '9')
n = n*10 + *s++ - '0'; n = n * 10 + *s++ - '0';
return n; return n;
} }
void* void *memmove(void *vdst, const void *vsrc, int n) {
memmove(void *vdst, const void *vsrc, int n)
{
char *dst; char *dst;
const char *src; const char *src;
dst = vdst; dst = vdst;
src = vsrc; src = vsrc;
if (src > dst) { if (src > dst) {
while(n-- > 0) while (n-- > 0)
*dst++ = *src++; *dst++ = *src++;
} else { } else {
dst += n; dst += n;
src += n; src += n;
while(n-- > 0) while (n-- > 0)
*--dst = *--src; *--dst = *--src;
} }
return vdst; return vdst;
} }
int int memcmp(const void *s1, const void *s2, uint n) {
memcmp(const void *s1, const void *s2, uint n)
{
const char *p1 = s1, *p2 = s2; const char *p1 = s1, *p2 = s2;
while (n-- > 0) { while (n-- > 0) {
if (*p1 != *p2) { if (*p1 != *p2) {
@ -140,8 +118,6 @@ memcmp(const void *s1, const void *s2, uint n)
return 0; return 0;
} }
void * void *memcpy(void *dst, const void *src, uint n) {
memcpy(void *dst, const void *src, uint n)
{
return memmove(dst, src, n); return memmove(dst, src, n);
} }

View File

@ -1,41 +1,42 @@
#include "kernel/types.h"
struct stat; struct stat;
// system calls // system calls
int fork(void); int fork(void);
int exit(int) __attribute__((noreturn)); int exit(int) __attribute__((noreturn));
int wait(int*); int wait(int *);
int pipe(int*); int pipe(int *);
int write(int, const void*, int); int write(int, const void *, int);
int read(int, void*, int); int read(int, void *, int);
int close(int); int close(int);
int kill(int); int kill(int);
int exec(const char*, char**); int exec(const char *, char **);
int open(const char*, int); int open(const char *, int);
int mknod(const char*, short, short); int mknod(const char *, short, short);
int unlink(const char*); int unlink(const char *);
int fstat(int fd, struct stat*); int fstat(int fd, struct stat *);
int link(const char*, const char*); int link(const char *, const char *);
int mkdir(const char*); int mkdir(const char *);
int chdir(const char*); int chdir(const char *);
int dup(int); int dup(int);
int getpid(void); int getpid(void);
char* sbrk(int); char *sbrk(int);
int sleep(int); int sleep(int);
int uptime(void); int uptime(void);
// ulib.c // ulib.c
int stat(const char*, struct stat*); int stat(const char *, struct stat *);
char* strcpy(char*, const char*); char *strcpy(char *, const char *);
void *memmove(void*, const void*, int); void *memmove(void *, const void *, int);
char* strchr(const char*, char c); char *strchr(const char *, char c);
int strcmp(const char*, const char*); int strcmp(const char *, const char *);
void fprintf(int, const char*, ...); void fprintf(int, const char *, ...);
void printf(const char*, ...); void printf(const char *, ...);
char* gets(char*, int max); char *gets(char *, int max);
uint strlen(const char*); uint strlen(const char *);
void* memset(void*, int, uint); void *memset(void *, int, uint);
void* malloc(uint); void *malloc(uint);
void free(void*); void free(void *);
int atoi(const char*); int atoi(const char *);
int memcmp(const void *, const void *, uint); int memcmp(const void *, const void *, uint);
void *memcpy(void *, const void *, uint); void *memcpy(void *, const void *, uint);