- 论坛徽章:
- 0
|
回复 3# timespace
感谢指导,已经解决
可是boss的要求和这个不符,需要支持任意二进制文件,所以我不能用fgets,改用read。
修改后的问题是,父进程读不到子进程的输出。
enc_dec_test.c- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<unistd.h>
- #include <fcntl.h>
- /*****************MACRO*******************/
- #define BUFFER_SIZE 10
- #define USE_PIPE
- /******************variable******************/
- /******************function******************/
- void dumpMem(char *name, char *pdata, int len)
- {
- int i;
- fprintf(stderr, "%s %x:", name, (int)pdata);
- for (i = 0; i < len; i++)
- {
- if (0 == (i % 16))
- fprintf(stderr, "\n");
- fprintf(stderr, "0x%02x ", pdata[i]);
- }
- fprintf(stderr, "\n");
- }
- int main(int argc, char *argv[])
- {
- char buffer[BUFFER_SIZE];
- char buffer_enc[BUFFER_SIZE];
- char buffer_dec[BUFFER_SIZE];
- int read_cnt, total;
- int i, len;
- int child = 0;
- pid_t proc_enc, proc_dec;
- int pipe_in_enc[2];
- int pipe_out_enc[2];
- int pipe_in_dec[2];
- int pipe_out_dec[2];
- char *arg_enc[] = {"./enc_dec_test_sub", "enc", NULL};
- char *arg_dec[] = {"./enc_dec_test_sub", "dec", NULL};
- /* create enc pipe */
- pipe(pipe_in_enc);
- pipe(pipe_out_enc);
- if(!(proc_enc = fork()))
- {
- child = 1;
- close(pipe_in_enc[0]); // 1رո¸½ø3ìμĶá1üμàμÄ×ó½ø3ì¶á¶Ë
- close(pipe_out_enc[1]); // 1رո¸½ø3ìμÄD′1üμàμÄ×ó½ø3ìD′¶Ë
- #ifdef USE_PIPE
- close(STDOUT_FILENO);
- dup2(pipe_in_enc[1], STDOUT_FILENO); // ¸′ÖƸ¸½ø3ìμĶá1üμàμ½×ó½ø3ìμıê×¼êä3ö
- // close(pipe_in_enc[1]); // 1رÕòѸ′ÖÆμĶá1üμà
- close(STDIN_FILENO);
- dup2(pipe_out_enc[0], STDIN_FILENO); // ¸′ÖƸ¸½ø3ìμÄD′1üμàμ½×ó½ø3ìμıê×¼êäèë
- // close(pipe_out_enc[0]); // 1رÕòѸ′ÖÆμÄD′1üμà
- #endif
- execve(arg_enc[0], arg_enc, NULL);
- printf("enc pid is %d\n", getpid());
- }else { // ¸¸½ø3ì
- close(pipe_in_enc[1]); // 1رնá1üμàμÄD′¶Ë
- close(pipe_out_enc[0]); // 1رÕD′1üμàμĶá¶Ë
- }
- #if 1
- /* create dec pipe */
- pipe(pipe_in_dec);
- // pipe(pipe_out_dec);
- if(!(proc_dec = fork()))
- {
- child = 1;
- // close(pipe_in_enc[0]);
- close(pipe_in_enc[1]);
- close(pipe_out_enc[0]);
- close(pipe_out_enc[1]);
-
- close(pipe_in_dec[0]); // 1رո¸½ø3ìμĶá1üμàμÄ×ó½ø3ì¶á¶Ë
- // close(pipe_out_dec[1]); // 1رո¸½ø3ìμÄD′1üμàμÄ×ó½ø3ìD′¶Ë
- #ifdef USE_PIPE
- close(STDOUT_FILENO);
- dup2(pipe_in_dec[1], STDOUT_FILENO); // ¸′ÖƸ¸½ø3ìμĶá1üμàμ½×ó½ø3ìμıê×¼êä3ö
- // close(pipe_in_dec[1]); // 1رÕòѸ′ÖÆμĶá1üμà
- close(STDIN_FILENO);
- dup2(pipe_in_enc[0], STDIN_FILENO); // ¸′ÖƸ¸½ø3ìμÄD′1üμàμ½×ó½ø3ìμıê×¼êäèë
- // close(pipe_in_enc[0]); // 1رÕòѸ′ÖÆμÄD′1üμà
- #endif
- execve(arg_dec[0], arg_dec, NULL);
- }else { // ¸¸½ø3ì
- close(pipe_in_dec[1]); // 1رնá1üμàμÄD′¶Ë
- // close(pipe_out_dec[0]); // 1رÕD′1üμàμĶá¶Ë
- }
- #endif
- if (!child)
- {
- total = 0;
- do
- {
- read_cnt = read(STDIN_FILENO, buffer,BUFFER_SIZE);
- if (read_cnt)
- {
- write(pipe_out_enc[1], buffer, read_cnt);
- // read(pipe_in_enc[0], buffer_enc, read_cnt);
- // printf("get %d bytes\n", total);
- // write(1, buffer, read_cnt);
- #if 1
- // fprintf(stdout, "get %d bytes:%s from enc\n", strlen(buffer_enc), buffer_enc);
- // write(pipe_out_dec[1], buffer_enc, read_cnt);
- memset(buffer_dec, 0x5a, BUFFER_SIZE);
- read(pipe_in_dec[0], buffer_dec, read_cnt);
- total+=read_cnt;
- printf("parent get %d bytes\n", total);
- if (memcmp(buffer, buffer_dec, read_cnt))
- {
- printf("different data\n");
- dumpMem("parent orign", buffer, read_cnt);
- dumpMem("parent dec", buffer_dec, read_cnt);
- }
- #endif
- }
- }while(read_cnt);
- // printf("loop over\n");
- //enc
- close(pipe_out_enc[1]); // 1رÕD′1üμà
- close(pipe_in_enc[0]); // 1رնá1üμà
- //dec
- close(pipe_out_enc[1]); // 1رÕD′1üμà
- close(pipe_in_enc[0]); // 1رնá1üμà
- /* ê1óÃwaitÏμáDoˉêyμè′y×ó½ø3ìíË3ö2¢è¡μÃíË3ö′úÂë */
- wait4(proc_enc, NULL, 0, NULL);
- wait4(proc_dec, NULL, 0, NULL);
- }
- return 0;
- }
复制代码 enc_dec_test_sub.c- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- /*****************MACRO*******************/
- #define BUFFER_SIZE 10
- #define XOR 0X5A
- /******************variable******************/
- /******************function******************/
- void dumpMem(char *name, char *pdata, int len)
- {
- int i;
- fprintf(stderr, "%s %x:", name, (int)pdata);
- for (i = 0; i < len; i++)
- {
- if (0 == (i % 16))
- fprintf(stderr, "\n");
- fprintf(stderr, "0x%02x ", pdata[i]);
- }
- fprintf(stderr, "\n");
- }
- int main(int argc, char *argv[])
- {
- char buffer[BUFFER_SIZE];
- int cnt = 0;
- int i, total;
-
- /* process arguments */
- if (argc < 2)
- {
- fprintf(stderr, "usage: %s sub_id\n", argv[0]);
- return -1;
- }
- total = 0;
- do
- {
- cnt = read(0, buffer, BUFFER_SIZE);
- if (cnt)
- {
- total += cnt;
- // fprintf(stderr, "%s got %d bytes\n", argv[1], total);
- dumpMem(argv[1], buffer, cnt);
- // fprintf(stdout, "%s", buffer);
- for (i = 0; i < cnt; i++)
- {
- buffer[i]^=XOR;
- }
- // dumpMem(argv[1], buffer, cnt);
- write(1, buffer, cnt);
- // fflush(stdout);
- }
- }while(cnt);
- exit (0);
- }
复制代码 makefile- object:enc_dec.c
- gcc -g -o enc_dec -lpthread enc_dec.c
- gcc -g -o enc_dec_test -lpthread enc_dec_test.c
- gcc -g -o enc_dec_test_sub -lpthread enc_dec_test_sub.c
复制代码 运行结果- [root@linux enc_dec]# ./enc_dec_test
- a
- parent get 2 bytes
- different data
- parent orign bfbadb26:
- 0x61 0x0a
- parent dec bfbadb12:
- 0x00 0x00
- enc bfcdc5ce:
- 0x61 0x0a
- enc bfcdc5ce:
- 0x3b 0x50
- dec bfacad5e:
- 0x3b 0x50
- dec bfacad5e:
- 0x61 0x0a
复制代码 |
|