void ReadProcess(int fd[2]) { printf("Read Process : Time 1\n"); PipeRead(fd,READ_NUM); printf("Read Process : Time 2\n"); PipeRead(fd,READ_NUM); printf("Read Process : Time 3\n"); PipeRead(fd,READ_NUM + 20); }
void readBridgeFromPipe() { memset((void*) b, '\0', sizeof(Bridge)); int rc = PipeRead(pipe_id, (void *) b, sizeof(Bridge)); if (rc != sizeof(Bridge)) { TracePrintf(TRACE_LEVEL_TERMINAL_PROBLEM, "FAILED TO READ BRIDGE\n"); Exit(-1); } }
int main(int argc, char *argv[]) { TracePrintf(1, "\t===>pipe.c\n"); int pipe_id; int rc; if (PipeInit(&pipe_id) != 0) TracePrintf(1, "Failed to initialize pipe\n"); else TracePrintf(1, "Pipe Initialized. ID = %d\n", pipe_id); rc = Fork(); if (rc == 0) { char *to_write = (char *)malloc(5 * sizeof(char)); to_write[0] = 'a'; to_write[1] = 'b'; to_write[2] = 'c'; to_write[3] = 'd'; to_write[4] = 'e'; char *second_write = (char *)malloc(2 * sizeof(char)); second_write[0] = 'e'; second_write[1] = 'f'; int written; Pause(); TracePrintf(1, "\tCHILD: about to write to the lock\n"); written = PipeWrite(pipe_id, to_write, 4); TracePrintf(1, "\tCHILD: wrote %d characters to the lock\n", written); Pause(); TracePrintf(1, "\tCHILD: pausing once\n"); Pause(); written = PipeWrite(pipe_id, second_write, 2); TracePrintf(1, "\tCHILD: wrote to the pipe again (%d chars)\n", written); Pause(); Exit(0); } else { char *to_read = (char *)malloc(6 * sizeof(char)); int read; Pause(); Pause(); TracePrintf(1, "\tPARENT: reading 6 chars from the pipe (before it's ready\n"); read = PipeRead(pipe_id, to_read, 6); TracePrintf(1, "\tPARENT: read %d chars from the pipe. Results: %s\n", read, to_read); Pause(); Exit(0); } return 0; }
// Blocked wait until ack is received. BOOL ExLocalClientWaitAck(void *ctx) { Exchanger *ex = (Exchanger *)ctx; MCtrl mctrl; mctrl.code = SIG_RESTART; while (1) { if (PipeRead(&ex->channels[PIPE_S2C], ARG(mctrl)) == 0) { if (mctrl.code == SIG_ACK) break; } } return TRUE; }
// For control thread, we listen to the ctrl channel and change the status of the server. void *threaded_ctrl(void *ctx) { Exchanger *ex = (Exchanger *)ctx; MCtrl mctrl; while (! ex->done) { if (PipeRead(&ex->channels[PIPE_C2S], &mctrl, sizeof(MCtrl)) == 0) { if (mctrl.code != 0) { // All the flags will be reset after we call sendAck. printf("Get control signal. Code = %d\n", mctrl.code); __sync_fetch_and_or(&ex->ctrl_flag, 1 << mctrl.code); } } // Some sleep? } return NULL; }
// Send ack for any unusual signal received. BOOL ExLocalServerSendAckIfNecessary(void *ctx) { Exchanger *ex = (Exchanger *)ctx; // Clear the flag. unsigned char flag = get_flag(ex); // If the flag is not zero before cleanup, we need to send an ack (so that the client know we have received it). BOOL clean_flag = FALSE; BOOL send_ack = TRUE; if (flag != 0) { if (flag & (1 << SIG_RESTART)) { // Clean up the message queues. int num_discarded = 0; MBoard mboard; while (PipeRead(&ex->channels[PIPE_BOARD], ARG(mboard)) == 0) num_discarded ++; printf("#Board Discarded = %d\n", num_discarded); clean_flag = TRUE; } else if (flag & (1 << SIG_FINISHSOON)) { clean_flag = TRUE; // Do not need to send ack for FINISHSOON (No one is going to receive it). send_ack = FALSE; } } if (clean_flag) { printf("Summary: Board received = %d, Move sent = %d\n", ex->board_received, ex->move_sent); ex->board_received = 0; ex->move_sent = 0; // All states are resumed, then we clear the flag. (If we clear the flag before that, sendmove and receiveboard might run before the stats are reset). __sync_fetch_and_and(&ex->ctrl_flag, 0); // Send message. if (send_ack) { MCtrl mctrl; mctrl.code = SIG_ACK; while (! ex->done) { if (PipeWrite(&ex->channels[PIPE_S2C], ARG(mctrl)) == 0) { printf("Ack sent with previous flag = %d\n", flag); // Sent. return TRUE; } } } } // Not sent. return FALSE; }
// Server side, three cases // 1. Block on message with exit value = SIG_OK on newboard. // 2. Return immediately with exit value = SIG_RESTART // 3. Return immediately with exit value = SIG_HIGH_PR // If num_attempt == 0, then try indefinitely, otherwise try num_attempt. int ExLocalServerGetBoard(void *ctx, MBoard *mboard, int num_attempt) { Exchanger *ex = (Exchanger *)ctx; int count = 0; while (! ex->done && (num_attempt == 0 || count < num_attempt)) { // Check flag. unsigned char flag = get_flag(ex); if (flag & (1 << SIG_RESTART)) { return SIG_RESTART; } // Otherwise get the board, if succeed, return. if (PipeRead(&ex->channels[PIPE_BOARD], ARGP(mboard)) == 0) { ex->board_received ++; return SIG_OK; } else if (flag & (1 << SIG_FINISHSOON)) { // If there is no board to read and we want finish soon, return immediately. return SIG_NOPKG; } // Do I need to put some sleep here? count ++; } // queue_pop(&ex->q, board, sizeof(MBoard)); return SIG_NOPKG; }
// Receive move (not blocked) BOOL ExLocalClientGetMove(void *ctx, MMove *move) { Exchanger *ex = (Exchanger *)ctx; RET(PipeRead(&ex->channels[PIPE_MOVE], ARGP(move))); }
/*----------------------------------------------------------------------------*/ ssize_t mtcp_read(mctx_t mctx, int sockid, char *buf, size_t len) { mtcp_manager_t mtcp; socket_map_t socket; tcp_stream *cur_stream; struct tcp_recv_vars *rcvvar; int event_remaining; int ret; mtcp = GetMTCPManager(mctx); if (!mtcp) { return -1; } if (sockid < 0 || sockid >= CONFIG.max_concurrency) { TRACE_API("Socket id %d out of range.\n", sockid); errno = EBADF; return -1; } socket = &mtcp->smap[sockid]; if (socket->socktype == MTCP_SOCK_UNUSED) { TRACE_API("Invalid socket id: %d\n", sockid); errno = EBADF; return -1; } if (socket->socktype == MTCP_SOCK_PIPE) { return PipeRead(mctx, sockid, buf, len); } if (socket->socktype != MTCP_SOCK_STREAM) { TRACE_API("Not an end socket. id: %d\n", sockid); errno = ENOTSOCK; return -1; } /* stream should be in ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT */ cur_stream = socket->stream; if (!cur_stream || !(cur_stream->state >= TCP_ST_ESTABLISHED && cur_stream->state <= TCP_ST_CLOSE_WAIT)) { errno = ENOTCONN; return -1; } rcvvar = cur_stream->rcvvar; /* if CLOSE_WAIT, return 0 if there is no payload */ if (cur_stream->state == TCP_ST_CLOSE_WAIT) { if (!rcvvar->rcvbuf) return 0; if (rcvvar->rcvbuf->merged_len == 0) return 0; } /* return EAGAIN if no receive buffer */ if (socket->opts & MTCP_NONBLOCK) { if (!rcvvar->rcvbuf || rcvvar->rcvbuf->merged_len == 0) { errno = EAGAIN; return -1; } } SBUF_LOCK(&rcvvar->read_lock); #if BLOCKING_SUPPORT if (!(socket->opts & MTCP_NONBLOCK)) { while (rcvvar->rcvbuf->merged_len == 0) { if (!cur_stream || cur_stream->state != TCP_ST_ESTABLISHED) { SBUF_UNLOCK(&rcvvar->read_lock); errno = EINTR; return -1; } pthread_cond_wait(&rcvvar->read_cond, &rcvvar->read_lock); } } #endif ret = CopyToUser(mtcp, cur_stream, buf, len); event_remaining = FALSE; /* if there are remaining payload, generate EPOLLIN */ /* (may due to insufficient user buffer) */ if (socket->epoll & MTCP_EPOLLIN) { if (!(socket->epoll & MTCP_EPOLLET) && rcvvar->rcvbuf->merged_len > 0) { event_remaining = TRUE; } } /* if waiting for close, notify it if no remaining data */ if (cur_stream->state == TCP_ST_CLOSE_WAIT && rcvvar->rcvbuf->merged_len == 0 && ret > 0) { event_remaining = TRUE; } SBUF_UNLOCK(&rcvvar->read_lock); if (event_remaining) { if (socket->epoll) { AddEpollEvent(mtcp->ep, USR_SHADOW_EVENT_QUEUE, socket, MTCP_EPOLLIN); #if BLOCKING_SUPPORT } else if (!(socket->opts & MTCP_NONBLOCK)) { if (!cur_stream->on_rcv_br_list) { cur_stream->on_rcv_br_list = TRUE; TAILQ_INSERT_TAIL(&mtcp->rcv_br_list, cur_stream, rcvvar->rcv_br_link); mtcp->rcv_br_list_cnt++; } #endif } } TRACE_API("Stream %d: mtcp_read() returning %d\n", cur_stream->id, ret); return ret; }
int main(int argc, char* argv[]){ // test 1 : pipe, lock and cvar int lock_id, cvar_id, pipe_id; int pid,status; int condition=0; char *test = "Yalnix Works"; char *buffer = (char*)malloc(1024); TracePrintf(1, "init main: test pipe, lock, cvar.\n"); LockInit(&lock_id); CvarInit(&cvar_id); PipeInit(&pipe_id); TracePrintf(1, "init main: Lockid %d.\n", lock_id); TracePrintf(1, "init main: Cvarid %d.\n", cvar_id); TracePrintf(1, "init main: Pipeid %d.\n", pipe_id); pid = Fork(); if (pid == 0) { TracePrintf(1,"init main: child \n"); Acquire(lock_id); TracePrintf(1,"init main: child acquire the lock\n"); condition=1; TracePrintf(1,"init main: child condition %d\n",condition); PipeWrite(pipe_id, &condition,sizeof(int)); TracePrintf(1,"init main: child change the condition and write it to pipe\n"); TracePrintf(1,"init main: child cvar signal\n"); CvarSignal(cvar_id); Release(lock_id); TracePrintf(1,"init main: child write to pipe: %s\n",test); PipeWrite(pipe_id,test,20); TracePrintf(1,"init main: child release the lock\n"); Exit(0); } else{ TracePrintf(1,"init main: parent\n"); Acquire(lock_id); TracePrintf(1,"init main: parent acquire the lock\n"); while(!condition){ TracePrintf(1,"init main: parent cvar wait, condition %d\n",condition); CvarWait(cvar_id,lock_id); PipeRead(pipe_id,&condition,sizeof(int)); TracePrintf(1,"init main: parent read the condition from pipe, condition %d\n",condition); } TracePrintf(1,"init main: parent wake up\n"); Release(lock_id); TracePrintf(1,"init main: parent release the lock\n"); PipeRead(pipe_id,buffer,20); TracePrintf(1,"init main: parent read from pipe: %s\n",buffer); } Reclaim(lock_id); Reclaim(cvar_id); Reclaim(pipe_id); Exit(0); }
int main(int argc, char **argv) { int pid, i, j, ret; int exit_status; char *exec_argv[] = { "haha", NULL }; int *a = (int *)calloc(3, sizeof(int)); int num_a = 100; char *str; unsigned int delay_ticks = 2; char buf[2 * TERMINAL_MAX_LINE + 2]; char pipe_buf[1025]; int pipe_fd; int lock_fd; int cvar_fd; #ifdef SWAP_TEST free(a); a = (void *)calloc(num_a, PAGESIZE); if (a == NULL) goto loop; if (Fork() == 0) { while (1) { Delay(2); a[0] = 1000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 2000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 3000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 4000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; if (Fork() == 0) { while (1) { Delay(2); a[0] = 5000; TtyPrintf(CONSOLE, "pid = %u, a[0] = %u\n", GetPid(), a[0]); } } for (i = 0; i < num_a * PAGESIZE / sizeof(int); i += (PAGESIZE / sizeof(int))) a[i] = 100; #endif #ifdef PIPE_TEST ret = PipeInit(&pipe_fd); pid = Fork(); bzero(pipe_buf, sizeof(pipe_buf)); if (pid != ERROR && !pid) { TtyPrintf(CONSOLE, "pipe_fd = %u\n", pipe_fd); j = 0; Delay(1); while (1) { for (i = 0; i < sizeof(pipe_buf); i++) pipe_buf[i] = (j % 26) + 'a'; TtyPrintf(CONSOLE, ">>>>>>>>>>> write pipe\n"); ret = PipeWrite(pipe_fd, pipe_buf, sizeof(pipe_buf)); TtyPrintf(CONSOLE, "write pipe ret = %d, pid = %u\n", ret, GetPid()); j++; } Exit(0); } while (1) { bzero(pipe_buf, sizeof(pipe_buf)); TtyPrintf(CONSOLE, ">>>>>>>>>>> read pipe\n"); ret = PipeRead(pipe_fd, pipe_buf, sizeof(pipe_buf) - 7); TtyPrintf(CONSOLE, "<<<<<<<<<<< read pipe ret = %d, pid = %u, %s\n", ret, GetPid(), pipe_buf); } Reclaim(pipe_fd); #endif #ifdef CVAR_TEST ret = LockInit(&lock_fd); ret = CvarInit(&cvar_fd); pid = Custom0(0, 0, 0, 0); if (pid != ERROR && !pid) { Acquire(lock_fd); while (!condition) CvarWait(cvar_fd, lock_fd); Delay(2); Release(lock_fd); Exit(7); } Acquire(lock_fd); condition = 1; CvarSignal(cvar_fd); Release(lock_fd); ret = Reclaim(lock_fd); if (ret) Exit(-1); #endif #ifdef TTY_TEST for (i = 0; i < sizeof(buf) - 1; i++) buf[i] = '9'; buf[i] = '\0'; TtyPrintf(CONSOLE, buf); TtyPrintf(CONSOLE, "\n"); a[0] = 10; a[2] = 100; TtyPrintf(CONSOLE, "Enter somthing:\n"); bzero(buf, sizeof(buf)); ret = TtyRead(CONSOLE, buf, sizeof(buf)); TtyPrintf(CONSOLE, "You just entered: %s (len = %d)\n", buf, ret); #endif #ifdef COW_TEST if (argc == 2) delay_ticks = atoi(argv[1]); pid = Fork(); if (pid == ERROR) { Delay(2); return ERROR; } else if (!pid) { GetPid(); delay_ticks = 5; TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); pid = Fork(); if (pid != ERROR && !pid) { GetPid(); delay_ticks = 8; TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); Delay(delay_ticks); Exec("exec_test", exec_argv); } pid = Wait(&exit_status); TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); TtyPrintf(CONSOLE, " wait child = %u, status = %d\n", pid, exit_status); Delay(delay_ticks); Exit(10); } pid = Fork(); if (pid != ERROR && !pid) { incursive_func(0); GetPid(); delay_ticks = 9; TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); Delay(delay_ticks); Exit(100); } TtyPrintf(CONSOLE, " delay_ticks = %u, pid = %u\n", delay_ticks, GetPid()); Delay(delay_ticks); GetPid(); Wait(&exit_status); Wait(&exit_status); GetPid(); #endif loop: while (1) Delay(delay_ticks); return 0; }