pingpong finished
This commit is contained in:
3
Makefile
3
Makefile
@ -86,7 +86,7 @@ LD = $(TOOLPREFIX)ld
|
||||
OBJCOPY = $(TOOLPREFIX)objcopy
|
||||
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
|
||||
LABUPPER = $(shell echo $(LAB) | tr a-z A-Z)
|
||||
@ -189,6 +189,7 @@ UPROGS=\
|
||||
$U/_wc\
|
||||
$U/_zombie\
|
||||
$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/stat.h"
|
||||
#include "kernel/types.h"
|
||||
#include "user/user.h"
|
||||
|
||||
//
|
||||
// wrapper so that it's OK if main() does not call exit().
|
||||
//
|
||||
void
|
||||
_main()
|
||||
{
|
||||
void _main() {
|
||||
extern int main();
|
||||
main();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
char*
|
||||
strcpy(char *s, const char *t)
|
||||
{
|
||||
char *strcpy(char *s, const char *t) {
|
||||
char *os;
|
||||
|
||||
os = s;
|
||||
@ -25,17 +21,13 @@ strcpy(char *s, const char *t)
|
||||
return os;
|
||||
}
|
||||
|
||||
int
|
||||
strcmp(const char *p, const char *q)
|
||||
{
|
||||
int strcmp(const char *p, const char *q) {
|
||||
while (*p && *p == *q)
|
||||
p++, q++;
|
||||
return (uchar)*p - (uchar)*q;
|
||||
}
|
||||
|
||||
uint
|
||||
strlen(const char *s)
|
||||
{
|
||||
uint strlen(const char *s) {
|
||||
int n;
|
||||
|
||||
for (n = 0; s[n]; n++)
|
||||
@ -43,9 +35,7 @@ strlen(const char *s)
|
||||
return n;
|
||||
}
|
||||
|
||||
void*
|
||||
memset(void *dst, int c, uint n)
|
||||
{
|
||||
void *memset(void *dst, int c, uint n) {
|
||||
char *cdst = (char *)dst;
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
@ -54,18 +44,14 @@ memset(void *dst, int c, uint n)
|
||||
return dst;
|
||||
}
|
||||
|
||||
char*
|
||||
strchr(const char *s, char c)
|
||||
{
|
||||
char *strchr(const char *s, char c) {
|
||||
for (; *s; s++)
|
||||
if (*s == c)
|
||||
return (char *)s;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char*
|
||||
gets(char *buf, int max)
|
||||
{
|
||||
char *gets(char *buf, int max) {
|
||||
int i, cc;
|
||||
char c;
|
||||
|
||||
@ -81,9 +67,7 @@ gets(char *buf, int max)
|
||||
return buf;
|
||||
}
|
||||
|
||||
int
|
||||
stat(const char *n, struct stat *st)
|
||||
{
|
||||
int stat(const char *n, struct stat *st) {
|
||||
int fd;
|
||||
int r;
|
||||
|
||||
@ -95,9 +79,7 @@ stat(const char *n, struct stat *st)
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
atoi(const char *s)
|
||||
{
|
||||
int atoi(const char *s) {
|
||||
int n;
|
||||
|
||||
n = 0;
|
||||
@ -106,9 +88,7 @@ atoi(const char *s)
|
||||
return n;
|
||||
}
|
||||
|
||||
void*
|
||||
memmove(void *vdst, const void *vsrc, int n)
|
||||
{
|
||||
void *memmove(void *vdst, const void *vsrc, int n) {
|
||||
char *dst;
|
||||
const char *src;
|
||||
|
||||
@ -126,9 +106,7 @@ memmove(void *vdst, const void *vsrc, int n)
|
||||
return vdst;
|
||||
}
|
||||
|
||||
int
|
||||
memcmp(const void *s1, const void *s2, uint n)
|
||||
{
|
||||
int memcmp(const void *s1, const void *s2, uint n) {
|
||||
const char *p1 = s1, *p2 = s2;
|
||||
while (n-- > 0) {
|
||||
if (*p1 != *p2) {
|
||||
@ -140,8 +118,6 @@ memcmp(const void *s1, const void *s2, uint n)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
memcpy(void *dst, const void *src, uint n)
|
||||
{
|
||||
void *memcpy(void *dst, const void *src, uint n) {
|
||||
return memmove(dst, src, n);
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
#include "kernel/types.h"
|
||||
struct stat;
|
||||
|
||||
// system calls
|
||||
|
||||
Reference in New Issue
Block a user