process - Operating system processes in .c -
can explain me argc , argv in code , why variables parameters in main function? had in lectures example showed both variables i'm using them without knowing do.
main (argc, argv) char *argv[]; { int fd; extern int errno; if(argc < 2){ fprintf(stderr, "no file\n"); exit(1); } if((fd = creat(argv[1], 0777)) < 0){ fprintf(stderr, "cannot creat file %s\n", argv[1]); exit(1); } switch (fork()) { case -1: fprintf(stderr, "fork error\n"); exit(1); case 0: close(1); dup(fd); close(fd); execl("/bin/pwd", "pwd", null); perror("exec"); break; default: close(fd); } exit(0); }
argc
int
giving program number of arguments passed program operating system. argv
array of null-terminated strings containing actual arguments passed program.
the exact parameters operating system dependent, argv[0]
executable name including path, while argv[argc]
null
pointer. (thanks @weathervane pointing out) versions of c standard require argv[argc]
null pointer, , prior precedent hosted systems required ('freestanding' — aka embedded — systems run under different rules, now).
not programs require command-line arguments, integral part of operating system.
the c standard requires following definition if both parameters used:
int main(int argc, char *argv[]) ...
i doubt k&r c used in production code anymore.
Comments
Post a Comment