From e9e9e8e65b94e9c92f5761d7378a48ceb116e1e9 Mon Sep 17 00:00:00 2001 From: ljy <1134570045@qq.com> Date: Tue, 14 Jun 2022 17:31:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fork=E6=97=B6copy=20fd=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8dup=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/lwp/lwp_syscall.c | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index a9038186d0..3d6444c812 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -1590,40 +1590,19 @@ static int lwp_copy_files(struct rt_lwp *dst, struct rt_lwp *src) if (dst_fdt->fds) { struct dfs_fd *d_s; - struct dfs_fd *d_d; int i; dst_fdt->maxfd = src_fdt->maxfd; dfs_fd_lock(); - /* copy stdio */ + /* dup files */ for (i = 0; i < src_fdt->maxfd; i++) { d_s = fdt_fd_get(src_fdt, i); if (d_s) { - dfs_fm_lock(); - if (!d_s->fnode) - { - dfs_fm_unlock(); - continue; - } - d_s->fnode->ref_count++; - dfs_fm_unlock(); - - /* alloc dfs_fd struct */ - d_d = (struct dfs_fd *)rt_calloc(1, sizeof(struct dfs_fd)); - if (!d_d) - { - dfs_fd_unlock(); - return -1; - } - dst_fdt->fds[i] = d_d; - d_d->magic = d_s->magic; - d_d->ref_count = 1; - d_d->pos = d_s->pos; - d_d->fnode = d_s->fnode; - d_d->data = d_s->data; + dst_fdt->fds[i] = d_s; + d_s->ref_count++; } } dfs_fd_unlock(); -- Gitee From 7d13c372186bf6384b787525a13fa122f853cdd6 Mon Sep 17 00:00:00 2001 From: ljy <1134570045@qq.com> Date: Tue, 14 Jun 2022 17:33:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3pipe=E5=86=99=E7=AB=AF?= =?UTF-8?q?=E5=85=B3=E9=97=AD=EF=BC=8C=E8=AF=BB=E7=AB=AF=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/ipc/pipe.h | 2 ++ components/drivers/src/pipe.c | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/components/drivers/include/ipc/pipe.h b/components/drivers/include/ipc/pipe.h index 338063ace8..2545e41c94 100644 --- a/components/drivers/include/ipc/pipe.h +++ b/components/drivers/include/ipc/pipe.h @@ -35,6 +35,8 @@ struct rt_pipe_device rt_wqueue_t reader_queue; rt_wqueue_t writer_queue; + int writer; + int reader; struct rt_mutex lock; }; diff --git a/components/drivers/src/pipe.c b/components/drivers/src/pipe.c index 284104511b..91df5ddcf4 100644 --- a/components/drivers/src/pipe.c +++ b/components/drivers/src/pipe.c @@ -52,6 +52,15 @@ static int pipe_fops_open(struct dfs_fd *fd) rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); + if ((fd->flags & O_RDONLY) == O_RDONLY) + { + pipe->reader = 1; + } + + if ((fd->flags & O_WRONLY) == O_WRONLY) + { + pipe->writer = 1; + } if (fd->fnode->ref_count == 1) { pipe->fifo = rt_ringbuffer_create(pipe->bufsz); @@ -82,6 +91,20 @@ static int pipe_fops_close(struct dfs_fd *fd) device = &pipe->parent; rt_mutex_take(&pipe->lock, RT_WAITING_FOREVER); + if ((fd->flags & O_RDONLY) == O_RDONLY) + { + pipe->reader = 0; + } + + if ((fd->flags & O_WRONLY) == O_WRONLY) + { + pipe->writer = 0; + while (!rt_list_isempty(&pipe->reader_queue.waiting_list)) + { + rt_wqueue_wakeup(&pipe->reader_queue, (void*)POLLIN); + } + } + if (fd->fnode->ref_count == 1) { if (pipe->fifo != RT_NULL) @@ -139,7 +162,7 @@ static int pipe_fops_read(struct dfs_fd *fd, void *buf, size_t count) { len = rt_ringbuffer_get(pipe->fifo, buf, count); - if (len > 0) + if (len > 0 || pipe->writer == 0) { break; } @@ -427,6 +450,8 @@ rt_pipe_t *rt_pipe_create(const char *name, int bufsz) rt_mutex_init(&pipe->lock, name, RT_IPC_FLAG_FIFO); rt_wqueue_init(&pipe->reader_queue); rt_wqueue_init(&pipe->writer_queue); + pipe->writer = 0; + pipe->reader = 0; RT_ASSERT(bufsz < 0xFFFF); pipe->bufsz = bufsz; -- Gitee