-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
538 changed files
with
19,121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
aaa | ||
bbb | ||
ccc | ||
ddd | ||
eee | ||
fff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
bbb | ||
ccc | ||
eee | ||
fff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
aaa | ||
ddd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Oops, something went wrong.