~cpp /** pipe를 통한 간단한 프로세스 통신의 일예 **/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #define BUFSIZE 30 int main(int argc, char** argv) { int fd[2]; char buffer[BUFSIZE]; pid_t pid; int state; state = pipe(fd); if (state == -1) { puts("pipe() error"); exit(1); } pid = fork(); if(pid == -1) { puts("fork() error"); exit(1); } else if (pid == 0) { write(fd[1], "Good\n", 6); } else { read(fd[0], buffer, BUFSIZE); puts(buffer); } return 0; }gdb 를 이용하기 위해서는 gcc 옵션에 -g를 주고 컴파일 해야함.
gcc pipe1.c -o pipe1 -g
GNU gdb 6.4-debian Copyright 2005 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1". (gdb)
(gdb) list 6 #define BUFSIZE 30 7 8 int main(int argc, char** argv) 9 { 10 int fd[2]; 11 char buffer[BUFSIZE]; 12 pid_t pid; 13 int state; 14 15 state = pipe(fd); (gdb)
(gdb) run 15 Starting program: /home/staff/sapius/source/ipc_test/pipe1 15 Good Program exited normally. (gdb) list 16 if (state == -1) { 17 puts("pipe() error"); 18 exit(1); 19 } 20 21 pid = fork(); 22 if(pid == -1) { 23 puts("fork() error"); 24 exit(1); 25 } (gdb)
(gdb) break 21 Breakpoint 1 at 0x80484cc: file pipe1.c, line 21. (gdb) run Starting program: /home/staff/sapius/source/ipc_test/pipe1 Breakpoint 1, main (argc=1, argv=0xbfe98394) at pipe1.c:21 21 pid = fork(); (gdb) print state $1 = 0 (gdb) print pid $2 = 0 (gdb) print buffer $3 = "涌E\203\004\b\001\000\000\000\224\203涌\030\203涌o\205\004\b?217湯?205\004\b" (gdb)
(gdb) break 21 Breakpoint 1 at 0x80484cc: file pipe1.c, line 21. (gdb) run Starting program: /home/staff/sapius/source/ipc_test/pipe1 Breakpoint 1, main (argc=1, argv=0xbfda24b4) at pipe1.c:21 21 pid = fork(); (gdb) step 22 if(pid == -1) {
(gdb) shell gcc pipe3.c -o pipe3 -g (gdb) exec-file pipe3 (gdb) list 1 #include <stdio.h> 2 #include <unistd.h> 3 //#include <sys/types.h> 4 #include <stdlib.h> 5 6 #define BUFSIZE 30 7 8 int main(int argc, char** argv) 9 { 10 int fd[2];
(gdb) help edit Edit specified file or function. With no argument, edits file containing most recent line listed. Editing targets can be specified in these ways: FILE:LINENUM, to edit at that line in that file, FUNCTION, to edit at the beginning of that function, FILE:FUNCTION, to distinguish among like-named static functions. *ADDRESS, to edit at the line containing that address. Uses EDITOR environment variable contents as editor (or ex as default). (gdb) help load Dynamically load FILE into the running program, and record its symbols for access from GDB. (gdb) help shell Execute the rest of the line as a shell command. With no arguments, run an inferior shell.