int main(void *arg) { int i, count, fid, pid; struct tst16 *p = NULL; int pids[2 * NB_PROCS]; (void)arg; p = (struct tst16*) shm_create("test16_shm"); assert(p != NULL); assert(getprio(getpid()) == 128); for (count = 1; count <= 100; count++) { fid = pcreate(count); assert(fid >= 0); p->count = count; p->fid = fid; pid = start("proc16_1", 2000, 128, 0); assert(pid > 0); for (i=0; i<=count; i++) { assert(psend(fid, i) == 0); test_it(); } assert(waitpid(pid, 0) == pid); assert(pdelete(fid) == 0); } p->count = 20000; fid = pcreate(50); assert(fid >= 0); p->fid = fid; for (i = 0; i< NB_PROCS; i++) { pid = start("proc16_2", 2000, 127, 0); assert(pid > 0); pids[i] = pid; } for (i=0; i < NB_PROCS; i++) { pid = start("proc16_3", 2000, 127, 0); assert(pid > 0); pids[NB_PROCS + i] = pid; } for (i=0; i < 2 * NB_PROCS; i++) { assert(waitpid(pids[i], 0) == pids[i]); } assert(pcount(fid, &count) == 0); assert(count == 0); assert(pdelete(fid) == 0); shm_release("test16_shm"); printf("ok.\n"); return 0; }
/*------------------------------------------------------------------------ * tcpout - handle events affecting TCP output processing *------------------------------------------------------------------------ */ PROCESS tcpout(void) { struct tcb *ptcb; int i; tcps_oport = pcreate(TCPQLEN); signal(Net.sema); /* synchronize on startup */ while (TRUE) { i = preceive(tcps_oport); ptcb = &tcbtab[TCB(i)]; if (ptcb->tcb_state <= TCPS_CLOSED) continue; /* a rogue; ignore it */ wait(ptcb->tcb_mutex); if (ptcb->tcb_state <= TCPS_CLOSED) continue; /* TCB deallocated */ if (EVENT(i) == DELETE) /* same for all states */ tcbdealloc(ptcb); else tcposwitch[ptcb->tcb_ostate](TCB(i), EVENT(i)); if (ptcb->tcb_state != TCPS_FREE) signal(ptcb->tcb_mutex); } }
int main(int argc, char *argv[]) { printf("*****************************\nTest shm\n" "*****************************\n"); (void)argc; (void)argv; const char *c="page_test_shm"; int fid=pcreate(1); int ret; int pid=start("test_shm1",10000,getprio(getpid()),(void *)fid); void *pagep = shm_create(c); assert(pagep!=NULL); *((int*)pagep+4)=23; psend(fid, 1); preceive(fid,&ret); printf("[process %i] : %i\n",getpid(),*((int*)pagep+4)); pdelete(fid); shm_release(c); waitpid(pid,&ret); assert(shm_acquire(c)==NULL); printf("\n*****************************\nTest fini\n" "*****************************\n"); return 0; }
int _hang (int argc, char ** argv) { int pid = pcreate("smallhang", 0, NULL); prun(pid); while(1); return 0; }
int runcmd_timeout(const char *cmd, cetype_t enc, int wait, int visible, const char *fin, const char *fout, const char *ferr, int timeout, int *timedout) { if (!wait && timeout) error("Timeout with background running processes is not supported."); HANDLE hIN = getInputHandle(fin), hOUT, hERR; int ret = 0; PROCESS_INFORMATION pi; int close1 = 0, close2 = 0, close3 = 0; if (hIN && fin && fin[0]) close1 = 1; hOUT = getOutputHandle(fout, 0); if (!hOUT) return 1; if (fout && fout[0]) close2 = 1; if (fout && fout[0] && ferr && streql(fout, ferr)) hERR = hOUT; else { hERR = getOutputHandle(ferr, 1); if (!hERR) return 1; if (ferr && ferr[0]) close3 = 1; } memset(&pi, 0, sizeof(pi)); pcreate(cmd, enc, !wait, visible, hIN, hOUT, hERR, &pi); if (pi.hProcess) { if (wait) { RCNTXT cntxt; begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv, R_NilValue, R_NilValue); cntxt.cend = &terminate_process; cntxt.cenddata = π DWORD timeoutMillis = (DWORD) (1000*timeout); ret = pwait2(pi.hProcess, timeoutMillis, timedout); endcontext(&cntxt); snprintf(RunError, 501, _("Exit code was %d"), ret); ret &= 0xffff; } else ret = 0; CloseHandle(pi.hProcess); } else { ret = NOLAUNCH; } if (close1) CloseHandle(hIN); if (close2) CloseHandle(hOUT); if (close3) CloseHandle(hERR); return ret; }
//¼ÓÔزå¼þ CTBoxComponent *TBoxPlugLoader::LoadTBoxPlug() { SetDllDirectory(m_plugFolder); HMODULE hm = ::LoadLibrary(m_plugInfo.plugDLL); if (hm != NULL) { pCreateComponent pcreate = (pCreateComponent)GetProcAddress(hm,"CreateComponent"); if (NULL == pcreate) { return NULL; } m_component = pcreate(); return m_component; } return NULL; }
/*------------------------------------------------------------------------ * igmp_update - send (delayed) IGMP host group updates *------------------------------------------------------------------------ */ PROCESS igmp_update(void) { struct hg *phg; HostGroup.hi_uport = pcreate(HG_TSIZE); while (1) { phg = (struct hg *)preceive(HostGroup.hi_uport); wait(HostGroup.hi_mutex); if (phg->hg_state == HGS_DELAYING) { phg->hg_state = HGS_IDLE; igmp(IGT_HREPORT, phg->hg_ifnum, phg->hg_ipa); } signal(HostGroup.hi_mutex); } }
/** Starts a shell, it's a user process, but yes, resides in the kernel's code */ int tty_main (int argc, char ** argv) { clear_screen(); char cadena[50]; int status = 0; // 0: logged out; 1: logged in int val = 0; int tty_number = 0; val = 0; tty_number = atoi(argv[1]) + 1; char * input; char * username; char * password; printf("Monix v1 - TTY %d\n", tty_number); printf("Cristian Pereyra - Legajo 51190\n"); printf("Sistemas Operativos - 2011 - ITBA\n"); printf("Dennis Ritchie RIP\n"); int child; while(1) { switch (status){ case 1: printf("user@tty%d:", tty_number); input = (char *) getConsoleString(1); process_input(input, tty_number); if(getuid(NULL) == -1) { status = 0; } break; case 0: child = pcreate("su", 1, NULL); prun(child); waitpid(child); if(getuid(NULL) != -1) { status = 1; } break; } } return 0; }
static void makelinks(void) { LINK *link; rtrun = true; // Make the links. for (link = link_hd; link; link = link->next) { // Is this link already established? if (link->flags & (p_linked | p_linkini)) continue; // Already linked through some other node? // If so, making this link would create a loop. if (node_find(link->call)) continue; // Fire up the process to handle this link. link->flags = p_linkini; pcreate("RT", RT_STACK, link_out, PR_LINK, link); } }
static CIRCUIT *circuit_new(int flags, int s) { CIRCUIT *circuit; if (MemLow) return NULL; // If RT is starting up, fire up the control process. if (!circuit_hd) Rt_Control = pcreate("RT_Control", RT_STACK, rt_control, PR_NONE, NULL); if (!Rt_Control) return NULL; circuit = zallocw(sizeof(CIRCUIT)); sl_ins_hd(circuit, circuit_hd); circuit->s = s; circuit->flags = flags; circuit->proc = CurProc; return circuit; }
/* Used for external commands in file.show() and edit(), and for system(intern=FALSE). Also called from postscript(). wait != 0 says wait for child to terminate before returning. visible = -1, 0, 1 for hide, minimized, default fin is either NULL or the name of a file from which to redirect stdin for the child. fout/ferr are NULL (use NUL:), "" (use standard streams) or filenames. */ int runcmd(const char *cmd, cetype_t enc, int wait, int visible, const char *fin, const char *fout, const char *ferr) { HANDLE hIN = getInputHandle(fin), hOUT, hERR; int ret = 0; PROCESS_INFORMATION pi; int close1 = 0, close2 = 0, close3 = 0; if (hIN && fin && fin[0]) close1 = 1; hOUT = getOutputHandle(fout, 0); if (!hOUT) return 1; if (fout && fout[0]) close2 = 1; if (fout && fout[0] && ferr && streql(fout, ferr)) hERR = hOUT; else { hERR = getOutputHandle(ferr, 1); if (!hERR) return 1; if (ferr && ferr[0]) close3 = 1; } memset(&pi, 0, sizeof(pi)); pcreate(cmd, enc, !wait, visible, hIN, hOUT, hERR, &pi); if (!pi.hProcess) return NOLAUNCH; if (wait) { RCNTXT cntxt; begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv, R_NilValue, R_NilValue); cntxt.cend = &terminate_process; cntxt.cenddata = π ret = pwait2(pi.hProcess); endcontext(&cntxt); snprintf(RunError, 501, _("Exit code was %d"), ret); ret &= 0xffff; } else ret = 0; CloseHandle(pi.hProcess); if (close1) CloseHandle(hIN); if (close2) CloseHandle(hOUT); if (close3) CloseHandle(hERR); return ret; }
/*------------------------------------------------------------------------ * tcpinp - handle TCP segment coming in from IP *------------------------------------------------------------------------ */ PROCESS tcpinp(void) { struct ep *pep; struct ip *pip; struct tcp *ptcp; struct tcb *ptcb; tcps_iport = pcreate(TCPQLEN); signal(Net.sema); while (TRUE) { pep = (struct ep *)preceive(tcps_iport); if ((int)pep == SYSERR) break; pip = (struct ip *)pep->ep_data; if (tcpcksum(pep, pip->ip_len - IP_HLEN(pip))) { ++TcpInErrs; freebuf(pep); continue; } ptcp = (struct tcp *)pip->ip_data; tcpnet2h(ptcp); /* convert all fields to host order */ pep->ep_order |= EPO_TCP; ptcb = tcpdemux(pep); if (ptcb == 0) { ++TcpInErrs; tcpreset(pep); freebuf(pep); continue; } if (!tcpok(ptcb, pep)) tcpackit(ptcb, pep); else { tcpopts(ptcb, pep); tcpswitch[ptcb->tcb_state](ptcb, pep); } if (ptcb->tcb_state != TCPS_FREE) signal(ptcb->tcb_mutex); freebuf(pep); } }
/*------------------------------------------------------------------------ * netinit - initialize network data structures *------------------------------------------------------------------------ */ netinit() { struct netq *nqptr; int i; /* Initialize pool of network buffers and rest of Net structure */ if (clkruns == FALSE) panic("net: no clock"); Net.netpool = mkpool(EMAXPAK, NETBUFS); for (i=0 ; i<NETQS ; i++) { nqptr = &Net.netqs[i]; nqptr->valid = FALSE; nqptr->uport = -1; nqptr->xport = pcreate(NETQLEN); } Net.mavalid = FALSE; dtoIP(&Net.gateway, NETGATE); Net.nxtprt = ULPORT; Net.nmutex = screate(1); Net.npacket = Net.ndrop = Net.nover = 0; return(OK); }
int shell() { int err = 0; extern PCBDLL **PRIORITY_QUEUES; // Makin ur queues PCBDLL_CREATION PRIORITY_QUEUES = PCBDLL_creation( TDF_QUEUES ); buffer_size = BUFFER_SIZE; EXIT_PROMPT_LOOP = 0; strcpy(EXIT_STRING, "exit\0"); welcome_message(); // Debugging check // givemeready(); while (!EXIT_PROMPT_LOOP) { display_prompt(); // `tdf:$ ' currently SYS_REQ_ERR = sys_req(READ, TERMINAL, BUFFER, &buffer_size); err = append_history(BUFFER); PARSER_ERROR = parse_buffer(BUFFER, PARSED_CMD, ARGUMENTS); // The most trivial case if (!strcmp(BUFFER,"\n\0")) { //Just a New Line continue; } switch (PARSER_ERROR) { case PARSER_FAIL_WHITESPACE: continue; break; case PARSER_FAIL_LONGNAME: printf("Invalid command name entered. Must be less than 9 characters.\n"); continue; break; case PARSER_WIN_SINGLETON: //This is linked to PARSER_WIN_HAS_ARGS which follows case PARSER_WIN_HAS_ARGS: if (!strcmp(PARSED_CMD,EXIT_STRING)) { //Equal to EXIT_STRING if(exit_confirmation() == SHELL_EXIT_CONFIRM) { EXIT_PROMPT_LOOP = !EXIT_PROMPT_LOOP; break; } else { continue; } } break; default: printf("I have no idea how you got here. File a bug report!\n"); printf("Parser exited wth status of: %d\n", PARSER_ERROR); continue; break; } if (!strcmp(PARSED_CMD, "date")) { date(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "dispatch")) { dispatch ( ); } else if (!strcmp(PARSED_CMD, "display") || !strcmp(PARSED_CMD, "ls")) { display(ARGUMENTS); continue; } else if (!strcmp(PARSED_CMD, "hossgive")){ givemeready(); } else if (!strcmp(PARSED_CMD, "load")) { load( ARGUMENTS ); } else if (!strcmp(PARSED_CMD, "loadr3")) { loadr3 ( ); } else if (!strcmp(PARSED_CMD, "history")) { history(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "pblock")) { pblock(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "pcreate")) { pcreate(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "pdelete")) { pdelete(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "presume")) { presume(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "psetprio")) { psetprio(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "pshow")) { pshow(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "pshowall")) { pshowall(); } else if (!strcmp(PARSED_CMD, "pshowblk")) { pshowblk(); } else if (!strcmp(PARSED_CMD, "pshowrd")) { pshowrd(); } else if (!strcmp(PARSED_CMD, "psuspend")) { psuspend(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "punblock")) { punblock(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "version")) { version(ARGUMENTS); } else if (!strcmp(PARSED_CMD, "help")) { help(ARGUMENTS); } else { //printf("Command not found.\n"); continue; } } departing_message(); // Clean up our queues PCBDLL_freeall( ); return err; }
void init_keyboard() { fid_keyboard=pcreate(20); sem=screate(1); echo_fct=echo_fct_standard; }
/* finput is either NULL or the name of a file from which to redirect stdin for the child. visible = -1, 0, 1 for hide, minimized, default io = 0 to read stdout from pipe, 1 to write to pipe, 2 to read stderr from pipe, 3 to read both stdout and stderr from pipe. */ rpipe * rpipeOpen(const char *cmd, cetype_t enc, int visible, const char *finput, int io, const char *fout, const char *ferr) { rpipe *r; HANDLE hTHIS, hIN, hOUT, hERR, hReadPipe, hWritePipe; DWORD id; BOOL res; int close1 = 0, close2 = 0, close3 = 0; if (!(r = (rpipe *) malloc(sizeof(struct structRPIPE)))) { strcpy(RunError, _("Insufficient memory (rpipeOpen)")); return NULL; } r->active = 0; r->pi.hProcess = NULL; r->thread = NULL; res = CreatePipe(&hReadPipe, &hWritePipe, NULL, 0); if (res == FALSE) { rpipeClose(r); strcpy(RunError, "CreatePipe failed"); return NULL; } if(io == 1) { /* pipe for R to write to */ hTHIS = GetCurrentProcess(); r->read = hReadPipe; DuplicateHandle(hTHIS, hWritePipe, hTHIS, &r->write, 0, FALSE, DUPLICATE_SAME_ACCESS); CloseHandle(hWritePipe); CloseHandle(hTHIS); /* This sends stdout and stderr to NUL: */ pcreate(cmd, enc, 1, visible, r->read, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, &(r->pi)); r->active = 1; if (!r->pi.hProcess) return NULL; else return r; } /* pipe for R to read from */ hTHIS = GetCurrentProcess(); r->write = hWritePipe; DuplicateHandle(hTHIS, hReadPipe, hTHIS, &r->read, 0, FALSE, DUPLICATE_SAME_ACCESS); CloseHandle(hReadPipe); CloseHandle(hTHIS); hIN = getInputHandle(finput); /* a file or (usually NUL:) */ if (hIN && finput && finput[0]) close1 = 1; if ((io == 0 || io == 3)) hOUT = r->write; else { if (fout && fout[0]) close2 = 1; hOUT = getOutputHandle(fout, 0); } if (io >= 2) hERR = r->write; else { if (ferr && ferr[0]) close3 = 1; hERR = getOutputHandle(ferr, 1); } pcreate(cmd, enc, 0, visible, hIN, hOUT, hERR, &(r->pi)); if (close1) CloseHandle(hIN); if (close2) CloseHandle(hOUT); if (close3) CloseHandle(hERR); r->active = 1; if (!r->pi.hProcess) return NULL; if (!(r->thread = CreateThread(NULL, 0, threadedwait, r, 0, &id))) { rpipeClose(r); strcpy(RunError, "CreateThread failed"); return NULL; } return r; }
void xscreate(struct sem *s) { assert((s->fid = pcreate(2)) >= 0); }
/** Reads the command received and excecutes the corrisponding process sending it it's arguments and also responds to special characters.*/ int process_input(const char * input, int tty_number) { int piped = 0; char file_call_buffer[1024]; char stdout_call_buffer[128]; char stdin_call_buffer[128]; int stdout_file = 0; int stdin_file = 0; int read_ptr = 0; int stdin = 0; int stdout = 0; do { int i = 0; int write_i = 0; for(; i < 128; ++i) { stdout_call_buffer[i] = 0; stdin_call_buffer[i] = 0; } for(i = 0; i < 1024; ++i) { file_call_buffer[i] = 0; } i = read_ptr; char * file_call; int valid_input = 0; char * current_buffer = file_call_buffer; for(; input[i] && input[i] != '|'; ++i) { if(strlen(current_buffer) > 0 ) { if(input[i] == '>') { current_buffer = stdout_call_buffer; valid_input = 0; write_i = 0; stdout_file = 1; continue; } else if (input[i] == '<') { current_buffer = stdin_call_buffer; valid_input = 0; write_i = 0; stdin_file = 1; continue; } } else { if (input[i] == '>' || input[i] == '<') { return 0; } } valid_input = input[i] != ' '; if(!(write_i == 0 && !valid_input)) { current_buffer[write_i++] = input[i]; } } if(i == 0 && input[i] == '|') { return 0; } piped = (input[i] == '|') && !stdin_file && !stdout_file; i++; if(piped) { read_ptr = i; } int n = 0; char** strs = split_string(file_call_buffer, ' ', &n); int pid = pcreate(strs[0], n, strs); if(pid != -1) { if(stdout_file) { int _stdout = open(stdout_call_buffer, O_WR | O_NEW); if(_stdout >= 0) { pdup2(pid, _stdout, STDOUT); } else { printf("TTY: file %s could not be opened for output, invalid perms?\n", stdout_call_buffer); } } if(stdin_file) { int _stdin = open(stdin_call_buffer, O_RD); if(_stdin >= 0) { pdup2(pid, _stdin, STDIN); } else { printf("TTY: file %s could not be opened for input, invalid perms?\n", stdin_call_buffer); } } // For pipe if(stdin) { pdup2(pid, stdin, STDIN); } char cad[20]; if(piped) { cad[0] = '.'; itoa(pid, cad + 1); stdout = mkfifo(cad, 0600); pdup2(pid,stdout,STDOUT); stdin = stdout; stdout = 0; } prun(pid); if(!string_ends_with(file_call_buffer, '&') && !piped) { waitpid(pid); rm(cad); } } } while(piped); return 1; }
int main(int argc, char **argv) { pid_t child_pid; /* child process id */ pthread_t monitor_thread; /* monitor thread */ char *args[CMD_ARGS_MAX+1], buffer_cmd[LINE_COMMAND_MAX]; /* cmd related */ char buffer_log[LINE_LOGFILE_MAX]; /* log related */ if((g_lst_children = lst_new()) == NULL) { if(fprintf(stderr, "lst_new: couldn't create list\n") < 0) handle_error("fprintf"); exit(EXIT_FAILURE); } g_log_file = f_open(PATH_LOGFILE_STR, "a+"); while(!feof(g_log_file)) { f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* NULL or iteracao # */ if(feof(g_log_file)) break; if(sscanf(buffer_log, "%*s %d", &g_iterations) != 1) handle_error("sscanf"); f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* PID: # time: # s */ f_gets(buffer_log, LINE_LOGFILE_MAX, g_log_file); /* total time: # s */ if(sscanf(buffer_log, "%*[^0-9] %d", &g_total_time) != 1) handle_error("sscanf"); } ++g_iterations; cond_init(&g_child_cv); cond_init(&g_monitoring_cv); mutex_init(&g_mutex); pcreate(&monitor_thread, process_monitor, NULL); while(true) { int numargs = readLineArguments(args, CMD_ARGS_MAX + 1, buffer_cmd, LINE_COMMAND_MAX); if(args[0] == NULL) continue; if(numargs < 0 || (numargs > 0 && !strcmp(args[0], COMMAND_EXIT_STR))) { mutex_lock(&g_mutex); g_monitoring = false; cond_signal(&g_monitoring_cv); mutex_unlock(&g_mutex); pjoin(monitor_thread); lst_print(g_lst_children); destroySharedResources(); f_close(g_log_file); return EXIT_SUCCESS; } else { FILE *fp; /* To check file existance */ if ((fp = fopen(args[0], "r")) == NULL) perror(args[0]); else { f_close(fp); mutex_lock(&g_mutex); while(g_num_children == MAXPAR) cond_wait(&g_child_cv, &g_mutex); mutex_unlock(&g_mutex); if((child_pid = fork()) < 0) perror("fork"); else if(child_pid == 0) { execv(args[0],args); destroySharedResources(); handle_error("execv"); } else { mutex_lock(&g_mutex); if(insert_new_process(g_lst_children, child_pid, time(NULL)) != 0) { fprintf(stderr, "insert_new_process: failed to insert new process\n"); exit(EXIT_FAILURE); } ++g_num_children; cond_signal(&g_monitoring_cv); mutex_unlock(&g_mutex); } } } } }