hw1
fork + collatz
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
static void collatz(int n) {
printf("%d ,", n);
if (n == 1) return;
else if (n % 2 == 0) collatz(n / 2);
else collatz(3*n + 1);
}
int main()
{
pid_t pid;
int a = 100;
pid = fork();
if(pid<0)
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if(pid==0)
{
printf("child pid=%d\n", getpid());
printf("a address%p a=%d\n", (void*)&a, a);
printf("Child[+++]\n");
collatz(8);
printf("\nChild[---]\n");
}
else
{
printf("parent pid=%d\n", getpid());
a = 300;
printf("a address%p a=%d\n", (void*)&a, a);
printf("Parent[+++]\n");
wait(NULL);
printf("Child Complete\n");
printf("Parent[---]\n");
getchar();
exit(0);
}
return 0;
}
pipe + fork + read/write
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BUFFER_SIZE 80
#define READ_END 0
#define WRITE_END 1
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("The usage of this program is:\n");
printf("FileCopy input.txt copy.txt\n");
return 1;
}
else {
int pid;
int fd[2];
char *infile = argv[1];
char *outfile = argv[2];
if (pipe(fd) == -1) {
fprintf(stderr, "Pipe failed");
return 1;
}
printf("fd[0]=%d\n", fd[0]);
printf("fd[1]=%d\n", fd[1]);
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork error");
return 1;
}
if (pid > 0) {
int in;
int len;
char buffer[BUFFER_SIZE];
close(fd[READ_END]);
in = open(infile, O_RDONLY);
if (in == -1) {
fprintf(stderr, "Error opening file %s\n", infile);
return 1;
}
while ( 1 ) {
memset( buffer, 0, sizeof(buffer));
len = read(in, buffer, BUFFER_SIZE);
printf("parent len=%d\n",len);
if( len <= 0 ) break;
write(fd[WRITE_END], buffer, len );
}
close(in);
close(fd[WRITE_END]);
wait(NULL);
}
else {
int out;
int len;
char buffer[BUFFER_SIZE];
close(fd[WRITE_END]);
out = open(outfile, O_WRONLY | O_TRUNC | O_CREAT);
while(1) {
memset(buffer, 0, sizeof(buffer));
len = read(fd[READ_END], buffer, BUFFER_SIZE) ;
printf("child len=%d\n", len);
if (len <= 0 ) break;
write(out,buffer, len );
printf("buf1:%s\n",buffer);
}
close(out);
close(fd[READ_END]);
}
}
return 0;
}