From ea9e3ce17caafd6bce1caaaa18174f6d63ff021d Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Tue, 20 Mar 2012 00:47:18 +0400 Subject: [PATCH] IPC --- c/ipc/Makefile | 147 +++++++++++++++++++++++++++++++++++++++++++++ c/ipc/makelog.c | 37 ++++++++++++ c/ipc/pipes.c | 33 ++++++++++ c/ipc/printns.c | 35 +++++++++++ c/ipc/typeclient.c | 23 +++++++ c/ipc/typeserver.c | 32 ++++++++++ 6 files changed, 307 insertions(+) create mode 100644 c/ipc/Makefile create mode 100644 c/ipc/makelog.c create mode 100644 c/ipc/pipes.c create mode 100644 c/ipc/printns.c create mode 100644 c/ipc/typeclient.c create mode 100644 c/ipc/typeserver.c diff --git a/c/ipc/Makefile b/c/ipc/Makefile new file mode 100644 index 0000000..3c7d0f6 --- /dev/null +++ b/c/ipc/Makefile @@ -0,0 +1,147 @@ +# Makefile generated by command: smake.sh -t makelog -t pipes -t printns -t typeclient -t typeserver +# This file is generated with smake.sh. +# You can use this make file with instruction make to +# use one of build mode: debug, profile, develop, release. +# No need to call make clean if You make with other mode, +# because the Makefile containes rules for automatically clean. +# Some usage examples: +# make # default mode is debug +# CFLAGS="-O2 -march=core2 -mtune=core2 -msse4.1 -mfpmath=sse -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1" make mode=develop +# CFLAGS="-O2 -march=amdfam10 -mtune=amdfam10 -msse4a --mfpmath=sse -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1" make mode=profile +# CFLAGS="-O2 -march=k6-2 -mtune=k6-2 -m3dnow --mfpmath=387 -fomit-frame-pointer -pipe" LDFLAGS="-Wl,-O1" make mode=release +# Report bugs to + +#_________________________________ +# ENVIRONMENT | +#________________________________| +TARGET0=makelog +TARGET1=pipes +TARGET2=printns +TARGET3=typeclient +TARGET4=typeserver +TARGETS= $(TARGET0) $(TARGET1) $(TARGET2) $(TARGET3) $(TARGET4) +CC=cc +CXX=c++ +LIBS= +SRC= +INCLUDES= +#________________________________________ +# BUILD SCRIPT (don't change) | +#_______________________________________| +ifeq ($(mode),) + mode = debug +endif +ifeq ($(mode),debug) + CFLAGS := -O0 -g -DDEBUG -std=gnu99 -pedantic -Wextra -Wconversion $(CFLAGS) + LDFLAGS := $(LDFLAGS) +endif +ifeq ($(mode),profile) + CFLAGS := -O0 -g -DDEBUG -p -ftest-coverage -Wcoverage-mismatch $(CFLAGS) + LDFLAGS := -g -p $(LDFLAGS) +endif +ifeq ($(mode),develop) + CFLAGS := -O2 -g -DDEBUG $(CFLAGS) + LDFLAGS := -O1 $(LDFLAGS) +endif +ifeq ($(mode),release) + CFLAGS := -O2 $(CFLAGS) + LDFLAGS := -O1 $(LDFLAGS) +endif + +CFLAGS += -Wall $(INCLUDES) +LDFLAGS += -Wall $(LIBS) + +all: + @make change_make_options &>/dev/null + +make $(TARGETS) + +ifneq ($(mode),debug) +ifneq ($(mode),profile) +ifneq ($(mode),develop) +ifneq ($(mode),release) + @echo "Invalid build mode." + @echo "Please use 'make mode=release', 'make mode=develop', 'make mode=profile' or 'make mode=debug'" + @exit 1 +endif +endif +endif +endif + @echo ".........................." + @echo "Building on "$(mode)" mode " + @echo "CFLAGS=$(CFLAGS)" + @echo "LDFLAGS=$(LDFLAGS)" + @echo ".........................." + +OLD_BUILD_MODE=$(shell grep ^MODE make_options.out 2>/dev/null | sed 's~^MODE=~~') +OLD_BUILD_CFLAGS=$(shell grep ^CFLAGS make_options.out 2>/dev/null | sed 's~^CFLAGS=~~') +OLD_BUILD_LDFLAGS=$(shell grep ^LDFLAGS make_options.out 2>/dev/null | sed 's~^LDFLAGS=~~') +change_make_options: +ifneq ($(mode)|$(CFLAGS)|$(LDFLAGS), $(OLD_BUILD_MODE)|$(OLD_BUILD_CFLAGS)|$(OLD_BUILD_LDFLAGS)) + @echo CLEANING... + @make clean + @echo "MODE=$(mode)" > make_options.out + @echo "CFLAGS=$(CFLAGS)" >> make_options.out + @echo "LDFLAGS=$(LDFLAGS)" >> make_options.out +endif + +%.o : + $(CC) -c $(CFLAGS) $(SRC) -o $@ $< + +clean: + $(RM) *.o *.out callgrind.out.* *.gcno $(TARGETS) + +.PHONY: all change_make_options clean + +#_________________________________ +# R U L E S | +#________________________________| +target_objs0 = \ + makelog.o + +$(TARGET0): $(target_objs0) + $(CC) $(LDFLAGS) -o $@ $(target_objs0) + + +target_objs1 = \ + pipes.o + +$(TARGET1): $(target_objs1) + $(CC) $(LDFLAGS) -o $@ $(target_objs1) + + +target_objs2 = \ + printns.o + +$(TARGET2): $(target_objs2) + $(CC) $(LDFLAGS) -o $@ $(target_objs2) + + +target_objs3 = \ + typeclient.o + +$(TARGET3): $(target_objs3) + $(CC) $(LDFLAGS) -o $@ $(target_objs3) + + +target_objs4 = \ + typeserver.o + +$(TARGET4): $(target_objs4) + $(CC) $(LDFLAGS) -o $@ $(target_objs4) + + +makelog.o: \ + makelog.c + +pipes.o: \ + pipes.c + +printns.o: \ + printns.c + +typeclient.o: \ + typeclient.c + +typeserver.o: \ + typeserver.c + diff --git a/c/ipc/makelog.c b/c/ipc/makelog.c new file mode 100644 index 0000000..3b4dbc3 --- /dev/null +++ b/c/ipc/makelog.c @@ -0,0 +1,37 @@ +/* + Popen/Pclose Demo by Andrei Borovsky . + This code is freeware. +*/ + +#include +#include + +#define BUF_SIZE 0x100 + +int main(int argc, char * argv[]) +{ + FILE * f; + FILE * o; + int len; + char buf[BUF_SIZE]; + if (argc != 2) + { + printf("использование: makelog \"\"\n"); + return -1; + } + f = popen(argv[1], "r"); + if (f == NULL) + { + perror("ошибка:\n"); + return -1; + } + o = fopen("log.txt", "w"); + while ((len = fread(buf, 1, BUF_SIZE, f)) != 0) + { + write(1, buf, len); + fwrite(buf, 1, len, o); + } + pclose(f); + fclose(o); + return 0; +} diff --git a/c/ipc/pipes.c b/c/ipc/pipes.c new file mode 100644 index 0000000..9a5745c --- /dev/null +++ b/c/ipc/pipes.c @@ -0,0 +1,33 @@ +/* + Anonymous Pipes Demo by Andrei Borovsky . + This code is freeware. +*/ + +#include +#include +#include + +int main (int argc, char * argv[]) +{ + int pipedes[2]; + pid_t pid; + pipe(pipedes); + pid = fork(); + if ( pid > 0 ) + { + char *str = "String passed via pipe\n"; + close(pipedes[0]); + write(pipedes[1], (void *) str, strlen(str) + 1); + close(pipedes[1]); + } + else + { + char buf[1024]; + int len; + close(pipedes[1]); + while ((len = read(pipedes[0], buf, 1024)) != 0) + write(2, buf, len); + close(pipedes[0]); + } + return 0; +} diff --git a/c/ipc/printns.c b/c/ipc/printns.c new file mode 100644 index 0000000..8909b8f --- /dev/null +++ b/c/ipc/printns.c @@ -0,0 +1,35 @@ +/* + Child process output redirection Demo by Andrei Borovsky . + This code is freeware. +*/ + +#include +#include +#include +#include + +#define BUF_SIZE 0x100 + +int main (int argc, char * argv[]) +{ + int pipedes[2]; + pid_t pid; + pipe(pipedes); + pid = fork(); + if ( pid > 0 ) + { + char buf[BUF_SIZE]; + int len; + close(pipedes[1]); + while ((len = read(pipedes[0], buf, BUF_SIZE)) > 0) + write(1, buf, len); + close(pipedes[0]); + } + else + { + close(pipedes[0]); + dup2(pipedes[1], 1); + execve("/bin/netstat", NULL, NULL); + } + return 0; +} diff --git a/c/ipc/typeclient.c b/c/ipc/typeclient.c new file mode 100644 index 0000000..b122435 --- /dev/null +++ b/c/ipc/typeclient.c @@ -0,0 +1,23 @@ +/* + Named Pipe Demo by Andrei Borovsky . + This code is freeware. +*/ + +#include + +#define FIFO_NAME "./fifofile" + +int main () +{ + FILE * f; + char ch; + f = fopen(FIFO_NAME, "r"); + do + { + ch = fgetc(f); + putchar(ch); + } while (ch != 'q'); + fclose(f); + unlink(FIFO_NAME); + return 0; +} diff --git a/c/ipc/typeserver.c b/c/ipc/typeserver.c new file mode 100644 index 0000000..63ea3ef --- /dev/null +++ b/c/ipc/typeserver.c @@ -0,0 +1,32 @@ +/* + Named Pipe Demo by Andrei Borovsky . + This code is freeware. +*/ + +#include +#include +#include + +#define FIFO_NAME "./fifofile" + +int main(int argc, char * argv[]) +{ + FILE * f; + char ch; + mkfifo(FIFO_NAME, 0600); + f = fopen(FIFO_NAME, "w"); + if (f == NULL) + { + printf("Не удалось открыть файл\n"); + return -1; + } + do + { + ch = getchar(); + fputc(ch, f); + if (ch == 10) fflush(f); + } while (ch != 'q'); + fclose(f); + unlink(FIFO_NAME); + return 0; +}