[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
Решения
lcuc_pipe.c:
1 #include <sys/types.h>
2 #include <osfcn.h>
3 #include <ctype.h>
4
5 main(int argc, char **argv)
6 {
7 int pid;
8 int fd[2];
9 static char *lines[3] ={ "Here are 3 lines of text.\n",
10 "You will see all lower case\n",
11 "made to upper!!\n"
12 };
13 char input[1000];
14 int i;
15 int rtn;
16
17 if (pipe(fd) == -1) {
18 perror(argv[0]);
19 exit(1);
20 }
21 if ((pid = fork()) > 0) {/* parent */
22 close(fd[0]);
23 for(i=0; i<3; i++)
24 write(fd[1], lines[i], strlen(lines[i]));
25 close(fd[1]);
26 }
27 else if (pid == 0) {/* child */
28 close(fd[1]);
29 while ((rtn=read(fd[0], input, 1000))> 0) {
30 for(i=0; i<rtn; i++)
31 if(islower(input[i]))
32 input[i] = toupper(input[i]);
33 write(1, input, rtn);
34 }
35 close(fd[0]);
36 }
37 else {/* cannot fork */
38 perror(argv[0]);
39 exit(2);
40 }
41
42 exit(0);
43 }
Связь с использованием функций стандартной библиотеки
lcuc_popen.c
1 #include <stdio.h>
2
3 main()
4 {
5 FILE *fptr;
6 char *lines[3] ={ "Here are 3 lines of text.\n",
7 "You will see all lower case\n",
8 "made to upper!!\n"
9 };
10 int i;
11
12 fptr = popen("lcuc", "w");
13
14 for(i=0; i<3; i++)
15 fputs(lines[i], fptr);
16
17 pclose(fptr);
18 }
lcuc.c
1 #include <ctype.h>
2 #include <osfcn.h>
3
4 main()
5 {
6 char input[1000];
7 int i;
8 int rtn;
9
10 while ((rtn=read(0, input, 1000))> 0) {
11 for(i=0; i<rtn; i++)
12 if(islower(input[i]))
13 input[i] = toupper(input[i]);
14 write(1, input, rtn);
15 }
16 exit(0);
17 }
Подсчет пустых строк в файле
blanklines.c:
1 #include <stdio.h>
2 #define TRUE 1
3 #define FALSE 0
4
5 main(int argc, char **argv)
6 {
7 FILE *fpin, *fpout;
8 char line[BUFSIZ];
9
10 if ((fpin = fopen(argv[1], "r")) ==
11 (FILE *) NULL) {
12 perror(argv[0]);
13 exit(1);
14 }
15
16 fpout = popen("wc -l", "w");
17 while (fgets(line, BUFSIZ, fpin) != (char *)NULL)
18 if (line[0] == '\n')
19 fputs(line, fpout);
20
21 fclose(fpin);
22 pclose(fpout);
23 }
Генератор случайных чисел - сортированный
randlist.c
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <libgen.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 main()
8 {
9 int i;
10 FILE *ptrs[2];
11 char num[4];
12
13 srand(time(0));
14 p2open("/bin/sort", ptrs);
15
16 for( i = 1; i <= 100; i++) {
17 sprintf(num, "%02d\n", rand() % 100);
18 fputs(num, ptrs[0]);
19 }
20 fclose(ptrs[0]);
21
22 i = 1;
23 while(fgets(num,4,ptrs[1]) != NULL) {
24 num[2] = '\0';
25 printf(" %s", num);
26 if ( (i++ % 10) == 0)
27 putchar('\n');
28 }
29 putchar('\n');
30 }
Интерпретатор shell - Продолжение
shell5.c:
1 /* PRIMITIVE JOB CONTROL SHELL */
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <signal.h>
5 #include <fcntl.h>
6 #include <setjmp.h>
7 #include <wait.h>
8 #include "shell.h"
9 #include <sys/procset.h>
10 #include <siginfo.h>
11
12 char *infile, *outfile, *appfile;
13 struct command cmds[MAXCMDS];
14 char bkgrnd;
15 jmp_buf env;
16 pid_t cpid;
17 extern int errno;
18
19 main(int argc, char **argv)
20 {
21 char line[1024]; /* allow large command lines */
22 int ncmds, i;
23 char prompt[50]; /* shell prompt */
24 int ifd, ofd; /* for i/o file redirection */
25 void sigint(int);
26 siginfo_t siginfo_ds;
27 struct { /* NEW - COMMAND PIPES */
28 int pipfd[2]; /* file descriptors for pipe(2) call */
29 } pipes[MAXCMDS - 1]; /* enough pipes for max number of
cmds. */
30 pid_t pipe_job_id; /* PID of "source command" */
31 /* To be pgid of all cmds in pipeline
*/
32
33 sigset(SIGINT, sigint);
34 sigset(SIGTSTP, sigint);
35 sprintf(prompt,"[%s] ", argv[0]);
36
37 setjmp(env);
38 while(promptline(prompt,line,sizeof(line)) > 0) { /*
until eof */
39 if ((ncmds = parseline(line)) <= 0)
40 continue; /* read next line */
41 #ifdef DEBUG
42 {
43 int i, j;
44 for (i = 0; i < ncmds; i++) {
45 for (j = 0; cmds[i].cmdargs[j] != (char *) NULL; j++)
46 fprintf(stderr, "cmd[%d].cmdargs[%d] = %s\n",
47 i, j, cmds[i].cmdargs[j]);
48 fprintf(stderr, "cmds[%d].cmdflag = %o\n", i,
cmds[i].cmdflag);
49 }
50 }
51 #endif
52
53 for (i = 0; i < ncmds; i++) {
54 if (cmds[i].cmdflag & OUTPIP) /* NEW - Creat pipes */
55 pipe(pipes[i].pipfd);
56 if (!(cpid = fork())) {
57 if(cmds[i].cmdflag & INPIP) { /* Make each "job"
*/
58 if(setpgid(0,pipe_job_id) < 0) {/* a separate
process*/
59 perror("setpgid - pipe reader");
60 }
61 } /*group - A pipeline is*/
62 else /*one job */
63 if(setpgid(0, 0) < 0)
64 perror("setpgid - job leader");
65
66
67 if (bkgrnd) {
68 signal(SIGINT, SIG_IGN);
69 signal(SIGQUIT, SIG_IGN);
70 }
71 else {
72 signal(SIGTTOU, SIG_IGN);
73 if(!(cmds[i].cmdflag & INPIP))
74 if(tcsetpgrp(2, getpid()) < 0)
75 perror("tcsetpgrp - child");
76 signal(SIGTTOU, SIG_DFL);
77 }
78
79 /* NEW - COMMAND PIPES */
80 /* output to pipe */
81 if(cmds[i].cmdflag & OUTPIP)
82 close(1);
83 dup(pipes[i].pipfd[1]);
84 close(pipes[i].pipfd[1]);
85
86 /* input from pipe */
87 if(cmds[i].cmdflag & INPIP)
88 close(0);
89 dup(pipes[i - 1].pipfd[0]);
90 close(pipes[i - 1].pipfd[0]);
91
92 /* input redirection */
93 if ((infile != (char *) NULL) && i == 0) {
94 /* check for i == 0 because only */
95 /* first command can have input */
96 /* redirected */
97 if ((ifd = open(infile, O_RDONLY)) < 0) {
98 fprintf(stderr,
99 "%s: %s: cannot open\n",
100 argv[0], infile);
101 exit(2);
102 }
103 close(0); /* close standard input */
104 dup(ifd); /* ifd is now stdin */
105 close(ifd);
106 }
107
108 /* output redirection */
109 if ((outfile != (char *) NULL) && i == ncmds-1) {
110 /* create new file */
111 if ((ofd = open(outfile,
112 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
113 fprintf(stderr,
114 "%s: %s: cannot create\n",
115 argv[0], outfile);
116 exit(2);
117 }
118 close(1);
119 dup(ofd);
120 close(ofd);
121 } else if ((appfile != (char *) NULL) &&
122 i == ncmds-1) {
123 if ((ofd = open(appfile,
124 O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) {
125 fprintf(stderr,
126 "%s: %s: cannot open\n",
127 argv[0], appfile);
128 exit(2);
129 }
130 close(1);
131 dup(ofd);
132 close(ofd);
133 }
134
135 execvp(cmds[i].cmdargs[0], cmds[i].cmdargs);
136 fprintf(stderr, "%s: not found\n",
137 cmds[i].cmdargs[0]);
138 exit(1); /* make sure child exits */
139 } /* close child block */
140
141 /* NEW - Parent must close both ends of pipe */
142 if (cmds[i].cmdflag & OUTPIP)
143 close(pipes[i].pipfd[1]);
144 if (cmds[i].cmdflag & INPIP)
145 close(pipes[i - 1].pipfd[0]);
146
147 if(i == 0 && (cmds[i].cmdflag & OUTPIP)) /* Record pid
of */
148 pipe_job_id = cpid; /*"source" command in
pipeline*/
149
150 if (!bkgrnd) {
151 if(!(cmds[i].cmdflag & OUTPIP)) /*NEW - ONLY WAIT ON
SINK*/
152 waitid(P_PID, cpid, &siginfo_ds, WEXITED |
WSTOPPED);
153
154 signal(SIGTTOU, SIG_IGN);
155 if(tcsetpgrp(2,getpgrp()) < 0) /* Put parent in
foreground */
156 perror("tcsetpgrp - parent");
157 signal(SIGTTOU, SIG_DFL);
158 if(siginfo_ds.si_code == CLD_STOPPED) { /* Restart
stopped */
159 fprintf(stderr, "%d", cpid); /* child which is
*/
160 sigsend(P_PID, cpid, SIGCONT); /* now in
background */
161 }
162 putchar('\n');
163 }
164 else
165 fprintf(stderr, "%d\n", cpid);
166 }
167
168 } /* close while */
169 }
170
171 void sigint(int sig)
172 {
173 putchar('\n');
174 sigrelse(sig);
175 longjmp(env, 1);
176 }
Игровая программа
gametwopipes.c:
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <osfcn.h>
5 #include <stdlib.h>
6 #define GUESSFIFO "Guessfifo"
7 #define RESPNSFIFO "Respnsfifo"
8
9 main(int argc, char argv[])
10 {
11 int number, guess;
12 int fdguess, fdresp;
13
14 if (mknod(GUESSFIFO, S_IFIFO | 0666, 0) == -1) {
15 perror("Guess");
16 exit(1);
17 }
18 if (mknod(RESPNSFIFO, S_IFIFO | 0666, 0) == -1) {
19 perror(RESPNSFIFO);
20 exit(2);
21 }
22 if ((fdguess = open(GUESSFIFO, O_RDONLY)) == -1) {
23 perror(GUESSFIFO);
24 exit(3);
25 }
26 if ((fdresp = open(RESPNSFIFO, O_WRONLY)) == -1) {
27 perror(GUESSFIFO);
28 exit(4);
29 }
30
31 number = rand() % 1000;
32
33 while(read(fdguess, (char *) &guess,sizeof(guess)) > 0) {
34 if (guess > number)
35 write(fdresp, "H", 1);
36 else if (guess < number)
37 write(fdresp, "L", 1);
38 else
39 write(fdresp, "E", 1);
40 }
41 close(fdguess);
42 close(fdresp);
43 unlink(GUESSFIFO);
44 unlink(RESPNSFIFO);
45 exit(0);
46 }
player.c:
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <osfcn.h>
5 #define GUESSFIFO "Guessfifo"
6 #define RESPNSFIFO "Respnsfifo"
7
8 main(int argc, char *argv[])
9 {
10 int higuess, loguess, guess;
11 int fdguess, fdresp;
12 char response;
13
14 if((fdguess = open(GUESSFIFO,O_WRONLY)) == -1) {
15 perror(GUESSFIFO);
16 exit(1);
17 }
18 if((fdresp = open(RESPNSFIFO,O_RDONLY)) == -1) {
19 perror(GUESSFIFO);
20 exit(2);
21 }
22
23 loguess = 0;
24 higuess = 1000;
25 guess = 500;
26 write(fdguess, (char *) &guess, sizeof(guess));
27 while (read(fdresp, &response, 1) > 0) {
28 if (response == 'H') {
29 printf("%d is too high\n", guess);
30 higuess = guess;
31 guess = (higuess + loguess)/2;
32 }
33 else if (response == 'L') {
34 printf("%d is too low\n", guess);
35 loguess = guess;
36 guess = (higuess + loguess)/2;
37 }
38 else {
39 printf("%d is correct\n", guess);
40 break;
41 }
42 write(fdguess, (char *) &guess, sizeof(guess));
43 }
44 close(fdguess);
45 close(fdresp);
46 exit(0);
47 }