Skip to content

Instantly share code, notes, and snippets.

@ahmpro
Last active August 29, 2015 14:01
Show Gist options
  • Save ahmpro/ad39cdfe5255f059f1a2 to your computer and use it in GitHub Desktop.
Save ahmpro/ad39cdfe5255f059f1a2 to your computer and use it in GitHub Desktop.
gamma - lab 4 on "Operating Systems" in MAI
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <setjmp.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#define input_pipe "/tmp/input_pipe"
#define gamm_pipe "/tmp/gamm_pipe"
#define output_gamma "gamma.txt"
#define output_text "text.txt"
#define BUF_SIZE 8192
int main()
{
int input_fd, in_gam_fd, out_gam_fd, out_fd;
int in_char, gamma_char;
int rnd_in_in, rnd_in_gamma;
int gammed;
input_fd = open(input_pipe, O_RDONLY);
if (input_fd == -1) {
return 1;
}
in_gam_fd = open(gamm_pipe, O_RDONLY);
if (in_gam_fd == -1) {
close(input_fd);
return 1;
}
out_gam_fd=open(output_gamma, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (out_gam_fd == -1) {
close(input_fd);
close(in_gam_fd);
return 1;
}
out_fd=open(output_text, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (out_fd == -1) {
close(input_fd);
close(in_gam_fd);
close(out_gam_fd);
return 1;
}
rnd_in_in = read(input_fd, &in_char, 1);
rnd_in_gamma = read(in_gam_fd, &gamma_char, 1);
while(rnd_in_in > 0 && rnd_in_gamma > 0) {
gammed = in_char ^ gamma_char;
//printf("got '%ld' and '%ld' and calc '%ld'\n", in_char, gamma_char, gammed);
write(out_fd, &gammed, 1);
write(out_gam_fd, &gamma_char, 1);
rnd_in_in = read(input_fd, &in_char, 1);
rnd_in_gamma = read(in_gam_fd, &gamma_char, 1);
}
close(input_fd);
close(out_fd);
close(in_gam_fd);
close(out_gam_fd);
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <setjmp.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#define BUF_SIZE 8192
#define gamm_pipe "/tmp/gamm_pipe"
void enter_something(int in_fd, int out_fd);
int open_pipe()
{
int fd;
if (mkfifo(gamm_pipe, 0777)) {
perror("mkfifo");
unlink(gamm_pipe);
remove(gamm_pipe);
mkfifo(gamm_pipe, 0777);
}
fd = open(gamm_pipe, O_WRONLY);
return fd;
}
void close_pipe() {
unlink(gamm_pipe);
remove(gamm_pipe);
}
void enter_from_file(char **argv, int out_fd) {
int in_fd = open(argv[1], O_RDONLY);
printf("gamma try load %s", argv[1]);
if (in_fd == -1) {
printf("Error in openning the input file\n");
return;
}
enter_something(in_fd, out_fd);
}
void enter_random(int out_fd) {
int random_fd=open("/dev/urandom", O_RDONLY);
if (random_fd == -1) {
return;
}
enter_something(random_fd, out_fd);
}
void enter_something(int in_fd, int out_fd) {
int rnd_in, rnd_out;
int got;
for(;;) {
rnd_in = read(in_fd, &got, 1);
rnd_out = write(out_fd, &got, 1);
if (rnd_out != rnd_in) {
break;
}
}
close(in_fd);
close(out_fd);
}
int main(int argc, char *argv[])
{
int pipe_fd;
pipe_fd=open_pipe();
if (argc == 1) {
enter_random(pipe_fd);
}
if (argc == 2)
enter_from_file(argv, pipe_fd);
close_pipe();
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <setjmp.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#define BUF_SIZE 8192
#define input_pipe "/tmp/input_pipe"
void enter_something(int in_fd, int out_fd);
int open_pipe()
{
int fd;
if ( mkfifo(input_pipe, 0777) ) {
perror("mkfifo");
unlink(input_pipe);
remove(input_pipe);
mkfifo(input_pipe, 0777);
}
fd = open(input_pipe, O_WRONLY);
return fd;
}
int close_pipe(int fd) {
close(fd);
unlink(input_pipe);
remove(input_pipe);
return 0;
}
void enter_from_file(char **argv, int out_fd) {
int in_fd;
in_fd = open(argv[1], O_RDONLY);
if (in_fd == -1) {
printf("Error in openning the input file\n");
return;
}
enter_something(in_fd, out_fd);
}
void enter_something(int in_fd, int out_fd) {
int ret_in;
int ret_out;
char buff[BUF_SIZE];
int got;
while((ret_in = read(in_fd, &got, 1)) > 0) {
ret_out = write(out_fd, &got, 1);
if (ret_out != ret_in) {
printf("Error in writing \n");
return;
}
}
close(in_fd);
}
int main(int argc, char *argv[])
{
int pipe_fd;
pipe_fd=open_pipe();
if (argc == 1) {
enter_something(STDIN_FILENO, pipe_fd);
}
if (argc == 2)
enter_from_file(argv, pipe_fd);
close_pipe(pipe_fd);
return 0;
}
all:
gcc -Wall -std=gnu99 gamma.c -o gamma
gcc -Wall -std=gnu99 input.c -o input
gcc -Wall -std=gnu99 encoder.c -o encoder
clean:
rm gamma
rm input
rm encoder
rm gamma.txt
rm text.txt
check:
echo ---+ gamma.c
cppcheck --enable=all --inconclusive --std=posix gamma.c
echo ---+ input.c
cppcheck --enable=all --inconclusive --std=posix input.c
echo ---+ encoder.c
cppcheck --enable=all --inconclusive --std=posix encoder.c
#/home/l1s/linux-source/scripts/checkpatch.pl -f encoder.c
test:
rm text.txt
rm gamma.txt
rm text_in.txt
rm gamma_in.txt
echo "It's works" > sample.txt
(./input sample.txt &)
(./gamma &)
(./encoder)
mv text.txt text_in.txt
mv gamma.txt gamma_in.txt
(./input text_in.txt &)
(./gamma gamma_in.txt &)
(./encoder)
md5sum text.txt
md5sum sample.txt
hexdump -C sample.txt
hexdump -C gamma_in.txt
hexdump -C text_in.txt
hexdump -C gamma.txt
hexdump -C text.txt
well:
clear
make clean -i
make
make -i test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment