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; }
void WriteProcess(int fd[2]) { PipeWrite(fd,WRITE_NUM); printf("Write Process: Sleep - 10 Seconds ...\n"); sleep(10); printf("Write Process: Wake up then Why not rewrite 70 characters ? \n"); PipeWrite(fd,WRITE_NUM); printf("Write Process : Done\n"); }
BOOL ExLocalClientSendFinishSoon(void *ctx) { Exchanger *ex = (Exchanger *)ctx; MCtrl mctrl; mctrl.code = SIG_FINISHSOON; // Make sure it is sent. BLOCK(PipeWrite(&ex->channels[PIPE_C2S], ARG(mctrl))); }
// Send restart signal (in block mode) once the search is over BOOL ExLocalClientSendRestart(void *ctx) { Exchanger *ex = (Exchanger *)ctx; MCtrl mctrl; mctrl.code = SIG_RESTART; // Make sure it is sent. BLOCK(PipeWrite(&ex->channels[PIPE_C2S], ARG(mctrl))); }
void writeBridgeToPipe() { int rc = PipeWrite(pipe_id, (void *) b, sizeof(Bridge)); if (rc != sizeof(Bridge)) { TracePrintf(TRACE_LEVEL_TERMINAL_PROBLEM, "FAILED TO WRITE BRIDGE\n"); Exit(-1); } }
// Block send moves, once CNN finish evaluation. // If done is set, don't send anything. BOOL ExLocalServerSendMove(void *ctx, MMove *move) { Exchanger *ex = (Exchanger *)ctx; if (move->seq == 0) return FALSE; while (! ex->done) { unsigned char flag = get_flag(ex); if (flag & (1 << SIG_RESTART)) break; if (PipeWrite(&ex->channels[PIPE_MOVE], ARGP(move)) == 0) { ex->move_sent ++; return TRUE; } } return FALSE; }
UINT WINAPI Sender(LPVOID arg) { PPIPE ppipe = (PPIPE)arg; HANDLE p = PipeOpenWrite(NAMEPIPE); INT i = 0; while (i < STREAM_SIZE) { PipeWrite(p, input + i++, 1); } // close pipe access via "p" handle PipeClose(p); return 0; }
// 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; }
/* In some cases the response is expected to be not filled out. Some requests will have response filled only in case of errors. */ int PipeReadWriteData(const char *base_cmd, const char *args, const char *request, Rlist **response, int pipe_timeout_secs, int pipe_termination_check_secs) { assert(base_cmd); assert(args); char *command = StringFormat("%s %s", base_cmd, args); IOData io = cf_popen_full_duplex(command, false, true); if (io.write_fd == -1 || io.read_fd == -1) { Log(LOG_LEVEL_INFO, "Some error occurred while communicating with %s", command); free(command); return -1; } Log(LOG_LEVEL_DEBUG, "Opened fds %d and %d for command '%s'.", io.read_fd, io.write_fd, command); if (PipeWrite(&io, request) != strlen(request)) { Log(LOG_LEVEL_VERBOSE, "Couldn't send whole data to application '%s'.", base_cmd); free(command); return -1; } /* We can have some error message here. */ Rlist *res = PipeReadData(&io, pipe_timeout_secs, pipe_termination_check_secs); /* If script returns non 0 status */ int close = cf_pclose_full_duplex(&io); if (close != EXIT_SUCCESS) { Log(LOG_LEVEL_VERBOSE, "Command '%s' returned with non zero return code: %d", command, close); free(command); RlistDestroy(res); return -1; } free(command); *response = res; return 0; }
UINT WINAPI ClientTest2(LPVOID arg) { //HANDLE p = PipeOpenWrite(NAMEPIPE); HANDLE p = PipeOpenWrite(NAMEPIPE); MESSAGE m; char currchar = 'A'; // send NTRIES_TEST2 messages for (int n = 0; n < NTRIES_TEST2; ++n) { FillMessage(m, currchar); if (currchar == 'Z') currchar = 'A'; else currchar++; PipeWrite(p, m, MSG_SIZE); } PipeClose(p); return 0; }
UINT WINAPI ClientTest3(LPVOID arg) { HANDLE p = PipeOpenWrite(NAMEPIPE); MESSAGE m; // send NTRIES messages for (int n = 0; n < NTRIES_TEST3; ++n) { EnterCriticalSection(&clientMutex); FillMessage(m, currchar); if (currchar == 'Z') currchar = 'A'; else currchar++; LeaveCriticalSection(&clientMutex); PipeWrite(p, m, MSG_SIZE); } PipeClose(p); return 0; }
int PipeWriteData(const char *base_cmd, const char *args, const char *data) { assert(base_cmd); assert(args); char *command = StringFormat("%s %s", base_cmd, args); IOData io = cf_popen_full_duplex(command, false, true); free(command); if (io.write_fd == -1 || io.read_fd == -1) { Log(LOG_LEVEL_VERBOSE, "Error occurred while opening pipes for " "communication with application '%s'.", base_cmd); return -1; } Log(LOG_LEVEL_DEBUG, "Opened fds %d and %d for command '%s'.", io.read_fd, io.write_fd, args); int res = 0; if (PipeWrite(&io, data) != strlen(data)) { Log(LOG_LEVEL_VERBOSE, "Was not able to send whole data to application '%s'.", base_cmd); res = -1; } /* If script returns non 0 status */ int close = cf_pclose_full_duplex(&io); if (close != EXIT_SUCCESS) { Log(LOG_LEVEL_VERBOSE, "Application '%s' returned with non zero return code: %d", base_cmd, close); res = -1; } return res; }
// Send board (not blocked) BOOL ExLocalClientSendBoard(void *ctx, MBoard *board) { Exchanger *ex = (Exchanger *)ctx; RET(PipeWrite(&ex->channels[PIPE_BOARD], ARGP(board))); }
/*----------------------------------------------------------------------------*/ ssize_t mtcp_write(mctx_t mctx, int sockid, char *buf, size_t len) { mtcp_manager_t mtcp; socket_map_t socket; tcp_stream *cur_stream; struct tcp_send_vars *sndvar; 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 PipeWrite(mctx, sockid, buf, len); } if (socket->socktype != MTCP_SOCK_STREAM) { TRACE_API("Not an end socket. id: %d\n", sockid); errno = ENOTSOCK; return -1; } cur_stream = socket->stream; if (!cur_stream || !(cur_stream->state == TCP_ST_ESTABLISHED || cur_stream->state == TCP_ST_CLOSE_WAIT)) { errno = ENOTCONN; return -1; } if (len <= 0) { if (socket->opts & MTCP_NONBLOCK) { errno = EAGAIN; return -1; } else { return 0; } } sndvar = cur_stream->sndvar; SBUF_LOCK(&sndvar->write_lock); #if BLOCKING_SUPPORT if (!(socket->opts & MTCP_NONBLOCK)) { while (sndvar->snd_wnd <= 0) { TRACE_SNDBUF("Waiting for available sending window...\n"); if (!cur_stream || cur_stream->state != TCP_ST_ESTABLISHED) { SBUF_UNLOCK(&sndvar->write_lock); errno = EINTR; return -1; } pthread_cond_wait(&sndvar->write_cond, &sndvar->write_lock); TRACE_SNDBUF("Sending buffer became ready! snd_wnd: %u\n", sndvar->snd_wnd); } } #endif ret = CopyFromUser(mtcp, cur_stream, buf, len); SBUF_UNLOCK(&sndvar->write_lock); if (ret > 0 && !(sndvar->on_sendq || sndvar->on_send_list)) { SQ_LOCK(&mtcp->ctx->sendq_lock); sndvar->on_sendq = TRUE; StreamEnqueue(mtcp->sendq, cur_stream); /* this always success */ SQ_UNLOCK(&mtcp->ctx->sendq_lock); mtcp->wakeup_flag = TRUE; } if (ret == 0 && (socket->opts & MTCP_NONBLOCK)) { ret = -1; errno = EAGAIN; } /* if there are remaining sending buffer, generate write event */ if (sndvar->snd_wnd > 0) { if (socket->epoll & MTCP_EPOLLOUT && !(socket->epoll & MTCP_EPOLLET)) { AddEpollEvent(mtcp->ep, USR_SHADOW_EVENT_QUEUE, socket, MTCP_EPOLLOUT); #if BLOCKING_SUPPORT } else if (!(socket->opts & MTCP_NONBLOCK)) { if (!cur_stream->on_snd_br_list) { cur_stream->on_snd_br_list = TRUE; TAILQ_INSERT_TAIL(&mtcp->snd_br_list, cur_stream, sndvar->snd_br_link); mtcp->snd_br_list_cnt++; } #endif } } TRACE_API("Stream %d: mtcp_write() 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; }