Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nguaki committed Jul 7, 2018
1 parent 74a9847 commit 4ee4edd
Show file tree
Hide file tree
Showing 538 changed files with 19,121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 1stfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aaa
bbb
ccc
ddd
eee
fff
4 changes: 4 additions & 0 deletions 2ndfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bbb
ccc
eee
fff
2 changes: 2 additions & 0 deletions 3rdfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
aaa
ddd
Binary file added DD
Binary file not shown.
69 changes: 69 additions & 0 deletions DD.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

int main (int argc, char *argBB[]) {

int C3, waitVal, waitVal2, ps;
C3 = fork();

if(C3 < 0)
{
printf("Fork failed");
exit(1);
}

if(C3 != 0)
{
printf("\nPid = %d Code DD: created proccess Pid = %d (code CC)\n", getpid(), C3);
}
if( C3 == 0 )
{
if( execv("CC", 0) == -1 ){
printf("\nexecv failed\n");
exit(0);
}
}


ps = fork();
if(ps < 0)
{
printf("Fork failed");
exit(1);
}

if(ps != 0)
{
printf("\nPid = %d Code DD: created proccess Pid = %d (code ps)\n", getpid(), ps);
}

if( ps == 0 )
{
char command[50];
strcpy(command, "ps -u username");
system(command);
exit(11);
kill(ps, SIGKILL);//KILL PROCCESS PS HERE
}

waitVal = wait(&waitVal2);

if(waitVal == ps)
{
printf("\nPid= %d Code DD: process Pid = %d terminated\n", getpid(), ps);
printf("\nPid = %d Code DD: killing process Pid = %d\n", getpid(), C3);
kill(C3, SIGKILL);
printf("\nPid= %d Code DD: process Pid = %d terminated\n", getpid(), C3);
printf("\nPid = %d Code DD: terminating\n", getpid());
exit(7);
}

return 0;



}
16 changes: 16 additions & 0 deletions Head First C/ch 10/diner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>

int
main( int argc, char *argv[])
{
int i;
printf("# of Diners:%i\n", atoi(argv[1]) );

for( i = 2; i < (atoi)(argv[1]) + 2; i ++ )
printf( "Dish: %s\n", argv[i] );

printf( "DRINK: %s\n", getenv("DRINK"));

return 0;
}
Binary file added Head First C/ch 10/dup2_fileno
Binary file not shown.
84 changes: 84 additions & 0 deletions Head First C/ch 10/dup2_fileno.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jan 29, 17
//
// Demonstration of fork() and execle() command.
// By using child process, now it goes thru all three loop.
// But still there is a problem.
// These child processes overlap each other.
// In order to resolve this overlap problem, we need to make sure
// each child process terminates before starting a new process.
// We do this with waitpid() command.
// Furthermore, we use dup2() command to redirect the stdout to
// a text file.
// The text file looks like as follows:
/*
# of Diners:2
# of Diners:2
Dish: chicken
Dish: chicken
Dish: beef
Dish: beef
DRINK: COKE
DRINK: ORANGE JUICE
# of Diners:2
Dish: chicken
Dish: beef
DRINK: ICE-TEA
*/
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

void
error( char *msg )
{
fprintf( stderr, "%s: %d\n", msg, strerror(errno));
//exit(1);
}

int
main()
{

char *drinks[] = { "ORANGE JUICE", "COKE", "ICE-TEA" };
char drink[50];

int i, pid_status;
FILE *f = fopen( "output.txt", "w");

if( !f ){
error( "Cannot open a file to write.");
}


for( i=0; i<3; i++ ){
sprintf( drink, "DRINK=%s", drinks[i] );
char *myEnv[] = { drink, NULL };

pid_t pid = fork();

if( pid == -1 ){
error( "Can't fork process. \n" );
}

if( !pid ){

printf( "file number = %u\n", fileno(f));

if( dup2( fileno(f), 1) == -1 ){
error( "Can't redirect Standard output");

perror("dup2 failed");
}
if( execle( "/home/ubuntu/workspace/Head First C/ch 9/diner", "/home/ubuntu/workspace/Head First C/ch 9/diner", "2", "chicken", "beef", NULL, myEnv ) == -1 )
{
error( "Cannot run diner\n");
}
if( waitpid(pid, &pid_status, 0) == -1 ){
error( "Error waiting for child process" );
}
}
}
return 0;
}
Binary file added Head First C/ch 10/fileno
Binary file not shown.
12 changes: 12 additions & 0 deletions Head First C/ch 10/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# of Diners:2
Dish: chicken
Dish: beef
DRINK: COKE
# of Diners:2
Dish: chicken
Dish: beef
DRINK: ICE-TEA
# of Diners:2
Dish: chicken
Dish: beef
DRINK: ORANGE JUICE
Binary file added Head First C/ch 10/pipe
Binary file not shown.
120 changes: 120 additions & 0 deletions Head First C/ch 10/pipe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Jan 29, 17
//
// Exercising fork() and execle() and dup2() and pipe command.
// By using child process, now it goes thru all three loop.
// But still there is a problem.
// These child processes overlap each other.
// In order to resolve this overlap problem, we need to make sure
// each child process terminates before starting a new process.
// We do this with waitpid() command.
// Furthermore, we use dup2() command to redirect the stdout to
// a text file.
// The text file looks like as follows:
//
// This program takes one step further by creating a pipe between
// child and parent process. The stdout of the child process is
// piped to stdin of the parent process.
/*
parents: # of Diners:2
parents: Dish: chicken
parents: Dish: beef
parents: DRINK: ORANGE JUICE
parents: # of Diners:2
parents: Dish: chicken
parents: Dish: beef
parents: DRINK: COKE
parents: # of Diners:2
parents: Dish: chicken
parents: Dish: beef
parents: DRINK: ICE-TEA
*/
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

void
error( char *msg )
{
fprintf( stderr, "%s: %d\n", msg, strerror(errno));
//exit(1);
}

int
main()
{

char *drinks[] = { "ORANGE JUICE", "COKE", "ICE-TEA" };
char drink[50];

int fd[2];

int i, pid_status;



for( i=0; i<3; i++ ){
if( pipe(fd) == -1 ){
error( "Cannot create a pipe.\n" );
}

sprintf( drink, "DRINK=%s", drinks[i] );
char *myEnv[] = { drink, NULL };

pid_t pid = fork();

if( pid == -1 ){
error( "Can't fork process. \n" );
}

if( !pid ){

//close reading end of the pipe.
close(fd[0]);
//make STDOUT same as the pipe writing.
//In other words, what ever is written on STDOUT will be written to the pipe.
//STDOUT equals to writing end of the PIPE.
if( dup2( fd[1], 1) == -1 ){
error( "Can't redirect Standard output");

perror("dup2 failed");
}
if( execle( "/home/ubuntu/workspace/Head First C/ch 9/diner", "/home/ubuntu/workspace/Head First C/ch 9/diner", "2", "chicken", "beef", NULL, myEnv ) == -1 )
{
error( "Cannot run diner\n");
}
if( waitpid(pid, &pid_status, 0) == -1 ){
error( "Error waiting for child process" );
}
}


//Whatever written is written to the pipe will be considered as STDIN.
//STDIN = reading end of the pipe.
dup2(fd[0], 0);

//Close the written end of the pipe.
//This part is needed. Without it, this works only for the first loop.
close(fd[1]);

char line[255];

while( fgets(line, 255, stdin) ){
printf("parents: %s\n", line);
}
}
return 0;
}
Binary file added Head First C/ch 10/raise_alarm
Binary file not shown.
Loading

0 comments on commit 4ee4edd

Please sign in to comment.