int main() { int fd = 0; int pid = 0; char buf[10] = {0}; if ((fd = open("fd_in_fork_test.c", O_RDONLY)) < 0) { failure("open failed"); } read(fd, buf, 10); printpos("Before fork", fd); if ((pid = fork()) < 0) { failure("fork failed"); } else if (0 == pid) { printpos("Child before read", fd); read(fd, buf, 10); printpos("Child after read", fd); } else { wait(NULL); printpos("Parent after wait", fd); } close(fd); return 0; }
main() { int fd; int pid; char buf[10]; if((fd=open("data",O_RDONLY))<0) { failure("open failed"); } read(fd,buf,10); printpos("Before fork",fd); if((pid=fork())<0) { failure("fork failed"); } else if(!pid) { printpos("Child before read",fd); read(fd,buf,10); printpos("child after read",fd); } else { wait(NULL); printpos("parent after wait",fd); } }
int gofor(int llen, int rlen, int turn, int forward) { encoderReset(r_encoder); encoderReset(l_encoder); int ol=0; int or=0; dstat=0; printf("going for %d\t%d degrees with dir %d\t%d\n\r",llen,rlen,turn,forward); #ifndef SIM while((abs(encoderGet(l_encoder))<llen &&abs(encoderGet(r_encoder))<rlen)){ c_status("gofor",llen); controldrive(turn,forward); simtank(<ank,encoderGet(l_encoder-ol)-ol,encoderGet(r_encoder)-or); ol=encoderGet(l_encoder); or=encoderGet(r_encoder); delay(20); } #endif dstat=1; controldrive(0,0); printf("Motion complete, expected:"); printpos(&ctank); printf("\tintegrated: "); printpos(<ank); printf("\n\r"); return encoderGet(l_encoder)>encoderGet(l_encoder) ? encoderGet(l_encoder) : encoderGet(r_encoder); }
main() { int fd; /* 文件描述符 */ int pid;/* 进程标识符 */ char buf[10]; /* 数据缓冲区 */ /* 打开文件 */ if ((fd=open("data",O_RDONLY))<0) failure("open failed"); read(fd,buf,10); /* advance file pointer */ printpos("Before fork",fd); /* fork 新进程 */ if ((pid=fork())<0) failure("fork failed"); else if (!pid) /* 子进程 */ printpos("Child before read",fd); read(fd,buf,10); printpos("child after read",fd); else {
void errorposf(SrcPos *p, char *fmt, ...) { va_list va; va_start(va, fmt); vfprintf(stderr, fmt, va); va_end(va); fprintf(stderr, " at %s:%d:%d\n", p->file, p->line, p->col); printpos(p); exit(1); }
int main(int argc, char **argv){ int i; char *p; GC_INIT(); ::cgc1::cgc_root_t hash_bucket_root(hash_buckets); hash_buckets=reinterpret_cast<node*>(::cgc1::cgc_malloc(sizeof(node)*7313)); progname = BaseName(argv[0]); yyinit(); for (p=argv[0]; *p; p++) if (*p=='/') progname = p+1; for (i=1; i<argc; i++) { if (EQUAL == strcmp(argv[i],"--help")) { usage(); exit(0); } if (EQUAL == strcmp(argv[i],"-dep")) { stop_after_dep = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-cxx")) { do_cxx = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-noline")) { noline = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-pthreadlocal")) { pthreadThreadLocal=TRUE; compilerThreadLocal=FALSE; continue; } if (EQUAL == strcmp(argv[i],"-typecodes")) { printtypecodes(); return 0; } if (EQUAL == strcmp(argv[i],"-noarraychks")) { arraychks = FALSE; continue; } if (EQUAL == strcmp(argv[i],"-nocasechks")) { casechks = FALSE; continue; } if (EQUAL == strcmp(argv[i],"-nomacros")) { nomacros = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-O")) { arraychks = FALSE; casechks = FALSE; continue; } if (EQUAL == strcmp(argv[i],"-tabwidth")) { i++; if (i < argc) tabwidth = atoi(argv[i]); continue; } if (EQUAL == strcmp(argv[i],"-yydebug")) { yydebug = 1; continue; } if (EQUAL == strcmp(argv[i],"-debug")) { debug = TRUE; continue; } if (EQUAL == strcmp(argv[i],"-v")) { puts(Version); puts(Copyright); continue; } if ('-' == argv[i][0] && 'I' == argv[i][1]) { if (argv[i][2] == 0) { error("-I option: missing directory"); usage(); exit(1); } char buf[256]; strcpy(buf,sigpath); strcat(buf,":"); strcat(buf,argv[i]+2); sigpath = strperm(buf); continue; } if ('-' == argv[i][0]) { error("unrecognized option %s\n",argv[i]); usage(); exit(1); } if ( EQUAL == strcmp(".d",tail(argv[i])) || EQUAL == strcmp(".dd",tail(argv[i])) ) { node f; do_this_cxx = do_cxx || EQUAL == strcmp(".dd",tail(argv[i])); global_scope = new(struct SCOPE); readsetup(global_scope); targetname = newsuffixbase(argv[i],""); f = readfile(argv[i]); if (debug) { char *n = newsuffixbase(argv[i],".out"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } put("After parsing:\n"); pp(f); fflush(stdout); } outfilename = newsuffixbase(argv[i], do_this_cxx ? "-tmp.cc" : "-tmp.c"); { char *n = newsuffixbase(argv[i],".dep.tmp"); dependfile = fopen(n,"w"); if (dependfile == NULL) fatal("can't open file %s",n); } f = chkprogram(f); if (debug) { char *n = newsuffixbase(argv[i],".log"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } pprintl(f); } { node t = global_scope->signature; char *n = newsuffixbase(argv[i],".sig.tmp"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } printf("-- generated by %s\n\n",progname); while (t != NULL) { dprint(CAR(t)); put(";\n"); t = CDR(t); } } if (stop_after_dep) quit(); checkfordeferredsymbols(); if (debug) { char *n = newsuffixbase(argv[i],".sym"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } printsymboltable(); printtypelist(); printstringlist(); } if (n_errors > 0) { quit(); } if (TRUE) { char *n = newsuffixbase(argv[i],"-exports.h.tmp"); if (NULL == freopen(n,"w", stdout)) { fatal("can't open file %s",n); } printf("#ifndef %s_included\n",targetname); printf("#define %s_included\n",targetname); declarationsstrings = reverse(declarationsstrings); while (declarationsstrings) { node s = unpos(car(declarationsstrings)); assert(isstrconst(s)); put_unescape(s->body.string_const.characters); put("\n"); declarationsstrings = cdr(declarationsstrings); } put(declarations_header); /* printtypecodes(); */ cprinttypes(); put(declarations_trailer); put("#endif\n"); } if (TRUE) { if (NULL == freopen(outfilename,"w", stdout)) { fatal("can't open file %s",outfilename); } printf("#include \"%s\"\n",newsuffixbase(argv[i],"-exports.h")); put(code_header); headerstrings = reverse(headerstrings); while (headerstrings) { locn(car(headerstrings)); printpos(); node s = unpos(car(headerstrings)); assert(isstrconst(s)); put_unescape(s->body.string_const.characters); put("\n"); locn(NULL); headerstrings = cdr(headerstrings); } cprintsemi(f); } } else { fprintf(stderr,"unknown file type %s\n",argv[i]); usage(); exit(1); } } quit(); return 0; }