#include <sys/types.h> #include <unistd.h> pid_t fork(void); |
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int value = fork(); /* new process */ printf("In main: value = %d\n", value); return 0; } |
In main: value = 17219 In main: value = 0 |
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int value1, value2; value1 = fork(); /* new process */ value2 = fork(); printf("In main: value = %d\n", value); return 0; } |
In main: value = 26164 In main: value = 0 In main: value = 0 In main: value = 0 In main: value = 26165 In main: value = 26166 In main: value = 26164 In main: value = 0 |
#include <stdio.h> #include <time.h> int fib(int); int fork(void); void sleep(unsigned); int main(void) { int begin = time(NULL), i; if (fork() == 0) /* child */ for (i = 0; i < 30; ++i) printf("fib(%2d) = %d\n", i, fib(i)); else /* parent */ for (i = 0; i < 30; ++i) { sleep(2); printf("elapsed time = %d\n", time(NULL) - begin); } return 0; } int fib(int n) { if (n <= 1) return n; else return (fib(n - 1) + fib(n - 2)); } |
#include <unistd.h> extern char ** environ; int execl( const char * path, const char * arg, ...); int execlp( const char * file, const char * arg, ...); int execle( const char * path, const char * arg , ..., char * const envp[]); int execv( const char * path, char * const argv[]); int execvp( const char * file, char * const argv[]); |
/* file pgm-exec.c */ #include <stdio.h> #include <unistd.h> int main(void) { int choice = 0; printf("%s\n%s\n%s", "The parent process will be overlaid.", "You have a choice.", "Input 1 or 2: "); scanf("%d", &choice); putchar('\n'); if (choice == 1) execl("pgm1", "pgm1", "a", "b", "c", 0); if (choice == 2) execl("pgm2", "pgm2", "1", "2", "3", "go", 0); printf("ERROR: You did not input 1 or 2.\n"); return 0; } |
#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <string.h> #define MAXLINE 100 int main(void) { char buf[MAXLINE]; pid_t pid; int status; printf("%% "); /* print prompt (printf requires %% to print %) */ while (fgets(buf, MAXLINE, stdin) != NULL) { buf[strlen(buf) - 1] = 0; /* replace newline with null */ if ( (pid = fork()) < 0) fprintf(stderr,"fork error"); else if (pid == 0) { /* child */ execlp(buf, buf, (char *) 0); fprintf(stderr,"couldn't execute: %s\n", buf); return 127; } /* parent */ if ( (pid = waitpid(pid, &status, 0)) < 0) fprintf(stderr,"waitpid error"); printf("%% "); } return 0; } |
#include <unistd.h> int pipe(int filedes[2]); |
#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <ctype.h> #define MAXLINE 100 int main(void) { int n, fd[2]; pid_t pid; char line[MAXLINE]; int i; char c; if (pipe(fd) < 0) fprintf(stderr, "pipe error \n"); if ((pid = fork()) < 0) fprintf(stderr, "fork error \n"); else if (pid > 0) { /* parent */ close(fd[0]); write(fd[1], "\nhello world\n", 13); } else { /* child */ close(fd[1]); n = read(fd[0],line,MAXLINE); for (i = 0; i < n; i++) { c = toupper(line[i]); write(STDOUT_FILENO, &c, 1); } } //fprintf(stderr,"exit from pid = %d\n", getpid()); return 0; } |
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #define N 3 int add_vector(int v[]); void error_exit(char * s); int main(void) { int a[N][N] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}; int i, row_sum, sum = 0; int pd[2]; /* pipe descriptors */ if (pipe(pd) == -1) /* create a pipe */ error_exit("pipe() failed"); for (i = 0; i < N; ++i) if (fork() == 0) { /* child process */ row_sum = add_vector(a[i]); if (write(pd[1], &row_sum, sizeof(int)) == -1) error_exit("write() failed"); return 0; /* return from child */ } for (i = 0; i < N; ++i) { if (read(pd[0], &row_sum, sizeof(int)) == -1) error_exit("read() failed"); sum += row_sum; } printf("Sum of the array = %d\n", sum); return 0; } int add_vector(int v[]) { int i, vector_sum = 0; for (i = 0; i < N; ++i) vector_sum += v[i]; return vector_sum; } void error_exit(char *s) { fprintf(stderr, "\nERROR: %s - bye!\n", s); exit(1); } |
#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> /* environment variable, or default */ #define PAGER "${PAGER:-more}" #define MAXLINE 100 void err_sys(const char * str); int main(int argc, char * argv[]) { char line[MAXLINE]; FILE * fpin, * fpout; if (argc != 2) { fprintf(stderr,"usage: a.out filename\n"); exit(127); } if ((fpin = fopen(argv[1], "r")) == NULL) err_sys("can't open input file for reading"); if ((fpout = popen(PAGER, "w")) == NULL) err_sys("popen error"); /* copy argv[1] to pager */ while (fgets(line, MAXLINE, fpin) != NULL) { if (fputs(line, fpout) == EOF) err_sys("fputs error to pipe"); } if (ferror(fpin)) err_sys("fgets error"); if (pclose(fpout) == -1) err_sys("pclose error"); exit(0); } /* print error message and quit */ void err_sys(const char *str) { fprintf(stderr," %s\n",str); exit(1); } |
void (*signal(int signum, void (*sighandler)(int)))(int); |
#include <stdio.h> #include <signal.h> #include <stdlib.h> #define MAXSTRING 100 void cntrl_c_handler(int sig); int fib(int n); int main(void) { int i; signal(SIGINT, cntrl_c_handler); for (i = 0; i < 46; ++i) printf("fib(%2d) = %d\n", i, fib(i)); return 0; } void cntrl_c_handler(int sig) { char answer[MAXSTRING]; printf("\n\n%s%d\n\n%s", "Interrupt received! Signal = ", sig, "Do you wish to continue or quit? "); scanf("%s", answer); if (*answer == 'c') signal(SIGINT, cntrl_c_handler); else exit(1); } int fib(int n) { if (n <= 1) return n; else return (fib(n - 1) + fib(n - 2)); } |
// using a signal handler // segmentation violation---trying to access a place that is not ours // in bits/signum.h // #define SIGSEGV 11 /* Segmentation violation (ANSI). */ #include <stdio.h> #include <signal.h> // this file calls bits/signum.h #include <stdlib.h> #define MAXSTRING 100 void segv_handler(int sig); int main(int argc, char *argv[], char *env[]) { int i; signal(SIGSEGV, segv_handler); fprintf(stderr," writing the contents of argv[-20000] %s\n",argv[-20000]); fprintf(stderr," next instruction\n"); } void segv_handler(int sig) { char answer[MAXSTRING]; printf("\n\n%s%d\n\n%s", "Interrupt received! Signal = ", sig, "Do you wish to (c)ontinue or (q)uit? "); scanf("%s", answer); if (*answer == 'c') signal(SIGSEGV, segv_handler); else exit(1); } |
#include <stdio.hgt; #define MARKER ">> " int main(int argc, char * argv[]) { int val; printf("\n"); printf(MARKER " Input an integer: "); scanf("%d", &val); printf(MARKER "Value being returned: %d\n\n", val); return val; } |
#!/usr/bin/csh ## ## Experiment with the status. ## echo --- echo At the top: status = $status try_me echo --- echo After try_me: status = $status try_me set val = $status echo --- echo After try_me again: val = $val echo "" echo --- echo To exit from the while loop, input 0. while ($val) try_me set val = $status echo In the loop: val = $val end |
--- At the top: status = 0 >> Input an integer: 3 >> Value being returned: 3 --- After try_me: status = 0 >> Input an integer: 7 >> Value being returned: 7 --- After try_me again: val = 7 |