pingpong finished
This commit is contained in:
3
Makefile
3
Makefile
@@ -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
21
user/pingpong.c
Normal file
21
user/pingpong.c
Normal 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);
|
||||||
|
}
|
||||||
52
user/ulib.c
52
user/ulib.c
@@ -1,22 +1,18 @@
|
|||||||
#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;
|
||||||
@@ -25,17 +21,13 @@ strcpy(char *s, const char *t)
|
|||||||
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++)
|
||||||
@@ -43,9 +35,7 @@ strlen(const char *s)
|
|||||||
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++) {
|
||||||
@@ -54,18 +44,14 @@ memset(void *dst, int c, uint n)
|
|||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char *strchr(const char *s, char c) {
|
||||||
strchr(const char *s, char c)
|
|
||||||
{
|
|
||||||
for (; *s; s++)
|
for (; *s; s++)
|
||||||
if (*s == c)
|
if (*s == c)
|
||||||
return (char *)s;
|
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;
|
||||||
|
|
||||||
@@ -81,9 +67,7 @@ gets(char *buf, int max)
|
|||||||
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;
|
||||||
|
|
||||||
@@ -95,9 +79,7 @@ stat(const char *n, struct stat *st)
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int atoi(const char *s) {
|
||||||
atoi(const char *s)
|
|
||||||
{
|
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
@@ -106,9 +88,7 @@ atoi(const char *s)
|
|||||||
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;
|
||||||
|
|
||||||
@@ -126,9 +106,7 @@ memmove(void *vdst, const void *vsrc, int n)
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "kernel/types.h"
|
||||||
struct stat;
|
struct stat;
|
||||||
|
|
||||||
// system calls
|
// system calls
|
||||||
|
|||||||
Reference in New Issue
Block a user